From 9ea7f1db2c98e73f93d7d19e07528b33eca4ad6f Mon Sep 17 00:00:00 2001 From: Mostafa Date: Wed, 9 Oct 2024 20:44:39 +0800 Subject: [PATCH] add C++ integration tests --- rust/chains/tw_pactus/src/transaction.rs | 3 +- .../tests/chains/pactus/test_cases.rs | 1 + src/proto/Pactus.proto | 12 +++-- ...TWAnyAddressTests.cpp => AddressTests.cpp} | 0 tests/chains/Pactus/CoinTypeTests.cpp | 38 ++++++++++++++ tests/chains/Pactus/CompilerTests.cpp | 52 +++++++++++++++++++ tests/chains/Pactus/SignerTests.cpp | 32 ++++++++++++ tests/chains/Pactus/TWAnySignerTests.cpp | 17 ------ tests/chains/Pactus/TestCases.h | 42 +++++++++++++++ .../{TWCoinTypeTests.cpp => WalletTests.cpp} | 26 +--------- 10 files changed, 175 insertions(+), 48 deletions(-) rename tests/chains/Pactus/{TWAnyAddressTests.cpp => AddressTests.cpp} (100%) create mode 100644 tests/chains/Pactus/CoinTypeTests.cpp create mode 100644 tests/chains/Pactus/CompilerTests.cpp create mode 100644 tests/chains/Pactus/SignerTests.cpp delete mode 100644 tests/chains/Pactus/TWAnySignerTests.cpp create mode 100644 tests/chains/Pactus/TestCases.h rename tests/chains/Pactus/{TWCoinTypeTests.cpp => WalletTests.cpp} (62%) diff --git a/rust/chains/tw_pactus/src/transaction.rs b/rust/chains/tw_pactus/src/transaction.rs index 1a9e3cdc708..f38e4132beb 100644 --- a/rust/chains/tw_pactus/src/transaction.rs +++ b/rust/chains/tw_pactus/src/transaction.rs @@ -148,8 +148,7 @@ impl Transaction { Some(trx) => { let payload = match &trx.payload { Pactus::Proto::mod_TransactionMessage::OneOfpayload::transfer(pld) => { - let private_key = PrivateKey::try_from(input.private_key.as_ref())?; - let sender = Address::from_public_key(&private_key.public())?; + let sender = Address::from_str(&pld.sender)?; let receiver = Address::from_str(&pld.receiver)?; Box::new(TransferPayload { diff --git a/rust/tw_tests/tests/chains/pactus/test_cases.rs b/rust/tw_tests/tests/chains/pactus/test_cases.rs index 6e93bba23cf..39490963be0 100644 --- a/rust/tw_tests/tests/chains/pactus/test_cases.rs +++ b/rust/tw_tests/tests/chains/pactus/test_cases.rs @@ -56,6 +56,7 @@ pub mod transfer_1 { pub fn pactus_sign_transfer_input() -> Proto::SigningInput<'static> { let transfer_payload = Proto::TransferPayload { + sender: "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr".into(), receiver: "pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra".into(), amount: 20000, }; diff --git a/src/proto/Pactus.proto b/src/proto/Pactus.proto index 8dfd9e6b0d0..ce3f9d3a02e 100644 --- a/src/proto/Pactus.proto +++ b/src/proto/Pactus.proto @@ -24,8 +24,9 @@ message TransactionMessage { } // Transfer payload for creating a Transfer transaction between two accounts. -// The sender's address is the same as the signer's address. message TransferPayload { + // The sender's account address. + string sender = 1; // The receiver's account address. string receiver = 2; // The amount to be transferred, specified in NanoPAC. @@ -33,14 +34,15 @@ message TransferPayload { } // Bond payload for creating a Bond transaction from an account to a validator. -// The sender's address is the same as the signer's address. message BondPayload { + // The sender's account address. + string sender = 1; // The receiver's validator address. - string receiver = 1; + string receiver = 2; // The stake amount in NanoPAC. - int64 stake = 2; + int64 stake = 3; // The public key of the validator (only set when creating a new validator). - string public_key = 3; + string public_key = 4; } // Input data necessary to create a signed transaction. diff --git a/tests/chains/Pactus/TWAnyAddressTests.cpp b/tests/chains/Pactus/AddressTests.cpp similarity index 100% rename from tests/chains/Pactus/TWAnyAddressTests.cpp rename to tests/chains/Pactus/AddressTests.cpp diff --git a/tests/chains/Pactus/CoinTypeTests.cpp b/tests/chains/Pactus/CoinTypeTests.cpp new file mode 100644 index 00000000000..cd28ee9de14 --- /dev/null +++ b/tests/chains/Pactus/CoinTypeTests.cpp @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +#include "TestUtilities.h" +#include "Pactus/Entry.h" +#include "PrivateKey.h" +#include "PublicKey.h" +#include +#include +#include +#include + +namespace TW::Pactus::tests { + +TEST(PactusCoinType, TWCoinType) { + const auto coin = TWCoinTypePactus; + const auto symbol = WRAPS(TWCoinTypeConfigurationGetSymbol(coin)); + const auto id = WRAPS(TWCoinTypeConfigurationGetID(coin)); + const auto name = WRAPS(TWCoinTypeConfigurationGetName(coin)); + const auto txId = WRAPS(TWStringCreateWithUTF8Bytes("")); + const auto txUrl = WRAPS(TWCoinTypeConfigurationGetTransactionURL(coin, txId.get())); + const auto accId = WRAPS(TWStringCreateWithUTF8Bytes("")); + const auto accUrl = WRAPS(TWCoinTypeConfigurationGetAccountURL(coin, accId.get())); + + assertStringsEqual(id, "pactus"); + assertStringsEqual(name, "Pactus"); + assertStringsEqual(symbol, "PAC"); + ASSERT_EQ(TWCoinTypeConfigurationGetDecimals(coin), 9); + ASSERT_EQ(TWCoinTypeBlockchain(coin), TWBlockchainPactus); + ASSERT_EQ(TWCoinTypeP2pkhPrefix(coin), 0); + ASSERT_EQ(TWCoinTypeP2shPrefix(coin), 0); + ASSERT_EQ(TWCoinTypeStaticPrefix(coin), 0); + assertStringsEqual(txUrl, "https://pacviewer.com/transaction/"); + assertStringsEqual(accUrl, "https://pacviewer.com/address/"); +} + +} \ No newline at end of file diff --git a/tests/chains/Pactus/CompilerTests.cpp b/tests/chains/Pactus/CompilerTests.cpp new file mode 100644 index 00000000000..160bcf54acc --- /dev/null +++ b/tests/chains/Pactus/CompilerTests.cpp @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +#include "HexCoding.h" +#include "PrivateKey.h" +#include "PublicKey.h" +#include "TestUtilities.h" +#include "TransactionCompiler.h" +#include "proto/Pactus.pb.h" +#include "proto/TransactionCompiler.pb.h" +#include "TestCases.h" + +#include + +using namespace TW; + +TEST(PactusCompiler, CompileAndSign) { + // Set up a signing input. + + auto input = TransferTransaction1::createSigningInput(); + + auto inputString = input.SerializeAsString(); + auto inputStrData = TW::Data(inputString.begin(), inputString.end()); + + // Pre-hash the transaction. + auto preImageHashesData = TransactionCompiler::preImageHashes(TWCoinTypePactus, inputStrData); + TxCompiler::Proto::PreSigningOutput preSigningOutput; + preSigningOutput.ParseFromArray(preImageHashesData.data(), static_cast(preImageHashesData.size())); + auto actualDataToSign = data(preSigningOutput.data()); + + EXPECT_EQ(preSigningOutput.error(), Common::Proto::OK); + EXPECT_EQ(hex(actualDataToSign), TransferTransaction1::DATA_TO_SIGN); + + // // Sign the pre-hash data. + + auto privateKey = PrivateKey(parse_hex(TransferTransaction1::PRIVATE_KEY_HEX)); + auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeED25519).bytes; + auto signature = privateKey.sign(actualDataToSign, TWCurveED25519); + EXPECT_EQ(hex(signature), "50ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd09"); + + // Compile the transaction. + + auto outputData = TransactionCompiler::compileWithSignatures(TWCoinTypePactus, inputStrData, {signature}, {publicKey}); + TW::Pactus::Proto::SigningOutput output; + output.ParseFromArray(outputData.data(), static_cast(outputData.size())); + + EXPECT_EQ(output.error(), Common::Proto::OK); + ASSERT_EQ(hex(output.signed_transaction_data()), "000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa"); + ASSERT_EQ(hex(output.signature()), "50ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd09"); + ASSERT_EQ(hex(output.transaction_id()), "34cd4656a98f7eb996e83efdc384cefbe3a9c52dca79a99245b4eacc0b0b4311"); +} diff --git a/tests/chains/Pactus/SignerTests.cpp b/tests/chains/Pactus/SignerTests.cpp new file mode 100644 index 00000000000..b7a6adb06f1 --- /dev/null +++ b/tests/chains/Pactus/SignerTests.cpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +#include "HexCoding.h" +#include "PrivateKey.h" +#include "PublicKey.h" +#include "TestUtilities.h" +#include "TransactionCompiler.h" +#include "proto/Pactus.pb.h" +#include "proto/TransactionCompiler.pb.h" +#include "TestCases.h" + +#include + +using namespace TW; + +TEST(PactusSigner, Sign) { + // Set up a signing input. + auto input = TransferTransaction1::createSigningInput(); + + auto privateKey = PrivateKey(parse_hex(TransferTransaction1::PRIVATE_KEY_HEX)); + input.set_private_key(privateKey.bytes.data(), privateKey.bytes.size()); + + TW::Pactus::Proto::SigningOutput output; + ANY_SIGN(input, TWCoinTypePactus); + + EXPECT_EQ(output.error(), Common::Proto::OK); + ASSERT_EQ(hex(output.signed_transaction_data()), "000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa"); + ASSERT_EQ(hex(output.signature()), "50ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd09"); + ASSERT_EQ(hex(output.transaction_id()), "34cd4656a98f7eb996e83efdc384cefbe3a9c52dca79a99245b4eacc0b0b4311"); +} diff --git a/tests/chains/Pactus/TWAnySignerTests.cpp b/tests/chains/Pactus/TWAnySignerTests.cpp deleted file mode 100644 index a8988d9e632..00000000000 --- a/tests/chains/Pactus/TWAnySignerTests.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// Copyright © 2017 Trust Wallet. - -#include -#include "HexCoding.h" - -#include "TestUtilities.h" -#include - -using namespace TW; - -// TODO: Finalize tests - -TEST(TWAnySignerPactus, Sign) { - // TODO: Finalize test implementation -} diff --git a/tests/chains/Pactus/TestCases.h b/tests/chains/Pactus/TestCases.h new file mode 100644 index 00000000000..a3de8cfc9bf --- /dev/null +++ b/tests/chains/Pactus/TestCases.h @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +#pragma once + +#include "proto/Pactus.pb.h" +#include "proto/TransactionCompiler.pb.h" + +#include + +namespace TransferTransaction1 { + +static TW::Pactus::Proto::SigningInput createSigningInput() { + TW::Pactus::Proto::SigningInput input; + + // Set up the transaction message + TW::Pactus::Proto::TransactionMessage* trx = input.mutable_transaction(); + trx->set_lock_time(0x00030201); + trx->set_fee(1000); + trx->set_memo("test"); + + // Set up the transfer payload + TW::Pactus::Proto::TransferPayload* pld = trx->mutable_transfer(); + pld->set_sender("pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr"); + pld->set_receiver("pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra"); + pld->set_amount(20000); + + return input; +} + +const std::string PRIVATE_KEY_HEX = "4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6"; +const std::string PRE_HASH = + "b5e97db07fa0bd0e5598aa3643a9bc6f6693bddc1a9fec9e674a461eaa00b19307968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3063000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e74087472616e7366657200022007968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3008e803000000000000fe4d3200000000006400000000000000c2276ada0000000021"; +const std::string SIGNATURE = + "50ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd09"; +const std::string DATA_TO_SIGN = + "0101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c01"; +const std::string SIGNED_TRANSACTION_DATA = + "000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa"; +const std::string TRANSACTION_ID = "34cd4656a98f7eb996e83efdc384cefbe3a9c52dca79a99245b4eacc0b0b4311"; +} // namespace TransferTransaction1 diff --git a/tests/chains/Pactus/TWCoinTypeTests.cpp b/tests/chains/Pactus/WalletTests.cpp similarity index 62% rename from tests/chains/Pactus/TWCoinTypeTests.cpp rename to tests/chains/Pactus/WalletTests.cpp index 2a6636f44d6..bcd3076f398 100644 --- a/tests/chains/Pactus/TWCoinTypeTests.cpp +++ b/tests/chains/Pactus/WalletTests.cpp @@ -13,34 +13,12 @@ namespace TW::Pactus::tests { -TEST(TWPactusCoinType, TWCoinType) { - const auto coin = TWCoinTypePactus; - const auto symbol = WRAPS(TWCoinTypeConfigurationGetSymbol(coin)); - const auto id = WRAPS(TWCoinTypeConfigurationGetID(coin)); - const auto name = WRAPS(TWCoinTypeConfigurationGetName(coin)); - const auto txId = WRAPS(TWStringCreateWithUTF8Bytes("")); - const auto txUrl = WRAPS(TWCoinTypeConfigurationGetTransactionURL(coin, txId.get())); - const auto accId = WRAPS(TWStringCreateWithUTF8Bytes("")); - const auto accUrl = WRAPS(TWCoinTypeConfigurationGetAccountURL(coin, accId.get())); - - assertStringsEqual(id, "pactus"); - assertStringsEqual(name, "Pactus"); - assertStringsEqual(symbol, "PAC"); - ASSERT_EQ(TWCoinTypeConfigurationGetDecimals(coin), 9); - ASSERT_EQ(TWCoinTypeBlockchain(coin), TWBlockchainPactus); - ASSERT_EQ(TWCoinTypeP2pkhPrefix(coin), 0); - ASSERT_EQ(TWCoinTypeP2shPrefix(coin), 0); - ASSERT_EQ(TWCoinTypeStaticPrefix(coin), 0); - assertStringsEqual(txUrl, "https://pacviewer.com/transaction/"); - assertStringsEqual(accUrl, "https://pacviewer.com/address/"); -} - -TEST(TWPactusCoinType, DerivationPath) { +TEST(PactusWallet, DerivationPath) { auto derivationPath = TWCoinTypeDerivationPath(TWCoinTypePactus); assertStringsEqual(WRAPS(derivationPath), "m/44'/21888'/3'/0'"); } -TEST(TWPactusCoinType, HDWallet) { +TEST(PactusWallet, HDWallet) { auto mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon cactus"; auto passphrase = ""; auto wallet = WRAP(TWHDWallet, TWHDWalletCreateWithMnemonic(STRING(mnemonic).get(), STRING(passphrase).get()));