Skip to content

Commit

Permalink
Merge branch 'dev' into feature/imkey-bitcoin-psbt
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoguang1010 committed Aug 15, 2024
2 parents 19c66ac + 3fbf1da commit 1375ab5
Show file tree
Hide file tree
Showing 30 changed files with 335 additions and 94 deletions.
64 changes: 58 additions & 6 deletions token-core/tcx-atom/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,36 @@ use tcx_primitive::TypedPublicKey;

// size of address
pub const LENGTH: usize = 20;

#[derive(PartialEq, Eq, Clone)]
pub struct AtomAddress(String);

impl Address for AtomAddress {
fn from_public_key(public_key: &TypedPublicKey, _coin: &CoinInfo) -> Result<Self> {
let prefix = "cosmos";

fn from_public_key(public_key: &TypedPublicKey, coin: &CoinInfo) -> Result<Self> {
let pub_key_bytes = public_key.to_bytes();
let mut bytes = [0u8; LENGTH];
let pub_key_hash = ripemd160(&sha256(&pub_key_bytes));
bytes.copy_from_slice(&pub_key_hash[..LENGTH]);
let hrp = if (coin.hrp.is_empty()) {
"cosmos"
} else {
coin.hrp.as_str()
};

Ok(AtomAddress(bech32::encode(
prefix,
hrp,
bytes.to_base32(),
Variant::Bech32,
)?))
}

fn is_valid(address: &str, _coin: &CoinInfo) -> bool {
fn is_valid(address: &str, coin: &CoinInfo) -> bool {
let ret = bech32::decode(address);
if let Ok(val) = ret {
let (hrp, data, _) = val;
let data = Vec::from_base32(&data).unwrap();

if hrp.as_str() != "cosmos" {
if coin.hrp != hrp.as_str() {
return false;
}

Expand Down Expand Up @@ -78,6 +82,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
hrp: "cosmos".to_string(),
}
}

Expand Down Expand Up @@ -154,10 +159,57 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "cosmos".to_string(),
};
let address = AtomAddress::from_public_key(&pub_key, &coin_info)
.unwrap()
.to_string();
assert_eq!(address, "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02");
}

#[test]
fn test_sub_net() {
let pub_key_hex = "0317f65e6736ad182b47e386af40af0f26fb524bdd94e172a6145a6603d65a44b2";
let pub_key =
TypedPublicKey::from_slice(CurveType::SECP256k1, &Vec::from_hex(pub_key_hex).unwrap())
.unwrap();

let testcase = [
(
"dydx",
"m/44'/118'/0'/0/0",
"dydx1m566v5rcklnac8vc0dftfu4lnvznhlu7yg830z",
),
(
"osmo",
"m/44'/118'/0'/0/0",
"osmo1m566v5rcklnac8vc0dftfu4lnvznhlu79269e8",
),
(
"neutron",
"m/44'/118'/0'/0/0",
"neutron1m566v5rcklnac8vc0dftfu4lnvznhlu7fwqh4j",
),
(
"stride",
"m/44'/118'/0'/0/0",
"stride1m566v5rcklnac8vc0dftfu4lnvznhlu7w6ffme",
),
];
for (hrp, path, expected_addr) in testcase {
let coin_info = CoinInfo {
coin: "COSMOS".to_string(),
derivation_path: path.to_string(),
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: hrp.to_string(),
};

let address = AtomAddress::from_public_key(&pub_key, &coin_info)
.unwrap()
.to_string();
assert_eq!(address, expected_addr)
}
}
}
1 change: 1 addition & 0 deletions token-core/tcx-atom/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};
let mut guard = KeystoreGuard::unlock_by_password(&mut keystore, TEST_PASSWORD).unwrap();

