-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathpublic_key.rs
58 lines (51 loc) · 1.71 KB
/
public_key.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
use tw_coin_entry::coin_context::CoinContext;
use tw_cosmos_sdk::public_key::{
CosmosPublicKey, JsonPublicKey, ProtobufPublicKey, PublicKeyParams,
};
use tw_keypair::ecdsa::secp256k1;
use tw_keypair::{tw, KeyPairError, KeyPairResult};
use tw_memory::Data;
use tw_proto::{google, to_any};
pub struct GreenfieldPublicKey(pub secp256k1::PublicKey);
impl JsonPublicKey for GreenfieldPublicKey {
fn public_key_type(&self) -> String {
"/cosmos.crypto.eth.ethsecp256k1.PubKey".to_string()
}
}
impl ProtobufPublicKey for GreenfieldPublicKey {
fn to_proto(&self) -> google::protobuf::Any {
let proto = tw_cosmos_sdk::proto::cosmos::crypto::eth::ethsecp256k1::PubKey {
key: self.0.compressed().to_vec(),
};
to_any(&proto)
}
}
impl CosmosPublicKey for GreenfieldPublicKey {
fn from_private_key(
_coin: &dyn CoinContext,
private_key: &tw::PrivateKey,
// Ignore custom public key parameters.
_params: Option<PublicKeyParams>,
) -> KeyPairResult<Self> {
let public_key = private_key
.get_public_key_by_type(tw::PublicKeyType::Secp256k1)?
.to_secp256k1()
.ok_or(KeyPairError::InvalidPublicKey)?
.clone();
Ok(GreenfieldPublicKey(public_key))
}
fn from_bytes(
_coin: &dyn CoinContext,
public_key_bytes: &[u8],
// Ignore custom public key parameters.
_params: Option<PublicKeyParams>,
) -> KeyPairResult<Self> {
secp256k1::PublicKey::try_from(public_key_bytes).map(GreenfieldPublicKey)
}
fn to_bytes(&self) -> Data {
self.0.compressed().to_vec()
}
}