1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import common = require('./common');
|
16 | import * as _ from 'lodash';
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const Blockchain = function(bitgo) {
|
22 | this.bitgo = bitgo;
|
23 | };
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Blockchain.prototype.getAddress = function(params, callback) {
|
34 | params = params || {};
|
35 | common.validateParams(params, ['address'], [], callback);
|
36 |
|
37 | return this.bitgo.get(this.bitgo.url('/address/' + params.address))
|
38 | .result()
|
39 | .nodeify(callback);
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | Blockchain.prototype.getAddressTransactions = function(params, callback) {
|
49 | params = params || {};
|
50 | common.validateParams(params, ['address'], [], callback);
|
51 |
|
52 |
|
53 | return this.bitgo.get(this.bitgo.url('/address/' + params.address + '/tx'))
|
54 | .result()
|
55 | .nodeify(callback);
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Blockchain.prototype.getAddressUnspents = function(params, callback) {
|
66 | params = params || {};
|
67 | common.validateParams(params, ['address'], [], callback);
|
68 |
|
69 | let url = this.bitgo.url('/address/' + params.address + '/unspents');
|
70 | if (params.limit) {
|
71 | if (!_.isInteger(params.limit)) {
|
72 | throw new Error('invalid limit - number expected');
|
73 | }
|
74 | url += '?limit=' + (params.limit * 1e8);
|
75 | }
|
76 |
|
77 | return this.bitgo.get(url)
|
78 | .result()
|
79 | .then(function(body) {
|
80 | return body.unspents;
|
81 | })
|
82 | .nodeify(callback);
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | Blockchain.prototype.getTransaction = function(params, callback) {
|
93 | params = params || {};
|
94 | common.validateParams(params, ['id'], [], callback);
|
95 |
|
96 | return this.bitgo.get(this.bitgo.url('/tx/' + params.id))
|
97 | .result()
|
98 | .nodeify(callback);
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Blockchain.prototype.getTransactionByInput = function(params, callback) {
|
110 | params = params || {};
|
111 | common.validateParams(params, ['txid'], [], callback);
|
112 | if (!_.isInteger(params.vout)) {
|
113 | throw new Error('invalid vout - number expected');
|
114 | }
|
115 | return this.bitgo.get(this.bitgo.url('/tx/input/' + params.txid + '/' + params.vout))
|
116 | .result()
|
117 | .nodeify(callback);
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | Blockchain.prototype.getBlock = function(params, callback) {
|
128 | params = params || {};
|
129 | common.validateParams(params, ['id'], [], callback);
|
130 |
|
131 | return this.bitgo.get(this.bitgo.url('/block/' + params.id))
|
132 | .result()
|
133 | .nodeify(callback);
|
134 | };
|
135 |
|
136 | module.exports = Blockchain;
|