Expand Down
35 changes: 35 additions & 0 deletions token-core/tcx-btc-kin/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl Display for BtcKinAddress {

#[cfg(test)]
mod tests {
use bitcoin::SchnorrSighashType::Default;
use std::str::FromStr;
use tcx_common::{FromHex, ToHex};

Expand All @@ -273,6 +274,7 @@ mod tests {

use crate::address::BtcKinAddress;
use crate::tcx_keystore::Address;
use crate::tests::sample_hd_keystore;
use crate::BtcKinNetwork;

#[test]
Expand Down Expand Up @@ -302,6 +304,18 @@ mod tests {
.unwrap()
.to_string();
assert_eq!(addr, "bc1qum864wd9nwsc0u9ytkctz6wzrw6g7zdntm7f4e");

let network = BtcKinNetwork::find_by_coin("DOGECOIN", "MAINNET").unwrap();
let addr = BtcKinAddress::p2pkh(&pub_key, &network)
.unwrap()
.to_string();
assert_eq!(addr, "DSBWjKzZtz7fPzu4N6mBRwQFHCQ6KQSjue");

let network = BtcKinNetwork::find_by_coin("DOGECOIN", "TESTNET").unwrap();
let addr = BtcKinAddress::p2pkh(&pub_key, &network)
.unwrap()
.to_string();
assert_eq!(addr, "nqEaTLjUpxaPGyUFPvQdgLzYX4nPLCD1Py");
}

#[test]
Expand Down Expand Up @@ -341,6 +355,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
};
let ltc_xprv_str = BtcKinAddress::extended_private_key(&anprv, &coin_info).unwrap();
assert_eq!("xprv9yrdwPSRnvomqFK4u1y5uW2SaXS2Vnr3pAYTjJjbyRZR8p9BwoadRsCxtgUFdAKeRPbwvGRcCSYMV69nNK4N2kadevJ6L5iQVy1SwGKDTHQ", ltc_xprv_str);
Expand All @@ -360,6 +375,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
};
let ltc_xprv_str = BtcKinAddress::extended_public_key(&anpub, &coin_info).unwrap();
assert_eq!("xpub6JeaAjhtvtjCDnEo4Bjr7uEbGccaHnJtLY4aBnMaAYGjkBRB3fP9XvjcCbNjMiU1n5tt7dYKVgHPGzh3t3W6eLBxavxABTaoQ2jhbiQrfe4", ltc_xprv_str);
Expand Down Expand Up @@ -451,6 +467,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
};
let address = BtcKinAddress::from_public_key(&pub_key, &coin_info)
.unwrap()
Expand All @@ -477,6 +494,23 @@ mod tests {
assert_eq!(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4");
}

#[test]
fn test_dogecoin_address() {
let mut hd = sample_hd_keystore();
let account = hd
.derive_coin::<BtcKinAddress>(&CoinInfo {
coin: "DOGECOIN".to_string(),
derivation_path: "m/44'/3'/0'/0/0".to_string(),
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
})
.unwrap();
assert_eq!(account.address, "DQ4tVEqdPWHc1aVBm4Sfwft8XyNRPMEchR");
assert_eq!(account.ext_pub_key, "xpub6CDSaXHQokkKmHHG2kNCFZeirJkcZgRZE97ZZUtViif3SFHSNVAvRpWC3CxeRt2VZetEGCcPTmWEFpKF4NDeeZrMNPQgfUaX5Hkw89kW8qE");
}


#[test]
fn test_bip84_spec_vector() {
let pub_key = TypedPublicKey::from_slice(
Expand All @@ -491,6 +525,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "VERSION_0".to_string(),
hrp: "".to_string(),
};
let address = BtcKinAddress::from_public_key(&pub_key, &coin_info)
.unwrap()
Expand Down
2 changes: 2 additions & 0 deletions token-core/tcx-btc-kin/src/bch_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};
let ret = BchAddress::from_public_key(&pk, &wrong_coin_info);
assert_eq!(format!("{}", ret.err().unwrap()), "missing_network");
Expand Down Expand Up @@ -287,6 +288,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
};
let address = BchAddress::from_public_key(&pub_key, &coin_info)
.unwrap()
Expand Down
18 changes: 18 additions & 0 deletions token-core/tcx-btc-kin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub use transaction::{BtcKinTxInput, BtcKinTxOutput, OmniTxInput, Utxo};
pub const BITCOIN: &str = "BITCOIN";
pub const BITCOINCASH: &str = "BITCOINCASH";

pub const DOGECOIN: &str = "DOGECOIN";

pub const LITECOIN: &str = "LITECOIN";

pub const OMNI: &str = "OMNI";
Expand Down Expand Up @@ -90,6 +92,22 @@ pub mod bitcoincash {
pub type MessageOutput = crate::transaction::BtcMessageOutput;
}

pub mod dogecoin {
use crate::DOGECOIN;

pub static CHAINS: [&str; 1] = [DOGECOIN];

pub type Address = crate::BtcKinAddress;

pub type TransactionInput = crate::transaction::BtcKinTxInput;

pub type TransactionOutput = crate::transaction::BtcKinTxOutput;

pub type MessageInput = crate::transaction::BtcMessageInput;

pub type MessageOutput = crate::transaction::BtcMessageOutput;
}

