Skip to content

Commit

Permalink
add C++ integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Oct 9, 2024
1 parent b4fb941 commit 9ea7f1d
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 48 deletions.
3 changes: 1 addition & 2 deletions rust/chains/tw_pactus/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions rust/tw_tests/tests/chains/pactus/test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
12 changes: 7 additions & 5 deletions src/proto/Pactus.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ 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.
int64 amount = 3;
}

// 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.
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions tests/chains/Pactus/CoinTypeTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <TrustWalletCore/TWCoinTypeConfiguration.h>
#include <TrustWalletCore/TWAnyAddress.h>
#include <TrustWalletCore/TWHDWallet.h>
#include <gtest/gtest.h>

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/");
}

}
52 changes: 52 additions & 0 deletions tests/chains/Pactus/CompilerTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

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<int>(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<int>(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");
}
32 changes: 32 additions & 0 deletions tests/chains/Pactus/SignerTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

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");
}
17 changes: 0 additions & 17 deletions tests/chains/Pactus/TWAnySignerTests.cpp

This file was deleted.

42 changes: 42 additions & 0 deletions tests/chains/Pactus/TestCases.h
Original file line number Diff line number Diff line change
@@ -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 <string>

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
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 9ea7f1d

Please sign in to comment.