forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScript.cpp
More file actions
314 lines (277 loc) · 9.89 KB
/
Copy pathScript.cpp
File metadata and controls
314 lines (277 loc) · 9.89 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// 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 "Script.h"
#include "Address.h"
#include "CashAddress.h"
#include "SegwitAddress.h"
#include "../Base58.h"
#include "../Coin.h"
#include "../BinaryCoding.h"
#include "../Data.h"
#include "../Decred/Address.h"
#include "../Groestlcoin/Address.h"
#include "../Hash.h"
#include "../PublicKey.h"
#include "../Zcash/TAddress.h"
#include <TrustWalletCore/TWBitcoinOpCodes.h>
#include <algorithm>
#include <cassert>
#include <set>
using namespace TW;
using namespace TW::Bitcoin;
Data Script::hash() const {
return Hash::ripemd(Hash::sha256(bytes));
}
bool Script::isPayToScriptHash() const {
// Extra-fast test for pay-to-script-hash
return bytes.size() == 23 && bytes[0] == OP_HASH160 && bytes[1] == 0x14 &&
bytes[22] == OP_EQUAL;
}
bool Script::isPayToWitnessScriptHash() const {
// Extra-fast test for pay-to-witness-script-hash
return bytes.size() == 22 && bytes[0] == OP_0 && bytes[1] == 0x14;
}
bool Script::isWitnessProgram() const {
if (bytes.size() < 4 || bytes.size() > 42) {
return false;
}
if (bytes[0] != OP_0 && (bytes[0] < OP_1 || bytes[0] > OP_16)) {
return false;
}
return bytes[1] + 2 == bytes.size();
}
bool Script::matchPayToPubkey(Data& result) const {
if (bytes.size() == PublicKey::secp256k1ExtendedSize + 2 &&
bytes[0] == PublicKey::secp256k1ExtendedSize && bytes.back() == OP_CHECKSIG) {
result.clear();
std::copy(std::begin(bytes) + 1, std::begin(bytes) + 1 + PublicKey::secp256k1Size,
std::back_inserter(result));
return true;
}
if (bytes.size() == PublicKey::secp256k1Size + 2 && bytes[0] == PublicKey::secp256k1Size &&
bytes.back() == OP_CHECKSIG) {
result.clear();
std::copy(std::begin(bytes) + 1, std::begin(bytes) + 1 + PublicKey::secp256k1Size,
std::back_inserter(result));
return true;
}
return false;
}
bool Script::matchPayToPubkeyHash(Data& result) const {
if (bytes.size() == 25 && bytes[0] == OP_DUP && bytes[1] == OP_HASH160 && bytes[2] == 20 &&
bytes[23] == OP_EQUALVERIFY && bytes[24] == OP_CHECKSIG) {
result.clear();
std::copy(std::begin(bytes) + 3, std::begin(bytes) + 3 + 20, std::back_inserter(result));
return true;
}
return false;
}
bool Script::matchPayToScriptHash(Data& result) const {
if (!isPayToScriptHash()) {
return false;
}
result.clear();
std::copy(std::begin(bytes) + 2, std::begin(bytes) + 22, std::back_inserter(result));
return true;
}
bool Script::matchPayToWitnessPublicKeyHash(Data& result) const {
if (bytes.size() == 22 && bytes[0] == OP_0 && bytes[1] == 0x14) {
result.clear();
std::copy(std::begin(bytes) + 2, std::end(bytes), std::back_inserter(result));
return true;
}
return false;
}
bool Script::matchPayToWitnessScriptHash(Data& result) const {
if (bytes.size() == 34 && bytes[0] == OP_0 && bytes[1] == 0x20) {
result.clear();
std::copy(std::begin(bytes) + 2, std::end(bytes), std::back_inserter(result));
return true;
}
return false;
}
/// Decodes a small integer
static inline int decodeNumber(uint8_t opcode) {
if (opcode == OP_0) {
return 0;
}
assert(opcode >= OP_1 && opcode <= OP_16);
return static_cast<int>(opcode) - static_cast<int>(OP_1 - 1);
}
bool Script::matchMultisig(std::vector<Data>& keys, int& required) const {
keys.clear();
required = 0;
if (bytes.size() < 1 || bytes.back() != OP_CHECKMULTISIG) {
return false;
}
size_t it = 0;
uint8_t opcode;
Data operand;
auto op = getScriptOp(it, opcode, operand);
if (!op || !TWOpCodeIsSmallInteger(opcode)) {
return false;
}
required = decodeNumber(opcode);
while (true) {
auto res = getScriptOp(it, opcode, operand);
if (!res) {
break;
}
if (!PublicKey::isValid(operand, TWPublicKeyTypeSECP256k1)) {
break;
}
keys.push_back(operand);
}
if (!TWOpCodeIsSmallInteger(opcode)) {
return false;
}
auto expectedCount = decodeNumber(opcode);
if (keys.size() != expectedCount || expectedCount < required) {
return false;
}
if (it + 1 != bytes.size()) {
return false;
}
return true;
}
bool Script::getScriptOp(size_t& index, uint8_t& opcode, Data& operand) const {
operand.clear();
// Read instruction
if (index >= bytes.size()) {
return false;
}
opcode = bytes[index];
index += 1;
if (opcode > OP_PUSHDATA4) {
return true;
}
// Immediate operand
size_t size = 0;
if (opcode < OP_PUSHDATA1) {
size = static_cast<size_t>(opcode);
} else if (opcode == OP_PUSHDATA1) {
if (bytes.size() - index < 1) {
return false;
}
size = index;
index += 1;
} else if (opcode == OP_PUSHDATA2) {
if (bytes.size() - index < 2) {
return false;
}
size = static_cast<size_t>(decode16LE(bytes.data() + index));
index += 2;
} else if (opcode == OP_PUSHDATA4) {
if (bytes.size() - index < 4) {
return false;
}
size = static_cast<size_t>(decode32LE(bytes.data() + index));
index += 4;
}
if (bytes.size() - index < size) {
return false;
}
operand = Data(bytes.begin() + index, bytes.begin() + index + size);
index += size;
return true;
}
Script Script::buildPayToPublicKeyHash(const Data& hash) {
assert(hash.size() == 20);
Script script;
script.bytes.push_back(OP_DUP);
script.bytes.push_back(OP_HASH160);
script.bytes.push_back(20);
script.bytes.insert(script.bytes.end(), hash.begin(), hash.end());
script.bytes.push_back(OP_EQUALVERIFY);
script.bytes.push_back(OP_CHECKSIG);
return script;
}
Script Script::buildPayToScriptHash(const Data& scriptHash) {
assert(scriptHash.size() == 20);
Script script;
script.bytes.push_back(OP_HASH160);
script.bytes.push_back(20);
script.bytes.insert(script.bytes.end(), scriptHash.begin(), scriptHash.end());
script.bytes.push_back(OP_EQUAL);
return script;
}
Script Script::buildPayToWitnessProgram(const Data& program) {
assert(program.size() == 20 || program.size() == 32);
Script script;
script.bytes.push_back(OP_0);
script.bytes.push_back(static_cast<byte>(program.size()));
script.bytes.insert(script.bytes.end(), program.begin(), program.end());
return script;
}
Script Script::buildPayToWitnessPubkeyHash(const Data& hash) {
return Script::buildPayToWitnessProgram(hash);
}
Script Script::buildPayToWitnessScriptHash(const Data& scriptHash) {
return Script::buildPayToWitnessProgram(scriptHash);
}
void Script::encode(Data& data) const {
encodeVarInt(bytes.size(), data);
std::copy(std::begin(bytes), std::end(bytes), std::back_inserter(data));
}
Script Script::buildForAddress(const std::string& string, enum TWCoinType coin) {
if (Address::isValid(string)) {
auto address = Address(string);
auto p2pkh = TW::p2pkhPrefix(coin);
auto p2sh = TW::p2shPrefix(coin);
if (p2pkh == address.bytes[0]) {
// address starts with 1/L
auto data = Data();
data.reserve(Address::size - 1);
std::copy(address.bytes.begin() + 1, address.bytes.end(), std::back_inserter(data));
return buildPayToPublicKeyHash(data);
} else if (p2sh == address.bytes[0]) {
// address starts with 3/M
auto data = Data();
data.reserve(Address::size - 1);
std::copy(address.bytes.begin() + 1, address.bytes.end(), std::back_inserter(data));
return buildPayToScriptHash(data);
}
} else if (SegwitAddress::isValid(string)) {
auto result = SegwitAddress::decode(string);
// address starts with bc/ltc
auto program = result.first.witnessProgram;
return buildPayToWitnessProgram(program);
} else if (CashAddress::isValid(string)) {
auto address = CashAddress(string);
auto bitcoinAddress = address.legacyAddress();
return buildForAddress(bitcoinAddress.string(), TWCoinTypeBitcoinCash);
} else if (Decred::Address::isValid(string)) {
auto bytes = Base58::bitcoin.decodeCheck(string, Hash::blake256d);
if (bytes[1] == TW::p2pkhPrefix(TWCoinTypeDecred)) {
return buildPayToPublicKeyHash(Data(bytes.begin() + 2, bytes.end()));
}
if (bytes[1] == TW::p2shPrefix(TWCoinTypeDecred)) {
return buildPayToScriptHash(Data(bytes.begin() + 2, bytes.end()));
}
} else if (Groestlcoin::Address::isValid(string)) {
auto address = Groestlcoin::Address(string);
auto data = Data();
data.reserve(Address::size - 1);
std::copy(address.bytes.begin() + 1, address.bytes.end(), std::back_inserter(data));
if (address.bytes[0] == TW::p2pkhPrefix(TWCoinTypeGroestlcoin)) {
return buildPayToPublicKeyHash(data);
}
if (address.bytes[0] == TW::p2shPrefix(TWCoinTypeGroestlcoin)) {
return buildPayToScriptHash(data);
}
} else if (Zcash::TAddress::isValid(string)) {
auto address = Zcash::TAddress(string);
auto data = Data();
data.reserve(Address::size - 2);
std::copy(address.bytes.begin() + 2, address.bytes.end(), std::back_inserter(data));
if (address.bytes[1] == TW::p2pkhPrefix(TWCoinTypeZcash)) {
return buildPayToPublicKeyHash(data);
} else if (address.bytes[1] == TW::p2shPrefix(TWCoinTypeZcash)) {
return buildPayToScriptHash(data);
}
}
return {};
}