-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
678 lines (603 loc) · 24.4 KB
/
index.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
var bitcoinjs = require('bitcoinjs-lib')
var BigNumber = require('bignumber.js')
var _ = require('lodash')
var encodeAssetId = require('cc-assetid-encoder')
var cc = require('cc-transaction')
var findBestMatchByNeededAssets = require('./modules/findBestMatchByNeededAssets')
var Buffer = require('safe-buffer').Buffer
var debug = require('debug')('cc-transaction-builder')
var errors = require('cc-errors')
var bufferReverse = require('buffer-reverse')
var CC_TX_VERSION = 0x02
var ColoredCoinsBuilder = function (properties) {
properties = properties || {}
if (typeof properties.network !== 'undefined' && properties.network !== 'testnet' && properties.network !== 'mainnet') {
throw new Error('"network" must be either "testnet" or "mainnet"')
}
this.network = properties.network || 'mainnet' // 'testnet' or 'mainnet'
if (properties.defaultFee) {
this.defaultFee = parseInt(properties.defaultFee)
}
this.defaultFeePerKb = parseInt(properties.defaultFeePerKb) || 25000
this.mindustvalue = parseInt(properties.mindustvalue) || 600
this.mindustvaluemultisig = parseInt(properties.mindustvaluemultisig) || 700
this.writemultisig = properties.writemultisig || true
}
ColoredCoinsBuilder.prototype.buildIssueTransaction = function (args) {
var self = this
if (!args.utxos) {
throw new Error('Must have "utxos"')
}
if (!args.fee && !self.defaultFee) {
throw new Error('Must have "fee"')
}
if (!args.issueAddress) {
throw new Error('Must have "issueAddress"')
}
if (!args.amount) {
throw new Error('Must have "amount"')
}
if (args.fee) {
args.fee = parseInt(args.fee)
}
args.divisibility = args.divisibility || 0
args.aggregationPolicy = args.aggregationPolicy || 'aggregatable'
var txb = new bitcoinjs.TransactionBuilder(self.network === 'testnet' ? bitcoinjs.networks.testnet : bitcoinjs.networks.bitcoin)
// find inputs to cover the issuance
var ccArgs = self._addInputsForIssueTransaction(txb, args)
if (!ccArgs.success) {
throw new errors.NotEnoughFundsError({ type: 'issue' })
}
_.assign(ccArgs, args)
var res = self._encodeColorScheme(ccArgs)
res.assetId = ccArgs.assetId
return res
}
ColoredCoinsBuilder.prototype._addInputsForIssueTransaction = function (txb, args) {
var self = this
var utxos = args.utxos
var assetId = ''
var current
var cost
// simple mode
if (args.financeOutput) {
current = new BigNumber(args.financeOutput.value)
cost = new BigNumber(self._getIssuanceCost(args))
txb.addInput(args.financeOutputTxid, args.financeOutput.n)
if (args.flags && args.flags.injectPreviousOutput) {
var chunks = bitcoinjs.script.decompile(new Buffer(args.financeOutput.scriptPubKey.hex, 'hex'))
txb.tx.ins[txb.tx.ins.length - 1].script = bitcoinjs.script.compile(chunks)
}
assetId = self._encodeAssetId(
args.reissueable,
args.financeOutputTxid,
args.financeOutput.n,
args.financeOutput.scriptPubKey.hex,
args.divisibility,
args.aggregationPolicy)
return {txb: txb, args: args, change: current - cost, assetId: assetId, totalInputs: {amount: current}}
}
// add to transaction enough inputs so we can cover the cost
// send change if any back to us
current = new BigNumber(0)
cost = new BigNumber(self._getIssuanceCost(args))
var change = new BigNumber(0)
var hasEnoughEquity = utxos.some(function (utxo) {
if (!isInputInTx(txb.tx, utxo.txid, utxo.index) && !(utxo.assets && utxo.assets.length)) {
debug('current amount ' + utxo.value + ' needed ' + cost)
debug('utxo.txid', utxo.txid)
debug('utxo.index', utxo.index)
txb.addInput(utxo.txid, utxo.index)
if (txb.tx.ins.length === 1) { // encode asset
debug(txb.tx.ins[0].script)
assetId = self._encodeAssetId(
args.reissueable,
utxo.txid,
utxo.index,
utxo.scriptPubKey.hex,
args.divisibility,
args.aggregationPolicy)
}
debug('math: ' + current.toNumber() + ' ' + utxo.value)
current = current.add(utxo.value)
if (args.flags && args.flags.injectPreviousOutput) {
var chunks = bitcoinjs.script.decompile(new Buffer(utxo.scriptPubKey.hex, 'hex'))
txb.tx.ins[txb.tx.ins.length - 1].script = bitcoinjs.script.compile(chunks)
}
debug('current amount: ' + current + ' projected cost: ' + cost + ' are were there yet: ' + (current.comparedTo(cost) >= 0))
} else {
debug('skipping utxo for input, asset found in utxo: ' + utxo.txid + ':' + utxo.index)
}
return current.comparedTo(cost) >= 0
})
debug('hasEnoughEquity: ' + hasEnoughEquity)
if (!hasEnoughEquity) {
return {success: false}
}
change = current - cost
debug('finished adding inputs to tx')
debug('change ' + change)
return {success: true, txb: txb, change: change, assetId: assetId, totalInputs: { amount: current }}
}
ColoredCoinsBuilder.prototype._getIssuanceCost = function (args) {
var self = this
var fee = args.fee || self.defaultFee
var totalCost = fee
debug('_getTotalIssuenceCost: fee =', fee)
if (args.transfer && args.transfer.length) {
args.transfer.forEach(function (to) {
totalCost += self.mindustvalue
})
}
// TODO: calculate multisig only if actually needed
if (args.metadata) {
totalCost += self.writemultisig ? self.mindustvaluemultisig : 0
}
// change
totalCost += self.mindustvalue
debug('_getTotalIssuenceCost: totalCost =', totalCost)
return totalCost
}
ColoredCoinsBuilder.prototype._encodeAssetId = function (reissueable, txid, nvout, hex, divisibility, aggregationPolicy) {
var opts = {
ccdata: [{
type: 'issuance',
lockStatus: !reissueable,
divisibility: divisibility,
aggregationPolicy: aggregationPolicy
}],
vin: [{
txid: txid,
vout: nvout,
previousOutput: {
hex: hex
}
}]
}
if (!reissueable) {
debug('sending assetIdEncoder locked, first input = ' + txid + ':' + nvout)
} else {
debug('sending assetIdEncoder unlocked, first input previousOutput = ', opts.vin[0].previousOutput)
}
debug('encoding asset is locked: ' + !reissueable)
debug(opts)
var assetId = encodeAssetId(opts)
debug('assetId: ' + assetId)
return assetId
}
ColoredCoinsBuilder.prototype._encodeColorScheme = function (args) {
var self = this
var addMultisig = false
var encoder = cc.newTransaction(0x4343, CC_TX_VERSION)
var reedemScripts = []
var coloredOutputIndexes = []
var txb = args.txb
var coloredAmount = args.amount
var fee = args.fee || self.defaultFee
var lockStatus
if (typeof args.lockStatus !== 'undefined') {
lockStatus = args.lockStatus
} else if (typeof args.reissueable !== 'undefined') {
lockStatus = !args.reissueable
} else if (typeof args.reissuable !== 'undefined') {
lockStatus = !args.reissuable
}
if (typeof lockStatus === 'undefined') {
// default
lockStatus = true
}
encoder.setLockStatus(lockStatus)
encoder.setAmount(args.amount, args.divisibility)
encoder.setAggregationPolicy(args.aggregationPolicy)
if (args.torrentHash) {
encoder.setHash(args.torrentHash, args.sha2)
}
if (args.transfer) {
args.transfer.forEach(function (transferobj, i) {
debug('payment ' + transferobj.amount + ' ' + txb.tx.outs.length)
encoder.addPayment(0, transferobj.amount, txb.tx.outs.length)
coloredAmount -= transferobj.amount
// check multisig
if (transferobj.pubKeys && transferobj.m) {
var multisig = self._generateMultisigAddress(transferobj.pubKeys, transferobj.m)
reedemScripts.push({index: txb.tx.outs.length, reedemScript: multisig.reedemScript, address: multisig.address})
txb.addOutput(multisig.address, self.mindustvalue)
} else {
txb.addOutput(transferobj.address, self.mindustvalue)
}
})
}
if (coloredAmount < 0) {
throw new errors.CCTransactionConstructionError({ explanation: 'transferring more than issued' })
}
// add OP_RETURN
debug('before encode done')
var buffer = encoder.encode()
debug('encoding done, buffer: ', buffer)
if (buffer.leftover && buffer.leftover.length > 0) {
encoder.shiftOutputs()
buffer = encoder.encode()
addMultisig = true
reedemScripts.forEach(function (item) { item.index += 1 })
}
var ret = bitcoinjs.script.compile([
bitcoinjs.opcodes.OP_RETURN,
buffer.codeBuffer
])
txb.addOutput(ret, 0)
// add array of colored ouput indexes
encoder.payments.forEach(function (payment) {
coloredOutputIndexes.push(payment.output)
})
// need to encode hashes in first tx
if (addMultisig) {
if (buffer.leftover && buffer.leftover.length === 1) {
self._addHashesOutput(txb.tx, args.pubKeyReturnMultisigDust, buffer.leftover[0])
} else if (buffer.leftover && buffer.leftover.length === 2) {
self._addHashesOutput(txb.tx, args.pubKeyReturnMultisigDust, buffer.leftover[1], buffer.leftover[0])
} else {
throw new Error('enough room for hashes: we offsetted inputs for nothing')
}
}
// add change
var allOutputValues = _.sumBy(txb.tx.outs, function (output) { return output.value })
debug('all inputs: ' + args.totalInputs.amount + ' all outputs: ' + allOutputValues)
var lastOutputValue = args.totalInputs.amount - (allOutputValues + fee)
if (lastOutputValue < self.mindustvalue) {
var totalCost = self.mindustvalue + args.totalInputs.amount.toNumber()
throw new errors.NotEnoughFundsError({
type: 'issuance',
fee: fee,
totalCost: totalCost,
missing: self.mindustvalue - lastOutputValue
})
}
var splitChange = !(args.financeChangeAddress == false)
var changeAddress = args.financeChangeAddress || args.issueAddress
if (splitChange && lastOutputValue >= 2 * self.mindustvalue && coloredAmount > 0) {
var bitcoinChange = lastOutputValue - self.mindustvalue
lastOutputValue = self.mindustvalue
debug('adding bitcoin change output with: ' + bitcoinChange)
txb.addOutput(changeAddress, bitcoinChange)
}
if (coloredAmount > 0) {
// there's a colored change output
coloredOutputIndexes.push(txb.tx.outs.length)
}
debug('adding change output with: ' + lastOutputValue)
debug('total inputs: ' + args.totalInputs.amount)
debug('total fee: ' + fee)
debug('total output without fee: ' + allOutputValues)
txb.addOutput(args.issueAddress, lastOutputValue || args.change)
debug('txHex ', txb.tx.toHex())
return { txHex: txb.tx.toHex(), multisigOutputs: reedemScripts, coloredOutputIndexes: _.uniq(coloredOutputIndexes) }
}
ColoredCoinsBuilder.prototype._generateMultisigAddress = function (pubKeys, m) {
var self = this
var ecpubkeys = []
pubKeys.forEach(function (key) {
ecpubkeys.push(bitcoinjs.ECPubKey.fromHex(key))
})
var script = bitcoinjs.scripts.multisigOutput(m, ecpubkeys)
var hash = bitcoinjs.crypto.hash160(script)
var multisigAdress = new bitcoinjs.Address(hash, (self.network === 'testnet') ? 0xc4 : 0x05)
var sendto = multisigAdress.toBase58Check()
return { address: sendto, reedemScript: script.toHex() }
}
ColoredCoinsBuilder.prototype._addHashesOutput = function (tx, address, sha2, sha1) {
var self = this
var chunks = []
chunks.push(bitcoinjs.opcodes.OP_1)
chunks.push(address ? new Buffer(address, 'hex') : new Buffer('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'))
chunks.push(Buffer.concat([new Buffer('03', 'hex'), sha2], 33))
if (sha1) {
chunks.push(Buffer.concat([new Buffer('030000000000000000000000', 'hex'), sha1], 33))
chunks.push(bitcoinjs.opcodes.OP_3)
} else {
chunks.push(bitcoinjs.opcodes.OP_2)
}
chunks.push(bitcoinjs.opcodes.OP_CHECKMULTISIG)
debug('chunks', chunks)
var script = bitcoinjs.script.compile(chunks)
// try compute value to pass mindust
// TODO: actually comput it with the fee from the api request, this assumes static fee per kb
tx.outs.unshift({ script: script, value: self._getNoneMinDustByScript(script) })
}
ColoredCoinsBuilder.prototype._getNoneMinDustByScript = function (script) {
var self = this
// add 9 to aacount for bitcoind SER_DISK serilaztion before the multiplication
return (((self.defaultFeePerKb * (script.length + 148 + 9)) / 1000) * 3)
}
function isInputInTx (tx, txid, index) {
return tx.ins.some(function (input) {
var id = bufferReverse(input.hash)
return (id.toString('hex') === txid && input.index === index)
})
}
ColoredCoinsBuilder.prototype._insertSatoshiToTransaction = function (utxos, txb, missing, inputsValue, metadata) {
debug('missing: ' + missing)
var paymentDone = false
var missingbn = new BigNumber(missing)
var financeValue = new BigNumber(0)
var currentAmount = new BigNumber(0)
if (metadata.financeOutput && metadata.financeOutputTxid) {
if (isInputInTx(txb.tx, metadata.financeOutputTxid, metadata.financeOutput.n)) { return false }
financeValue = new BigNumber(metadata.financeOutput.value)
debug('finance sent through api with value ' + financeValue.toNumber())
if (financeValue.minus(missingbn) >= 0) {
// TODO: check there is no asset here
debug('funding tx ' + metadata.financeOutputTxid)
txb.tx.addInput(metadata.financeOutputTxid, metadata.financeOutput.n)
inputsValue.amount += financeValue.toNumber()
if (metadata.flags && metadata.flags.injectPreviousOutput) {
var chunks = bitcoinjs.script.decompile(new Buffer(metadata.financeOutput.scriptPubKey.hex, 'hex'))
txb.tx.ins[txb.ins.length - 1].script = bitcoinjs.script.compile(chunks)
}
paymentDone = true
return paymentDone
} else {
debug('finance output not added to transaction finace value: ' + financeValue.toNumber() + ' still needed: ' + missingbn.toNumber())
}
} else {
debug('no financeOutput was given')
}
var hasEnoughEquity = utxos.some(function (utxo) {
utxo.value = Math.round(utxo.value)
if (!isInputInTx(txb.tx, utxo.txid, utxo.index) && !(utxo.assets && utxo.assets.length)) {
debug('current amount ' + utxo.value + ' needed ' + missing)
txb.addInput(utxo.txid, utxo.index)
inputsValue.amount += utxo.value
currentAmount = currentAmount.add(utxo.value)
if (metadata.flags && metadata.flags.injectPreviousOutput) {
var chunks = bitcoinjs.script.decompile(new Buffer(utxo.scriptPubKey.hex, 'hex'))
txb.tx.ins[txb.tx.ins.length - 1].script = bitcoinjs.script.compile(chunks)
}
}
return currentAmount.comparedTo(missingbn) >= 0
})
debug('hasEnoughEquity: ' + hasEnoughEquity)
return hasEnoughEquity
}
ColoredCoinsBuilder.prototype._tryAddingInputsForFee = function (txb, utxos, totalInputs, metadata, satoshiCost) {
var self = this
debug('tryAddingInputsForFee: current transaction value: ' + totalInputs.amount + ' projected cost: ' + satoshiCost)
if (satoshiCost > totalInputs.amount) {
if (!self._insertSatoshiToTransaction(utxos, txb, (satoshiCost - totalInputs.amount), totalInputs, metadata)) {
debug('not enough satoshi in account for fees')
return false
}
} else { debug('No need for additional finance') }
return true
}
ColoredCoinsBuilder.prototype.buildSendTransaction = function (args) {
var self = this
if (!args.utxos) {
throw new Error('Must have "utxos"')
}
if (!args.to) {
throw new Error('Must have "to"')
}
if (!args.fee && !self.defaultFee) {
throw new Error('Must have "fee"')
}
if (args.fee) {
args.fee = parseInt(args.fee)
}
var txb = new bitcoinjs.TransactionBuilder(self.network === 'testnet' ? bitcoinjs.networks.testnet : bitcoinjs.networks.bitcoin)
return self._addInputsForSendTransaction(txb, args)
}
ColoredCoinsBuilder.prototype._computeCost = function (withfee, args) {
var self = this
var fee = withfee ? (args.fee || args.minfee) : 0
if (args.to && args.to.length) {
args.to.forEach(function (to) {
fee += self.mindustvalue
})
}
if (args.rules || args.metadata) { fee += self.writemultisig ? self.mindustvaluemultisig : 0 }
fee += self.mindustvalue
debug('comupteCost: ' + fee)
return fee
}
ColoredCoinsBuilder.prototype._getInputAmountNeededForTx = function (tx, fee) {
var self = this
var total = fee
tx.outs.forEach(function (output) {
total += self._getNoneMinDustByScript(output.script, fee)
})
return total
}
ColoredCoinsBuilder.prototype._getChangeAmount = function (tx, fee, totalInputValue) {
var allOutputValues = _.sumBy(tx.outs, function (output) { return output.value })
debug('getChangeAmount: all inputs: ' + totalInputValue.amount + ' all outputs: ' + allOutputValues)
return (totalInputValue.amount - (allOutputValues + fee))
}
ColoredCoinsBuilder.prototype._addInputsForSendTransaction = function (txb, args) {
var self = this
var satoshiCost = self._computeCost(true, args)
var totalInputs = { amount: 0 }
var reedemScripts = []
var coloredOutputIndexes = []
debug('addInputsForSendTransaction')
if (args.from) {
debug('got unspents for address: ' + args.from)
} else {
debug('got unspents from parmameter: ' + args.utxos)
if (args.utxos[0] && args.utxos[0].scriptPubKey && args.utxos[0].scriptPubKey.addresses && args.utxos[0].scriptPubKey.addresses[0]) {
args.from = args.utxos[0].scriptPubKey.addresses[0]
}
}
var assetList = {}
args.to.forEach(function (to) {
debug(to.assetId)
if (!assetList[to.assetId]) {
assetList[to.assetId] = { amount: 0, addresses: [], done: false, change: 0, encodeAmount: 0, inputs: [] }
}
assetList[to.assetId].amount += to.amount
if (to.burn) {
assetList[to.assetId].addresses.push({ address: 'burn', amount: to.amount })
} else if (!to.address && to.pubKeys && to.m) { // generate a multisig address, remember to return the redeem scripts
var multisig = self._generateMultisigAddress(to.pubKeys, to.m)
assetList[to.assetId].addresses.push({ address: multisig.address, amount: to.amount, reedemScript: multisig.reedemScript })
} else {
assetList[to.assetId].addresses.push({ address: to.address, amount: to.amount })
}
})
debug('finished creating per asset list')
for (var asset in assetList) {
debug('working on asset: ' + asset)
debug(args.utxos)
var assetUtxos = args.utxos.filter(function (element, index, array) {
if (!element.assets) { return false }
return element.assets.some(function (a) {
debug('checking ' + a.assetId + ' and ' + asset)
return (a.assetId === asset)
})
})
if (assetUtxos && assetUtxos.length > 0) {
debug('have utxo list')
var key = asset
assetUtxos.forEach(function (utxo) {
if (utxo.used) {
debug('utxo', utxo)
throw new Error('Output ' + utxo.txid + ':' + utxo.index + ' is already spent!')
}
})
if (!findBestMatchByNeededAssets(assetUtxos, assetList, key, txb, totalInputs, args)) { throw new Error('Not enough units of asset ' + key + ' to cover transfer transaction') }
} else {
debug('no utxo list')
throw new Error('No output with the requested asset: ' + asset)
}
}
debug('reached encoder')
var encoder = cc.newTransaction(0x4343, CC_TX_VERSION)
if (!self._tryAddingInputsForFee(txb, args.utxos, totalInputs, args, satoshiCost)) {
throw new errors.NotEnoughFundsError({
type: 'issuance',
fee: args.fee,
totalCost: satoshiCost,
missing: satoshiCost - totalInputs.amount
})
}
for (asset in assetList) {
var currentAsset = assetList[asset]
debug('encoding asset ' + asset)
if (!currentAsset.done) {
debug('current asset state is bad ' + asset)
throw new errors.NotEnoughAssetsError({ asset: asset })
}
debug(currentAsset.addresses)
var uniqAssets = _.uniqBy(currentAsset.addresses, function (item) { return item.address })
debug('uniqAssets = ', uniqAssets)
uniqAssets.forEach(function (address) {
debug('adding output ' + (txb.tx.outs ? txb.tx.outs.length : 0) + ' for address: ' + address.address + ' with satoshi value ' + self.mindustvalue + ' asset value: ' + address.amount)
var addressAmountLeft = address.amount
debug('currentAsset = ', currentAsset, ', currentAsset.inputs.length = ', currentAsset.inputs.length)
currentAsset.inputs.some(function (input) {
if (!input.amount) { return false }
if (addressAmountLeft - input.amount > 0) {
debug('mapping to input ' + input.index + ' with amount ' + input.amount)
if (address.address === 'burn') {
encoder.addBurn(input.index, input.amount)
} else {
encoder.addPayment(input.index, input.amount, (txb.tx.outs ? txb.tx.outs.length : 0))
}
addressAmountLeft -= input.amount
debug('left to map from next input ' + addressAmountLeft)
input.amount = 0
return false
} else {
debug('mapping to input ' + input.index + ' with amount ' + addressAmountLeft)
if (address.address === 'burn') {
encoder.addBurn(input.index, addressAmountLeft)
} else {
encoder.addPayment(input.index, addressAmountLeft, (txb.tx.outs ? txb.tx.outs.length : 0))
}
input.amount -= addressAmountLeft
addressAmountLeft = 0
return true
}
})
debug('putting output in transaction')
if (address.address !== 'burn') {
txb.addOutput(address.address, self.mindustvalue)
}
if (address.reedemScript) {
reedemScripts.push({ index: txb.tx.outs.length - 1, reedemScript: address.reedemScript, address: address.address })
}
debug(txb.tx)
debug('adding output ' + (txb.tx.outs.length - 1))
})
debug('done adding colored outputs')
}
debug('before using encoder')
// add metadata if we have any
if (args.torrentHash && args.sha2 && self.writemultisig) {
encoder.setHash(args.torrentHash, args.sha2)
}
var buffer = encoder.encode()
if (buffer.leftover && buffer.leftover.length > 0) {
encoder.shiftOutputs()
reedemScripts.forEach(function (item) { item.index += 1 })
buffer = encoder.encode()
if (buffer.leftover.length === 1) {
self._addHashesOutput(txb.tx, args.pubKeyReturnMultisigDust, buffer.leftover[0])
} else if (buffer.leftover.length === 2) {
self._addHashesOutput(txb.tx, args.pubKeyReturnMultisigDust, buffer.leftover[1], buffer.leftover[0])
} else {
throw new errors.CCTransactionConstructionError()
}
}
// add array of colored ouput indexes
encoder.payments.forEach(function (payment) {
if (typeof payment.output !== 'undefined') coloredOutputIndexes.push(payment.output)
})
debug('encoding done')
var ret = bitcoinjs.script.compile([
bitcoinjs.opcodes.OP_RETURN,
buffer.codeBuffer
])
txb.addOutput(ret, 0)
var lastOutputValue = self._getChangeAmount(txb.tx, args.fee, totalInputs)
var coloredChange = _.keys(assetList).some(function (assetId) {
return assetList[assetId].change > 0
})
var splitChange = !(args.financeChangeAddress == false)
var changeAddress = args.financeChangeAddress || (Array.isArray(args.from) ? args.from[0] : args.from)
var numOfChanges = (splitChange && coloredChange && lastOutputValue >= 2 * self.mindustvalue) ? 2 : 1
if (lastOutputValue < numOfChanges * self.mindustvalue) {
debug('trying to add additionl inputs to cover transaction')
satoshiCost = self._getInputAmountNeededForTx(txb.tx, args.fee) + numOfChanges * self.mindustvalue
if (!self._tryAddingInputsForFee(txb, args.utxos, totalInputs, args, satoshiCost)) {
throw new errors.NotEnoughFundsError({
type: 'transfer',
fee: args.fee,
totalCost: satoshiCost,
missing: self.mindustvalue - lastOutputValue
})
}
lastOutputValue = self._getChangeAmount(txb.tx, args.fee, totalInputs)
}
if (numOfChanges === 2) {
txb.addOutput(changeAddress, lastOutputValue - self.mindustvalue)
lastOutputValue = self.mindustvalue
}
if (coloredChange) {
coloredOutputIndexes.push(txb.tx.outs.length)
}
txb.addOutput(Array.isArray(args.from) ? args.from[0] : args.from, lastOutputValue)
debug('success')
return { txHex: txb.tx.toHex(), metadataSha1: args.torrentHash, multisigOutputs: reedemScripts, coloredOutputIndexes: _.uniqBy(coloredOutputIndexes) }
}
ColoredCoinsBuilder.prototype.buildBurnTransaction = function (args) {
var self = this
args = args || {}
var to = args.transfer || []
var burn = args.burn || []
burn.forEach(function (burnItem) { burnItem.burn = true })
to.push.apply(to, burn)
delete args.transfer
args.to = to
return self.buildSendTransaction(args)
}
module.exports = ColoredCoinsBuilder