Skip to content

Commit

Permalink
Method to eliminate duplicate implementiations and unify the processi…
Browse files Browse the repository at this point in the history
…ng of hex in tcx_common
  • Loading branch information
tyrone committed Dec 10, 2023
1 parent 93589e1 commit bf8fc21
Show file tree
Hide file tree
Showing 66 changed files with 503 additions and 709 deletions.
9 changes: 7 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions token-core/tcx-atom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tcx-crypto = { path = "../tcx-crypto" }
tcx-keystore = { path = "../tcx-keystore" }
tcx-primitive = { path = "../tcx-primitive" }
tcx-constants = { path = "../tcx-constants" }
tcx-common = { path = "../tcx-common" }

bech32 = "0.9.1"
failure = "0.1.8"
Expand Down
14 changes: 6 additions & 8 deletions token-core/tcx-atom/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::str::FromStr;

use bech32::{FromBase32, ToBase32, Variant};
use tcx_common::{ripemd160, sha256};
use tcx_constants::CoinInfo;
use tcx_crypto::hash;
use tcx_keystore::{Address, Result};
use tcx_primitive::TypedPublicKey;

Expand All @@ -17,7 +17,7 @@ impl Address for AtomAddress {

let pub_key_bytes = public_key.to_bytes();
let mut bytes = [0u8; LENGTH];
let pub_key_hash = hash::ripemd160(&hash::sha256(&pub_key_bytes));
let pub_key_hash = ripemd160(&sha256(&pub_key_bytes));
bytes.copy_from_slice(&pub_key_hash[..LENGTH]);

Ok(AtomAddress(
Expand Down Expand Up @@ -66,8 +66,8 @@ mod tests {
use crate::address::AtomAddress;
use tcx_keystore::Address;

use tcx_common::FromHex;
use tcx_constants::{CoinInfo, CurveType};
use tcx_crypto::hex;
use tcx_primitive::TypedPublicKey;

fn get_test_coin() -> CoinInfo {
Expand All @@ -94,11 +94,9 @@ mod tests {
];

for (sec_key, expected_addr) in test_cases {
let pub_key = TypedPublicKey::from_slice(
CurveType::SECP256k1,
&hex::hex_to_bytes(sec_key).unwrap(),
)
.unwrap();
let pub_key =
TypedPublicKey::from_slice(CurveType::SECP256k1, &Vec::from_hex(sec_key).unwrap())
.unwrap();

let addr = AtomAddress::from_public_key(&pub_key, &get_test_coin()).unwrap();
assert_eq!(addr.to_string(), expected_addr);
Expand Down
6 changes: 3 additions & 3 deletions token-core/tcx-atom/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tcx_keystore::{
Keystore, Result, SignatureParameters, Signer, TransactionSigner as TraitTransactionSigner,
};

use tcx_crypto::{hash, hex};
use tcx_common::{sha256, FromHex};

use base64;

Expand All @@ -15,8 +15,8 @@ impl TraitTransactionSigner<AtomTxInput, AtomTxOutput> for Keystore {
params: &SignatureParameters,
tx: &AtomTxInput,
) -> Result<AtomTxOutput> {
let data = hex::hex_to_bytes(&tx.raw_data)?;
let hash = hash::sha256(&data);
let data = Vec::from_hex_auto(&tx.raw_data)?;
let hash = sha256(&data);

let sign_result =
self.secp256k1_ecdsa_sign_recoverable(&hash[..], &params.derivation_path)?;
Expand Down
11 changes: 6 additions & 5 deletions token-core/tcx-btc-kin/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ impl Display for BtcKinAddress {
#[cfg(test)]
mod tests {
use std::str::FromStr;
use tcx_common::{FromHex, ToHex};

use tcx_constants::coin_info::coin_info_from_param;
use tcx_constants::{CoinInfo, CurveType};
Expand All @@ -300,7 +301,7 @@ mod tests {
#[test]
pub fn test_btc_kin_address() {
let pub_key_str = "02506bc1dc099358e5137292f4efdd57e400f29ba5132aa5d12b18dac1c1f6aaba";
let pub_key = hex::decode(pub_key_str).unwrap();
let pub_key = Vec::from_hex(pub_key_str).unwrap();
let network = BtcKinNetwork::find_by_coin("LITECOIN", "MAINNET").unwrap();
let addr = BtcKinAddress::p2shwpkh(&pub_key, &network)
.unwrap()
Expand Down Expand Up @@ -390,19 +391,19 @@ mod tests {
#[test]
pub fn test_script_pubkey() {
let addr = BtcKinAddress::from_str("MR5Hu9zXPX3o9QuYNJGft1VMpRP418QDfW").unwrap();
let script = hex::encode(addr.script_pubkey().as_bytes());
let script = addr.script_pubkey().as_bytes().to_hex();
assert_eq!("a914bc64b2d79807cd3d72101c3298b89117d32097fb87", script);

let addr = BtcKinAddress::from_str("ltc1qum864wd9nwsc0u9ytkctz6wzrw6g7zdn08yddf").unwrap();
let script = hex::encode(addr.script_pubkey().as_bytes());
let script = addr.script_pubkey().as_bytes().to_hex();
assert_eq!("0014e6cfaab9a59ba187f0a45db0b169c21bb48f09b3", script);

let addr = BtcKinAddress::from_str("Ldfdegx3hJygDuFDUA7Rkzjjx8gfFhP9DP").unwrap();
let script = hex::encode(addr.script_pubkey().as_bytes());
let script = addr.script_pubkey().as_bytes().to_hex();
assert_eq!("76a914ca4d8acded69ce4f05d0925946d261f86c675fd888ac", script);

let addr = BtcKinAddress::from_str("3Js9bGaZSQCNLudeGRHL4NExVinc25RbuG").unwrap();
let script = hex::encode(addr.script_pubkey().as_bytes());
let script = addr.script_pubkey().as_bytes().to_hex();
assert_eq!("a914bc64b2d79807cd3d72101c3298b89117d32097fb87", script);
}

Expand Down
13 changes: 9 additions & 4 deletions token-core/tcx-btc-kin/src/bch_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl ScriptPubkey for BchAddress {
#[cfg(test)]
mod tests {
use super::{remove_bch_prefix, BchAddress};
use tcx_common::FromHex;

use crate::address::WIFDisplay;
use tcx_constants::coin_info::coin_info_from_param;
Expand Down Expand Up @@ -131,8 +132,10 @@ mod tests {
let addr = BchAddress::from_public_key(
&TypedPublicKey::from_slice(
CurveType::SECP256k1,
&hex::decode("026b5b6a9d041bc5187e0b34f9e496436c7bff261c6c1b5f3c06b433c61394b868")
.unwrap(),
&Vec::from_hex(
"026b5b6a9d041bc5187e0b34f9e496436c7bff261c6c1b5f3c06b433c61394b868",
)
.unwrap(),
)
.unwrap(),
&coin_info,
Expand All @@ -147,8 +150,10 @@ mod tests {
let addr = BchAddress::from_public_key(
&TypedPublicKey::from_slice(
CurveType::SECP256k1,
&hex::decode("026b5b6a9d041bc5187e0b34f9e496436c7bff261c6c1b5f3c06b433c61394b868")
.unwrap(),
&Vec::from_hex(
"026b5b6a9d041bc5187e0b34f9e496436c7bff261c6c1b5f3c06b433c61394b868",
)
.unwrap(),
)
.unwrap(),
&coin_info,
Expand Down
14 changes: 7 additions & 7 deletions token-core/tcx-btc-kin/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<T: Address + ScriptPubkey + FromStr<Err = failure::Error>> KinTransaction<T
if let Some(op_return) = &self.op_return {
tx_outs.push(TxOut {
value: 0,
script_pubkey: Script::new_op_return(&hex::decode(op_return)?),
script_pubkey: Script::new_op_return(&Vec::from_hex(op_return)?),
});
}

Expand Down Expand Up @@ -411,7 +411,7 @@ impl TransactionSigner<OmniTxInput, BtcKinTxOutput> for Keystore {
*/
let create_omni_op_return = || {
let mut wtr = Vec::new();
wtr.write(&hex::decode("6f6d6e6900000000").unwrap())
wtr.write(&Vec::from_hex("6f6d6e6900000000").unwrap())
.unwrap();
wtr.write_u32::<BigEndian>(tx.property_id).unwrap();
wtr.write_u64::<BigEndian>(tx.amount).unwrap();
Expand Down Expand Up @@ -790,29 +790,29 @@ mod tests {

let actual = ks.sign_transaction(&params, &tx_input).unwrap();

let tx = Transaction::deserialize(&hex::decode(&actual.raw_tx).unwrap()).unwrap();
let tx = Transaction::deserialize(&Vec::from_hex(&actual.raw_tx).unwrap()).unwrap();

let msg = Message::from_slice(
&hex::decode("f01ba76b329132e48188ad10d00791647ee6d2f7fee5ef397f3481993c898de3")
&Vec::from_hex("f01ba76b329132e48188ad10d00791647ee6d2f7fee5ef397f3481993c898de3")
.unwrap(),
)
.unwrap();
let sig = Signature::from_slice(&tx.input[0].witness.to_vec()[0]).unwrap();
let pub_key = XOnlyPublicKey::from_slice(
&hex::decode("8f4ca6a7384f50a1fe00cba593d5a834b480c65692a76ae6202e1ce46cb1c233")
&Vec::from_hex("8f4ca6a7384f50a1fe00cba593d5a834b480c65692a76ae6202e1ce46cb1c233")
.unwrap(),
)
.unwrap();
assert!(sig.verify(&msg, &pub_key).is_ok());

let msg = Message::from_slice(
&hex::decode("d0691b5ac1b338b9341790ea69417cb454cf346a718342fb4a846dbb8ae142e8")
&Vec::from_hex("d0691b5ac1b338b9341790ea69417cb454cf346a718342fb4a846dbb8ae142e8")
.unwrap(),
)
.unwrap();
let sig = Signature::from_slice(&tx.input[1].witness.to_vec()[0]).unwrap();
let pub_key = XOnlyPublicKey::from_slice(
&hex::decode("9303a116174dd21ea473766659568ac24eb6b828c3ee998982d2ba070ea06155")
&Vec::from_hex("9303a116174dd21ea473766659568ac24eb6b828c3ee998982d2ba070ea06155")
.unwrap(),
)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-ckb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tcx-crypto = { path = "../tcx-crypto" }
tcx-keystore = { path = "../tcx-keystore" }
tcx-primitive = { path = "../tcx-primitive" }
tcx-constants = { path = "../tcx-constants" }
tcx-common = { path = "../tcx-common"}

prost = "0.11.2"
bytes = "1.3.0"
Expand Down
6 changes: 4 additions & 2 deletions token-core/tcx-ckb/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ mod tests {

let pub_key = TypedPublicKey::from_slice(
CurveType::SECP256k1,
&hex::decode("024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01")
.unwrap(),
&Vec::from_hex(
"024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
)
.unwrap(),
)
.unwrap();
let addr = CkbAddress::from_public_key(&pub_key, &coin_info).unwrap();
Expand Down
43 changes: 0 additions & 43 deletions token-core/tcx-ckb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use failure::Fail;

pub use address::CkbAddress;
pub use serializer::Serializer;
use tcx_keystore::Result;
pub use transaction::{CachedCell, CellInput, CkbTxInput, CkbTxOutput, OutPoint, Script, Witness};

#[derive(Fail, Debug, PartialEq)]
Expand Down Expand Up @@ -47,52 +46,10 @@ pub enum Error {
#[fail(display = "invalid_hex_value")]
InvalidHexValue,
}

pub fn hex_to_bytes(value: &str) -> Result<Vec<u8>> {
let result = if value.starts_with("0x") || value.starts_with("0X") {
hex::decode(&value[2..])
} else {
hex::decode(&value[..])
};

result.map_err(|_| Error::InvalidHexValue.into())
}

pub mod nervos {
use tcx_constants::{CoinInfo, CurveType};
use tcx_keystore::{Account, Keystore};

pub const CHAINS: [&'static str; 1] = ["NERVOS"];

pub type Address = crate::address::CkbAddress;
pub type TransactionInput = crate::transaction::CkbTxInput;
pub type TransactionOutput = crate::transaction::CkbTxOutput;

pub fn enable_account(
_: &str,
_index: u32,
keystore: &mut Keystore,
) -> Result<Vec<Account>, failure::Error> {
keystore.derive_coins::<crate::CkbAddress>(&[CoinInfo {
coin: "NERVOS".to_string(),
derivation_path: "m/44'/309'/0'/0/0".to_string(),
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
}])
}
}

#[cfg(test)]
mod tests {
use crate::hex_to_bytes;

#[test]
pub fn hex_convert() {
let v: Vec<u8> = vec![];
assert_eq!(v, hex_to_bytes("0x").unwrap());
assert_eq!(vec![0x01], hex_to_bytes("0x01").unwrap());
assert_eq!(vec![0x02], hex_to_bytes("0x02").unwrap());
assert_eq!(vec![0x02, 0x11], hex_to_bytes("0x0211").unwrap());
}
}
Loading

0 comments on commit bf8fc21

Please sign in to comment.