-
Notifications
You must be signed in to change notification settings - Fork 33
/
rpc_utxo_as_utxo.js
86 lines (70 loc) · 2.2 KB
/
rpc_utxo_as_utxo.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
const constants = require('./constants');
const formats = constants.addressFormats;
const {keys} = Object;
/** Derive UTXO details from RPC UTXO details
{
address: <Chain Address String>
address_type: <Address Type String>
amount_sat: <UTXO Tokens Value String>
confirmations: <Total Confirmation Count In Best Chain String>
outpoint: {
output_index: <Transaction Vout Index Number>
txid_str: <Hex Encoded Transaction Id String>
}
pk_script: <Hex Encoded Public Key Output Script String>
}
@throws
<Error>
@returns
{
address: <Chain Address String>
address_format: <Chain Address Format String>
confirmation_count: <Confirmation Count Number>
output_script: <Output Script Hex String>
tokens: <Unspent Tokens Number>
transaction_id: <Transaction Id Hex String>
transaction_vout: <Transaction Output Index Number>
}
*/
module.exports = utxo => {
if (!utxo) {
throw new Error('ExpectedRpcUtxoToDeriveUtxoDetails');
}
if (!utxo.address) {
throw new Error('ExpectedAddressInUtxoResponse');
}
if (!utxo.address_type) {
throw new Error('ExpectedAddressTypeInListedUtxo');
}
const format = keys(formats).find(k => formats[k] === utxo.address_type);
if (!format) {
throw new Error('UnexpectedAddressTypeInUtxoResponse');
}
if (!utxo.amount_sat) {
throw new Error('ExpectedValueOfUnspentOutputInUtxosResponse');
}
if (utxo.confirmations === undefined) {
throw new Error('ExpectedConfCountForUtxoInUtxoResponse');
}
if (!utxo.outpoint) {
throw new Error('ExpectedOutpointForUtxoInUtxosResponse');
}
if (utxo.outpoint.output_index === undefined) {
throw new Error('ExpectedOutpointIndexForUtxoInUtxosResponse');
}
if (!utxo.outpoint.txid_str) {
throw new Error('ExpectedTransactionIdForUtxoInUtxosResponse');
}
if (!utxo.pk_script) {
throw new Error('ExpectedScriptPubForUtxoInUtxosResponse');
}
return {
address: utxo.address,
address_format: format,
confirmation_count: Number(utxo.confirmations),
output_script: utxo.pk_script,
tokens: Number(utxo.amount_sat),
transaction_id: utxo.outpoint.txid_str,
transaction_vout: utxo.outpoint.output_index,
};
};