-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.js
582 lines (527 loc) · 17.3 KB
/
backend.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
var Q = require('q');
var WalletCore = require('cc-wallet-core');
var cclib = WalletCore.cclib;
var ColorTarget = cclib.ColorTarget;
var ColorValue = cclib.ColorValue;
var bitcoin = cclib.bitcoin;
var OperationalTx = WalletCore.tx.OperationalTx;
var RawTx = WalletCore.tx.RawTx;
var CoinList = WalletCore.coin.CoinList;
var transformTx = WalletCore.tx.transformTx;
var Coin = WalletCore.coin.Coin;
var inherits = require('util').inherits;
var BIP39 = require('bip39');
var _ = require('lodash')
var fs = require('fs')
var parambulator = require('parambulator')
var request = require('request')
var tsmbackend = require('./tsmbackend')
var wallet = null;
var scannerUrl;
var chromaNodeUrl;
var coin_cache = {};
function runDynamicFees (wallet, opts) {
opts = _.assign({
minfee: 10,
maxfee: 500,
feeurl: "https://api.blockchain.info/mempool/fees",
feeinterval: 60000
}, opts)
if (!opts.feeurl) return
setInterval(function () {
request(opts.feeurl,
function (error, response, body) {
if (error) { console.log("Error getting recommended fees:", error) }
else if (response.statusCode === 200) {
var res = JSON.parse(body)
var recFee = res.regular;
if (recFee) {
if (recFee < opts.minfee) {
console.log("Recommended fee ", recFee, " is below minimal, using minimal value ", opts.minfee);
recFee = opts.minfee;
} else if (recFee > opts.maxfee) {
console.log("Recommended fee ", recFee, " is above maximal, using maximal value ", opts.maxfee);
recFee = opts.maxfee;
} else {
console.log("Using recommended fee ", recFee);
}
wallet.bitcoinNetwork.feePerKb = recFee * 1000;
} else {
console.log("Recommended fee is out of bounds", recFee)
}
}
})
}, opts.feeinterval)
}
function add_coin_to_cache(coin) {
// txId:outIndex is the key
coin_cache[coin.toString()] = coin;
}
function find_cached_coin(txId, outIndex) {
return coin_cache[txId + ":" + outIndex.toString()];
}
function initializeWallet(opts, done) {
var systemAssetDefinitions = [];
wallet = new WalletCore.Wallet(opts);
wallet.on('error', function (error) {
console.log(error.stack || error);
});
tsmbackend.setWallet(wallet)
wallet.once('syncStop', function () { done(wallet); })
}
function initializeScanner(url) {
console.log("cc-scanner url: " + url);
scannerUrl = url;
}
function initialize(opts, done) {
initializeScanner(opts.scannerUrl);
initializeWallet(opts.walletOpts, done);
// TODO: relies on internals of Chromanode connector
chromaNodeUrl = wallet.blockchain.connector._requestURL
runDynamicFees(wallet, opts.otherArgs);
}
function getScriptFromTargetData(target) {
var target_script = target.script;
if (!target_script) {
var target_addr = target.address;
if (!target_addr)
throw new Error('neither target.script nor target.address is provided');
target_script = bitcoin.Address.fromBase58Check(target_addr).toOutputScript().toHex();
}
return target_script;
}
function CustomOperationalTx(wallet, spec) {
this.wallet = wallet
this.spec = spec
this.targets = []
var self = this
if (spec.targets)
spec.targets.forEach(function (target) {
var colorDesc = target.color
var colordef = wallet.getColorDefinitionManager().resolveByDesc(colorDesc)
var colorValue = new ColorValue(colordef, parseInt(target.value, 10))
var colorTarget = new ColorTarget(getScriptFromTargetData(target), colorValue)
self.targets.push(colorTarget)
})
}
inherits(CustomOperationalTx, OperationalTx);
CustomOperationalTx.prototype.getChangeAddress = function (colordef) {
var color_desc = colordef.getDesc();
var address = this.spec.changeAddress[color_desc];
if (!address)
throw Error('Change address is not specified for color: "' + color_desc + '"');
return address;
};
CustomOperationalTx.prototype._getCoinsForColor = function (colordef) {
var color_desc = colordef.getDesc();
var self = this;
var sourceAddresses = this.spec.sourceAddresses || {}
var sourceCoins = this.spec.sourceCoins || {}
if (!sourceAddresses[color_desc] &&
!sourceCoins[color_desc])
throw new Error('source addresses/coins are not provided for "' + color_desc + '"');
if (sourceCoins[color_desc] && sourceAddresses[color_desc])
throw new Error('either source addresses or coins need to be specified, not both, for "' + color_desc + '"');
if (sourceCoins[color_desc]) {
var coinsQ = Q.all(sourceCoins[color_desc].map(
function (outpoint) {
return fetchCoin(self.wallet, color_desc, outpoint).then(function (coin) {
if (!coin) throw new Error('color mismatch in source coins for "' + color_desc + '"');
return coin
})
}))
return coinsQ.then(function (coins) {
return new CoinList(coins)
})
}
else return getUnspentCoins(this.wallet,
sourceAddresses[color_desc],
color_desc)
.then(function (coins) {
console.log('got coins:', coins)
return new CoinList(coins)
})
};
function makeCoin(context, colordef, rawCoin) {
if (!rawCoin.address) { // let's try to figure out address
var script = bitcoin.Script.fromHex(rawCoin.script)
var addresses = bitcoin.util.getAddressesFromScript(
script, context.getBitcoinNetwork());
if (addresses.length === 1)
rawCoin.address = addresses[0];
}
// figure out color value
var cvQ = null;
if (colordef.getColorType() === 'uncolored')
cvQ = Q(new ColorValue(colordef, rawCoin.value))
else {
var bc = context.getBlockchain()
var cd = context.getColorData()
function getTxFn(txId, cb) {
function onFulfilled(txHex) { cb(null, bitcoin.Transaction.fromHex(txHex)) }
function onRejected(error) { cb(error) }
bc.getTx(txId).then(onFulfilled, onRejected)
}
cvQ = Q.ninvoke(cd, 'getCoinColorValue',
rawCoin, colordef, getTxFn);
}
return cvQ.then(function (cv) {
if (cv === null) return null;
var coin = new Coin(rawCoin, {
isAvailable: true,
getCoinMainColorValue: cv
});
// check if coins are colored when uncolored requested
if (colordef.getColorType() === 'uncolored') {
return getTxColorValues({txId: rawCoin.txId,
outIndex: rawCoin.outIndex})
.then(function (result) {
if (result.colorValues && result.colorValues[rawCoin.outIndex]) {
return null
} else {
add_coin_to_cache(coin)
return coin
}
})
} else {
add_coin_to_cache(coin)
return coin
}
})
}
function fetchCoin(context, color_desc, outpoint) {
var bc = context.getBlockchain();
var colordef = context.getColorDefinitionManager().resolveByDesc(color_desc);
return bc.getTx(outpoint.txId).then(function (rawtx) {
var tx = bitcoin.Transaction.fromHex(rawtx)
var output = tx.outs[outpoint.outIndex]
return makeCoin(context, colordef,
{txId: outpoint.txId,
outIndex: outpoint.outIndex,
value: output.value,
script: output.script.toHex()
})
})
}
function getUnspentCoins(context, addresses, color_desc) {
var bc = context.getBlockchain();
var colordef = context.getColorDefinitionManager().resolveByDesc(color_desc);
return bc.addressesQuery(addresses, {status: 'unspent'}).then(function (res) {
return Q.all(res.unspent.map(function (unspent) {
return makeCoin(context, colordef, {
txId: unspent.txid,
outIndex: unspent.vount,
value: parseInt(unspent.value, 10),
script: unspent.script
})
})).then(function (coins) {
return _.filter(coins);
})
})
}
function validateParams(data, paramCheck) {
var deferred = Q.defer()
var callback = deferred.makeNodeResolver()
//paramCheck.validate(data, callback)
callback(null, true)
return deferred.promise;
}
var getUnspentCoinsParamCheck = parambulator(
{
required$: ['color', 'addresses'],
addresses: {
type$: 'array',
'*': {type$: 'string'}
},
color: {
type$: 'string'
}
}
)
function getUnspentCoinsData (data) {
return validateParams(data, getUnspentCoinsParamCheck)
.then(function () {
if (!data.addresses) throw new Error("requires addresses")
if (typeof data.color === 'undefined')
throw new Error("requires color");
return getUnspentCoins(wallet, data.addresses, data.color);
}).then(function (coins) {
return Q.all(coins.map(function (coin) {
return Q.ninvoke(coin, 'getMainColorValue', null, null).then(
function (cv) {
var rawCoin = coin.toRawCoin();
//delete rawCoin['address']; // TODO: detect address properly
rawCoin.color = data.color;
rawCoin.colorValue = cv.getValue();
return rawCoin
})
}))
})
}
var createTransferTxParamCheck = parambulator(
{
required$: ['targets'],
targets: {
type$:'array',
'**': {
'address': { type$:'string' },
'color': { type$:'string' },
'value': { type$:'integer' }
}
},
sourceCoins: {
type$: 'object',
'*': {type$: 'array'}
},
sourceAddresses: {
type$: 'object',
'*': {type$: 'array'}
},
changeAddress: {
type$: 'object',
'*': {type$: 'string'}
}
}
)
function createTransferTx(data) {
return validateParams(data, createTransferTxParamCheck)
.then(function () {
var opTxS = new CustomOperationalTx(wallet, data);
return Q.nfcall(transformTx, opTxS, 'composed', {})
})
.then(function (composedTx) {
return Q.nfcall(transformTx, composedTx, 'raw', {}).then(function (tx) {
return {
tx: tx.toHex(true),
inputCoins: composedTx.getTxIns().map(function (txin) {
var coin = find_cached_coin(txin.txId, txin.outIndex);
if (coin) return coin.toRawCoin();
else return null;
})
}
})
})
}
var createIssueTxParamCheck = parambulator(
{
required$: ['target', 'colorKernel'],
target: {
required$: ['value'],
'address': { type$:'string' },
'script': { type$:'string' },
'value': { type$:'integer' }
},
sourceAddresses: {
type$: 'object',
'*': {type$: 'array'}
},
sourceCoins: {
type$: 'object',
'*': {type$: 'array'}
},
changeAddress: {
type$: 'object',
'*': {type$: 'string'}
},
colorKernel: {
type$:'string',
eq$: 'epobc'
}
}
)
function createIssueTx(data) {
return validateParams(data, createIssueTxParamCheck)
.then(function () {
if (data.targets && !data.target) {
if (data.targets.length > 1 || data.targets.length == 0) throw new Error('issuance transaction should have a single target');
data.target = data.targets[0];
delete data['targets'];
}
if (data.targets) throw new Error('both target and targets fields are set');
if (!data.target) throw new Error('no target provided');
var opTxS = new CustomOperationalTx(wallet, data);
opTxS.addTarget(new ColorTarget(
getScriptFromTargetData(data.target),
new ColorValue(cclib.ColorDefinitionManager.getGenesis(), // genesis output marker
parseInt(data.target.value, 10))));
if (data.colorKernel !== 'epobc') throw new Error('only epobc kernel is supported')
var cdefCls = cclib.ColorDefinitionManager.getColorDefenitionClsForType('epobc');
console.log('compose...')
return Q.nfcall(cdefCls.composeGenesisTx, opTxS).then(function (composedTx) {
console.log('transforming to raw...')
return Q.nfcall(transformTx, composedTx, 'raw', {}).then(function (tx) {
console.log('done');
return { tx: tx.toHex(true),
inputCoins: composedTx.getTxIns().map(function (txin) {
var coin = find_cached_coin(txin.txId, txin.outIndex);
if (coin) return coin.toRawCoin();
else return null;
})
}
})
})
})
}
function checkUnspent(tx) {
var txId = tx.txId;
var outIndex = tx.outIndex;
var value = tx.colorValue;
var path = '/v2/transactions/spent'
var query = '?otxid=' + txId +'&oindex=' + outIndex;
var url = chromaNodeUrl + path + query;
var deferred = Q.defer()
request(url,
function (error, response, body) {
if (error) {
deferred.reject(error);
}
if (response.statusCode == 200) {
var answer = JSON.parse(body)
if (answer.status !== 'fail') {
var spent = answer.data.spent
deferred.resolve(spent ? null : tx);
return
}
}
deferred.reject(
new Error('ChromaNode error at:' + url
+ ' , code:' + response.statusCode))
})
return deferred.promise;
}
function filterUnspent(data) {
var txList = data.coins || [];
return Q.all(_.map(txList, checkUnspent))
.then(function(newTxList) {
return {
coins: _.compact(newTxList)
}
})
};
var getTxColorValuesParamCheck = parambulator(
{
required$: ['txId'],
txId: {type$: 'string'},
outIndices: {type$:'array'},
outIndex: {type$: 'integer'}
}
)
function getTxColorValues(data) {
// getTxColorValues, basically just call cc-scanner API getTxColorValues.
return validateParams(data, getTxColorValuesParamCheck)
.then(function () {
var deferred = Q.defer()
request({
method: 'post',
uri: scannerUrl + 'getTxColorValues',
body: data, json: true
}, function (error, response, body){
if (error) {
deferred.reject(error);
return
}
if (response.statusCode == 200) {
deferred.resolve(body.data);
} else {
console.error('cc-scanner returned this:' + body);
deferred.reject(
new Error('cc-scanner returned status:' + response.statusCode))
}
})
return deferred.promise;
})
}
var getAllColoredCoinsParamCheck = parambulator(
{
required$: ['color'],
color: {type$: 'string'},
unspent: {type$: 'string', enum$:['true','false']}
}
)
function getAllColoredCoins(data) {
// getAllColoredCoins, basically just call cc-scanner API getAllColoredCoins.
//
// Additionally, caller might request only unspent coins (using 'unspent' parameter),
// in that case we need to additionally filter coins using chromanode /transactions/spent API
//
return validateParams(data, getAllColoredCoinsParamCheck)
.then(function () {
var color_desc = data.color
var deferred = Q.defer()
var url = scannerUrl + 'getAllColoredCoins?color_desc=' + color_desc
request(url,
function (error, response, body) {
if (error) {
deferred.reject(error);
}
if (response.statusCode == 200) {
var unspent = (data.unspent === 'true')
var answer = JSON.parse(body)
if (unspent) {
filterUnspent(answer)
.fail(function (reason) {
deferred.reject(reason)
})
.done(function (filtered) {
deferred.resolve(filtered);
})
} else {
deferred.resolve(answer);
}
} else {
console.error('cc-scanner returned this:' + body);
deferred.reject(
new Error('cc-scanner returned status:' + response.statusCode))
}
})
return deferred.promise;
})
}
var broadcastTxParamCheck = parambulator(
{
required$: ['tx'],
tx: {type$: 'string'}
}
)
function getTx(data) {
return wallet.getBlockchain().getTx(data.txId)
}
function broadcastTx(data) {
// chromanode returns from transaction/send sooner than it adds
// transaction to database, which is undesirable for a high-level API,
// so we wait until it is added to chromanode's DB
return validateParams(data, broadcastTxParamCheck)
.then(function () {
var bc = wallet.getBlockchain();
var txId = bitcoin.Transaction.fromHex(data.tx).getId();
console.log("Broadcast", txId, data.tx.length/2, "bytes");
return bc.sendTx(data.tx).then(function () {
console.log('sent tx to chromanode, waiting for it to appear...')
return Q.Promise(function (resolve, reject) {
var tries = 0;
function dotry () {
console.log("polling " + txId + " " + tries);
tries += 1;
if (tries > 120) { // give up after 2 minutes
reject(new Error('timeout waiting for chromanode to accept ' + txId))
return
}
bc.getTxBlockHash(txId).done(resolve, function () {
Q.delay(1000).done(dotry)
})
}
dotry();
})
})
})
}
module.exports = {
initialize: initialize,
createIssueTx: createIssueTx,
createTransferTx: createTransferTx,
getUnspentCoinsData: getUnspentCoinsData,
getAllColoredCoins: getAllColoredCoins,
getTxColorValues: getTxColorValues,
getTx: getTx,
broadcastTx: broadcastTx
}