Skip to content

Commit

Permalink
test: test case optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoguang1010 committed Dec 20, 2024
1 parent 75cd34e commit 559ebde
Showing 1 changed file with 91 additions and 49 deletions.
140 changes: 91 additions & 49 deletions token-core/tcx-ton/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::str::FromStr;
use tcx_common::ToHex;
use tcx_constants::CoinInfo;
use tcx_keystore::{Address, Result};
use tcx_primitive::TypedPublicKey;
Expand Down Expand Up @@ -37,16 +36,10 @@ impl Address for TonAddress {
Ok(TonAddress(address))
}

fn is_valid(address: &str, coin: &CoinInfo) -> bool {
let result = TonAddressLib::from_base64_std_flags(address);
if let Ok(ton_addr) = result {
if (ton_addr.2 && coin.network.eq_ignore_ascii_case("TESTNET"))
|| (!ton_addr.2 && coin.network.eq_ignore_ascii_case("MAINNET"))
{
true
} else {
false
}
fn is_valid(address: &str, _coin: &CoinInfo) -> bool {
let result = TonAddressLib::from_base64_url_flags(address);
if result.is_ok() {
true
} else {
false
}
Expand All @@ -70,7 +63,9 @@ impl ToString for TonAddress {
#[cfg(test)]
mod test {
use tcx_constants::{CoinInfo, CurveType, TEST_MNEMONIC, TEST_PASSWORD};
use tcx_keystore::{HdKeystore, Keystore, Metadata};
use tcx_keystore::{Address, HdKeystore, Keystore, Metadata};

use super::TonAddress;

#[test]
fn test_ton_address() {
Expand All @@ -79,7 +74,7 @@ mod test {
let mut keystore = Keystore::Hd(hd);
keystore.unlock_by_password(TEST_PASSWORD).unwrap();

let coin = CoinInfo {
let mut coin = CoinInfo {
coin: "TON".to_string(),
derivation_path: "m/44'/607'/0'".to_string(),
curve: CurveType::ED25519,
Expand All @@ -89,70 +84,117 @@ mod test {
//V5R1 code
contract_code: "te6ccgECFAEAAoEAART/APSkE/S88sgLAQIBIAIDAgFIBAUBAvIOAtzQINdJwSCRW49jINcLHyCCEGV4dG69IYIQc2ludL2wkl8D4IIQZXh0brqOtIAg1yEB0HTXIfpAMPpE+Cj6RDBYvZFb4O1E0IEBQdch9AWDB/QOb6ExkTDhgEDXIXB/2zzgMSDXSYECgLmRMOBw4hAPAgEgBgcCASAICQAZvl8PaiaECAoOuQ+gLAIBbgoLAgFIDA0AGa3OdqJoQCDrkOuF/8AAGa8d9qJoQBDrkOuFj8AAF7Ml+1E0HHXIdcLH4AARsmL7UTQ1woAgAR4g1wsfghBzaWduuvLgin8PAeaO8O2i7fshgwjXIgKDCNcjIIAg1yHTH9Mf0x/tRNDSANMfINMf0//XCgAK+QFAzPkQmiiUXwrbMeHywIffArNQB7Dy0IRRJbry4IVQNrry4Ib4I7vy0IgikvgA3gGkf8jKAMsfAc8Wye1UIJL4D95w2zzYEAP27aLt+wL0BCFukmwhjkwCIdc5MHCUIccAs44tAdcoIHYeQ2wg10nACPLgkyDXSsAC8uCTINcdBscSwgBSMLDy0InXTNc5MAGk6GwShAe78uCT10rAAPLgk+1V4tIAAcAAkVvg69csCBQgkXCWAdcsCBwS4lIQseMPINdKERITAJYB+kAB+kT4KPpEMFi68uCR7UTQgQFB1xj0BQSdf8jKAEAEgwf0U/Lgi44UA4MH9Fvy4Iwi1woAIW4Bs7Dy0JDiyFADzxYS9ADJ7VQAcjDXLAgkji0h8uCS0gDtRNDSAFETuvLQj1RQMJExnAGBAUDXIdcKAPLgjuLIygBYzxbJ7VST8sCN4gAQk1vbMeHXTNA=".to_string(),
};

//V5R1-TESTNET
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"0QDBKGsYs49NgdqM4gMoiVMV9Re5hM-yy3nvR_4XB0ZbUHzx",
acc.address
);

let coin = CoinInfo {
coin: "TON".to_string(),
derivation_path: "m/44'/607'/0'".to_string(),
curve: CurveType::ED25519,
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
chain_id: "".to_string(),
//V5R1 code
contract_code: "te6ccgECFAEAAoEAART/APSkE/S88sgLAQIBIAIDAgFIBAUBAvIOAtzQINdJwSCRW49jINcLHyCCEGV4dG69IYIQc2ludL2wkl8D4IIQZXh0brqOtIAg1yEB0HTXIfpAMPpE+Cj6RDBYvZFb4O1E0IEBQdch9AWDB/QOb6ExkTDhgEDXIXB/2zzgMSDXSYECgLmRMOBw4hAPAgEgBgcCASAICQAZvl8PaiaECAoOuQ+gLAIBbgoLAgFIDA0AGa3OdqJoQCDrkOuF/8AAGa8d9qJoQBDrkOuFj8AAF7Ml+1E0HHXIdcLH4AARsmL7UTQ1woAgAR4g1wsfghBzaWduuvLgin8PAeaO8O2i7fshgwjXIgKDCNcjIIAg1yHTH9Mf0x/tRNDSANMfINMf0//XCgAK+QFAzPkQmiiUXwrbMeHywIffArNQB7Dy0IRRJbry4IVQNrry4Ib4I7vy0IgikvgA3gGkf8jKAMsfAc8Wye1UIJL4D95w2zzYEAP27aLt+wL0BCFukmwhjkwCIdc5MHCUIccAs44tAdcoIHYeQ2wg10nACPLgkyDXSsAC8uCTINcdBscSwgBSMLDy0InXTNc5MAGk6GwShAe78uCT10rAAPLgk+1V4tIAAcAAkVvg69csCBQgkXCWAdcsCBwS4lIQseMPINdKERITAJYB+kAB+kT4KPpEMFi68uCR7UTQgQFB1xj0BQSdf8jKAEAEgwf0U/Lgi44UA4MH9Fvy4Iwi1woAIW4Bs7Dy0JDiyFADzxYS9ADJ7VQAcjDXLAgkji0h8uCS0gDtRNDSAFETuvLQj1RQMJExnAGBAUDXIdcKAPLgjuLIygBYzxbJ7VST8sCN4gAQk1vbMeHXTNA=".to_string(),
};

//V5R1-MAINNET
coin.network = "MAINNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"UQDBKGsYs49NgdqM4gMoiVMV9Re5hM-yy3nvR_4XB0ZbUMd7",
acc.address
);

let coin = CoinInfo {
coin: "TON".to_string(),
derivation_path: "m/44'/607'/0'".to_string(),
curve: CurveType::ED25519,
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
chain_id: "".to_string(),
//V4R2 code
contract_code: "te6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f0x/THwL4I7vyZO1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8QERITAubQAdDTAyFxsJJfBOAi10nBIJJfBOAC0x8hghBwbHVnvSKCEGRzdHK9sJJfBeAD+kAwIPpEAcjKB8v/ydDtRNCBAUDXIfQEMFyBAQj0Cm+hMbOSXwfgBdM/yCWCEHBsdWe6kjgw4w0DghBkc3RyupJfBuMNBgcCASAICQB4AfoA9AQw+CdvIjBQCqEhvvLgUIIQcGx1Z4MesXCAGFAEywUmzxZY+gIZ9ADLaRfLH1Jgyz8gyYBA+wAGAIpQBIEBCPRZMO1E0IEBQNcgyAHPFvQAye1UAXKwjiOCEGRzdHKDHrFwgBhQBcsFUAPPFiP6AhPLassfyz/JgED7AJJfA+ICASAKCwBZvSQrb2omhAgKBrkPoCGEcNQICEekk30pkQzmkD6f+YN4EoAbeBAUiYcVnzGEAgFYDA0AEbjJftRNDXCx+AA9sp37UTQgQFA1yH0BDACyMoHy//J0AGBAQj0Cm+hMYAIBIA4PABmtznaiaEAga5Drhf/AABmvHfaiaEAQa5DrhY/AAG7SB/oA1NQi+QAFyMoHFcv/ydB3dIAYyMsFywIizxZQBfoCFMtrEszMyXP7AMhAFIEBCPRR8qcCAHCBAQjXGPoA0z/IVCBHgQEI9FHyp4IQbm90ZXB0gBjIywXLAlAGzxZQBPoCFMtqEssfyz/Jc/sAAgBsgQEI1xj6ANM/MFIkgQEI9Fnyp4IQZHN0cnB0gBjIywXLAlAFzxZQA/oCE8tqyx8Syz/Jc/sAAAr0AMntVGliJeU=".to_string();
coin.network = "TESTNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"0QAz8TQ4n_ktc--IpefK-d5ABVpTR-FciJgLZr9vUpEgaGxk",
acc.address
);
//V4R2-MAINNET
coin.network = "MAINNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"UQAz8TQ4n_ktc--IpefK-d5ABVpTR-FciJgLZr9vUpEgaNfu",
acc.address
);
//V4R1-TESTNET
coin.contract_code = "te6cckECFQEAAvUAART/APSkE/S88sgLAQIBIAIDAgFIBAUE+PKDCNcYINMf0x/THwL4I7vyY+1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8REhMUA+7QAdDTAwFxsJFb4CHXScEgkVvgAdMfIYIQcGx1Z70ighBibG5jvbAighBkc3RyvbCSXwPgAvpAMCD6RAHIygfL/8nQ7UTQgQFA1yH0BDBcgQEI9ApvoTGzkl8F4ATTP8glghBwbHVnupEx4w0kghBibG5juuMABAYHCAIBIAkKAFAB+gD0BDCCEHBsdWeDHrFwgBhQBcsFJ88WUAP6AvQAEstpyx9SEMs/AFL4J28ighBibG5jgx6xcIAYUAXLBSfPFiT6AhTLahPLH1Iwyz8B+gL0AACSghBkc3Ryuo41BIEBCPRZMO1E0IEBQNcgyAHPFvQAye1UghBkc3Rygx6xcIAYUATLBVjPFiL6AhLLassfyz+UEDRfBOLJgED7AAIBIAsMAFm9JCtvaiaECAoGuQ+gIYRw1AgIR6STfSmRDOaQPp/5g3gSgBt4EBSJhxWfMYQCAVgNDgARuMl+1E0NcLH4AD2ynftRNCBAUDXIfQEMALIygfL/8nQAYEBCPQKb6ExgAgEgDxAAGa3OdqJoQCBrkOuF/8AAGa8d9qJoQBBrkOuFj8AAbtIH+gDU1CL5AAXIygcVy//J0Hd0gBjIywXLAiLPFlAF+gIUy2sSzMzJcfsAyEAUgQEI9FHypwIAbIEBCNcYyFQgJYEBCPRR8qeCEG5vdGVwdIAYyMsFywJQBM8WghAF9eEA+gITy2oSyx/JcfsAAgBygQEI1xgwUgKBAQj0WfKn+CWCEGRzdHJwdIAYyMsFywJQBc8WghAF9eEA+gIUy2oTyx8Syz/Jc/sAAAr0AMntVEap808=".to_string();
coin.network = "TESTNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"0QC1hu4IopANSj01rqYO2Omy8uAKz_QDCkxzs2AwXdl2Pyw8",
acc.address
);
//V4R1-MAINNET
coin.network = "MAINNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"UQC1hu4IopANSj01rqYO2Omy8uAKz_QDCkxzs2AwXdl2P5e2",
acc.address
);
//V3R1-TESTNET
coin.contract_code = "te6cckEBAQEAYgAAwP8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/T/9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVD++buA=".to_string();
coin.network = "TESTNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"0QCXvH7F6nL71NHulDevmD8jxta-YlED42hHy_rr9KI1Bjbu",
acc.address
);
//V3R1-MAINNET
coin.network = "MAINNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"UQCXvH7F6nL71NHulDevmD8jxta-YlED42hHy_rr9KI1Bo1k",
acc.address
);
//V3R2-TESTNET
coin.contract_code = "te6cckEBAQEAcQAA3v8AIN0gggFMl7ohggEznLqxn3Gw7UTQ0x/THzHXC//jBOCk8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/T/9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVBC9ba0=".to_string();
coin.network = "TESTNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"0QCg7HwngbcHdJN0QQ4u9KiIXba2FS7A7sbME4zGHXm-78Tv",
acc.address
);
//V3R2-MAINNET
coin.network = "MAINNET".to_string();
let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"UQCg7HwngbcHdJN0QQ4u9KiIXba2FS7A7sbME4zGHXm-739l",
acc.address
);
}

