forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.js
More file actions
111 lines (97 loc) · 2.74 KB
/
Copy pathblockchain.js
File metadata and controls
111 lines (97 loc) · 2.74 KB
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
//
// Blockchain Object
// BitGo accessor to a any Bitcoin address.
// Using this does not require authentication and is unrelated to BitGo wallet management.
//
// Copyright 2014, BitGo, Inc. All Rights Reserved.
//
var request = require('superagent');
var common = require('./common');
//
// Constructor
//
var Blockchain = function(bitgo) {
this.bitgo = bitgo;
};
//
// Get an address
// Fetch an address summary information.
// Includes balance and pending balance.
//
// Parameters include:
// address: the address to get
//
Blockchain.prototype.getAddress = function(params, callback) {
params = params || {};
common.validateParams(params, ['address'], [], callback);
return this.bitgo.get(this.bitgo.url("/address/" + params.address))
.result()
.nodeify(callback);
};
//
// Get address transactions
// List the transactions for a given address
// Parameters include:
// address: the address to get transactions for
//
Blockchain.prototype.getAddressTransactions = function(params, callback) {
params = params || {};
common.validateParams(params, ['address'], [], callback);
// TODO: support start and limit params
return this.bitgo.get(this.bitgo.url("/address/" + params.address + "/tx"))
.result()
.nodeify(callback);
};
//
// Unspent Transactions
// List the unspent outputs for a given address
// Parameters include:
// address: the address to get unspent transactions
// limit: return enough unspents to accumulate to at least this amount (in satoshis).
//
Blockchain.prototype.getAddressUnspents = function(params, callback) {
params = params || {};
common.validateParams(params, ['address'], [], callback);
var url = this.bitgo.url("/address/" + params.address + '/unspents');
if (params.limit) {
if (typeof(params.limit) != 'number') {
throw new Error('invalid limit - number expected');
}
url += '?limit=' + (params.limit * 1e8);
}
return this.bitgo.get(url)
.result()
.then(function(body) {
return body.unspents;
})
.nodeify(callback);
};
//
// Get transaction
// Fetch transaction details.
//
// Parameters include:
// id: the transaction id to get
//
Blockchain.prototype.getTransaction = function(params, callback) {
params = params || {};
common.validateParams(params, ['id'], [], callback);
return this.bitgo.get(this.bitgo.url("/tx/" + params.id))
.result()
.nodeify(callback);
};
//
// Get block
// Fetch block details.
//
// Parameters include:
// id: the block hash to get, or latest for the latest
//
Blockchain.prototype.getBlock = function(params, callback) {
params = params || {};
common.validateParams(params, ['id'], [], callback);
return this.bitgo.get(this.bitgo.url("/block/" + params.id))
.result()
.nodeify(callback);
};
module.exports = Blockchain;