From 559ebde6698275b778ea9048d367f201318b698e Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Fri, 20 Dec 2024 10:57:07 +0800 Subject: [PATCH] test: test case optimized --- token-core/tcx-ton/src/address.rs | 140 +++++++++++++++++++----------- 1 file changed, 91 insertions(+), 49 deletions(-) diff --git a/token-core/tcx-ton/src/address.rs b/token-core/tcx-ton/src/address.rs index a7106170..8d28f035 100644 --- a/token-core/tcx-ton/src/address.rs +++ b/token-core/tcx-ton/src/address.rs @@ -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; @@ -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 } @@ -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() { @@ -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, @@ -89,7 +84,7 @@ 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::(&coin) .unwrap(); @@ -97,18 +92,8 @@ mod test { "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::(&coin) .unwrap(); @@ -116,18 +101,9 @@ mod test { "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: "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(), - }; - + //V4R2-TESTNET + coin.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(); + coin.network = "TESTNET".to_string(); let acc = keystore .derive_coin::(&coin) .unwrap(); @@ -135,24 +111,90 @@ mod test { "0QAz8TQ4n_ktc--IpefK-d5ABVpTR-FciJgLZr9vUpEgaGxk", acc.address ); + //V4R2-MAINNET + coin.network = "MAINNET".to_string(); + let acc = keystore + .derive_coin::(&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::(&coin) + .unwrap(); + assert_eq!( + "0QC1hu4IopANSj01rqYO2Omy8uAKz_QDCkxzs2AwXdl2Pyw8", + acc.address + ); + //V4R1-MAINNET + coin.network = "MAINNET".to_string(); + let acc = keystore + .derive_coin::(&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::(&coin) + .unwrap(); + assert_eq!( + "0QCXvH7F6nL71NHulDevmD8jxta-YlED42hHy_rr9KI1Bjbu", + acc.address + ); + //V3R1-MAINNET + coin.network = "MAINNET".to_string(); + let acc = keystore + .derive_coin::(&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::(&coin) + .unwrap(); + assert_eq!( + "0QCg7HwngbcHdJN0QQ4u9KiIXba2FS7A7sbME4zGHXm-78Tv", + acc.address + ); + //V3R2-MAINNET + coin.network = "MAINNET".to_string(); + let acc = keystore + .derive_coin::(&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::(&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) } }