This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspv.js
119 lines (96 loc) · 2.87 KB
/
spv.js
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
"use strict";
const bcoin = require("bcoin");
const Client = require("bitcoin-core");
const Logger = require("blgr");
const logger = new Logger({
level: "debug"
});
bcoin.set("regtest");
const BITCOIN_AUTH = {
protocol: "http",
username: "bitcoin",
password: "54pLR_f7-G6is32LP-7nbhzZSbJs_2zSATtZV_r05yg=",
host: "localhost",
port: "18443"
};
const bitcoinClient = new Client(BITCOIN_AUTH);
const node = new bcoin.SPVNode({
network: "regtest",
logger,
memory: true
});
// We do not need the RPC interface
node.rpc = null;
const walletdb = new bcoin.wallet.WalletDB({
memory: false,
location: "./bcoin-db/",
logger: logger
});
(async () => {
await node.open();
await walletdb.open();
await node.connect();
const wallet = await walletdb.create({ logger: logger });
wallet.logger = logger;
const walletAddress = await wallet.receiveAddress();
console.log("Created wallet with address %s", walletAddress);
// Add our address to the SPV filter.
node.pool.watchAddress(walletAddress);
// Start the blockchain sync.
node.startSync();
// Get ready to receive transactions!
node.on("tx", tx => {
console.log("Received TX:\n", tx);
walletdb.addTX(tx);
console.log("TX added to wallet DB!");
});
node.on("block", async block => {
console.log("Received Block:\n", block);
await walletdb.addBlock(block);
console.log("Block added to wallet DB!");
console.log("Balance:", await wallet.getBalance());
if (block.txs.length > 0) {
block.txs.forEach(tx => {
walletdb.addTX(tx);
console.log("TX added to wallet DB!");
});
}
});
wallet.on("balance", balance => {
console.log("Balance updated:\n", balance.toJSON());
});
const netAddr = await node.pool.hosts.addNode("127.0.0.1:18444");
const peer = node.pool.createOutbound(netAddr);
node.pool.peers.add(peer);
console.log("Peers:", await bitcoinClient.getPeerInfo());
await bitcoinClient.generate(101);
const fundingTxId = await bitcoinClient.sendToAddress(
walletAddress.toString(),
0.9
);
console.log("Transaction:", fundingTxId);
await bitcoinClient.generate(1);
const rawFundingTx = await bitcoinClient.getRawTransaction(fundingTxId);
console.log("rawFundingTx:", rawFundingTx);
await new Promise(r => setTimeout(r, 1000));
await bitcoinClient.generate(1);
console.log("Balance:", await wallet.getBalance());
const tx = await wallet.send({
witness: true,
outputs: [
{
address:
"bcrt1qp6xfd6qnun0v8ztd0jne8yve0cf2uyxaxn4mmd0akvd7ccyy49msg56d4u",
value: 15000
}
]
});
await node.pool.broadcast(tx);
console.log("Bcoin tx hash:", tx.txid());
await new Promise(r => setTimeout(r, 10000));
const rawtx = await bitcoinClient.getRawTransaction(tx.txid());
console.log("rawtx:", rawtx);
})().catch(err => {
console.error(err.stack);
process.exit(1);
});