Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(da-clients): add Celestia client #2983

Merged
merged 23 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: use secp256k1 instead of k256
  • Loading branch information
dimazhornyk committed Oct 21, 2024
commit d33a0de4b6f971fd50647e29a999a84fb3f503d8
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ subxt-signer = { version = "0.34", default-features = false }
celestia-types = "0.6.1"
bech32 = "0.11.0"
ripemd = "0.1.3"
k256 = "0.13.4"
tonic = "0.11.0"
pbjson-types = "0.6.0"

Expand Down
3 changes: 2 additions & 1 deletion core/node/da_clients/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ jsonrpsee = { workspace = true, features = ["ws-client"] }
http.workspace = true
bincode.workspace = true
celestia-types.workspace = true
k256.workspace = true
secp256k1.workspace = true
sha2.workspace = true
prost.workspace = true
bech32.workspace = true
ripemd.workspace = true
Expand Down
45 changes: 23 additions & 22 deletions core/node/da_clients/src/celestia/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::{
fmt::{Display, Formatter, Result},
str::FromStr,
time::{Duration, Instant},
};

use celestia_types::Blob;
use k256::{
ecdsa::{signature::Signer, Signature, SigningKey, VerifyingKey},
sha2,
sha2::{Digest, Sha256},
};
use prost::{bytes::Bytes, Message, Name};
use secp256k1::{PublicKey, Secp256k1, SecretKey};
use sha2::Digest;
use tonic::transport::Channel;

use super::{
Expand All @@ -29,7 +27,7 @@ use super::{
},
v1beta1::Coin,
},
crypto::secp256k1,
crypto::secp256k1 as ec_proto,
tx::v1beta1::{
mode_info::{Single, Sum},
service_client::ServiceClient as TxClient,
Expand All @@ -50,7 +48,7 @@ pub(crate) struct RawCelestiaClient {
grpc_channel: Channel,
address: String,
chain_id: String,
signing_key: SigningKey,
signing_key: SecretKey,
}

impl RawCelestiaClient {
Expand All @@ -59,9 +57,9 @@ impl RawCelestiaClient {
private_key: String,
chain_id: String,
) -> anyhow::Result<Self> {
let bytes = hex::decode(private_key.trim())?;
let signing_key = SigningKey::from_slice(bytes.as_slice())?;
let address = get_address(*signing_key.verifying_key())?;
let signing_key = SecretKey::from_str(&private_key)
.map_err(|e| anyhow::anyhow!("Failed to parse private key: {}", e))?;
let address = get_address(signing_key.public_key(&Secp256k1::new()))?;

Ok(Self {
grpc_channel,
Expand Down Expand Up @@ -405,7 +403,7 @@ fn new_signed_tx(
gas_limit: u64,
fee: u64,
chain_id: String,
signing_key: &SigningKey,
signing_key: &SecretKey,
) -> Tx {
const SIGNING_MODE_INFO: Option<ModeInfo> = Some(ModeInfo {
sum: Some(Sum::Single(Single { mode: 1 })),
Expand All @@ -421,16 +419,16 @@ fn new_signed_tx(
..Fee::default()
};

let public_key = secp256k1::PubKey {
let public_key = ec_proto::PubKey {
key: Bytes::from(
(*signing_key.verifying_key())
.to_encoded_point(true)
.as_bytes()
signing_key
.public_key(&Secp256k1::new())
.serialize()
.to_vec(),
),
};
let public_key_as_any = pbjson_types::Any {
type_url: secp256k1::PubKey::type_url(),
type_url: ec_proto::PubKey::type_url(),
value: public_key.encode_to_vec().into(),
};
let auth_info = AuthInfo {
Expand Down Expand Up @@ -459,11 +457,15 @@ fn new_signed_tx(
account_number: base_account.account_number,
}
.encode_to_vec();
let signature: Signature = signing_key.sign(&bytes_to_sign);
let hashed_bytes: [u8; 32] = sha2::Sha256::digest(bytes_to_sign).into();
let signature = secp256k1::Secp256k1::new().sign_ecdsa(
&secp256k1::Message::from_slice(&hashed_bytes[..]).unwrap(), // unwrap is safe here because we know the length of the hashed bytes
signing_key,
);
Tx {
body: Some(tx_body),
auth_info: Some(auth_info),
signatures: vec![Bytes::from(signature.to_bytes().to_vec())],
signatures: vec![Bytes::from(signature.serialize_compact().to_vec())],
}
}

Expand Down Expand Up @@ -566,10 +568,10 @@ fn new_msg_pay_for_blobs(blobs: &[Blob], signer: String) -> anyhow::Result<MsgPa
})
}

fn get_address(public_key: VerifyingKey) -> anyhow::Result<String> {
fn get_address(public_key: PublicKey) -> anyhow::Result<String> {
use ripemd::{Digest, Ripemd160};

let sha_digest = sha2::Sha256::digest(public_key.to_sec1_bytes());
let sha_digest = sha2::Sha256::digest(public_key.serialize().to_vec());
let ripemd_digest = Ripemd160::digest(&sha_digest[..]);
let mut bytes = [0u8; ADDRESS_LENGTH];
bytes.copy_from_slice(&ripemd_digest[..ADDRESS_LENGTH]);
Expand All @@ -585,8 +587,7 @@ pub(super) struct BlobTxHash([u8; 32]);

impl BlobTxHash {
pub(super) fn compute(blob_tx: &BlobTx) -> Self {
let sha2 = Sha256::digest(&blob_tx.tx);
Self(sha2.into())
Self(sha2::Sha256::digest(&blob_tx.tx).into())
}

pub(super) fn hex(self) -> String {
Expand Down
Loading