Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
ed25519 derived key verification
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoguang1010 committed Sep 29, 2020
1 parent 6d4c1a1 commit fc93edf
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 17 deletions.
10 changes: 5 additions & 5 deletions tcx-primitive/src/ed25519_bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn transform_ed25519_bip32_error(err: Bip32Error) -> KeyError {

impl Ed25519DeterministicPrivateKey {
pub fn from_seed(seed: &[u8]) -> Result<Self> {
let mut hmac_engine: HmacEngine<sha512::Hash> = HmacEngine::new(b"Tezos seed");
let mut hmac_engine: HmacEngine<sha512::Hash> = HmacEngine::new(b"Bitcoin seed");
hmac_engine.input(seed);
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
let mut temp_byte: [u8; 32] = [0; 32];
Expand Down Expand Up @@ -201,10 +201,10 @@ mod test {
.map(|path| hex::encode(esk.derive(path).unwrap().0.public().as_ref()))
.collect::<Vec<String>>();
let expected_pub_keys = vec![
"67d938aa12c4a2b9674bf537e55e9a97473179e51e673b1d499f043610e1b79a8beadbe246cdd26dc6b8b52cb68c91e710587baf9e92f306cd2c542aff0ed541",
"c7bfafb77f7dc6d800c6e6aed1d87e76a645c81112cc35e057d9417380b0de8394126f5bfe3bff56a038db7527612630270a76151f91c98e4ae0447f5cd02c6c",
"5ffbb9f23edb48fc0c6dde8645cd169dae06f226fdd0e8c4cfdf1bd291fb3d6cf0aed085e3306991396a721b21ea4399fdc256abfec6c043fd00180a5b2083a2",
"ebaee91cce22f9163c9f06d6c268c76798da3868a87342856ebc978627f84da8c8ed8ce278f31238f2efb82f198e9151bdcd192d1a5065d354f7f5ed9d03278f",
"d0c5ee97112a8a6f192ec44ab10f6a51bbfa327f7736e8e8b30b9ec636bc533b4a5a79f69bacc08e3c546cf8cc912ab820eb933f18af0630eb9c5978c41c37e2",
"72cc175bb3cec0b14c145c60dfc8044857af20408d13bd27d37bb22131802650ca56b9788a0c4bfc220326eb1f03911fd504c180f988e69370dd1f3140193671",
"33b203d6231b7bd8a465799e210345c12446b1be99918e32392ce89c77c1d9fa371db326eba11ac066afa58f895d459272862957a5df8b48b6d81cadd5d36196",
"8cc4569d3ab5a83638b65072349075ad58126204c1085e64492fac932e8077c942c1fc372392916a4c28ffd37ae81653ac7ed82311ff4ac1a6d72a763da1ae81",
];
assert_eq!(pub_keys, expected_pub_keys);
}
Expand Down
4 changes: 4 additions & 0 deletions tcx-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ fn main() {
// for target in targets {
// println!("cargo:rustc-link-search=../../android/tokencore/build/intermediates/cmake/release/obj/{}/", target);
// }

//tcx-tezos
env::set_var("OUT_DIR", "../tcx-tezos/src");
prost_build::compile_protos(&["src/tezos.proto"], &["src/"]).unwrap();
}
20 changes: 20 additions & 0 deletions tcx-proto/src/tezos.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";
package transaction;
message TezosRawTxIn {
string rawData = 1;
}

message TezosTxOut {
string signature = 1;
}

message TezosKeystoreParam {
string keystore = 1;
string password = 2;
string chainType = 3;
bool overwrite = 4;
}

message ExportTezosKeystoreResult {
string keystore = 1;
}
6 changes: 5 additions & 1 deletion tcx-tezos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ tcx-crypto = { path = "../tcx-crypto" }
blake2b_simd = "0.5.10"
hex = "0.3.1"
bitcoin = "0.21.0"
ring = "0.16.9"
ring = "0.16.9"

prost = "0.6.1"
bytes = "0.5.4"
prost-types = "0.6.1"
30 changes: 20 additions & 10 deletions tcx-tezos/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ impl Address for TezosAddress {
fn from_public_key(public_key: &TypedPublicKey, coin: &CoinInfo) -> Result<String> {
let tz1_prefix = hex::decode("06A19F")?;
//get public key
let pubkey = public_key.as_secp256k1()?.to_compressed();
let pubkey = public_key.to_bytes();
//Perform Blake2B hashing on the public key(no prefix)
let mut params = Params::new();
params.hash_length(20);
let genericHash = params.hash(&pubkey[1..]);
let generic_hash = params.hash(&pubkey[..32]);
//sha256Twice(prefix<3> + public key hash<20>)
let mut prefixed_generic_hash = vec![];
prefixed_generic_hash.extend_from_slice(tz1_prefix.as_ref());
prefixed_generic_hash.extend_from_slice(genericHash.as_bytes());
prefixed_generic_hash.extend_from_slice(generic_hash.as_bytes());
let double_hash_result = sha256_hash(&sha256_hash(&prefixed_generic_hash));
prefixed_generic_hash.extend_from_slice(&double_hash_result[..4]);
//base58Encode(prefix<3> + public key hash<20> + checksum<4>)
Expand Down Expand Up @@ -63,25 +63,35 @@ mod test {
#[test]
fn from_public_key_test() {
let coin_info = CoinInfo {
coin: "NERVOS".to_string(),
coin: "TEZOS".to_string(),
derivation_path: "".to_string(),
curve: CurveType::SECP256k1,
curve: CurveType::ED25519,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
};

let pub_key = TypedPublicKey::from_slice(
CurveType::SECP256k1,
&hex::decode("024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01")
CurveType::ED25519,
&hex::decode("4a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01")
.unwrap(),
)
.unwrap();
assert_eq!(
TezosAddress::from_public_key(&pub_key, &coin_info)
.ok()
.unwrap(),
TezosAddress::from_public_key(&pub_key, &coin_info).unwrap(),
"tz1dLEU3WfzCrDq2bvoEz4cfLP5wg4S7xNo9"
);

let pub_key = TypedPublicKey::from_slice(
CurveType::ED25519,
&hex::decode("d0c5ee97112a8a6f192ec44ab10f6a51bbfa327f7736e8e8b30b9ec636bc533b")
.unwrap(),
)
.unwrap();
//tz1MSaHcwz8vqQKTq9YsxZWfM5PhqFLB2B17
println!(
"###->{}",
TezosAddress::from_public_key(&pub_key, &coin_info).unwrap()
);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions tcx-tezos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod address;
pub mod signer;
pub mod transaction;

mod tests {
Expand Down
28 changes: 28 additions & 0 deletions tcx-tezos/src/signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//use crate::transaction::{TezosRawTxIn, TezosTxOut};
//use tcx_chain::Keystore;
//use tcx_constants::Result;
//
//pub(crate) fn hash_unsigned_payload(payload: &[u8]) -> Result<Vec<u8>> {
// if payload.len() > PAYLOAD_HASH_THRESHOLD {
// Ok(blake2_256(&payload).to_vec())
// } else {
// Ok(payload.to_vec())
// }
//
//
//impl TraitTransactionSigner<TezosRawTxIn, SubstrateTxOut> for Keystore {
// fn sign_transaction(
// &mut self,
// symbol: &str,
// address: &str,
// tx: &SubstrateRawTxIn,
// ) -> Result<TezosTxOut> {
// let raw_data_bytes = if tx.raw_data.starts_with("0x") {
// tx.raw_data[2..].to_string()
// } else {
// tx.raw_data.clone()
// };
// let raw_data_bytes = hex::decode(&raw_data_bytes)?;
// let hash = hash_unsigned_payload(&raw_data_bytes)?;
// }
//}
27 changes: 26 additions & 1 deletion tcx-tezos/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TezosRawTxIn {
#[prost(string, tag = "1")]
pub raw_data: std::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TezosTxOut {
#[prost(string, tag = "1")]
pub signature: std::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TezosKeystoreParam {
#[prost(string, tag = "1")]
pub keystore: std::string::String,
#[prost(string, tag = "2")]
pub password: std::string::String,
#[prost(string, tag = "3")]
pub chain_type: std::string::String,
#[prost(bool, tag = "4")]
pub overwrite: bool,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ExportTezosKeystoreResult {
#[prost(string, tag = "1")]
pub keystore: std::string::String,
}

0 comments on commit fc93edf

Please sign in to comment.