-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitcoin-core.ts
71 lines (64 loc) · 1.88 KB
/
bitcoin-core.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
import { Network } from "bitcoinjs-lib";
import * as bjs from "bitcoinjs-lib";
import * as bip32 from "bip32";
import { BlockchainClient } from "./client";
import { Transaction } from "./transaction";
import { Networks } from "../utils/networks";
import { RpcClient } from "../utils/rpc-client";
export class BitcoinCoreClient implements BlockchainClient {
rpc: RpcClient;
network: Network;
constructor(
url: string,
username: string,
password: string,
network: string
) {
this.rpc = new RpcClient(url, username, password);
this.network = Networks[network];
}
private async _getTransaction(hash: string) {
return this.rpc.call("gettransaction", hash);
}
async getAddressTransactions(address: string) {
const receivedByAddress = await this.rpc.call(
"listreceivedbyaddress",
0,
false,
true,
address
);
if (receivedByAddress.length === 0) return [];
const txs = await Promise.all(
receivedByAddress[0].txids.map((hash: string) =>
this._getTransaction(hash)
)
);
return txs.map((tx: any) => {
const transaction: Transaction = {
hash: tx.txid,
confirmations: tx.confirmations,
amount: Math.abs(tx.amount),
};
return transaction;
});
}
async importWallet(xpub: string, numAddresses: number) {
const baseNode = bip32.fromBase58(xpub, this.network);
const addresses = [];
for (let i = 0; i < numAddresses; i++) {
const publicKey = baseNode.derive(0).derive(i).publicKey;
const address = bjs.payments.p2wpkh({
pubkey: publicKey,
network: this.network,
}).address;
addresses.push(address);
}
const addrRequest = addresses.map((addr) => ({
scriptPubKey: { address: addr },
watchonly: true,
timestamp: "now",
}));
return this.rpc.call("importmulti", addrRequest);
}
}