-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathtransaction_builder.dart
400 lines (367 loc) · 12.8 KB
/
transaction_builder.dart
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import 'dart:convert';
import 'dart:typed_data';
import 'package:bitcoin_flutter/src/utils/constants/op.dart';
import 'package:meta/meta.dart';
import 'package:hex/hex.dart';
import 'package:bs58check/bs58check.dart' as bs58check;
import 'package:bech32/bech32.dart';
import 'utils/script.dart' as bscript;
import 'ecpair.dart';
import 'models/networks.dart';
import 'transaction.dart';
import 'address.dart';
import 'payments/index.dart' show PaymentData;
import 'payments/p2pkh.dart';
import 'payments/p2wpkh.dart';
import 'classify.dart';
const int MAX_OP_RETURN_SIZE = 100;
class TransactionBuilder {
NetworkType network;
int maximumFeeRate;
List<Input> _inputs;
Transaction _tx;
Map _prevTxSet = {};
TransactionBuilder({NetworkType network, int maximumFeeRate}) {
this.network = network ?? bitcoin;
this.maximumFeeRate = maximumFeeRate ?? 2500;
this._inputs = [];
this._tx = new Transaction();
this._tx.version = 2;
}
List<Input> get inputs => _inputs;
factory TransactionBuilder.fromTransaction(Transaction transaction,
[NetworkType network]) {
final txb = new TransactionBuilder(network: network);
// Copy transaction fields
txb.setVersion(transaction.version);
txb.setLockTime(transaction.locktime);
// Copy outputs (done first to avoid signature invalidation)
transaction.outs.forEach((txOut) {
txb.addOutput(txOut.script, txOut.value);
});
transaction.ins.forEach((txIn) {
txb._addInputUnsafe(
txIn.hash,
txIn.index,
new Input(
sequence: txIn.sequence,
script: txIn.script,
witness: txIn.witness));
});
// fix some things not possible through the public API
// print(txb.toString());
// txb.__INPUTS.forEach((input, i) => {
// fixMultisigOrder(input, transaction, i);
// });
return txb;
}
setVersion(int version) {
if (version < 0 || version > 0xFFFFFFFF)
throw ArgumentError('Expected Uint32');
_tx.version = version;
}
setLockTime(int locktime) {
if (locktime < 0 || locktime > 0xFFFFFFFF)
throw ArgumentError('Expected Uint32');
// if any signatures exist, throw
if (this._inputs.map((input) {
if (input.signatures == null) return false;
return input.signatures.map((s) {
return s != null;
}).contains(true);
}).contains(true)) {
throw new ArgumentError('No, this would invalidate signatures');
}
_tx.locktime = locktime;
}
int addOutput(dynamic data, int value) {
var scriptPubKey;
if (data is String) {
scriptPubKey = Address.addressToOutputScript(data, this.network);
} else if (data is Uint8List) {
scriptPubKey = data;
} else {
throw new ArgumentError('Address invalid');
}
if (!_canModifyOutputs()) {
throw new ArgumentError('No, this would invalidate signatures');
}
return _tx.addOutput(scriptPubKey, value);
}
int addOutputData(dynamic data) {
var scriptPubKey;
if (data is String) {
if (data.length <= MAX_OP_RETURN_SIZE) {
scriptPubKey = bscript.compile([OPS['OP_RETURN'], utf8.encode(data)]);
} else {
throw new ArgumentError('Too much data embedded, max OP_RETURN size is '+MAX_OP_RETURN_SIZE.toString());
}
} else if (data is Uint8List) {
scriptPubKey = data;
} else {
throw new ArgumentError('Invalid data');
}
if (!_canModifyOutputs()) {
throw new ArgumentError('No, this would invalidate signatures');
}
return _tx.addOutput(scriptPubKey, 0);
}
int addInput(dynamic txHash, int vout,
[int sequence, Uint8List prevOutScript]) {
if (!_canModifyInputs()) {
throw new ArgumentError('No, this would invalidate signatures');
}
Uint8List hash;
var value;
if (txHash is String) {
hash = Uint8List.fromList(HEX.decode(txHash).reversed.toList());
} else if (txHash is Uint8List) {
hash = txHash;
} else if (txHash is Transaction) {
final txOut = txHash.outs[vout];
prevOutScript = txOut.script;
value = txOut.value;
hash = txHash.getHash();
} else {
throw new ArgumentError('txHash invalid');
}
return _addInputUnsafe(
hash,
vout,
new Input(
sequence: sequence, prevOutScript: prevOutScript, value: value));
}
sign(
{@required int vin,
@required ECPair keyPair,
Uint8List redeemScript,
int witnessValue,
Uint8List witnessScript,
int hashType}) {
if (keyPair.network != null &&
keyPair.network.toString().compareTo(network.toString()) != 0)
throw new ArgumentError('Inconsistent network');
if (vin >= _inputs.length)
throw new ArgumentError('No input at index: $vin');
hashType = hashType ?? SIGHASH_ALL;
if (this._needsOutputs(hashType))
throw new ArgumentError('Transaction needs outputs');
final input = _inputs[vin];
final ourPubKey = keyPair.publicKey;
if (!_canSign(input)) {
if (witnessValue != null) {
input.value = witnessValue;
}
if (redeemScript != null && witnessScript != null) {
// TODO p2wsh
}
if (redeemScript != null) {
// TODO
}
if (witnessScript != null) {
// TODO
}
if (input.prevOutScript != null && input.prevOutType != null) {
var type = classifyOutput(input.prevOutScript);
if (type == SCRIPT_TYPES['P2WPKH']) {
input.prevOutType = SCRIPT_TYPES['P2WPKH'];
input.hasWitness = true;
input.signatures = [null];
input.pubkeys = [ourPubKey];
input.signScript = new P2PKH(
data: new PaymentData(pubkey: ourPubKey),
network: this.network)
.data
.output;
} else {
// DRY CODE
Uint8List prevOutScript = pubkeyToOutputScript(ourPubKey);
input.prevOutType = SCRIPT_TYPES['P2PKH'];
input.signatures = [null];
input.pubkeys = [ourPubKey];
input.signScript = prevOutScript;
}
} else {
Uint8List prevOutScript = pubkeyToOutputScript(ourPubKey);
input.prevOutType = SCRIPT_TYPES['P2PKH'];
input.signatures = [null];
input.pubkeys = [ourPubKey];
input.signScript = prevOutScript;
}
}
var signatureHash;
if (input.hasWitness) {
signatureHash = this
._tx
.hashForWitnessV0(vin, input.signScript, input.value, hashType);
} else {
signatureHash =
this._tx.hashForSignature(vin, input.signScript, hashType);
}
// enforce in order signing of public keys
var signed = false;
for (var i = 0; i < input.pubkeys.length; i++) {
if (HEX.encode(ourPubKey).compareTo(HEX.encode(input.pubkeys[i])) != 0)
continue;
if (input.signatures[i] != null)
throw new ArgumentError('Signature already exists');
final signature = keyPair.sign(signatureHash);
input.signatures[i] = bscript.encodeSignature(signature, hashType);
signed = true;
}
if (!signed) throw new ArgumentError('Key pair cannot sign for this input');
}
Transaction build() {
return _build(false);
}
Transaction buildIncomplete() {
return _build(true);
}
Transaction _build(bool allowIncomplete) {
if (!allowIncomplete) {
if (_tx.ins.length == 0)
throw new ArgumentError('Transaction has no inputs');
if (_tx.outs.length == 0)
throw new ArgumentError('Transaction has no outputs');
}
final tx = Transaction.clone(_tx);
for (var i = 0; i < _inputs.length; i++) {
if (_inputs[i].pubkeys != null &&
_inputs[i].signatures != null &&
_inputs[i].pubkeys.length != 0 &&
_inputs[i].signatures.length != 0) {
if (_inputs[i].prevOutType == SCRIPT_TYPES['P2PKH']) {
P2PKH payment = new P2PKH(
data: new PaymentData(
pubkey: _inputs[i].pubkeys[0],
signature: _inputs[i].signatures[0]),
network: network);
tx.setInputScript(i, payment.data.input);
tx.setWitness(i, payment.data.witness);
} else if (_inputs[i].prevOutType == SCRIPT_TYPES['P2WPKH']) {
P2WPKH payment = new P2WPKH(
data: new PaymentData(
pubkey: _inputs[i].pubkeys[0],
signature: _inputs[i].signatures[0]),
network: network);
tx.setInputScript(i, payment.data.input);
tx.setWitness(i, payment.data.witness);
}
} else if (!allowIncomplete) {
throw new ArgumentError('Transaction is not complete');
}
}
if (!allowIncomplete) {
// do not rely on this, its merely a last resort
if (_overMaximumFees(tx.virtualSize())) {
throw new ArgumentError('Transaction has absurd fees');
}
}
return tx;
}
bool _overMaximumFees(int bytes) {
int incoming = _inputs.fold(0, (cur, acc) => cur + (acc.value ?? 0));
int outgoing = _tx.outs.fold(0, (cur, acc) => cur + (acc.value ?? 0));
int fee = incoming - outgoing;
int feeRate = fee ~/ bytes;
return feeRate > maximumFeeRate;
}
bool _canModifyInputs() {
return _inputs.every((input) {
if (input.signatures == null) return true;
return input.signatures.every((signature) {
if (signature == null) return true;
return _signatureHashType(signature) & SIGHASH_ANYONECANPAY != 0;
});
});
}
bool _canModifyOutputs() {
final nInputs = _tx.ins.length;
final nOutputs = _tx.outs.length;
return _inputs.every((input) {
if (input.signatures == null) return true;
return input.signatures.every((signature) {
if (signature == null) return true;
final hashType = _signatureHashType(signature);
final hashTypeMod = hashType & 0x1f;
if (hashTypeMod == SIGHASH_NONE) return true;
if (hashTypeMod == SIGHASH_SINGLE) {
// if SIGHASH_SINGLE is set, and nInputs > nOutputs
// some signatures would be invalidated by the addition
// of more outputs
return nInputs <= nOutputs;
}
return false;
});
});
}
bool _needsOutputs(int signingHashType) {
if (signingHashType == SIGHASH_ALL) {
return this._tx.outs.length == 0;
}
// if inputs are being signed with SIGHASH_NONE, we don't strictly need outputs
// .build() will fail, but .buildIncomplete() is OK
return (this._tx.outs.length == 0) &&
_inputs.map((input) {
if (input.signatures == null || input.signatures.length == 0)
return false;
return input.signatures.map((signature) {
if (signature == null) return false; // no signature, no issue
final hashType = _signatureHashType(signature);
if (hashType & SIGHASH_NONE != 0)
return false; // SIGHASH_NONE doesn't care about outputs
return true; // SIGHASH_* does care
}).contains(true);
}).contains(true);
}
bool _canSign(Input input) {
return input.pubkeys != null &&
input.signScript != null &&
input.signatures != null &&
input.signatures.length == input.pubkeys.length &&
input.pubkeys.length > 0;
}
_addInputUnsafe(Uint8List hash, int vout, Input options) {
String txHash = HEX.encode(hash);
Input input;
if (isCoinbaseHash(hash)) {
throw new ArgumentError('coinbase inputs not supported');
}
final prevTxOut = '$txHash:$vout';
if (_prevTxSet[prevTxOut] != null)
throw new ArgumentError('Duplicate TxOut: ' + prevTxOut);
if (options.script != null) {
input =
Input.expandInput(options.script, options.witness ?? EMPTY_WITNESS);
} else {
input = new Input();
}
if (options.value != null) input.value = options.value;
if (input.prevOutScript == null && options.prevOutScript != null) {
if (input.pubkeys == null && input.signatures == null) {
var expanded = Output.expandOutput(options.prevOutScript);
if (expanded.pubkeys != null && !expanded.pubkeys.isEmpty) {
input.pubkeys = expanded.pubkeys;
input.signatures = expanded.signatures;
}
}
input.prevOutScript = options.prevOutScript;
input.prevOutType = classifyOutput(options.prevOutScript);
}
int vin = _tx.addInput(hash, vout, options.sequence, options.script);
_inputs.add(input);
_prevTxSet[prevTxOut] = true;
return vin;
}
int _signatureHashType(Uint8List buffer) {
return buffer.buffer.asByteData().getUint8(buffer.length - 1);
}
Transaction get tx => _tx;
Map get prevTxSet => _prevTxSet;
}
Uint8List pubkeyToOutputScript(Uint8List pubkey, [NetworkType nw]) {
NetworkType network = nw ?? bitcoin;
P2PKH p2pkh =
new P2PKH(data: new PaymentData(pubkey: pubkey), network: network);
return p2pkh.data.output;
}