#[test]
fn test_is_valid() {
let coin = CoinInfo {
coin: "TON".to_string(),
derivation_path: "m/44'/607'/0'".to_string(),
curve: CurveType::ED25519,
network: "MAINNET".to_string(),
network: "TESTNET".to_string(),
seg_wit: "".to_string(),
chain_id: "".to_string(),
//V4R2 code
contract_code: "te6cckECFAEAAtQAART/APSkE/S88sgLAQIBIAIDAgFIBAUE+PKDCNcYINMf0x/THwL4I7vyZO1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8QERITAubQAdDTAyFxsJJfBOAi10nBIJJfBOAC0x8hghBwbHVnvSKCEGRzdHK9sJJfBeAD+kAwIPpEAcjKB8v/ydDtRNCBAUDXIfQEMFyBAQj0Cm+hMbOSXwfgBdM/yCWCEHBsdWe6kjgw4w0DghBkc3RyupJfBuMNBgcCASAICQB4AfoA9AQw+CdvIjBQCqEhvvLgUIIQcGx1Z4MesXCAGFAEywUmzxZY+gIZ9ADLaRfLH1Jgyz8gyYBA+wAGAIpQBIEBCPRZMO1E0IEBQNcgyAHPFvQAye1UAXKwjiOCEGRzdHKDHrFwgBhQBcsFUAPPFiP6AhPLassfyz/JgED7AJJfA+ICASAKCwBZvSQrb2omhAgKBrkPoCGEcNQICEekk30pkQzmkD6f+YN4EoAbeBAUiYcVnzGEAgFYDA0AEbjJftRNDXCx+AA9sp37UTQgQFA1yH0BDACyMoHy//J0AGBAQj0Cm+hMYAIBIA4PABmtznaiaEAga5Drhf/AABmvHfaiaEAQa5DrhY/AAG7SB/oA1NQi+QAFyMoHFcv/ydB3dIAYyMsFywIizxZQBfoCFMtrEszMyXP7AMhAFIEBCPRR8qcCAHCBAQjXGPoA0z/IVCBHgQEI9FHyp4IQbm90ZXB0gBjIywXLAlAGzxZQBPoCFMtqEssfyz/Jc/sAAgBsgQEI1xj6ANM/MFIkgQEI9Fnyp4IQZHN0cnB0gBjIywXLAlAFzxZQA/oCE8tqyx8Syz/Jc/sAAAr0AMntVGliJeU=".to_string(),
contract_code: "".to_string(),
};

let acc = keystore
.derive_coin::<crate::address::TonAddress>(&coin)
.unwrap();
assert_eq!(
"UQAz8TQ4n_ktc--IpefK-d5ABVpTR-FciJgLZr9vUpEgaNfu",
acc.address
);
let result =
TonAddress::is_valid("UQDBKGsYs49NgdqM4gMoiVMV9Re5hM-yy3nvR_4XB0ZbUMd7", &coin);
assert!(result);
let result =
TonAddress::is_valid("UQDBKGsYs49NgdqM4gMoiVMV9Re5hM-yy3nvR_4XB0ZbUMd8", &coin);
assert!(!result)
}
}

0 comments on commit 559ebde

Please sign in to comment.