Skip to content

Commit

Permalink
feat: cosmos derive account by chainId
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Sep 5, 2024
1 parent 744da3c commit 425341d
Show file tree
Hide file tree
Showing 28 changed files with 23 additions and 297 deletions.
38 changes: 10 additions & 28 deletions token-core/tcx-atom/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ use tcx_primitive::TypedPublicKey;
// size of address
pub const LENGTH: usize = 20;

fn verify_hrp(coin_info: &CoinInfo) -> Result<()> {
fn find_hrp(chain_id: &str) -> Result<String> {
let map = CHAIN_ID_HRP_MAP.read().unwrap();
let embed_hrp = map.get(&coin_info.chain_id);
let embed_hrp = map.get(chain_id);

let Some(embed_hrp) = embed_hrp else {
return Err(anyhow!("unknown_chain_id"))
};
if (&coin_info.hrp != embed_hrp) {
Err(anyhow!("chain_id_and_hrp_not_match"))
return if embed_hrp.is_some() {
Ok(embed_hrp.unwrap().to_string())
} else {
Ok(())
}
Err(anyhow!("unknown_chain_id"))
};
}

#[derive(PartialEq, Eq, Clone)]
Expand All @@ -34,15 +31,10 @@ impl Address for AtomAddress {
let mut bytes = [0u8; LENGTH];
let pub_key_hash = ripemd160(&sha256(&pub_key_bytes));
bytes.copy_from_slice(&pub_key_hash[..LENGTH]);
verify_hrp(&coin)?;
let hrp = if coin.hrp.is_empty() {
"cosmos"
} else {
coin.hrp.as_str()
};
let hrp = find_hrp(&coin.chain_id)?;

Ok(AtomAddress(bech32::encode(
hrp,
&hrp,
bytes.to_base32(),
Variant::Bech32,
)?))
Expand All @@ -54,10 +46,6 @@ impl Address for AtomAddress {
let (hrp, data, _) = val;
let data = Vec::from_base32(&data).unwrap();

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

if data.len() != 20 {
return false;
}
Expand Down Expand Up @@ -101,7 +89,6 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
hrp: "cosmos".to_string(),
}
}

Expand Down Expand Up @@ -179,7 +166,6 @@ 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()
Expand Down Expand Up @@ -228,7 +214,6 @@ mod tests {
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)
Expand All @@ -252,13 +237,12 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "cosmos-wrong".to_string(),
};
let address = AtomAddress::from_public_key(&pub_key, &coin_info);

assert_eq!(
format!("{}", address.err().unwrap()),
"chain_id_and_hrp_not_match"
format!("{}", address.unwrap().to_string()),
"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02"
);

let coin_info = CoinInfo {
Expand All @@ -268,7 +252,6 @@ 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);
Expand All @@ -281,7 +264,6 @@ 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);
Expand Down
1 change: 0 additions & 1 deletion token-core/tcx-atom/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ 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
6 changes: 0 additions & 6 deletions token-core/tcx-btc-kin/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ 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 @@ -382,7 +381,6 @@ 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 @@ -475,7 +473,6 @@ 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 @@ -513,7 +510,6 @@ mod tests {
curve: CurveType::SECP256k1,
network: "MAINNET".to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
})
.unwrap();
assert_eq!(account.address, "DQ4tVEqdPWHc1aVBm4Sfwft8XyNRPMEchR");
Expand Down Expand Up @@ -545,7 +541,6 @@ mod tests {
curve: CurveType::SECP256k1,
network: network.to_string(),
seg_wit: "NONE".to_string(),
hrp: "".to_string(),
};
let account = hd.derive_coin::<BtcKinAddress>(&coin_info).unwrap();
assert_eq!(account.ext_pub_key, xpub);
Expand All @@ -567,7 +562,6 @@ 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: 0 additions & 2 deletions token-core/tcx-btc-kin/src/bch_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ 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 @@ -290,7 +289,6 @@ 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
6 changes: 0 additions & 6 deletions token-core/tcx-btc-kin/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ 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 @@ -160,7 +159,6 @@ 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 @@ -185,7 +183,6 @@ 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 @@ -222,7 +219,6 @@ 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 @@ -260,7 +256,6 @@ 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 @@ -297,7 +292,6 @@ 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: 0 additions & 2 deletions token-core/tcx-btc-kin/src/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ 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 @@ -487,7 +486,6 @@ 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
1 change: 0 additions & 1 deletion token-core/tcx-btc-kin/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ 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
5 changes: 0 additions & 5 deletions token-core/tcx-ckb/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ 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 @@ -135,7 +134,6 @@ 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 @@ -152,7 +150,6 @@ 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 @@ -174,7 +171,6 @@ 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 @@ -201,7 +197,6 @@ 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: 0 additions & 8 deletions token-core/tcx-ckb/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

let mut ks =
Expand Down Expand Up @@ -442,7 +441,6 @@ 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 @@ -526,7 +524,6 @@ mod tests {
curve: CurveType::SECP256k1,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
hrp: "".to_string(),
};

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

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

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

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

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

let mut ks =
Expand Down
1 change: 0 additions & 1 deletion token-core/tcx-constants/src/btc_fork_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ 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 425341d

Please sign in to comment.