Skip to content

Commit b39dceb

Browse files
committed
Pubkey impl
1 parent 33f4f1a commit b39dceb

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

src/main.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ async fn run(run_options: config::RunOptions) {
211211
let api_client =
212212
ApiClient::try_new(run_options.server_url, None).expect("Failed to create API client");
213213

214-
let (pubkey, pubkey_evm) = signer.get_public_key().expect("Failed to get public key");
214+
let (pubkey, pubkey_evm) = signer
215+
.get_public_key()
216+
.await
217+
.expect("Failed to get public key");
215218
let evm_encded_public_key = format!("0x{}", hex::encode(pubkey_evm));
216219
tracing::info!(
217220
public_key = ?pubkey,
@@ -265,7 +268,10 @@ async fn main() {
265268
// Generate keypair (secret + public key)
266269
let (secret_key, _) = secp.generate_keypair(&mut rng);
267270
let signer = signer::FileSigner { secret_key };
268-
let (pubkey, pubkey_evm) = signer.get_public_key().expect("Failed to get public key");
271+
let (pubkey, pubkey_evm) = signer
272+
.get_public_key()
273+
.await
274+
.expect("Failed to get public key");
269275

270276
let guardian_key = GuardianKey {
271277
data: secret_key.secret_bytes().to_vec(),
@@ -485,8 +491,8 @@ mod tests {
485491
assert_eq!(result.unwrap_err().to_string(), INVALID_ACCUMULATOR_ADDRESS);
486492
}
487493

488-
#[test]
489-
fn test_parse_and_verify_proto_guardian_key() {
494+
#[tokio::test]
495+
async fn test_parse_and_verify_proto_guardian_key() {
490496
// The content below is generated by keygen script at:
491497
// https://github.com/wormhole-foundation/wormhole/blob/main/node/cmd/guardiand/keygen.go
492498
let content = "-----BEGIN WORMHOLE GUARDIAN PRIVATE KEY-----
@@ -511,7 +517,13 @@ mod tests {
511517
"f2f3127bff540c8441f99763f586858ef340c9962ad62b6181cd77203e81808f",
512518
);
513519
assert_eq!(
514-
hex::encode(signer.get_public_key().expect("Failed to get public key").1),
520+
hex::encode(
521+
signer
522+
.get_public_key()
523+
.await
524+
.expect("Failed to get public key")
525+
.1
526+
),
515527
"30e41be3f10d3ac813f91e49e189bbb948d030be",
516528
);
517529
}

src/signer.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use sha3::{Digest, Keccak256};
1414
#[async_trait]
1515
pub trait Signer: Send + Sync {
1616
async fn sign(&self, data: [u8; 32]) -> anyhow::Result<[u8; 65]>;
17-
fn get_public_key(&self) -> anyhow::Result<(PublicKey, [u8; 20])>;
17+
async fn get_public_key(&self) -> anyhow::Result<(PublicKey, [u8; 20])>;
1818
}
1919

2020
#[derive(Clone, Debug)]
@@ -73,6 +73,17 @@ impl FileSigner {
7373
}
7474
}
7575

76+
fn get_evm_public_key(public_key: &PublicKey) -> anyhow::Result<[u8; 20]> {
77+
let pubkey_uncompressed = public_key.serialize_uncompressed();
78+
let pubkey_hash: [u8; 32] = Keccak256::new_with_prefix(&pubkey_uncompressed[1..])
79+
.finalize()
80+
.into();
81+
let pubkey_evm: [u8; 20] = pubkey_hash[pubkey_hash.len() - 20..]
82+
.try_into()
83+
.map_err(|e| anyhow::anyhow!("Failed to convert public key hash to EVM format: {}", e))?;
84+
Ok(pubkey_evm)
85+
}
86+
7687
#[async_trait]
7788
impl Signer for FileSigner {
7889
async fn sign(&self, data: [u8; 32]) -> anyhow::Result<[u8; 65]> {
@@ -86,19 +97,10 @@ impl Signer for FileSigner {
8697
Ok(signature)
8798
}
8899

89-
fn get_public_key(&self) -> anyhow::Result<(PublicKey, [u8; 20])> {
100+
async fn get_public_key(&self) -> anyhow::Result<(PublicKey, [u8; 20])> {
90101
let secp = Secp256k1::new();
91102
let public_key = self.secret_key.public_key(&secp);
92-
let pubkey_uncompressed = public_key.serialize_uncompressed();
93-
let pubkey_hash: [u8; 32] = Keccak256::new_with_prefix(&pubkey_uncompressed[1..])
94-
.finalize()
95-
.into();
96-
let pubkey_evm: [u8; 20] =
97-
pubkey_hash[pubkey_hash.len() - 20..]
98-
.try_into()
99-
.map_err(|e| {
100-
anyhow::anyhow!("Failed to convert public key hash to EVM format: {}", e)
101-
})?;
103+
let pubkey_evm = get_evm_public_key(&public_key)?;
102104
Ok((public_key, pubkey_evm))
103105
}
104106
}
@@ -142,7 +144,20 @@ impl Signer for KMSSigner {
142144
})
143145
}
144146

145-
fn get_public_key(&self) -> anyhow::Result<(PublicKey, [u8; 20])> {
146-
todo!()
147+
async fn get_public_key(&self) -> anyhow::Result<(PublicKey, [u8; 20])> {
148+
let result = self
149+
.client
150+
.get_public_key()
151+
.key_id(self.arn.to_string())
152+
.send()
153+
.await
154+
.map_err(|e| anyhow::anyhow!("Failed to get public key from KMS: {}", e))?;
155+
let public_key = result
156+
.public_key
157+
.ok_or(anyhow::anyhow!("KMS did not return a public key"))?;
158+
let public_key = PublicKey::from_slice(public_key.as_ref())
159+
.map_err(|e| anyhow::anyhow!("Failed to create PublicKey from KMS: {}", e))?;
160+
let pubkey_evm = get_evm_public_key(&public_key)?;
161+
Ok((public_key, pubkey_evm))
147162
}
148163
}

0 commit comments

Comments
 (0)