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

Release 2.9.1 #116

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ members = [
"tcx",
"tcx-tezos",
"tcx-tester",
"tcx-eth2",
"zksync-crypto",
]

1 change: 1 addition & 0 deletions libs/secp256k1
Submodule secp256k1 added at d666ec
1 change: 0 additions & 1 deletion tcx-bch/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::address::BchAddress;
use crate::Result;
use bitcoin::Transaction;
use bitcoin_hashes::sha256d;
use std::str::FromStr;
use tcx_btc_fork::bip143_with_forkid::SighashComponentsWithForkId;
use tcx_btc_fork::signer::{LegacyTransactionSignComponent, SignHasher};
Expand Down
2 changes: 0 additions & 2 deletions tcx-btc-fork/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use bitcoin::hash_types::ScriptHash as ScriptHashType;
use bitcoin::network::constants::Network;
use bitcoin::util::address::Error as BtcAddressError;
use bitcoin::util::address::Payload;
use bitcoin::util::address::Payload::PubkeyHash;
use bitcoin::util::base58;
use bitcoin::{Address as BtcAddress, Script};
use bitcoin_hashes::hash160;
use bitcoin_hashes::Hash;
use core::result;
use std::fmt::{Display, Formatter};
Expand Down
3 changes: 1 addition & 2 deletions tcx-btc-fork/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use tcx_chain::{Keystore, TransactionSigner};

use bitcoin::{OutPoint, Script, Transaction, TxIn, TxOut};
use bitcoin_hashes::sha256d::Hash as Hash256;
use bitcoin_hashes::{sha256d, Hash};
use bitcoin_hashes::Hash;

use crate::Result;
use bitcoin::blockdata::script::Builder;
Expand Down
14 changes: 14 additions & 0 deletions tcx-constants/src/coin_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ lazy_static! {
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
});
coin_infos.push(CoinInfo {
coin: "ETHEREUM2".to_string(),
derivation_path: "m/12381/3600/0/0".to_string(),
curve: CurveType::BLS,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
});
coin_infos.push(CoinInfo {
coin: "ETHEREUM2".to_string(),
derivation_path: "m/12381/3600/0/0".to_string(),
curve: CurveType::BLS,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
});

