-
Notifications
You must be signed in to change notification settings - Fork 78
/
BitcoinRPCProvider.js
112 lines (91 loc) · 3.01 KB
/
BitcoinRPCProvider.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
import { flatten } from 'lodash'
import JsonRpcProvider from '../JsonRpcProvider'
export default class BitcoinRPCProvider extends JsonRpcProvider {
async decodeRawTransaction (rawTransaction) {
const data = await this.jsonrpc('decoderawtransaction', rawTransaction)
const { hash: txHash, txid: hash, vout } = data
const value = vout.reduce((p, n) => p + parseInt(n.value), 0)
const output = { hash, value, _raw: { hex: rawTransaction, data, txHash } }
return output
}
async isAddressUsed (address) {
address = String(address)
const utxo = await this.getUnspentTransactions(address)
return utxo.length !== 0
}
async getBalance (addresses) {
addresses = addresses
.map(address => String(address))
const _utxos = await Promise.all(addresses.map(address => this.getUnspentTransactions(address)))
const utxos = flatten(_utxos)
return utxos.reduce((acc, utxo) => acc + (utxo.amount * 1e8), 0)
}
async getUnspentTransactions (address) {
return this.jsonrpc('listunspent', 0, 9999999, [ address ])
}
async getTransactionHex (transactionHash) {
return (await this.jsonrpc('gettransaction', transactionHash)).hex
}
async generateBlock (numberOfBlocks) {
return this.jsonrpc('generate', numberOfBlocks)
}
async getBlockByHash (blockHash, includeTx) {
const data = await this.jsonrpc('getblock', blockHash)
const {
hash,
height: number,
time: timestamp,
difficulty,
size,
previousblockhash: parentHash,
nonce,
confirmations
} = data
let { tx: transactions } = data
if (includeTx) {
const txs = transactions.map(this.getMethod('getRawTransactionByHash'))
transactions = await Promise.all(txs)
}
return {
hash,
number,
timestamp,
difficulty,
size,
parentHash,
nonce,
transactions,
confirmations
}
}
async getBlockByNumber (blockNumber, includeTx) {
return this.getBlockByHash(await this.jsonrpc('getblockhash', blockNumber), includeTx)
}
async getBlockHeight () {
return this.jsonrpc('getblockcount')
}
async getTransactionByHash (transactionHash) {
const rawTx = await this.getRawTransactionByHash(transactionHash)
const tx = await this.decodeRawTransaction(rawTx)
try {
const data = await this.jsonrpc('gettransaction', transactionHash)
const { confirmations } = data
const output = Object.assign({}, tx, { confirmations })
if (confirmations > 0) {
const { blockhash: blockHash } = data
const { number: blockNumber } = await this.getBlockByHash(blockHash)
Object.assign(output, { blockHash, blockNumber })
}
return output
} catch (e) {
const output = Object.assign({}, tx)
return output
}
}
async getRawTransactionByHash (transactionHash) {
return this.jsonrpc('getrawtransaction', transactionHash)
}
async sendRawTransaction (rawTransaction) {
return this.jsonrpc('sendrawtransaction', rawTransaction)
}
}