forked from mrtnetwork/bitcoin_base
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathold_example.dart
More file actions
379 lines (297 loc) · 14.9 KB
/
Copy pathold_example.dart
File metadata and controls
379 lines (297 loc) · 14.9 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
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
// ignore_for_file: unused_element, unused_local_variable
import 'dart:convert';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:http/http.dart' as http;
BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);
final remind = sum - sumAll;
if (remind < BigInt.zero) {
throw ArgumentError("invalid values");
}
return remind;
}
Future<void> _brodcastTrasaction(String digets) async {
final params = {
"jsonrpc": "2.0",
"API_key": "27c54568-c97c-49a6-8667-993f8de2d4d8",
"method": "sendrawtransaction",
"params": [digets]
};
final cl = http.Client();
final res = await cl.post(
Uri.parse("https://go.getblock.io/2359f3004aef4fbba3a1c70f62f660d8"),
body: jsonEncode(params),
headers: {"Content-Type": "application/json"});
}
void _spendFrom2P2SHAnd2P2PKHAddress() async {
/// Define network
const network = BitcoinCashNetwork.testnet;
/// Define a seed using a hex string
final seed = BytesUtils.fromHexString(
"50b112c15987f0f1aa58430b6e1364cb4ac216a96b4a1e5030d63f62c3e13f0e9d5cfa97eb28584d112a7eddf53a0ca9f3ea58183f42c28f861162225bdab836");
/// Create a Bip32 key from the seed
final bip32 = Bip32Slip10Secp256k1.fromSeed(seed);
/// Generate child keys from the master key
final childKey1 = bip32.childKey(Bip32KeyIndex(1));
final childKey = bip32.childKey(Bip32KeyIndex(3));
/// Convert child key private keys to ECPrivate objects
final childKey1PrivateKey = ECPrivate.fromBytes(childKey1.privateKey.raw);
final childKey2PrivateKey = ECPrivate.fromBytes(childKey.privateKey.raw);
/// Convert child key public keys to ECPublic objects
final childKey1PublicKey = ECPublic.fromBytes(childKey1.publicKey.compressed);
final examplePublicKey = ECPublic.fromBytes(childKey.publicKey.compressed);
/// Define another private key using WIF
final ECPrivate examplePrivateKey = ECPrivate.fromWif(
'cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo',
netVersion: BitcoinNetwork.testnet.wifNetVer);
final examplePublicKey2 = examplePrivateKey.getPublic();
final msig2 = MultiSignatureAddress(threshold: 3, signers: [
MultiSignatureSigner(publicKey: examplePublicKey.toHex(), weight: 1),
MultiSignatureSigner(publicKey: childKey1PublicKey.toHex(), weight: 2),
]);
/// outputs
/// make sure pass network to address for validate, before sending create transaction
/// Create a P2shAddress instance from the specified BCH address and network
final out1 = P2shAddress.fromAddress(
address: "bchtest:pz2mcmtv8ect77a0fjqhl2cwcqswmvfq3qsxe5rq9r",
network: network);
/// Create a P2shAddress instance from the specified BCH address and network
final out2 = P2shAddress.fromAddress(
address: "bchtest:pp5w24ym8f7h8aspam24hk5msvz6yr9djvlguqda4e",
network: network);
/// Create a P2pkhAddress instance from the specified BCH address and network
final out3 = P2pkhAddress.fromAddress(
address: "bchtest:qr8kl5t2rajthcryx9wdcmfn4t4634dvev5ly5ewas",
network: network);
/// Create a P2shAddress instance from the specified BCH address and network
final out4 = P2pkhAddress.fromAddress(
address: "bchtest:pq7w6zt4vcv33yg54y3xfemm3k5d0ahceqmdanzcmz",
network: network);
/// Calculate the change value for the transaction
final change = _changeValue(
/// sum of utxos
BtcUtils.toSatoshi("0.101"),
[
BtcUtils.toSatoshi("0.03"),
/// Transaction fee
BtcUtils.toSatoshi("0.00003"),
]);
final b = ForkedTransactionBuilder(
outputs: [
/// Define a BitcoinOutput with the P2shAddress and a value of 0.01 BCH
BitcoinOutput(address: out1, value: BtcUtils.toSatoshi("0.01")),
/// Define a BitcoinOutput with the P2shAddress and a value of 0.01 BCH
BitcoinOutput(address: out2, value: BtcUtils.toSatoshi("0.01")),
/// Define a BitcoinOutput with the P2pkhAddress and a value of 0.01 BCH
BitcoinOutput(address: out3, value: BtcUtils.toSatoshi("0.01")),
/// Define a BitcoinOutput with the P2shAddress and a value equal to the 'change' variable
BitcoinOutput(address: out4, value: change),
],
/// Set the transaction fee to 0.00003 BCH
fee: BtcUtils.toSatoshi("0.00003"),
/// Specify the network for the litcoin transaction
network: network,
/// Define a list of Unspent Transaction outputs (UTXOs) for the Bitcoin transaction
utxos: [
UtxoWithAddress(
/// Create a UTXO using a BitcoinUtxo with specific details
utxo: BitcoinUtxo(
/// Transaction hash uniquely identifies the referenced transaction
txHash:
"fffd7bd09acdf87d383bb60d698f170fc4c3e74ebf50700e3724ae9a566d1b9b",
/// Value represents the amount of the UTXO in satoshis.
value: BtcUtils.toSatoshi("0.101"),
/// Vout is the output index of the UTXO within the referenced transaction
vout: 0,
/// Script type indicates the type of script associated with the UTXO's address
scriptType: examplePublicKey2.toP2pkhAddress().type,
),
/// Include owner details with the public key and address associated with the UTXO
ownerDetails: UtxoAddressDetails(
publicKey: examplePublicKey2.toHex(),
address: examplePublicKey2.toP2pkhAddress())),
]);
/// Build the transaction by invoking the buildTransaction method on the ForkedTransactionBuilder
final tr = b.buildTransaction((trDigest, utxo, publicKey, int sighash) {
/// For each input in the transaction, locate the corresponding private key
/// and sign the transaction digest to construct the unlocking script.
if (publicKey == childKey1PublicKey.toHex()) {
return childKey1PrivateKey.signInput(trDigest, sigHash: sighash);
}
if (publicKey == examplePublicKey.toHex()) {
return childKey2PrivateKey.signInput(trDigest, sigHash: sighash);
}
if (publicKey == examplePublicKey2.toHex()) {
return examplePrivateKey.signInput(trDigest, sigHash: sighash);
}
throw UnimplementedError();
});
/// Get the transaction ID
final txId = tr.txId();
/// Calculate the size of the transaction in bytes.
/// You can determine the transaction fee by multiplying the transaction size
/// Formula: transaction fee = (transaction size in bytes * fee rate in bytes)
final size = tr.getSize();
/// broadcast transaction
/// https://tbch.loping.net/tx/ece972a6d5d1b07062ed5aaab786b528b04a3cf675affdda8cb79ec805c53ce2
await _brodcastTrasaction(tr.serialize());
}
void _spendFrom2P2SHAnd1P2PKHAddress() async {
/// Define network
const network = BitcoinCashNetwork.testnet;
/// Define a seed using a hex string
final seed = BytesUtils.fromHexString(
"50b112c15987f0f1aa58430b6e1364cb4ac216a96b4a1e5030d63f62c3e13f0e9d5cfa97eb28584d112a7eddf53a0ca9f3ea58183f42c28f861162225bdab836");
/// Create a Bip32 key from the seed
final bip32 = Bip32Slip10Secp256k1.fromSeed(seed);
/// Generate child keys from the master key
final childKey1 = bip32.childKey(Bip32KeyIndex(1));
final childKey = bip32.childKey(Bip32KeyIndex(3));
/// Convert child key private keys to ECPrivate objects
final childKey1PrivateKey = ECPrivate.fromBytes(childKey1.privateKey.raw);
final childKey2PrivateKey = ECPrivate.fromBytes(childKey.privateKey.raw);
/// Convert child key public keys to ECPublic objects
final childKey1PublicKey = ECPublic.fromBytes(childKey1.publicKey.compressed);
final examplePublicKey = ECPublic.fromBytes(childKey.publicKey.compressed);
/// Define another private key from WIF
final ECPrivate examplePrivateKey = ECPrivate.fromWif(
'cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo',
netVersion: BitcoinNetwork.testnet.wifNetVer);
final examplePublicKey2 = examplePrivateKey.getPublic();
/// Create a MultiSignatureAddress with two signers and a threshold of 3
/// script = [OP_3, ch2PubPublicKey, ch1PubPublicKey,ch1PubPublicKey, OP_3, OP_CHECKMULTISIG]
final msig2 = MultiSignatureAddress(threshold: 3, signers: [
MultiSignatureSigner(publicKey: examplePublicKey.toHex(), weight: 1),
MultiSignatureSigner(publicKey: childKey1PublicKey.toHex(), weight: 2),
]);
/// Calculate the change value for the transaction
final change = _changeValue(
/// sum of utxos
BtcUtils.toSatoshi("0.10097"),
[
BtcUtils.toSatoshi("0.02"),
/// Transaction fee
BtcUtils.toSatoshi("0.00003"),
]);
/// outputs
/// make sure pass network to address for validate, before sending create transaction
/// Create a P2pkhAddress instance from the specified BCH address and network
final out1 = P2pkhAddress.fromAddress(
address: "bchtest:qr7nx7knh7q7ppkedf5wr7xk5zj3p7xzfgapvt5csc",
network: network);
/// Create a P2shAddress instance from the specified BCH address and network
final out2 = P2shAddress.fromAddress(
address: "bchtest:pz4xpuvkmtuz9k9rksdv5lnu5q6jqvc8hus60get4a",
network: network);
/// Create a P2shAddress instance from the specified BCH address and network
final out3 = P2shAddress.fromAddress(
address: "bchtest:prs0e39dpv0j2ysj00cec4t4x2k4833dsvmxeq8fr0",
network: network);
/// Calculate the change value for the transaction
final b = ForkedTransactionBuilder(
/// outputs
outputs: [
/// Define a BitcoinOutput with the P2pkhAddress and a value of 0.01 BCH
BitcoinOutput(address: out1, value: BtcUtils.toSatoshi("0.01")),
/// Define a BitcoinOutput with the P2shAddress and a value of 0.01 BCH
BitcoinOutput(address: out2, value: BtcUtils.toSatoshi("0.01")),
/// Define a BitcoinOutput with the P2shAddress and a value equal to the 'change' variable
BitcoinOutput(address: out3, value: change),
],
/// Set the transaction fee to 0.00003 BCH
fee: BtcUtils.toSatoshi("0.00003"),
/// Specify the network for the BCH transaction
network: network,
/// Add a memo to the transaction, linking to the GitHub repository
memo: "https://github.com/mrtnetwork",
/// Define a list of Unspent Transaction outputs (UTXOs) for the Bitcoin transaction
utxos: [
UtxoWithAddress(
/// Create a UTXO using a BitcoinUtxo with specific details
utxo: BitcoinUtxo(
/// Transaction hash uniquely identifies the referenced transaction
txHash:
"ece972a6d5d1b07062ed5aaab786b528b04a3cf675affdda8cb79ec805c53ce2",
/// Value represents the amount of the UTXO in satoshis.
value: BtcUtils.toSatoshi("0.01"),
/// Vout is the output index of the UTXO within the referenced transaction
vout: 0,
/// Script type indicates the type of script associated with the UTXO's address
scriptType: childKey1PublicKey.toP2pkInP2sh().type,
),
/// Include owner details with the public key and address associated with the UTXO
ownerDetails: UtxoAddressDetails(
publicKey: childKey1PublicKey.toHex(),
address: childKey1PublicKey.toP2pkInP2sh())),
UtxoWithAddress(
utxo: BitcoinUtxo(
/// Transaction hash uniquely identifies the referenced transaction
txHash:
"ece972a6d5d1b07062ed5aaab786b528b04a3cf675affdda8cb79ec805c53ce2",
/// Value represents the amount of the UTXO in satoshis.
value: BtcUtils.toSatoshi("0.01"),
/// Vout is the output index of the UTXO within the referenced transaction
vout: 1,
/// Script type indicates the type of script associated with the UTXO's address
scriptType: childKey1PublicKey.toP2pkhInP2sh().type,
),
/// Include owner details with the public key and address associated with the UTXO
ownerDetails: UtxoAddressDetails(
publicKey: childKey1PublicKey.toHex(),
address: childKey1PublicKey.toP2pkhInP2sh())),
UtxoWithAddress(
utxo: BitcoinUtxo(
/// Transaction hash uniquely identifies the referenced transaction
txHash:
"ece972a6d5d1b07062ed5aaab786b528b04a3cf675affdda8cb79ec805c53ce2",
/// Value represents the amount of the UTXO in satoshis.
value: BtcUtils.toSatoshi("0.01"),
/// Vout is the output index of the UTXO within the referenced transaction
vout: 2,
/// Script type indicates the type of script associated with the UTXO's address
scriptType: examplePublicKey.toP2pkhAddress().type,
),
/// Include owner details with the public key and address associated with the UTXO
ownerDetails: UtxoAddressDetails(
publicKey: examplePublicKey.toHex(),
address: examplePublicKey.toP2pkhAddress())),
UtxoWithAddress(
utxo: BitcoinUtxo(
/// Transaction hash uniquely identifies the referenced transaction
txHash:
"ece972a6d5d1b07062ed5aaab786b528b04a3cf675affdda8cb79ec805c53ce2",
/// Value represents the amount of the UTXO in satoshis.
value: BtcUtils.toSatoshi("0.07097"),
/// Vout is the output index of the UTXO within the referenced transaction
vout: 3,
/// Script type indicates the type of script associated with the UTXO's address
scriptType: msig2.toP2shAddress().type,
),
/// Include owner details with the multiSigAddress and address associated with the UTXO
ownerDetails: UtxoAddressDetails.multiSigAddress(
multiSigAddress: msig2, address: msig2.toP2shAddress())),
]);
final tr = b.buildTransaction((trDigest, utxo, publicKey, int sighash) {
if (publicKey == childKey1PublicKey.toHex()) {
return childKey1PrivateKey.signInput(trDigest, sigHash: sighash);
}
if (publicKey == examplePublicKey.toHex()) {
return childKey2PrivateKey.signInput(trDigest, sigHash: sighash);
}
if (publicKey == examplePublicKey2.toHex()) {
return examplePrivateKey.signInput(trDigest, sigHash: sighash);
}
throw UnimplementedError();
});
/// Get the transaction ID
final txId = tr.txId();
/// Calculate the size of the transaction in bytes.
/// You can determine the transaction fee by multiplying the transaction size
/// Formula: transaction fee = (transaction size in bytes * fee rate in bytes)
final size = tr.getSize();
/// broadcast transaction
/// https://tbch.loping.net/tx/205881575bf503903f7a572dacd944bebc77a2b4ca6fae81bb6b1a89b3af7be9
await _brodcastTrasaction(tr.serialize());
}