RwLock::new(coin_infos)
};
Expand Down
15 changes: 15 additions & 0 deletions tcx-eth2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "tcx-eth2"
version = "0.1.0"
authors = ["xiaoguang <xiaoguang101010@163.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tcx-chain = { path = "../tcx-chain" }
tcx-constants = { path = "../tcx-constants" }
tcx-primitive = { path = "../tcx-primitive" }
tcx-crypto = { path = "../tcx-crypto" }

hex = "0.3.1"
66 changes: 66 additions & 0 deletions tcx-eth2/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use tcx_chain::Address;
use tcx_chain::Result;
use tcx_constants::CoinInfo;
use tcx_primitive::TypedPublicKey;

pub struct Eth2Address();

impl Address for Eth2Address {
fn from_public_key(public_key: &TypedPublicKey, _coin: &CoinInfo) -> Result<String> {
let public_key_str = hex::encode(public_key.to_bytes());
Ok(public_key_str)
}

fn is_valid(address: &str, _coin: &CoinInfo) -> bool {
if address.is_empty() {
return false;
};
if address.starts_with("0x") {
if address.len() != 98 {
return false;
};
} else {
if address.len() != 96 {
return false;
};
};
true
}
}

#[cfg(test)]
mod test {
use crate::address::Eth2Address;
use tcx_chain::Address;
use tcx_constants::{CoinInfo, CurveType};

#[test]
fn is_valid_test() {
let coin_info = CoinInfo {
coin: "ETHEREUM2".to_string(),
derivation_path: "".to_string(),
curve: CurveType::BLS,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
};
let address = "0xb809eea8f4c1935fa6c0cc6d454a5b297c7856da4df4da96432ea2ef6fa86abf90f8553aaf8c6d605c94244d68d62eb8";
let valid_result = Eth2Address::is_valid(address, &coin_info);
assert!(valid_result);

let address = "b809eea8f4c1935fa6c0cc6d454a5b297c7856da4df4da96432ea2ef6fa86abf90f8553aaf8c6d605c94244d68d62eb8";
let valid_result = Eth2Address::is_valid(address, &coin_info);
assert!(valid_result);

let address = "";
let valid_result = Eth2Address::is_valid(address, &coin_info);
assert_eq!(false, valid_result);

let address = "b809eea8f4c1935fa6c0cc6d454a5b297c7856da4df4da96432ea2ef6fa86abf";
let valid_result = Eth2Address::is_valid(address, &coin_info);
assert_eq!(false, valid_result);

let address = "b809eea8f4c1935fa6c0cc6d454a5b297c7856da4df4da96432ea2ef6fa86abf90f8553aaf8c6d605c94244d68d62eb8b809eea8f4c1935fa6c0cc6d454a5b297c7856da4df4da96432ea2ef6fa86abf";
let valid_result = Eth2Address::is_valid(address, &coin_info);
assert_eq!(false, valid_result);
}
}
9 changes: 9 additions & 0 deletions tcx-eth2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod address;

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
4 changes: 3 additions & 1 deletion tcx-primitive/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ impl TraitPrivateKey for BLSPrivateKey {
type PublicKey = BLSPublicKey;

fn from_slice(data: &[u8]) -> Result<Self> {
Ok(BLSPrivateKey(PrivateKey::from_bytes(data)?))
let mut temp_data = data.to_vec();
temp_data.resize(32, 0u8);
Ok(BLSPrivateKey(PrivateKey::from_bytes(temp_data.as_ref())?))
}

fn public_key(&self) -> Self::PublicKey {
Expand Down
69 changes: 58 additions & 11 deletions tcx-primitive/src/bls_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ fn ikm_to_lamport_sk(ikm: &[u8], salt: &[u8], split_bytes: &mut [[u8; DIGEST_SIZ
}

fn parent_sk_to_lamport_pk(parent_sk: BigUint, index: BigUint) -> Vec<u8> {
let salt = index.to_bytes_be();
let mut salt = index.to_bytes_be();
while salt.len() < 4 {
salt.insert(0, 0x00);
}
let ikm = parent_sk.to_bytes_be();
let mut lamport_0 = [[0u8; DIGEST_SIZE]; NUM_DIGESTS];
ikm_to_lamport_sk(ikm.as_slice(), salt.as_slice(), &mut lamport_0);
Expand Down Expand Up @@ -156,7 +159,15 @@ fn hkdf_mod_r(ikm: &[u8]) -> BigUint {
let mut tmp = ikm.to_vec();
tmp.extend(b"\x00");

hkdf(b"BLS-SIG-KEYGEN-SALT-", &tmp, b"\x00\x30", &mut okm); // L=48, info=I2OSP(L,2)
// let digest_obj = digest::digest(&digest::SHA256, b"BLS-SIG-KEYGEN-SALT-");
let mut sha256 = Sha256::new();
sha256.update(b"BLS-SIG-KEYGEN-SALT-");
hkdf(
&sha256.finalize_fixed().to_vec(),
&tmp,
b"\x00\x30",
&mut okm,
); // L=48, info=I2OSP(L,2)
let r = BigUint::from_str_radix(
"73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",
16,
Expand Down Expand Up @@ -188,7 +199,6 @@ mod tests {
use crate::{Derive, DeterministicPrivateKey, PrivateKey};
use hex;
use num_bigint::BigUint;
use num_traits::{FromPrimitive, Num};

struct TestVector {
seed: &'static str,
Expand All @@ -202,21 +212,21 @@ mod tests {
let test_vectors = vec!(
TestVector{
seed : "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04",
master_sk : "5399117110774477986698372024995405256382522670366369834617409486544348441851",
master_sk : "6083874454709270928345386274498605044986640685124978867557563392430687146096",
child_index : "0",
child_sk : "11812940737387919040225825939013910852517748782307378293770044673328955938106",
child_sk : "20397789859736650942317412262472558107875392172444076792671091975210932703118",
},
TestVector{
seed: "3141592653589793238462643383279502884197169399375105820974944592",
master_sk: "36167147331491996618072159372207345412841461318189449162487002442599770291484",
master_sk: "29757020647961307431480504535336562678282505419141012933316116377660817309383",
child_index: "3141592653",
child_sk: "41787458189896526028601807066547832426569899195138584349427756863968330588237",
child_sk: "25457201688850691947727629385191704516744796114925897962676248250929345014287",
},
TestVector{
seed: "0099FF991111002299DD7744EE3355BBDD8844115566CC55663355668888CC00",
master_sk: "13904094584487173309420026178174172335998687531503061311232927109397516192843",
master_sk: "27580842291869792442942448775674722299803720648445448686099262467207037398656",
child_index: "4294967295",
child_sk: "12482522899285304316694838079579801944734479969002030150864436005368716366140",
child_sk: "29358610794459428860402234341874281240803786294062035874021252734817515685787",
}
);

Expand Down Expand Up @@ -253,12 +263,49 @@ mod tests {

assert_eq!(
hex::encode(dsk.private_key().to_bytes()),
"fbec74a665b4f52d36a1717c83b21e62051cd5cd90f1c81c4664a6f4bfcaef0b"
"7050b4223168ae407dee804d461fc3dbfe53f5dc5218debb8fab6379d559730d"
);

assert_eq!(
hex::encode(dsk.derive("m/0").unwrap().private_key().to_bytes()),
"3a5542a9fef97a0f6b776fbe5e8edb0e087457be81223b1e1f40836834e31d1a"
"8e0fe539158c9d590a771420cc033baedaf3749b5c08b5f85bd1e6146cbd182d"
);
}

#[test]
fn eth2_withdrawal_address_test() {
let dsk = BLSDeterministicPrivateKey::from_seed(
&hex::decode("ee3fce3ccf05a2b58c851e321077a63ee2113235112a16fc783dc16279ff818a549ff735ac4406c624235db2d37108e34c6cbe853cbe09eb9e2369e6dd1c5aaa").unwrap()).unwrap();
assert_eq!(
dsk.0,
"18563599344197674528480235454076968403807977642577320252460493386276600523197"
.parse::<BigUint>()
.expect("invalid master key format")
);

let child_sk = hex::encode(
dsk.derive("m/12381/3600/1/0/0")
.unwrap()
.private_key()
.to_bytes(),
);
assert_eq!(
child_sk,
"ba87c3a478ee2a5a26c48918cc99be88bc648bee3d38c2d5faad41872a9e0d06"
);

let dsk = BLSDeterministicPrivateKey::from_seed(
&hex::decode("ed93db74a05f1a93b607ac20b447152aedfeb1f541c75abbb415c068eacdd9cd4f46f97b4ee0bbe99255016e3121ff7d283c5ab9a5d235829870b76e6e070061").unwrap()).unwrap();

let child_sk = hex::encode(
dsk.derive("m/12381/3600/0/0/0")
.unwrap()
.private_key()
.to_bytes(),
);
assert_eq!(
child_sk,
"46c50b0327f01e713b27c976fcc893cf19cff729e75b70dc5caa8b3d8c1df700"
);
}
}
2 changes: 1 addition & 1 deletion tcx-primitive/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

use crate::bls::{BLSPrivateKey, BLSPublicKey};
use crate::bls_derive::BLSDeterministicPrivateKey;
use crate::ecc::TypedDeterministicPrivateKey::{Bip32Ed25519, Bip32Sepc256k1, SubSr25519};
use crate::ecc::TypedDeterministicPrivateKey::{Bip32Ed25519, SubSr25519};
use crate::ed25519::{Ed25519PrivateKey, Ed25519PublicKey};
use crate::ed25519_bip32::{Ed25519DeterministicPrivateKey, Ed25519DeterministicPublicKey};
use crate::sr25519::{Sr25519PrivateKey, Sr25519PublicKey};
Expand Down
9 changes: 4 additions & 5 deletions tcx-primitive/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ impl FromHex for Ed25519PublicKey {
#[cfg(test)]
mod test {
use crate::ed25519::{Ed25519PrivateKey, Ed25519PublicKey};
use crate::{FromHex, PrivateKey, PublicKey, Ss58Codec, ToHex};
use bitcoin_hashes::Hash;
use blake2b_simd::{blake2b, Params};
use crate::{FromHex, PrivateKey, PublicKey, ToHex};
use blake2b_simd::Params;
use hex;
#[test]
fn from_slice_test() {
Expand Down Expand Up @@ -122,8 +121,8 @@ mod test {
let msg = hex::decode("03ffaa").unwrap();
let mut params = Params::new();
params.hash_length(32);
let genericHash = params.hash(&msg[..]);
let sign_result = sk.sign(&genericHash.as_bytes()).unwrap();
let generic_hash = params.hash(&msg[..]);
let sign_result = sk.sign(&generic_hash.as_bytes()).unwrap();
// println!("sign result : {}", hex::encode(sign_result));
let expected_val = "eaab7f4066217b072b79609a9f76cdfadd93f8dde41763887e131c02324f18c8e41b1009e334baf87f9d2e917bf4c0e73165622e5522409a0c5817234a48cc02";
assert_eq!(hex::encode(sign_result), expected_val);
Expand Down
11 changes: 1 addition & 10 deletions tcx-primitive/src/ed25519_bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,7 @@ mod test {
use crate::Derive;
use bip39::{Language, Mnemonic, Seed};
use hex;
use iop_keyvault::{ExtendedPrivateKey, PrivateKey};

fn default_seed() -> Seed {
let mn = Mnemonic::from_phrase(
"inject kidney empty canal shadow pact comfort wife crush horse wife sketch",
Language::English,
)
.unwrap();
Seed::new(&mn, "")
}
use iop_keyvault::ExtendedPrivateKey;

#[test]
fn from_seed_test() {
Expand Down
25 changes: 25 additions & 0 deletions tcx-proto/src/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,30 @@ message WalletKeyParam {
string password = 2;
}

message ZksyncPrivateKeyFromSeedParam {
string seed = 1;
}

message ZksyncPrivateKeyFromSeedResult {
string privKey = 1;
}

message ZksyncSignMusigParam{
string privKey = 1;
string bytes = 2;
}

message ZksyncSignMusigResult{
string signature = 1;
}

message ZksyncPrivateKeyToPubkeyHashParam{
string privKey = 1;
}

message ZksyncPrivateKeyToPubkeyHashResult{
string pubKeyHash = 1;
}



Loading