-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
189 lines (162 loc) · 5.18 KB
/
client.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var request = require('request')
var inherits = require('util').inherits
var Q = require('q')
var _ = require('lodash')
function BaseClient(url) {
this.url = url
}
BaseClient.prototype.beforeRequestCallback = function () {}
BaseClient.prototype.afterRequestCallback = function () {}
BaseClient.prototype.setCallbacks = function (before, after) {
this.beforeRequestCallback = before
this.afterRequestCallback = after
}
BaseClient.prototype._postRequest = function (method, data) {
var self = this
var uri = this.url + method
var callInfo = {
httpMethod: "POST", uri: uri, method: method, data: data
}
this.beforeRequestCallback(callInfo)
return Q.nfcall(request, {
method: 'POST',
uri: uri,
body: data,
json: true
}).spread(function (response, body) {
callInfo.statusCode = response.statusCode
callInfo.body = body
callInfo.ok = (response.statusCode == 200)
self.afterRequestCallback(callInfo)
if (response.statusCode !== 200)
throw new Error(uri + ' returned status code ' + response.statusCode)
return body
}, function (error) {
callInfo.error = error
callInfo.ok = false
self.afterRequestCallback(callInfo)
throw error
})
}
BaseClient.prototype._getRequest = function (method, data) {
var self = this
var queryString = _.map(data, function (val, key) {
return [key, val].map(encodeURIComponent).join('=')
}).join('&')
var uri = this.url + method + '?' + queryString
var callInfo = {
httpMethod: "GET", uri: uri, method: method, data: data
}
this.beforeRequestCallback(callInfo)
return Q.nfcall(request, {
method: 'GET',
uri: uri,
json: true
}).spread(function (response, body) {
callInfo.statusCode = response.statusCode
callInfo.body = body
callInfo.ok = (response.statusCode == 200)
self.afterRequestCallback(callInfo)
if (response.statusCode !== 200)
throw new Error(uri + ' returned status code ' + response.statusCode)
return body
}, function (error) {
callInfo.error = error
callInfo.ok = false
self.afterRequestCallback(callInfo)
throw error
})
}
function APIClient (url) {
this.url = url
}
inherits(APIClient, BaseClient)
APIClient.prototype.createIssueTx = function (data) {
return this._postRequest('createIssueTx', data)
}
APIClient.prototype.createTransferTx = function (data) {
return this._postRequest('createTransferTx', data)
}
APIClient.prototype.getTx = function (txId) {
return this._getRequest('getTx', {txId: txId}).then(function (res) {
return res.tx
})
}
APIClient.prototype.newMonitoringGroup = function () {
var self = this
return this._postRequest('tsm/newMonitoringGroup', {})
.then(function (res) {
var tsmClient = new TSMClient(self.url, res.groupId)
tsmClient.setCallbacks(self.beforeRequestCallback, self.afterRequestCallback)
return tsmClient
})
}
APIClient.prototype.getMonitoringGroup = function (groupId) {
var tsmClient = new TSMClient(this.url, groupId)
tsmClient.setCallbacks(this.beforeRequestCallback, this.afterRequestCallback)
return Q(tsmClient)
}
APIClient.prototype.broadcastTx = function (txHex) {
return this._postRequest('broadcastTx', {tx: txHex})
}
APIClient.prototype.getUnspentCoins = function (addresses, color) {
return this._postRequest('getUnspentCoins',
{addresses: addresses, color: color})
.then(function (res) { return res.coins })
}
APIClient.prototype.getAllColoredCoins = function (color, unspent) {
if (!unspent) unspent = false
return this._getRequest('getAllColoredCoins',
{color: color, unspent: unspent})
}
APIClient.prototype.getTxColorValues = function (txId, outIndices) {
var data = {txId: txId}
if (outIndices !== undefined) {
if (_.isArray(outIndices) || outIndices === null)
data.outIndices = outIndices
else
data.outIndex = outIndices
}
return this._postRequest('getTxColorValues', data).then(function (res) {
return res.colorValues
})
}
APIClient.prototype.getTxOutputColorValue = function (txId, outIndex) {
return this._postRequest('getTxColorValues', {txId: txId, outIndex: outIndex}).then(function (res) {
return res.colorValues[outIndex]
})
}
function TSMClient(url, groupId) {
this.url = url
this.groupId = groupId
this.lastPoint = null
}
inherits(TSMClient, BaseClient)
TSMClient.prototype.getId = function () {
return this.groupId
}
TSMClient.prototype.addTx = function (txId) {
return this._postRequest('tsm/addTx',
{groupId: this.groupId, txId: txId})
}
TSMClient.prototype.addAddress = function (address) {
return this._postRequest('tsm/addAddress',
{groupId: this.groupId, address: address})
}
TSMClient.prototype.getUpdates = function () {
var self = this
return this._postRequest('tsm/getLog',
{groupId: this.groupId, fromPoint: this.lastPoint})
.then(function (res) {
self.lastPoint = res.lastPoint
return res.txStates
})
}
TSMClient.prototype.getLog = function (fromPoint) {
return this._postRequest('tsm/getLog',
{groupId: this.groupId, fromPoint: fromPoint})
}
TSMClient.prototype.setLastPoint = function (lastPoint) {
this.lastPoint = lastPoint
}
module.exports = APIClient