Skip to content

Commit

Permalink
feat: add hrp to coininfo
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Jul 30, 2024
1 parent 9681831 commit 70c1e84
Show file tree
Hide file tree
Showing 28 changed files with 261 additions and 93 deletions.
59 changes: 53 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,52 @@ 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",
),
];
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
5 changes: 5 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 Down Expand Up @@ -341,6 +342,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 +362,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 +454,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 Down Expand Up @@ -491,6 +495,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
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
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
5 changes: 5 additions & 0 deletions token-core/tcx-ckb/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: network.to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let pub_key = TypedPublicKey::from_slice(
Expand Down Expand Up @@ -132,6 +133,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: network.to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};
assert!(CkbAddress::is_valid(address, &coin_info));
}
Expand All @@ -147,6 +149,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: network.to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};
assert!(!CkbAddress::is_valid(address, &coin_info));
}
Expand All @@ -167,6 +170,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};
assert!(!CkbAddress::is_valid(invalid_address, &coin_info));
}
Expand All @@ -192,6 +196,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};
let address = CkbAddress::from_public_key(&pub_key, &coin_info)
.unwrap()
Expand Down
8 changes: 8 additions & 0 deletions token-core/tcx-ckb/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -439,6 +440,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let _account = ks.derive_coin::<CkbAddress>(&coin_info).unwrap().clone();
Expand Down Expand Up @@ -521,6 +523,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -586,6 +589,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -655,6 +659,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -692,6 +697,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -738,6 +744,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -884,6 +891,7 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-constants/src/btc_fork_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ mod test {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
})
.unwrap();

Expand Down
Loading

0 comments on commit 70c1e84

Please sign in to comment.