UNPKG

3.42 kBPlain TextView Raw
1/**
2 * @hidden
3 */
4
5/**
6 */
7//
8// Blockchain Object
9// BitGo accessor to a any Bitcoin address.
10// Using this does not require authentication and is unrelated to BitGo wallet management.
11//
12// Copyright 2014, BitGo, Inc. All Rights Reserved.
13//
14
15import common = require('./common');
16import * as _ from 'lodash';
17
18//
19// Constructor
20//
21const Blockchain = function(bitgo) {
22 this.bitgo = bitgo;
23};
24
25//
26// Get an address
27// Fetch an address summary information.
28// Includes balance and pending balance.
29//
30// Parameters include:
31// address: the address to get
32//
33Blockchain.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// Get address transactions
44// List the transactions for a given address
45// Parameters include:
46// address: the address to get transactions for
47//
48Blockchain.prototype.getAddressTransactions = function(params, callback) {
49 params = params || {};
50 common.validateParams(params, ['address'], [], callback);
51
52 // TODO: support start and limit params
53 return this.bitgo.get(this.bitgo.url('/address/' + params.address + '/tx'))
54 .result()
55 .nodeify(callback);
56};
57
58//
59// Unspent Transactions
60// List the unspent outputs for a given address
61// Parameters include:
62// address: the address to get unspent transactions
63// limit: return enough unspents to accumulate to at least this amount (in satoshis).
64//
65Blockchain.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// Get transaction
87// Fetch transaction details.
88//
89// Parameters include:
90// id: the transaction id to get
91//
92Blockchain.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// Get transaction that spends a specific output
103// Fetch transaction details.
104//
105// Parameters include:
106// txid: the transaction id of the output
107// vout: the position of the output on the transaction that created it
108//
109Blockchain.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// Get block
122// Fetch block details.
123//
124// Parameters include:
125// id: the block hash to get, or latest for the latest
126//
127Blockchain.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
136module.exports = Blockchain;