Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Polkadot signature encoding to support spec 1002005 #3897

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Polkadot/Extrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ Data Extrinsic::encodePayload() const {
}

if (use_new_spec) {
// mode (currently always 0)
data.push_back(0x00);
// mode (currently always 0)
data.push_back(0x00);
}

// specVersion
Expand All @@ -421,14 +421,16 @@ Data Extrinsic::encodePayload() const {
append(data, blockHash);

if (use_new_spec) {
// empty metadata hash
data.push_back(0x00);
// empty metadata hash
data.push_back(0x00);
}
return data;
}

Data Extrinsic::encodeSignature(const PublicKey& signer, const Data& signature) const {
Data data;
auto use_new_spec = requires_new_spec_compatbility(network, specVersion);

// version header
append(data, Data{extrinsicFormat | signedBit});
// signer public key
Expand All @@ -439,10 +441,17 @@ Data Extrinsic::encodeSignature(const PublicKey& signer, const Data& signature)
append(data, signature);
// era / nonce / tip
append(data, encodeEraNonceTip());

// fee asset id
if (!feeAssetId.empty()) {
append(data, feeAssetId);
}

if (use_new_spec) {
// mode (currently always 0)
data.push_back(0x00);
}

// call
append(data, call);
// append length
Expand Down
39 changes: 39 additions & 0 deletions tests/chains/Polkadot/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,43 @@ TEST(PolkadotSigner, Kusama_SignBond_NoController) {
ASSERT_EQ(hex(output.encoded()), "c101840088dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee00bc4d7a166bd1e7e2bfe9b53e81239c9e340d5a326f17c0a3d2768fcc127f20f4f85d888ecb90aa3ed9a0943f8ae8116b9a19747e563c8d8151dfe3b1b5deb40ca5020c0006000700b08ef01b02");
}

TEST(PolkadotSigner, SignTransfer_KusamaNewSpec) {
auto toAddress = Address("DAbYHrSQTULYZsuA1kvH2cQ33oBsCxxSRPM1XkhzGLeJuHG", ss58Prefix(TWCoinTypeKusama));

auto input = Proto::SigningInput();
auto genesisHashData = parse_hex("0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe");
input.set_genesis_hash(genesisHashData.data(), genesisHashData.size());
auto blockHashData = parse_hex("0x0c731c2b7f5332749432eae61cd5a919592965b28181cf9b73b0a1258ea73303");
input.set_block_hash(blockHashData.data(), blockHashData.size());
input.set_nonce(150);
input.set_spec_version(1002005);
{
PublicKey publicKey = privateKeyThrow2.getPublicKey(TWPublicKeyTypeED25519);
Address address = Address(publicKey);
EXPECT_EQ(address.string(), addressThrow2);
}
input.set_private_key(privateKeyThrow2.bytes.data(), privateKeyThrow2.bytes.size());
input.set_network(ss58Prefix(TWCoinTypeKusama));
input.set_transaction_version(26);

// era: for blockhash and block number, use curl -H "Content-Type: application/json" -H "Accept: text/plain" https://<polkadot-rpc-url>/transaction/material?noMeta=true
auto era = input.mutable_era();
era->set_block_number(23610713);
era->set_period(64);

auto balanceCall = input.mutable_balance_call();
auto transfer = balanceCall->mutable_transfer();
auto value = store(uint256_t(2000000000)); // 0.2
transfer->set_to_address(toAddress.string());
transfer->set_value(value.data(), value.size());

auto extrinsic = Extrinsic(input);
auto preimage = extrinsic.encodePayload();
EXPECT_EQ(hex(preimage), "0400001a2447c661c9b168bba4a2a178baef7d79eee006c1d145ffc832be76ff6ee9ce0300943577950159020000154a0f001a000000b0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe0c731c2b7f5332749432eae61cd5a919592965b28181cf9b73b0a1258ea7330300");

auto output = Signer::sign(input);
EXPECT_EQ(hex(output.encoded()), "450284009dca538b7a925b8ea979cc546464a3c5f81d2398a3a272f6f93bdf4803f2f78300fc5a463d3b6972ac7e0b701110f9d95d377be5b6a2f356765553104c04765fc0066c235c11dabde650d487760dc310003d607abceaf85a0a0f47f1a90e3680029501590200000400001a2447c661c9b168bba4a2a178baef7d79eee006c1d145ffc832be76ff6ee9ce0300943577");
}


} // namespace TW::Polkadot::tests
Loading