forked from mrtnetwork/bitcoin_base
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbch_example.dart
More file actions
101 lines (85 loc) · 3.49 KB
/
Copy pathbch_example.dart
File metadata and controls
101 lines (85 loc) · 3.49 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
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:example/services_examples/electrum/electrum_websocket_service.dart';
void main() async {
/// connect to electrum service with websocket
/// please see `services_examples` folder for how to create electrum websocket service
final service = await ElectrumWebSocketService.connect(
"wss://chipnet.imaginary.cash:50004");
/// create provider with service
final provider = ElectrumApiProvider(service);
/// initialize private key
final privateKey = ECPrivate.fromBytes(BytesUtils.fromHexString(
"f9061c5cb343c6b6a73900ee29509bb0bd2213319eea46d2f2a431068c9da06b"));
/// public key
final publicKey = privateKey.getPublic();
/// network
const network = BitcoinCashNetwork.testnet;
/// Derives a P2PKH address from the given public key and converts it to a Bitcoin Cash address
/// for enhanced accessibility within the network.
final p2pkhAddress =
BitcoinCashAddress.fromBaseAddress(publicKey.toP2pkhAddress());
/// Initialize two P2SH32 addresses for receiving funds.
final p2sh32Example1 = BitcoinCashAddress(
"bchtest:pw054wtjjc70rrvx4ftl4p63gluedyt0qmpgz705f8x3gxygrzarzls7vp2sj",
network: network);
final p2sh32Example2 = BitcoinCashAddress(
"bchtest:pvw39llgap0a4vm8jn9sjsvfsthah4wgemjlh6epdtzr3pl2fqtmsn3s4vcm7",
network: network);
/// Reads all UTXOs (Unspent Transaction outputs) associated with the account.
/// We does not need tokens utxo and we set to false.
final elctrumUtxos = await provider.request(ElectrumScriptHashListUnspent(
scriptHash: p2pkhAddress.baseAddress.pubKeyHash(),
includeTokens: false,
));
/// Converts all UTXOs to a list of UtxoWithAddress, containing UTXO information along with address details.
final List<UtxoWithAddress> utxos = elctrumUtxos
.map((e) => UtxoWithAddress(
utxo: e.toUtxo(p2pkhAddress.type),
ownerDetails: UtxoAddressDetails(
publicKey: publicKey.toHex(), address: p2pkhAddress.baseAddress)))
.toList();
/// som of utxos in satoshi
final sumOfUtxo = utxos.sumOfUtxosValue();
if (sumOfUtxo == BigInt.zero) {
return;
}
final bchTransaction = ForkedTransactionBuilder(
outputs: [
/// change input (sumofutxos - spend)
BitcoinOutput(
address: p2pkhAddress.baseAddress,
value: sumOfUtxo -
(BtcUtils.toSatoshi("0.0001") +
BtcUtils.toSatoshi("0.0001") +
BtcUtils.toSatoshi("0.00003")),
),
BitcoinOutput(
address: p2sh32Example1.baseAddress,
value: BtcUtils.toSatoshi("0.0001"),
),
BitcoinOutput(
address: p2sh32Example2.baseAddress,
value: BtcUtils.toSatoshi("0.0001"),
),
],
fee: BtcUtils.toSatoshi("0.00003"),
network: network,
utxos: utxos,
);
final transaaction =
bchTransaction.buildTransaction((trDigest, utxo, publicKey, sighash) {
return privateKey.signInput(trDigest, sigHash: sighash);
});
/// transaction ID
transaaction.txId();
/// for calculation fee
transaaction.getSize();
/// raw of encoded transaction in hex
final transactionRaw = transaaction.toHex();
/// send transaction to network
await provider
.request(ElectrumBroadCastTransaction(transactionRaw: transactionRaw));
/// done! check the transaction in block explorer
/// https://chipnet.imaginary.cash/tx/9e534f8a64f76b1af5ccf2522392697f2242fd215206a458cfe286bca4a3ec0a
}