This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitcoin_wallet.ts
174 lines (144 loc) · 4 KB
/
bitcoin_wallet.ts
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
import { Amount, Chain, Network, Pool, TX, WalletDB } from "bcoin";
import Logger from "blgr";
/**
* Interface defining the Bitcoin wallet used in the SDK.
* You can plug your own wallet by passing a custom implementation of this interface to the {@link ComitClient}.
*/
export interface BitcoinWallet {
getAddress(): Promise<string>;
getBalance(): Promise<number>;
sendToAddress(
address: string,
satoshis: number,
network: string
): Promise<string>;
broadcastTransaction(
transactionHex: string,
network: string
): Promise<string>;
getFee(): string;
}
/**
* Simple Bitcoin wallet based on [bcoin]{@link https://github.com/bcoin-org/bcoin}.
*/
export class InMemoryBitcoinWallet implements BitcoinWallet {
public static async newInstance(
network: string,
peerUri: string,
hdKey: string
): Promise<InMemoryBitcoinWallet> {
const parsedNetwork = Network.get(network);
const logger = new Logger({
level: "warning"
});
const walletdb = new WalletDB({
memory: true,
witness: true,
logger,
network: parsedNetwork
});
const chain = new Chain({
spv: true,
logger,
network: parsedNetwork
});
const pool = new Pool({
chain,
logger
});
await logger.open();
await pool.open();
await walletdb.open();
await chain.open();
await pool.connect();
const wallet = await walletdb.create({
logger,
network: parsedNetwork,
master: hdKey
});
const account = await wallet.getAccount(0);
for (let i = 0; i < 100; i++) {
pool.watchAddress(await account.deriveReceive(i).getAddress());
pool.watchAddress(await account.deriveChange(i).getAddress());
}
pool.startSync();
pool.on("tx", (tx: any) => {
walletdb.addTX(tx);
});
pool.on("block", (block: any) => {
walletdb.addBlock(block);
if (block.txs.length > 0) {
block.txs.forEach((tx: any) => {
walletdb.addTX(tx);
});
}
});
const netAddr = await pool.hosts.addNode(peerUri);
const peer = pool.createOutbound(netAddr);
pool.peers.add(peer);
return new InMemoryBitcoinWallet(
parsedNetwork,
walletdb,
pool,
chain,
wallet
);
}
private constructor(
public readonly network: any,
// @ts-ignore
private readonly walletdb: any,
private readonly pool: any,
// @ts-ignore
private readonly chain: any,
private readonly wallet: any
) {}
public async getBalance(): Promise<number> {
const balance = await this.wallet.getBalance();
// TODO: Balances stay unconfirmed, try to use bcoin.SPVNode (and set node.http to undefined) see if it catches the confirmations
const amount = new Amount(balance.toJSON().unconfirmed, "sat");
return amount.toBTC(true);
}
public async getAddress(): Promise<string> {
const receiveAddress = await this.wallet.receiveAddress(0);
return receiveAddress.toString(this.network);
}
public async sendToAddress(
address: string,
satoshis: number,
network: string
): Promise<string> {
this.assertNetwork(network);
const transaction = await this.wallet.send({
witness: true,
outputs: [
{
address,
value: satoshis
}
]
});
await this.pool.broadcast(transaction);
return transaction.txid();
}
public async broadcastTransaction(
transactionHex: string,
network: string
): Promise<string> {
this.assertNetwork(network);
const transaction = TX.fromRaw(transactionHex, "hex");
await this.pool.broadcast(transaction);
return transaction.txid();
}
public getFee(): string {
// should be dynamic in a real application
return "150";
}
private assertNetwork(network: string): void {
if (network !== this.network.type) {
throw new Error(
`This wallet is only connected to the ${this.network.type} network and cannot perform actions on the ${network} network`
);
}
}
}