forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtrinsic.cpp
More file actions
117 lines (110 loc) · 3.69 KB
/
Copy pathExtrinsic.cpp
File metadata and controls
117 lines (110 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Extrinsic.h"
using namespace TW;
using namespace TW::Polkadot;
static constexpr uint8_t signedBit = 0x80;
static constexpr uint8_t sigTypeEd25519 = 0x00;
Data Extrinsic::encodeEraNonceTip() const {
Data data;
// immortal era
append(data, era);
// nonce
append(data, encodeCompact(nonce));
// tip
append(data, encodeCompact(tip));
return data;
}
Data Extrinsic::encodeCall(const Proto::SigningInput& input) {
// call index from MetadataV10
Data data;
if (input.has_balance_call()) {
auto transfer = input.balance_call().transfer();
auto address = SS58Address(transfer.to_address(), byte(input.network()));
auto value = load(transfer.value());
// call index
append(data, {0x04, 0x00});
// destination
append(data, encodeAddress(address));
// value
append(data, encodeCompact(value));
} else if (input.has_staking_call()) {
auto staking = input.staking_call();
if (staking.has_bond()) {
auto address = SS58Address(staking.bond().validator(), byte(input.network()));
auto value = load(staking.bond().value());
auto reward = byte(staking.bond().reward_destination());
// call index
append(data, {0x06, 0x00});
// validator
append(data, encodeAddress(address));
// value
append(data, encodeCompact(value));
// reward destination
append(data, reward);
} else if (staking.has_bond_extra()) {
auto value = load(staking.unbond().value());
// call index
append(data, {0x06, 0x01});
// value
append(data, encodeCompact(value));
} else if (staking.has_unbond()) {
auto value = load(staking.unbond().value());
// call index
append(data, {0x06, 0x02});
// value
append(data, encodeCompact(value));
} else if (staking.has_withdraw_unbonded()) {
// call index
append(data, {0x06, 0x03});
} else if (staking.has_nominate()) {
std::vector<SS58Address> addresses;
for (auto& n : staking.nominate().nominators()) {
addresses.push_back(SS58Address(n, byte(input.network())));
}
// call index
append(data, {0x06, 0x05});
// nominators
append(data, encodeAddresses(addresses));
} else if (staking.has_chill()) {
// call index
append(data, {0x06, 0x06});
}
}
return data;
}
Data Extrinsic::encodePayload() const {
Data data;
// call
append(data, call);
// era / nonce / tip
append(data, encodeEraNonceTip());
// specVersion
encode32LE(specVersion, data);
// genesis hash
append(data, genesisHash);
// block hash
append(data, blockHash);
return data;
}
Data Extrinsic::encodeSignature(const PublicKey& signer, const Data& signature) const {
Data data;
// version header
append(data, Data{ static_cast<uint8_t>(version | signedBit)});
// signer public key
append(data, encodeAddress(signer));
// signature type
append(data, sigTypeEd25519);
// signature
append(data, signature);
// era / nonce / tip
append(data, encodeEraNonceTip());
// call
append(data, call);
// append length
encodeLengthPrefix(data);
return data;
}