pub mod omni {
use crate::OMNI;

Expand Down
7 changes: 7 additions & 0 deletions token-core/tcx-btc-kin/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tcx_keystore::{Address, Keystore, MessageSigner, SignatureParameters};

const UTXO: &str = "0000000000000000000000000000000000000000000000000000000000000000";
const TAG: &str = "BIP0322-signed-message";

fn get_spend_tx_id(data: &[u8], script_pub_key: Script) -> Result<Txid> {
let tag_hash = sha256(&TAG.as_bytes().to_vec());
let mut to_sign = Vec::new();
Expand Down Expand Up @@ -107,6 +108,7 @@ impl MessageSigner<BtcMessageInput, BtcMessageOutput> for Keystore {
curve: CurveType::SECP256k1,
network: params.network.to_string(),
seg_wit: params.seg_wit.to_string(),
hrp: "".to_string(),
};

let address = BtcKinAddress::from_public_key(&public_key, &coin_info)?;
Expand Down Expand Up @@ -156,6 +158,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "VERSION_0".to_string(),
hrp: "".to_string(),
};

let account = ks.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand All @@ -179,6 +182,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "P2WPKH".to_string(),
hrp: "".to_string(),
};

let account = ks.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand Down Expand Up @@ -214,6 +218,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
};

let account = ks.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand Down Expand Up @@ -250,6 +255,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "VERSION_0".to_string(),
hrp: "".to_string(),
};

let account = ks.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand Down Expand Up @@ -285,6 +291,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "VERSION_1".to_string(),
hrp: "".to_string(),
};

let account = ks.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand Down
22 changes: 21 additions & 1 deletion token-core/tcx-btc-kin/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct BtcKinNetwork {
pub xpub_prefix: [u8; 4],
pub xprv_prefix: [u8; 4],
}
const BTC_KIN_NETWORKS: [BtcKinNetwork; 6] = [
const BTC_KIN_NETWORKS: [BtcKinNetwork; 8] = [
BtcKinNetwork {
coin: "BITCOIN",
network: "MAINNET",
Expand Down Expand Up @@ -72,6 +72,26 @@ const BTC_KIN_NETWORKS: [BtcKinNetwork; 6] = [
xpub_prefix: [0x04, 0x35, 0x87, 0xcf],
xprv_prefix: [0x04, 0x35, 0x83, 0x94],
},
BtcKinNetwork {
coin: "DOGECOIN",
network: "TESTNET",
bech32_hrp: "",
p2pkh_prefix: 0x71,
p2sh_prefix: 0xc4,
private_prefix: 0xf1,
xpub_prefix: [0x04, 0x35, 0x87, 0xcf],
xprv_prefix: [0x04, 0x35, 0x83, 0x94],
},
BtcKinNetwork {
coin: "DOGECOIN",
network: "MAINNET",
bech32_hrp: "",
p2pkh_prefix: 0x1e,
p2sh_prefix: 0x16,
private_prefix: 0x9e,
xpub_prefix: [0x02, 0xfa, 0xca, 0xfd],
xprv_prefix: [0x02, 0xfa, 0xc3, 0x98],
},
];

impl BtcKinNetwork {
Expand Down
2 changes: 2 additions & 0 deletions token-core/tcx-btc-kin/src/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "VERSION_1".to_string(),
hrp: "".to_string(),
};

let account = hd.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand Down Expand Up @@ -484,6 +485,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "VERSION_1".to_string(),
hrp: "".to_string(),
};

let account = hd.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion token-core/tcx-btc-kin/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl<T: Address + ScriptPubkey + FromStr<Err = anyhow::Error>> KinTransaction<T>
curve: params.curve,
network: params.network.clone(),
seg_wit: params.seg_wit.clone(),
hrp: "".to_string(),
};

let change_script = if let Some(change_address_index) = self.change_address_index && keystore.derivable() {
Expand Down Expand Up @@ -1386,7 +1387,7 @@ mod tests {
op_return: None,
};

let mut ks = wif_keystore("mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1");
let mut ks = wif_keystore(TEST_WIF);

let params = SignatureParameters {
curve: CurveType::SECP256k1,
Expand Down
Loading

0 comments on commit 1375ab5

Please sign in to comment.