Skip to content
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
30 changes: 29 additions & 1 deletion rust/chains/tw_polkadot/src/call_encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tw_proto::Polkadot::Proto::{
mod_CallIndices::OneOfvariant as CallIndicesVariant,
mod_SigningInput::OneOfmessage_oneof as SigningVariant,
mod_Staking::{
Bond, BondAndNominate, Chill, ChillAndUnbond, Nominate,
Bond, BondAndNominate, BondExtra, BondExtraAndNominate, Chill, ChillAndUnbond, Nominate,
OneOfmessage_oneof as StakingVariant, Unbond,
},
Balance, CallIndices, Staking,
Expand Down Expand Up @@ -131,6 +131,30 @@ impl CallEncoder {
self.encode_batch(vec![first, second], &ban.call_indices)
}

fn encode_staking_bond_extra_and_nominate(
&self,
bean: &BondExtraAndNominate,
) -> EncodeResult<RawOwned> {
// Encode a bond call
let first = self.encode_call(&SigningVariant::staking_call(Proto::Staking {
message_oneof: StakingVariant::bond_extra(BondExtra {
value: bean.value.clone(),
call_indices: bean.bond_extra_call_indices.clone(),
}),
}))?;

// Encode a nominate call
let second = self.encode_call(&SigningVariant::staking_call(Proto::Staking {
message_oneof: StakingVariant::nominate(Nominate {
nominators: bean.nominators.clone(),
call_indices: bean.nominate_call_indices.clone(),
}),
}))?;

// Encode both calls as batched
self.encode_batch(vec![first, second], &bean.call_indices)
}

fn encode_staking_chill_and_unbond(&self, cau: &ChillAndUnbond) -> EncodeResult<RawOwned> {
let first = self.encode_call(&SigningVariant::staking_call(Proto::Staking {
message_oneof: StakingVariant::chill(Chill {
Expand All @@ -155,6 +179,10 @@ impl CallEncoder {
let batch = self.encode_staking_bond_and_nominate(ban)?;
Ok(Some(batch))
},
StakingVariant::bond_extra_and_nominate(bean) => {
let batch = self.encode_staking_bond_extra_and_nominate(bean)?;
Ok(Some(batch))
},
StakingVariant::chill_and_unbond(cau) => {
let batch = self.encode_staking_chill_and_unbond(cau)?;
Ok(Some(batch))
Expand Down
9 changes: 7 additions & 2 deletions rust/tw_tests/tests/chains/polkadot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ pub fn helper_encode_and_compile(
let mut pre_imager = PreImageHelper::<CompilerProto::PreSigningOutput>::default();
let preimage_output = pre_imager.pre_image_hashes(coin, &input);

assert_eq!(preimage_output.error, SigningError::OK);
assert_eq!(
preimage_output.error,
SigningError::OK,
"{}",
preimage_output.error_message
);
let preimage = preimage_output.data.to_hex();

// Step 2: Compile transaction info
Expand All @@ -87,7 +92,7 @@ pub fn helper_encode_and_compile(
// Compile transaction info
let mut compiler = CompilerHelper::<Proto::SigningOutput>::default();
let output = compiler.compile(coin, &input, vec![signature_bytes], vec![public_key]);
assert_eq!(output.error, SigningError::OK);
assert_eq!(output.error, SigningError::OK, "{}", output.error_message);
let signed = output.encoded.to_hex();

(preimage, signed)
Expand Down
48 changes: 47 additions & 1 deletion rust/tw_tests/tests/chains/polkadot/polkadot_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
//
// Copyright © 2017 Trust Wallet.

use crate::chains::polkadot::{balance_call, helper_encode_and_compile, GENESIS_HASH};
use crate::chains::polkadot::{
balance_call, helper_encode_and_compile, staking_call, GENESIS_HASH,
};
use std::borrow::Cow;
use tw_coin_registry::coin_type::CoinType;
use tw_encoding::hex::DecodeHex;
use tw_number::U256;
use tw_proto::Polkadot::Proto::{self, mod_Balance::Transfer};

#[test]
Expand Down Expand Up @@ -51,3 +55,45 @@ fn test_polkadot_compile_transfer() {
"390284d84accbb64934815506288fafbfc7d275e64aa4e3cd9c5392db6e83b13256bf300fb43727477caaa12542b9060856816d42eedef6ebf2e98e4f8dff4355fe384751925833c4a26b2fed1707aebe655cb3317504a61ee59697c086f7baa6ca06a099dfe00000500be4c21aa92dcba057e9b719ce1de970f774f064c09b13a3ea3009affb8cb5ec707000cdc0f21"
);
}

// Successfully broadcasted transaction on Polkadot mainnet:
// https://polkadot.subscan.io/extrinsic/26528041-2
#[test]
fn test_polkadot_compile_bond_extra_and_nominate() {
// Step 1: Prepare input.
let genesis_hash = GENESIS_HASH.decode_hex().unwrap();
let input = Proto::SigningInput {
network: 0,
nonce: 18,
block_hash: genesis_hash.clone().into(),
genesis_hash: genesis_hash.into(),
spec_version: 1005001,
transaction_version: 26,
message_oneof: staking_call(
Proto::mod_Staking::OneOfmessage_oneof::bond_extra_and_nominate(
Proto::mod_Staking::BondExtraAndNominate {
value: Cow::Owned(U256::from(50_000_000_000_u64).to_big_endian().to_vec()),
nominators: vec!["14Y4s6V1PWrwBLvxW47gcYgZCGTYekmmzvFsK1kiqNH2d84t".into()],
..Default::default()
},
),
),
..Default::default()
};

// Simulate signature, normally obtained from signature server
let signature = "0xb959bd1506e2863cd3f92bd703d31b3f38847e6425f8abd94fa1aed863b5e27c45008abb8d260d8d306b0f129ebe5e2eb845dc3e6585a08d4be231e4b886cd0e";
let public_key = "0x4883c6369ec0764c0cada1dcc55a4873d1c1c6a13400bf57c206e660302081ca";

let (preimage, signed) =
helper_encode_and_compile(CoinType::Polkadot, input, signature, public_key, true);

assert_eq!(
preimage,
"1a020807010700743ba40b070504009c665073980c9bdbd5620ef9a860b9f1efbeda8f10e13ef7431f6970d765a25700480000c9550f001a00000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c391b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c300"
);
assert_eq!(
signed,
"590284004883c6369ec0764c0cada1dcc55a4873d1c1c6a13400bf57c206e660302081ca00b959bd1506e2863cd3f92bd703d31b3f38847e6425f8abd94fa1aed863b5e27c45008abb8d260d8d306b0f129ebe5e2eb845dc3e6585a08d4be231e4b886cd0e004800001a020807010700743ba40b070504009c665073980c9bdbd5620ef9a860b9f1efbeda8f10e13ef7431f6970d765a257"
);
}
19 changes: 19 additions & 0 deletions src/proto/Polkadot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ message Staking {
CallIndices nominate_call_indices = 7;
}

// Bond extra, with nominators
message BondExtraAndNominate {
// amount (uint256, serialized big endian)
bytes value = 1;

// list of nominators
repeated string nominators = 2;

// Batch call indices
CallIndices call_indices = 3;

// Staking.BondExtra call indices
CallIndices bond_extra_call_indices = 4;

// Staking.Nominate call indices
CallIndices nominate_call_indices = 5;
}

// Bond extra amount
message BondExtra {
// amount (uint256, serialized big endian)
Expand Down Expand Up @@ -215,6 +233,7 @@ message Staking {
Chill chill = 7;
ChillAndUnbond chill_and_unbond = 8;
Rebond rebond = 9;
BondExtraAndNominate bond_extra_and_nominate = 10;
}
}

Expand Down
Loading