-
Notifications
You must be signed in to change notification settings - Fork 0
/
composer.js
819 lines (755 loc) · 31.1 KB
/
composer.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*jslint node: true */
"use strict";
var crypto = require('crypto');
var async = require('async');
var db = require('./db.js');
var constants = require('./constants.js');
var objectHash = require('./object_hash.js');
var objectLength = require("./object_length.js");
var ecdsaSig = require('./signature.js');
var mutex = require('./mutex.js');
var _ = require('lodash');
var storage = require('./storage.js');
var myWitnesses = require('./my_witnesses.js');
var parentComposer = require('./parent_composer.js');
var validation = require('./validation.js');
var writer = require('./writer.js');
var conf = require('./conf.js');
var profiler = require('./profiler.js');
var inputs = require('./inputs.js');
var hash_placeholder = "--------------------------------------------"; // 256 bits (32 bytes) base64: 44 bytes
var sig_placeholder = "----------------------------------------------------------------------------------------"; // 88 bytes
var bGenesis = false;
exports.setGenesis = function(_bGenesis){ bGenesis = _bGenesis; };
function repeatString(str, times){
if (str.repeat)
return str.repeat(times);
return (new Array(times+1)).join(str);
}
function sortOutputs(a,b){
var addr_comparison = a.address.localeCompare(b.address);
return addr_comparison ? addr_comparison : (a.amount - b.amount);
}
function createTextMessage(text){
return {
app: "text",
payload_location: "inline",
payload_hash: objectHash.getBase64Hash(text, storage.getMinRetrievableMci() >= constants.timestampUpgradeMci),
payload: text
};
}
// change goes back to the first paying address
function composeTextJoint(arrSigningAddresses, arrPayingAddresses, text, signer, callbacks){
composePaymentAndTextJoint(arrSigningAddresses, arrPayingAddresses, [{address: arrPayingAddresses[0], amount: 0}], text, signer, callbacks);
}
function composePaymentJoint(arrFromAddresses, arrOutputs, signer, callbacks){
composeJoint({paying_addresses: arrFromAddresses, outputs: arrOutputs, signer: signer, callbacks: callbacks});
}
function composePaymentAndTextJoint(arrSigningAddresses, arrPayingAddresses, arrOutputs, text, signer, callbacks){
composeJoint({
signing_addresses: arrSigningAddresses,
paying_addresses: arrPayingAddresses,
outputs: arrOutputs,
messages: [createTextMessage(text)],
signer: signer,
callbacks: callbacks
});
}
function composeContentJoint(from_address, app, payload, signer, callbacks){
var objMessage = {
app: app,
payload_location: "inline",
payload_hash: objectHash.getBase64Hash(payload, storage.getMinRetrievableMci() >= constants.timestampUpgradeMci),
payload: payload
};
composeJoint({
paying_addresses: [from_address],
outputs: [{address: from_address, amount: 0}],
messages: [objMessage],
signer: signer,
callbacks: callbacks
});
}
function composeDefinitionChangeJoint(from_address, definition_chash, signer, callbacks){
composeContentJoint(from_address, "address_definition_change", {definition_chash: definition_chash}, signer, callbacks);
}
function composeDataFeedJoint(from_address, data, signer, callbacks){
composeContentJoint(from_address, "data_feed", data, signer, callbacks);
}
function composeDataJoint(from_address, data, signer, callbacks){
composeContentJoint(from_address, "data", data, signer, callbacks);
}
function composeDefinitionTemplateJoint(from_address, arrDefinitionTemplate, signer, callbacks){
composeContentJoint(from_address, "definition_template", arrDefinitionTemplate, signer, callbacks);
}
function composePollJoint(from_address, question, arrChoices, signer, callbacks){
var poll_data = {question: question, choices: arrChoices};
composeContentJoint(from_address, "poll", poll_data, signer, callbacks);
}
function composeVoteJoint(from_address, poll_unit, choice, signer, callbacks){
var vote_data = {unit: poll_unit, choice: choice};
composeContentJoint(from_address, "vote", vote_data, signer, callbacks);
}
function composeProfileJoint(from_address, profile_data, signer, callbacks){
composeContentJoint(from_address, "profile", profile_data, signer, callbacks);
}
function composeAttestationJoint(from_address, attested_address, profile_data, signer, callbacks){
composeContentJoint(from_address, "attestation", {address: attested_address, profile: profile_data}, signer, callbacks);
}
function composeAssetDefinitionJoint(from_address, asset_definition, signer, callbacks){
composeContentJoint(from_address, "asset", asset_definition, signer, callbacks);
}
function composeAssetAttestorsJoint(from_address, asset, arrNewAttestors, signer, callbacks){
composeContentJoint(from_address, "asset_attestors", {asset: asset, attestors: arrNewAttestors}, signer, callbacks);
}
/*
params.signing_addresses must sign the message but they do not necessarily pay
params.paying_addresses pay for byte outputs and commissions
*/
function composeJoint(params){
var arrWitnesses = params.witnesses;
if (!arrWitnesses){
myWitnesses.readMyWitnesses(function(_arrWitnesses){
params.witnesses = _arrWitnesses;
composeJoint(params);
});
return;
}
/*if (conf.bLight && !params.lightProps){
var network = require('./network.js');
network.requestFromLightVendor(
'light/get_parents_and_last_ball_and_witness_list_unit',
{witnesses: arrWitnesses},
function(ws, request, response){
if (response.error)
return params.callbacks.ifError(response.error);
if (!response.parent_units || !response.last_stable_mc_ball || !response.last_stable_mc_ball_unit || typeof response.last_stable_mc_ball_mci !== 'number')
return params.callbacks.ifError("invalid parents from light vendor");
params.lightProps = response;
composeJoint(params);
}
);
return;
}*/
// try to use as few paying_addresses as possible. Assuming paying_addresses are sorted such that the most well-funded addresses come first
if (params.minimal && !params.send_all){
var callbacks = params.callbacks;
var arrCandidatePayingAddresses = params.paying_addresses;
var trySubset = function(count){
if (count > constants.MAX_AUTHORS_PER_UNIT)
return callbacks.ifNotEnoughFunds("Too many authors. Consider splitting the payment into two units.");
var try_params = _.clone(params);
delete try_params.minimal;
try_params.paying_addresses = arrCandidatePayingAddresses.slice(0, count);
try_params.callbacks = {
ifOk: callbacks.ifOk,
ifError: callbacks.ifError,
ifNotEnoughFunds: function(error_message){
if (count === arrCandidatePayingAddresses.length)
return callbacks.ifNotEnoughFunds(error_message);
trySubset(count+1); // add one more paying address
}
};
composeJoint(try_params);
};
return trySubset(1);
}
var arrSigningAddresses = params.signing_addresses || [];
var arrPayingAddresses = params.paying_addresses || [];
var arrOutputs = params.outputs || [];
var arrMessages = _.clone(params.messages || []);
var assocPrivatePayloads = params.private_payloads || {}; // those that correspond to a subset of params.messages
var fnRetrieveMessages = params.retrieveMessages;
// var lightProps = params.lightProps;
var signer = params.signer;
var callbacks = params.callbacks;
// if (conf.bLight && !lightProps)
// throw Error("no parent props for light");
//profiler.start();
var arrChangeOutputs = arrOutputs.filter(function(output) { return (output.amount === 0); });
var arrExternalOutputs = arrOutputs.filter(function(output) { return (output.amount > 0); });
if (arrChangeOutputs.length > 1)
throw Error("more than one change output");
if (arrChangeOutputs.length === 0)
throw Error("no change outputs");
if (arrPayingAddresses.length === 0)
throw Error("no payers?");
var arrFromAddresses = _.union(arrSigningAddresses, arrPayingAddresses).sort();
var objPaymentMessage = {
app: "payment",
payload_location: "inline",
payload_hash: hash_placeholder,
payload: {
inputs: [],
// first output is the change, it has 0 amount (placeholder) that we'll modify later.
// Then we'll sort outputs, so the change is not necessarity the first in the final transaction
outputs: arrChangeOutputs
// we'll add more outputs below
}
};
var total_amount = 0;
arrExternalOutputs.forEach(function(output){
objPaymentMessage.payload.outputs.push(output);
total_amount += output.amount;
});
arrMessages.push(objPaymentMessage);
var bMultiAuthored = (arrFromAddresses.length > 1);
var objUnit = {
version: constants.version,
alt: constants.alt,
//timestamp: Date.now(),
messages: arrMessages,
authors: []
};
var objJoint = {unit: objUnit};
if (params.earned_headers_commission_recipients) // it needn't be already sorted by address, we'll sort it now
objUnit.earned_headers_commission_recipients = params.earned_headers_commission_recipients.concat().sort(function(a,b){
return ((a.address < b.address) ? -1 : 1);
});
else if (bMultiAuthored) // by default, the entire earned hc goes to the change address
objUnit.earned_headers_commission_recipients = [{address: arrChangeOutputs[0].address, earned_headers_commission_share: 100}];
var total_input;
var last_ball_mci;
var unlock_callback;
var conn;
var lightProps;
var handleError = function(err){
//profiler.stop('compose');
unlock_callback();
if (typeof err === "object"){
if (err.error_code === "NOT_ENOUGH_FUNDS")
return callbacks.ifNotEnoughFunds(err.error);
throw Error("unknown error code in: "+JSON.stringify(err));
}
callbacks.ifError(err);
};
async.series([
function(cb){ // lock
mutex.lock(arrFromAddresses.map(function(from_address){ return 'c-'+from_address; }), function(unlock){
unlock_callback = unlock;
cb();
});
},
function(cb){ // lightProps
if (!conf.bLight)
return cb();
var network = require('./network.js');
network.requestFromLightVendor(
'light/get_parents_and_last_ball_and_witness_list_unit',
{witnesses: arrWitnesses},
function(ws, request, response){
if (response.error)
return handleError(response.error); // cb is not called
if (!response.parent_units || !response.last_stable_mc_ball || !response.last_stable_mc_ball_unit || typeof response.last_stable_mc_ball_mci !== 'number')
return handleError("invalid parents from light vendor"); // cb is not called
lightProps = response;
cb();
}
);
},
function(cb){ // start transaction
db.takeConnectionFromPool(function(new_conn){
conn = new_conn;
conn.query("BEGIN", function(){cb();});
});
},
function(cb){ // parent units
if (bGenesis) {
last_ball_mci = 0;
if (constants.timestampUpgradeMci === 0)
objUnit.timestamp = 1561049490; // Jun 20 2019 16:51:30 UTC
return cb();
}
function checkForUnstablePredecessors(){
var and_not_initial = (last_ball_mci >= constants.unstableInitialDefinitionUpgradeMci) ? "AND definition_chash!=address" : "";
conn.query(
// is_stable=0 condition is redundant given that last_ball_mci is stable
"SELECT 1 FROM units CROSS JOIN unit_authors USING(unit) \n\
WHERE (main_chain_index>? OR main_chain_index IS NULL) AND address IN(?) AND definition_chash IS NOT NULL " + and_not_initial + " \n\
UNION \n\
SELECT 1 FROM units JOIN address_definition_changes USING(unit) \n\
WHERE (main_chain_index>? OR main_chain_index IS NULL) AND address IN(?) \n\
UNION \n\
SELECT 1 FROM units CROSS JOIN unit_authors USING(unit) \n\
WHERE (main_chain_index>? OR main_chain_index IS NULL) AND address IN(?) AND sequence!='good'",
[last_ball_mci, arrFromAddresses, last_ball_mci, arrFromAddresses, last_ball_mci, arrFromAddresses],
function(rows){
if (rows.length > 0)
return cb("some definition changes or definitions or nonserials are not stable yet");
cb();
}
);
}
if (conf.bLight){
objUnit.parent_units = lightProps.parent_units;
objUnit.last_ball = lightProps.last_stable_mc_ball;
objUnit.last_ball_unit = lightProps.last_stable_mc_ball_unit;
last_ball_mci = lightProps.last_stable_mc_ball_mci;
objUnit.timestamp = lightProps.timestamp || Math.round(Date.now() / 1000);
return checkForUnstablePredecessors();
}
objUnit.timestamp = Math.round(Date.now() / 1000);
parentComposer.pickParentUnitsAndLastBall(
conn,
arrWitnesses,
objUnit.timestamp,
function(err, arrParentUnits, last_stable_mc_ball, last_stable_mc_ball_unit, last_stable_mc_ball_mci){
if (err)
return cb("unable to find parents: "+err);
objUnit.parent_units = arrParentUnits;
objUnit.last_ball = last_stable_mc_ball;
objUnit.last_ball_unit = last_stable_mc_ball_unit;
last_ball_mci = last_stable_mc_ball_mci;
checkForUnstablePredecessors();
}
);
},
function (cb) { // version
var bVersion2 = (last_ball_mci >= constants.timestampUpgradeMci || constants.timestampUpgradeMci === 0);
if (!bVersion2)
objUnit.version = constants.versionWithoutTimestamp;
else if (last_ball_mci < constants.includeKeySizesUpgradeMci)
objUnit.version = constants.versionWithoutKeySizes;
// calc or fix payload_hash of non-payment messages
objUnit.messages.forEach(function (message) {
if (message.app === 'payment')
return;
if (!message.payload_location && message.payload)
message.payload_location = 'inline';
if (message.payload_location === 'inline')
message.payload_hash = objectHash.getBase64Hash(message.payload, bVersion2);
});
cb();
},
function(cb){ // authors
composeAuthorsForAddresses(conn, arrFromAddresses, last_ball_mci, objUnit.last_ball_unit, signer, function(err, authors) {
if (err)
return cb(err);
objUnit.authors = authors;
cb();
});
},
function(cb){ // witnesses
if (bGenesis){
objUnit.witnesses = arrWitnesses;
return cb();
}
if (conf.bLight){
if (lightProps.witness_list_unit)
objUnit.witness_list_unit = lightProps.witness_list_unit;
else
objUnit.witnesses = arrWitnesses;
return cb();
}
// witness addresses must not have references
storage.determineIfWitnessAddressDefinitionsHaveReferences(conn, arrWitnesses, function(bWithReferences){
if (bWithReferences)
return cb("some witnesses have references in their addresses");
storage.findWitnessListUnit(conn, arrWitnesses, last_ball_mci, function(witness_list_unit){
if (witness_list_unit)
objUnit.witness_list_unit = witness_list_unit;
else
objUnit.witnesses = arrWitnesses;
cb();
});
});
},
// messages retrieved via callback
function(cb){
if (!fnRetrieveMessages)
return cb();
console.log("will retrieve messages");
fnRetrieveMessages(conn, last_ball_mci, bMultiAuthored, arrPayingAddresses, function(err, arrMoreMessages, assocMorePrivatePayloads){
console.log("fnRetrieveMessages callback: err code = "+(err ? err.error_code : ""));
if (err)
return cb((typeof err === "string") ? ("unable to add additional messages: "+err) : err);
Array.prototype.push.apply(objUnit.messages, arrMoreMessages);
if (assocMorePrivatePayloads && Object.keys(assocMorePrivatePayloads).length > 0)
for (var payload_hash in assocMorePrivatePayloads)
assocPrivatePayloads[payload_hash] = assocMorePrivatePayloads[payload_hash];
cb();
});
},
function(cb){ // input coins
objUnit.headers_commission = objectLength.getHeadersSize(objUnit);
var naked_payload_commission = objectLength.getTotalPayloadSize(objUnit); // without input coins
if (bGenesis){
var issueInput = {type: "issue", serial_number: 1, amount: constants.TOTAL_WHITEBYTES};
if (objUnit.authors.length > 1) {
issueInput.address = arrWitnesses[0];
}
objPaymentMessage.payload.inputs = [issueInput];
objUnit.payload_commission = objectLength.getTotalPayloadSize(objUnit);
total_input = constants.TOTAL_WHITEBYTES;
return cb();
}
if (params.inputs){ // input coins already selected
if (!params.input_amount)
throw Error('inputs but no input_amount');
total_input = params.input_amount;
objPaymentMessage.payload.inputs = params.inputs;
objUnit.payload_commission = objectLength.getTotalPayloadSize(objUnit);
return cb();
}
// all inputs must appear before last_ball
var target_amount = params.send_all ? Infinity : (total_amount + objUnit.headers_commission + naked_payload_commission);
inputs.pickDivisibleCoinsForAmount(
conn, null, arrPayingAddresses, last_ball_mci, target_amount, bMultiAuthored, params.spend_unconfirmed || conf.spend_unconfirmed || 'own',
function(arrInputsWithProofs, _total_input){
if (!arrInputsWithProofs)
return cb({
error_code: "NOT_ENOUGH_FUNDS",
error: "not enough spendable funds from "+arrPayingAddresses+" for "+target_amount
});
total_input = _total_input;
objPaymentMessage.payload.inputs = arrInputsWithProofs.map(function(objInputWithProof){ return objInputWithProof.input; });
objUnit.payload_commission = objectLength.getTotalPayloadSize(objUnit);
console.log("inputs increased payload by", objUnit.payload_commission - naked_payload_commission);
cb();
}
);
}
], function(err){
// we close the transaction and release the connection before signing as multisig signing may take very very long
// however we still keep c-ADDRESS lock to avoid creating accidental doublespends
conn.query(err ? "ROLLBACK" : "COMMIT", function(){
conn.release();
if (err)
return handleError(err);
// change, payload hash, signature, and unit hash
var change = total_input - total_amount - objUnit.headers_commission - objUnit.payload_commission;
if (change <= 0){
if (!params.send_all)
throw Error("change="+change+", params="+JSON.stringify(params));
return handleError({
error_code: "NOT_ENOUGH_FUNDS",
error: "not enough spendable funds from "+arrPayingAddresses+" for fees"
});
}
objPaymentMessage.payload.outputs[0].amount = change;
objPaymentMessage.payload.outputs.sort(sortOutputs);
objPaymentMessage.payload_hash = objectHash.getBase64Hash(objPaymentMessage.payload, objUnit.version !== constants.versionWithoutTimestamp);
var text_to_sign = objectHash.getUnitHashToSign(objUnit);
async.each(
objUnit.authors,
function(author, cb2){
var address = author.address;
async.each( // different keys sign in parallel (if multisig)
Object.keys(author.authentifiers),
function(path, cb3){
if (signer.sign){
signer.sign(objUnit, assocPrivatePayloads, address, path, function(err, signature){
if (err)
return cb3(err);
// it can't be accidentally confused with real signature as there are no [ and ] in base64 alphabet
if (signature === '[refused]')
return cb3('one of the cosigners refused to sign');
author.authentifiers[path] = signature;
cb3();
});
}
else{
signer.readPrivateKey(address, path, function(err, privKey){
if (err)
return cb3(err);
author.authentifiers[path] = ecdsaSig.sign(text_to_sign, privKey);
cb3();
});
}
},
function(err){
cb2(err);
}
);
},
function(err){
if (err)
return handleError(err);
objUnit.unit = objectHash.getUnitHash(objUnit);
if (bGenesis)
objJoint.ball = objectHash.getBallHash(objUnit.unit);
console.log(require('util').inspect(objJoint, {depth:null}));
// objJoint.unit.timestamp = Math.round(Date.now()/1000); // light clients need timestamp
if (Object.keys(assocPrivatePayloads).length === 0)
assocPrivatePayloads = null;
//profiler.stop('compose');
callbacks.ifOk(objJoint, assocPrivatePayloads, unlock_callback);
}
);
});
});
}
var TYPICAL_FEE = 1500;
var MAX_FEE = 20000;
function filterMostFundedAddresses(rows, estimated_amount){
if (!estimated_amount)
return rows.map(function(row){ return row.address; });
var arrFundedAddresses = [];
var accumulated_amount = 0;
for (var i=0; i<rows.length; i++){
arrFundedAddresses.push(rows[i].address);
accumulated_amount += rows[i].total;
if (accumulated_amount > estimated_amount + MAX_FEE)
break;
}
return arrFundedAddresses;
}
function readSortedFundedAddresses(asset, arrAvailableAddresses, estimated_amount, spend_unconfirmed, handleFundedAddresses){
if (arrAvailableAddresses.length === 0)
return handleFundedAddresses([]);
if (estimated_amount && typeof estimated_amount !== 'number')
throw Error('invalid estimated amount: '+estimated_amount);
// addresses closest to estimated amount come first
var order_by = estimated_amount ? "(SUM(amount)>"+estimated_amount+") DESC, ABS(SUM(amount)-"+estimated_amount+") ASC" : "SUM(amount) DESC";
db.query(
"SELECT * FROM ( \n\
SELECT address, SUM(amount) AS total \n\
FROM outputs \n\
CROSS JOIN units USING(unit) \n\
WHERE address IN(?) "+inputs.getConfirmationConditionSql(spend_unconfirmed)+" AND sequence='good' \n\
AND is_spent=0 AND asset"+(asset ? "=?" : " IS NULL")+" \n\
GROUP BY address ORDER BY "+order_by+" \n\
) AS t \n\
WHERE NOT EXISTS ( \n\
SELECT * FROM units CROSS JOIN unit_authors USING(unit) \n\
WHERE is_stable=0 AND unit_authors.address=t.address AND definition_chash IS NOT NULL AND definition_chash != unit_authors.address \n\
)",
asset ? [arrAvailableAddresses, asset] : [arrAvailableAddresses],
function(rows){
var arrFundedAddresses = filterMostFundedAddresses(rows, estimated_amount);
if (arrFundedAddresses.length > 0 || !asset)
return handleFundedAddresses(arrFundedAddresses);
storage.readAssetInfo(db, asset, objAsset => {
if (!objAsset)
throw Error("no such asset " + asset);
if (objAsset.issued_by_definer_only && arrAvailableAddresses.indexOf(objAsset.definer_address) >= 0 || !objAsset.issued_by_definer_only)
return handleFundedAddresses(objAsset.issued_by_definer_only ? [objAsset.definer_address] : arrAvailableAddresses);
handleFundedAddresses([]);
});
/* if (arrFundedAddresses.length === 0)
return handleFundedAddresses([]);
if (!asset || arrFundedAddresses.length === arrAvailableAddresses.length) // base asset or all available addresses already used
return handleFundedAddresses(arrFundedAddresses);
// add other addresses to pay for commissions (in case arrFundedAddresses don't have enough bytes to pay commissions)
var arrOtherAddresses = _.difference(arrAvailableAddresses, arrFundedAddresses);
readSortedFundedAddresses(null, arrOtherAddresses, TYPICAL_FEE, function(arrFundedOtherAddresses){
if (arrFundedOtherAddresses.length === 0)
return handleFundedAddresses(arrFundedAddresses);
handleFundedAddresses(arrFundedAddresses.concat(arrFundedOtherAddresses));
});*/
}
);
}
// tries to use as few of the params.available_paying_addresses as possible.
// note: it doesn't select addresses that have _only_ witnessing or headers commissions outputs
function composeMinimalJoint(params){
var estimated_amount = (params.send_all || params.retrieveMessages) ? 0 : params.outputs.reduce(function(acc, output){ return acc+output.amount; }, 0) + TYPICAL_FEE;
readSortedFundedAddresses(null, params.available_paying_addresses, estimated_amount, params.spend_unconfirmed || conf.spend_unconfirmed || 'own', function(arrFundedPayingAddresses){
if (arrFundedPayingAddresses.length === 0)
return params.callbacks.ifNotEnoughFunds("all paying addresses are unfunded");
var minimal_params = _.clone(params);
delete minimal_params.available_paying_addresses;
minimal_params.minimal = true;
minimal_params.paying_addresses = arrFundedPayingAddresses;
composeJoint(minimal_params);
});
}
function composeAndSaveMinimalJoint(params){
var params_with_save = _.clone(params);
params_with_save.callbacks = getSavingCallbacks(params.callbacks);
composeMinimalJoint(params_with_save);
}
function getSavingCallbacks(callbacks){
return {
ifError: callbacks.ifError,
ifNotEnoughFunds: callbacks.ifNotEnoughFunds,
ifOk: function(objJoint, assocPrivatePayloads, composer_unlock){
var objUnit = objJoint.unit;
var unit = objUnit.unit;
validation.validate(objJoint, {
ifUnitError: function(err){
composer_unlock();
callbacks.ifError("Validation error: "+err);
// throw Error("unexpected validation error: "+err);
},
ifJointError: function(err){
throw Error("unexpected validation joint error: "+err);
},
ifTransientError: function(err){
throw Error("unexpected validation transient error: "+err);
},
ifNeedHashTree: function(){
throw Error("unexpected need hash tree");
},
ifNeedParentUnits: function(arrMissingUnits){
throw Error("unexpected dependencies: "+arrMissingUnits.join(", "));
},
ifOk: function(objValidationState, validation_unlock){
console.log("base asset OK "+objValidationState.sequence);
if (objValidationState.sequence !== 'good'){
validation_unlock();
composer_unlock();
return callbacks.ifError("Bad sequence "+objValidationState.sequence);
}
postJointToLightVendorIfNecessaryAndSave(
objJoint,
function onLightError(err){ // light only
console.log("failed to post base payment "+unit);
var eventBus = require('./event_bus.js');
if (err.match(/signature/))
eventBus.emit('nonfatal_error', "failed to post unit "+unit+": "+err+"; "+JSON.stringify(objUnit), new Error());
validation_unlock();
composer_unlock();
callbacks.ifError(err);
},
function save(){
writer.saveJoint(
objJoint, objValidationState,
function(conn, cb){
if (typeof callbacks.preCommitCb === "function")
callbacks.preCommitCb(conn, objJoint, cb);
else
cb();
},
function onDone(err){
validation_unlock();
composer_unlock();
if (err)
return callbacks.ifError(err);
console.log("composer saved unit "+unit);
callbacks.ifOk(objJoint, assocPrivatePayloads);
}
);
}
);
} // ifOk validation
}); // validate
}
};
}
function postJointToLightVendorIfNecessaryAndSave(objJoint, onLightError, save){
if (conf.bLight){ // light clients cannot save before receiving OK from light vendor
var network = require('./network.js');
network.postJointToLightVendor(objJoint, function(response){
if (response === 'accepted')
save();
else
onLightError(response.error);
});
}
else
save();
}
function composeAndSavePaymentJoint(arrFromAddresses, arrOutputs, signer, callbacks){
composePaymentJoint(arrFromAddresses, arrOutputs, signer, getSavingCallbacks(callbacks));
}
function getMessageIndexByPayloadHash(objUnit, payload_hash){
for (var i=0; i<objUnit.messages.length; i++)
if (objUnit.messages[i].payload_hash === payload_hash)
return i;
throw Error("message not found by payload hash "+payload_hash);
}
function generateBlinding(){
return crypto.randomBytes(12).toString("base64");
}
function composeAuthorsAndMciForAddresses(conn, arrFromAddresses, signer, cb) {
myWitnesses.readMyWitnesses(function(arrWitnesses){
if (conf.bLight)
require('./network.js').requestFromLightVendor(
'light/get_parents_and_last_ball_and_witness_list_unit',
{witnesses: arrWitnesses},
function(ws, request, response){
if (response.error)
return cb(response.error);
if (!response.parent_units || !response.last_stable_mc_ball || !response.last_stable_mc_ball_unit || typeof response.last_stable_mc_ball_mci !== 'number')
return cb("invalid parents from light vendor");
composeAuthorsForAddresses(conn, arrFromAddresses, response.last_stable_mc_ball_mci, response.last_stable_mc_ball_unit, signer, cb);
}
);
else
parentComposer.pickParentUnitsAndLastBall(
conn,
arrWitnesses,
Math.round(Date.now()/1000),
function(err, arrParentUnits, last_stable_mc_ball, last_stable_mc_ball_unit, last_stable_mc_ball_mci){
if (err)
return cb("unable to find parents: "+err);
composeAuthorsForAddresses(conn, arrFromAddresses, last_stable_mc_ball_mci, last_stable_mc_ball_unit, signer, cb);
}
);
});
}
function composeAuthorsForAddresses(conn, arrFromAddresses, last_ball_mci, last_ball_unit, signer, cb) {
var authors = [];
async.eachSeries(arrFromAddresses, function(from_address, cb2){
function setDefinition(){
signer.readDefinition(conn, from_address, function(err, arrDefinition){
if (err)
return cb2(err);
objAuthor.definition = arrDefinition;
cb2();
});
}
var objAuthor = {
address: from_address,
authentifiers: {}
};
signer.readSigningPaths(conn, from_address, function(assocLengthsBySigningPaths){
var arrSigningPaths = Object.keys(assocLengthsBySigningPaths);
for (var j=0; j<arrSigningPaths.length; j++)
objAuthor.authentifiers[arrSigningPaths[j]] = repeatString("-", assocLengthsBySigningPaths[arrSigningPaths[j]]);
authors.push(objAuthor);
var and_stable = (last_ball_mci < constants.unstableInitialDefinitionUpgradeMci) ? "AND is_stable=1 AND main_chain_index<=" + parseInt(last_ball_mci) : "";
conn.query(
"SELECT 1 FROM unit_authors CROSS JOIN units USING(unit) \n\
WHERE address=? AND sequence='good' " + and_stable + " \n\
LIMIT 1",
[from_address],
function(rows){
if (rows.length === 0) // first message from this address
return setDefinition();
// try to find last stable change of definition, then check if the definition was already disclosed
conn.query(
"SELECT definition \n\
FROM address_definition_changes CROSS JOIN units USING(unit) LEFT JOIN definitions USING(definition_chash) \n\
WHERE address=? AND is_stable=1 AND sequence='good' AND main_chain_index<=? \n\
ORDER BY main_chain_index DESC LIMIT 1",
[from_address, last_ball_mci],
function(rows){
if (rows.length === 0) // no definition changes at all
return cb2();
var row = rows[0];
row.definition ? cb2() : setDefinition(); // if definition not found in the db, add it into the json
}
);
}
);
});
}, function(err) {
cb(err, authors, last_ball_unit);
});
}
exports.composePaymentAndTextJoint = composePaymentAndTextJoint;
exports.composeTextJoint = composeTextJoint;
exports.composePaymentJoint = composePaymentJoint;
exports.composeDefinitionChangeJoint = composeDefinitionChangeJoint;
exports.composeDataFeedJoint = composeDataFeedJoint;
exports.composeDataJoint = composeDataJoint;
exports.composeDefinitionTemplateJoint = composeDefinitionTemplateJoint;
exports.composePollJoint = composePollJoint;
exports.composeVoteJoint = composeVoteJoint;
exports.composeProfileJoint = composeProfileJoint;
exports.composeAttestationJoint = composeAttestationJoint;
exports.composeAssetDefinitionJoint = composeAssetDefinitionJoint;
exports.composeAssetAttestorsJoint = composeAssetAttestorsJoint;
exports.composeJoint = composeJoint;
exports.filterMostFundedAddresses = filterMostFundedAddresses;
exports.readSortedFundedAddresses = readSortedFundedAddresses;
exports.composeAndSaveMinimalJoint = composeAndSaveMinimalJoint;
exports.sortOutputs = sortOutputs;
exports.getSavingCallbacks = getSavingCallbacks;
exports.postJointToLightVendorIfNecessaryAndSave = postJointToLightVendorIfNecessaryAndSave;
exports.composeAndSavePaymentJoint = composeAndSavePaymentJoint;
exports.generateBlinding = generateBlinding;
exports.getMessageIndexByPayloadHash = getMessageIndexByPayloadHash;
exports.composeAuthorsAndMciForAddresses = composeAuthorsAndMciForAddresses;