Skip to content

Commit

Permalink
[Cardano]: Add TokenAmount transfer with assetName CIP-0067
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Nov 1, 2024
1 parent 1d9dbee commit 467b7b8
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/chains/Cardano/SigningTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,80 @@ TEST(CardanoSigning, SignTransferTokenMaxAmount_620b71) {
EXPECT_EQ(hex(txid), "620b719338efb419b0e1417bfbe01fc94a62d5669a4b8cbbf4e32ecc1ca3b872");
}

TEST(CardanoSigning, SignTransferTokenAmountNonUtf8) {
const auto ownAddress = "addr1q83kuum4jhwu3gxdwftdv2vezr0etmt3tp7phw5assltzl6t4afzguegnkcrdzp79vdcqswly775f33jvtpayl280qeqts960l";
const auto privateKey = "009aba22621d98e008c266a8d19c493f5f80a3a4f55048a83168a9c856726852fc240e6e95d7dc4e8ea599d09d64f84fdbe951b2282f5e5ed374252d17be9507643b2d078e607b5327397f212e4f6607ff0b6dfc93bdc9ad2bd0a682887edb9f304a573e99c7c2022c925511f004c7c9b89e8569080d09e2c53dfb1d53726852d4735794e3d32eac2b17d4d7c94742a77b7400b66fa11eaeb6ae38ba2dea84612f0c38fd68b9751ed4cb4ac48fb5e19f985f809fff1cfe5303fbfd29aca43d66";
const auto gensTokenPolicy = "dda5fdb1002f7389b33e036b6afee82a8189becb6cba852e8b79b4fb";
// Non UTF-8 assetName according to https://github.com/cardano-foundation/CIPs/tree/master/CIP-0067
const auto gensTokenNameHex = "0014df1047454e53";
const auto currentSlot = 138'888'357ul;

Proto::SigningInput input;
auto* utxo1 = input.add_utxos();
const auto txHash1 = parse_hex("7b377e0cf7b83d67bb6919008c38e1a63be86c4831a93ad0cb45778b9f2f7e28");
utxo1->mutable_out_point()->set_tx_hash(txHash1.data(), txHash1.size());
utxo1->mutable_out_point()->set_output_index(4);
utxo1->set_address(ownAddress);
utxo1->set_amount(1'700'000ul);
// GENS token (asset1266q2ewhgul7jh3xqpvjzqarrepfjuler20akr).
auto* token1 = utxo1->add_token_amount();
token1->set_policy_id(gensTokenPolicy);
token1->set_asset_name_hex(gensTokenNameHex);
const auto tokenAmount1 = store(uint256_t(44'660'987ul));
token1->set_amount(tokenAmount1.data(), tokenAmount1.size());

const auto privateKeyData = parse_hex(privateKey);
input.add_private_key(privateKeyData.data(), privateKeyData.size());
input.mutable_transfer_message()->set_to_address("addr1q875r037fjeqveg6xv5wke922ff897eyrnshlj3ryp4mypzt4afzguegnkcrdzp79vdcqswly775f33jvtpayl280qeq7zgptp");
input.mutable_transfer_message()->set_change_address(ownAddress);
input.mutable_transfer_message()->set_amount(666ul); // doesn't matter, max is used
auto* toToken = input.mutable_transfer_message()->mutable_token_amount()->add_token();
toToken->set_policy_id(gensTokenPolicy);
toToken->set_asset_name_hex(gensTokenNameHex);
const auto toTokenAmount = store(uint256_t(666ul)); // doesn't matter, max is used
input.mutable_transfer_message()->set_use_max_amount(true);
input.set_ttl(currentSlot + 7200ul);

Proto::TransactionPlan plan;
ANY_PLAN(input, plan, TWCoinTypeCardano);

EXPECT_EQ(plan.error(), Common::Proto::SigningError::OK);
{
EXPECT_EQ(plan.available_amount(), 1'700'000ul);
EXPECT_EQ(plan.amount(), 1'700'000ul - 167'818ul);
EXPECT_EQ(plan.fee(), 167'818ul);
EXPECT_EQ(plan.change(), 0ul);
EXPECT_EQ(plan.utxos_size(), 1);
EXPECT_EQ(plan.available_tokens_size(), 1);

EXPECT_EQ(load(plan.available_tokens(0).amount()), 44'660'987ul);
// `assetName` must be empty as it's not a UTF-8 string.
EXPECT_EQ(plan.available_tokens(0).asset_name(), "");
EXPECT_EQ(plan.available_tokens(0).asset_name_hex(), gensTokenNameHex);

EXPECT_EQ(plan.output_tokens_size(), 1);
EXPECT_EQ(load(plan.output_tokens(0).amount()), 44'660'987ul);
// `assetName` must be empty as it's not a UTF-8 string.
EXPECT_EQ(plan.output_tokens(0).asset_name(), "");
EXPECT_EQ(plan.output_tokens(0).asset_name_hex(), gensTokenNameHex);
EXPECT_EQ(plan.change_tokens_size(), 0);
}

// set plan with specific fee, to match the real transaction
*input.mutable_plan() = plan;

Proto::SigningOutput output;
ANY_SIGN(input, TWCoinTypeCardano);

// https://cardanoscan.io/transaction/
// curl -d '{"txHash":"620b71..b872","txBody":"83a400..08f6"}' -H "Content-Type: application/json" https://<cardano-node>/api/txs/submit
EXPECT_EQ(output.error(), Common::Proto::OK);
const auto encoded = data(output.encoded());
EXPECT_EQ(hex(encoded), "83a400818258207b377e0cf7b83d67bb6919008c38e1a63be86c4831a93ad0cb45778b9f2f7e2804018182583901fd41be3e4cb206651a3328eb64aa525272fb241ce17fca23206bb2044baf522473289db036883e2b1b8041df27bd44c63262c3d27d477832821a00176116a1581cdda5fdb1002f7389b33e036b6afee82a8189becb6cba852e8b79b4fba1480014df1047454e531a02a978fb021a00028f8a031a084760c5a10081825820748022805ee71f9fa31d06e60f14f0715a37c278c0690b565f26e1e1e83f930e5840386c5d05fb5cfdb11f1296e909a80314616cdd2779e5be5ea583e1a938ee8409f58b585c90248e1c0633638cc0f4517c03fdb59f17434267c2955e0fbbb3b609f6");
const auto txid = data(output.tx_id());
EXPECT_EQ(hex(txid), "df89e81fbaec7485ba65ac3a2ffe4121a888f4937d085f3ad4f7e8e5192dea74");
}

TEST(CardanoSigning, SignTransferTwoTokens) {
auto input = createSampleInput(7000000);
input.mutable_transfer_message()->set_amount(1500000);
Expand Down

0 comments on commit 467b7b8

Please sign in to comment.