-
Notifications
You must be signed in to change notification settings - Fork 96
/
wallet.js
2797 lines (2655 loc) · 118 KB
/
wallet.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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*jslint node: true */
"use strict";
const util = require('util');
var async = require('async');
var _ = require('lodash');
var db = require('./db.js');
var constants = require('./constants.js');
var conf = require('./conf.js');
var mutex = require('./mutex.js');
var objectHash = require('./object_hash.js');
var ecdsaSig = require('./signature.js');
var network = require('./network.js');
var storage = require('./storage.js');
var device = require('./device.js');
var walletGeneral = require('./wallet_general.js');
var lightWallet = require('./light_wallet.js');
var light = require('./light.js');
var walletDefinedByKeys = require('./wallet_defined_by_keys.js');
var walletDefinedByAddresses = require('./wallet_defined_by_addresses.js');
var eventBus = require('./event_bus.js');
var ValidationUtils = require("./validation_utils.js");
var composer = require('./composer.js');
var indivisibleAsset = require('./indivisible_asset.js');
var divisibleAsset = require('./divisible_asset.js');
var profiler = require('./profiler.js');
var breadcrumbs = require('./breadcrumbs.js');
var balances = require('./balances');
var Mnemonic = require('bitcore-mnemonic');
var inputs = require('./inputs.js');
var prosaic_contract = require('./prosaic_contract.js');
var arbiter_contract = require('./arbiter_contract.js');
var signed_message = require('./signed_message.js');
var aa_addresses = require('./aa_addresses.js');
var message_counter = 0;
var assocLastFailedAssetMetadataTimestamps = {};
var ASSET_METADATA_RETRY_PERIOD = 3600*1000;
var ASSET_METADATA_EXPIRY_PERIOD = 3 * 24 * 3600; // in seconds
function handleLightJustsaying(ws, subject, body){
switch (subject){
case 'light/have_updates':
lightWallet.refreshLightClientHistory();
break;
case 'light/sequence_became_bad':
light.updateAndEmitBadSequenceUnits(body);
break;
}
}
eventBus.on("handle_message_from_hub", handleMessageFromHub);
eventBus.on("message_for_light", handleLightJustsaying);
// called from UI after user confirms signing request initiated from another device, initiator device being the recipient of this message
function sendSignature(device_address, signed_text, signature, signing_path, address){
device.sendMessageToDevice(device_address, "signature", {signed_text: signed_text, signature: signature, signing_path: signing_path, address: address});
}
// one of callbacks MUST be called, otherwise the mutex will stay locked
function handleMessageFromHub(ws, json, device_pubkey, bIndirectCorrespondent, callbacks){
// serialize all messages from hub
mutex.lock(["from_hub"], function(unlock){
var oldcb = callbacks;
callbacks = {
ifOk: function(){oldcb.ifOk(); unlock();},
ifError: function(err){oldcb.ifError(err); unlock();}
};
var subject = json.subject;
var body = json.body;
if (!subject || typeof body == "undefined")
return callbacks.ifError("no subject or body");
//if (bIndirectCorrespondent && ["cancel_new_wallet", "my_xpubkey", "new_wallet_address"].indexOf(subject) === -1)
// return callbacks.ifError("you're indirect correspondent, cannot trust "+subject+" from you");
var from_address = objectHash.getDeviceAddress(device_pubkey);
switch (subject){
case "pairing":
device.handlePairingMessage(json, device_pubkey, callbacks);
break;
case "text":
message_counter++;
if (!ValidationUtils.isNonemptyString(body))
return callbacks.ifError("text body must be string");
// the wallet should have an event handler that displays the text to the user
eventBus.emit("text", from_address, body, message_counter);
callbacks.ifOk();
break;
case "object":
message_counter++;
if(typeof body !== 'object')
return callbacks.ifError("body must be object");
eventBus.emit("object", from_address, body, message_counter);
callbacks.ifOk();
break;
case "removed_paired_device":
// if(conf.bIgnoreUnpairRequests) {
// // unpairing is ignored
// callbacks.ifError("removed_paired_device ignored: "+from_address);
// } else {
determineIfDeviceCanBeRemoved(from_address, function(bRemovable){
if (!bRemovable)
return callbacks.ifError("device "+from_address+" is not removable");
if (conf.bIgnoreUnpairRequests){
db.query("UPDATE correspondent_devices SET is_blackhole=1 WHERE device_address=?", [from_address]);
return callbacks.ifOk();
}
device.removeCorrespondentDevice(from_address, function(){
eventBus.emit("removed_paired_device", from_address);
callbacks.ifOk();
});
});
// }
break;
case "chat_recording_pref":
message_counter++;
eventBus.emit("chat_recording_pref", from_address, body, message_counter);
callbacks.ifOk();
break;
case "create_new_wallet":
// {wallet: "base64", wallet_definition_template: [...]}
walletDefinedByKeys.handleOfferToCreateNewWallet(body, from_address, callbacks);
break;
case "cancel_new_wallet":
// {wallet: "base64"}
if (!ValidationUtils.isNonemptyString(body.wallet))
return callbacks.ifError("no wallet");
walletDefinedByKeys.deleteWallet(body.wallet, from_address, callbacks.ifOk);
break;
case "my_xpubkey": // allowed from non-correspondents
// {wallet: "base64", my_xpubkey: "base58"}
if (!ValidationUtils.isNonemptyString(body.wallet))
return callbacks.ifError("no wallet");
if (!ValidationUtils.isNonemptyString(body.my_xpubkey))
return callbacks.ifError("no my_xpubkey");
if (body.my_xpubkey.length > 112)
return callbacks.ifError("my_xpubkey too long");
walletDefinedByKeys.addDeviceXPubKey(body.wallet, from_address, body.my_xpubkey, callbacks.ifOk);
break;
case "wallet_fully_approved": // allowed from non-correspondents
// {wallet: "base64"}
if (!ValidationUtils.isNonemptyString(body.wallet))
return callbacks.ifError("no wallet");
walletDefinedByKeys.handleNotificationThatWalletFullyApproved(body.wallet, from_address, callbacks.ifOk);
break;
case "new_wallet_address":
// {wallet: "base64", is_change: (0|1), address_index: 1234, address: "BASE32"}
if (!ValidationUtils.isNonemptyString(body.wallet))
return callbacks.ifError("no wallet");
if (!(body.is_change === 0 || body.is_change === 1))
return callbacks.ifError("bad is_change");
if (!ValidationUtils.isNonnegativeInteger(body.address_index))
return callbacks.ifError("bad address_index");
if (!ValidationUtils.isValidAddress(body.address))
return callbacks.ifError("no address or bad address");
walletDefinedByKeys.addNewAddress(body.wallet, body.is_change, body.address_index, body.address, function(err){
if (err)
return callbacks.ifError(err);
callbacks.ifOk();
});
break;
case "create_new_shared_address":
// {address_definition_template: [...]}
if (!ValidationUtils.isArrayOfLength(body.address_definition_template, 2))
return callbacks.ifError("no address definition template");
walletDefinedByAddresses.validateAddressDefinitionTemplate(
body.address_definition_template, from_address,
function(err, assocMemberDeviceAddressesBySigningPaths){
if (err)
return callbacks.ifError(err);
// this event should trigger a confirmatin dialog, user needs to approve creation of the shared address and choose his
// own address that is to become a member of the shared address
eventBus.emit("create_new_shared_address", body.address_definition_template, assocMemberDeviceAddressesBySigningPaths);
callbacks.ifOk();
}
);
break;
case "approve_new_shared_address":
// {address_definition_template_chash: "BASE32", address: "BASE32", device_addresses_by_relative_signing_paths: {...}}
if (!ValidationUtils.isValidAddress(body.address_definition_template_chash))
return callbacks.ifError("invalid addr def c-hash");
if (!ValidationUtils.isValidAddress(body.address))
return callbacks.ifError("invalid address");
if (typeof body.device_addresses_by_relative_signing_paths !== "object"
|| Object.keys(body.device_addresses_by_relative_signing_paths).length === 0)
return callbacks.ifError("invalid device_addresses_by_relative_signing_paths");
walletDefinedByAddresses.approvePendingSharedAddress(body.address_definition_template_chash, from_address,
body.address, body.device_addresses_by_relative_signing_paths);
callbacks.ifOk();
break;
case "reject_new_shared_address":
// {address_definition_template_chash: "BASE32"}
if (!ValidationUtils.isValidAddress(body.address_definition_template_chash))
return callbacks.ifError("invalid addr def c-hash");
walletDefinedByAddresses.deletePendingSharedAddress(body.address_definition_template_chash);
callbacks.ifOk();
break;
case "new_shared_address":
// {address: "BASE32", definition: [...], signers: {...}}
walletDefinedByAddresses.handleNewSharedAddress(body, {
ifError: callbacks.ifError,
ifOk: function(){
callbacks.ifOk();
eventBus.emit('maybe_new_transactions');
}
});
break;
// request to sign a unit created on another device
// two use cases:
// 1. multisig: same address hosted on several devices
// 2. multilateral signing: different addresses signing the same message, such as a (dumb) contract
case "sign":
// {address: "BASE32", signing_path: "r.1.2.3", unsigned_unit: {...}}
if (!ValidationUtils.isValidAddress(body.address))
return callbacks.ifError("no address or bad address");
if (!ValidationUtils.isNonemptyString(body.signing_path) || body.signing_path.charAt(0) !== 'r')
return callbacks.ifError("bad signing path");
var objUnit = body.unsigned_unit;
if (typeof objUnit !== "object")
return callbacks.ifError("no unsigned unit");
var bJsonBased = (objUnit.version !== constants.versionWithoutTimestamp);
// replace all existing signatures with placeholders so that signing requests sent to us on different stages of signing become identical,
// hence the hashes of such unsigned units are also identical
objUnit.authors.forEach(function(author){
var authentifiers = author.authentifiers;
for (var path in authentifiers)
authentifiers[path] = authentifiers[path].replace(/./, '-');
});
var assocPrivatePayloads = body.private_payloads;
if ("private_payloads" in body){
if (typeof assocPrivatePayloads !== "object" || !assocPrivatePayloads)
return callbacks.ifError("bad private payloads");
for (var payload_hash in assocPrivatePayloads){
var payload = assocPrivatePayloads[payload_hash];
var hidden_payload = _.cloneDeep(payload);
if (payload.denomination) // indivisible asset. In this case, payload hash is calculated based on output_hash rather than address and blinding
hidden_payload.outputs.forEach(function(o){
delete o.address;
delete o.blinding;
});
var calculated_payload_hash = objectHash.getBase64Hash(hidden_payload, bJsonBased);
if (payload_hash !== calculated_payload_hash)
return callbacks.ifError("private payload hash does not match");
if (!ValidationUtils.isNonemptyArray(objUnit.messages))
return callbacks.ifError("no messages in unsigned unit");
if (objUnit.messages.filter(function(objMessage){ return (objMessage.payload_hash === payload_hash); }).length !== 1)
return callbacks.ifError("no such payload hash in the messages");
}
}
if (objUnit.messages){
var arrMessages = objUnit.messages;
if (!Array.isArray(arrMessages))
return callbacks.ifError("bad type of messages");
for (var i=0; i<arrMessages.length; i++){
if (arrMessages[i].payload === undefined)
continue;
var calculated_payload_hash = objectHash.getBase64Hash(arrMessages[i].payload, bJsonBased);
if (arrMessages[i].payload_hash !== calculated_payload_hash)
return callbacks.ifError("payload hash does not match");
}
}
else if (objUnit.signed_message){
// ok
}
else
return callbacks.ifError("neither messages nor signed_message");
// findAddress handles both types of addresses
findAddress(body.address, body.signing_path, {
ifError: callbacks.ifError,
ifLocal: function(objAddress){
// the commented check would make multilateral signing impossible
//db.query("SELECT 1 FROM extended_pubkeys WHERE wallet=? AND device_address=?", [row.wallet, from_address], function(sender_rows){
// if (sender_rows.length !== 1)
// return callbacks.ifError("sender is not cosigner of this address");
callbacks.ifOk();
if (objUnit.signed_message && !ValidationUtils.hasFieldsExcept(objUnit, ["signed_message", "authors", "version"])){
objUnit.unit = objectHash.getBase64Hash(objUnit); // exact value doesn't matter, it just needs to be there
return eventBus.emit("signing_request", objAddress, body.address, objUnit, assocPrivatePayloads, from_address, body.signing_path);
}
objUnit.unit = objectHash.getUnitHash(objUnit);
var objJoint = {unit: objUnit, unsigned: true};
eventBus.once("validated-"+objUnit.unit, function(bValid){
if (!bValid){
console.log("===== unit in signing request is invalid");
return;
}
// This event should trigger a confirmation dialog.
// If we merge coins from several addresses of the same wallet, we'll fire this event multiple times for the same unit.
// The event handler must lock the unit before displaying a confirmation dialog, then remember user's choice and apply it to all
// subsequent requests related to the same unit
eventBus.emit("signing_request", objAddress, body.address, objUnit, assocPrivatePayloads, from_address, body.signing_path);
});
// if validation is already under way, handleOnlineJoint will quickly exit because of assocUnitsInWork.
// as soon as the previously started validation finishes, it will trigger our event handler (as well as its own)
network.handleOnlineJoint(ws, objJoint);
//});
},
ifRemote: function(device_address){
if (device_address === from_address){
callbacks.ifError("looping signing request for address "+body.address+", path "+body.signing_path);
throw Error("looping signing request for address "+body.address+", path "+body.signing_path);
}
var text_to_sign = objectHash.getUnitHashToSign(body.unsigned_unit).toString("base64");
// I'm a proxy, wait for response from the actual signer and forward to the requestor
eventBus.once("signature-"+device_address+"-"+body.address+"-"+body.signing_path+"-"+text_to_sign, function(sig){
sendSignature(from_address, text_to_sign, sig, body.signing_path, body.address);
});
// forward the offer to the actual signer
device.sendMessageToDevice(device_address, subject, body);
callbacks.ifOk();
},
ifMerkle: function(bLocal){
callbacks.ifError("there is merkle proof at signing path "+body.signing_path);
},
ifUnknownAddress: function(){
callbacks.ifError("not aware of address "+body.address+" but will see if I learn about it later");
eventBus.once("new_address-"+body.address, function(){
// rewrite callbacks to avoid duplicate unlocking of mutex
handleMessageFromHub(ws, json, device_pubkey, bIndirectCorrespondent, { ifOk: function(){}, ifError: function(){} });
});
}
});
break;
case "signature":
// {signed_text: "base64 of sha256", signing_path: "r.1.2.3", signature: "base64"}
if (!ValidationUtils.isStringOfLength(body.signed_text, constants.HASH_LENGTH)) // base64 of sha256
return callbacks.ifError("bad signed text");
if (!ValidationUtils.isStringOfLength(body.signature, constants.SIG_LENGTH) && body.signature !== '[refused]')
return callbacks.ifError("bad signature length");
if (!ValidationUtils.isNonemptyString(body.signing_path) || body.signing_path.charAt(0) !== 'r')
return callbacks.ifError("bad signing path");
if (!ValidationUtils.isValidAddress(body.address))
return callbacks.ifError("bad address");
eventBus.emit("signature-" + from_address + "-" + body.address + "-" + body.signing_path + "-" + body.signed_text, body.signature);
callbacks.ifOk();
break;
case 'private_payments':
handlePrivatePaymentChains(ws, body, from_address, callbacks);
break;
case 'payment_notification':
// note that since the payments are public, an evil user might notify us about a payment sent by someone else
// (we'll be fooled to believe it was sent by the evil user). It is only possible if he learns our address, e.g. if we make it public.
// Normally, we generate a one-time address and share it in chat session with the future payer only.
var current_message_counter = ++message_counter;
var unit = body;
if (!ValidationUtils.isStringOfLength(unit, constants.HASH_LENGTH))
return callbacks.ifError("invalid unit in payment notification");
var bEmitted = false;
var emitPn = function(objJoint){
if (bEmitted)
return;
bEmitted = true;
emitNewPublicPaymentReceived(from_address, objJoint.unit, current_message_counter);
};
eventBus.once('saved_unit-'+unit, emitPn);
storage.readJoint(db, unit, {
ifNotFound: function(){
console.log("received payment notification for unit "+unit+" which is not known yet, will wait for it");
callbacks.ifOk();
},
ifFound: function(objJoint){
emitPn(objJoint);
eventBus.removeListener('saved_unit-'+unit, emitPn);
callbacks.ifOk();
}
});
break;
case 'prosaic_contract_offer':
body.peer_device_address = from_address;
if (!body.title || !body.text || !body.creation_date)
return callbacks.ifError("not all contract fields submitted");
if (!ValidationUtils.isValidAddress(body.peer_address) || !ValidationUtils.isValidAddress(body.my_address))
return callbacks.ifError("either peer_address or address is not valid in contract");
if (body.hash !== prosaic_contract.getHash(body)) {
if (body.hash === prosaic_contract.getHashV1(body))
return callbacks.ifError("received prosaic contract offer with V1 hash");
return callbacks.ifError("wrong contract hash");
}
if (!/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/.test(body.creation_date))
return callbacks.ifError("wrong contract creation date");
db.query("SELECT 1 FROM my_addresses WHERE address=?", [body.my_address], function(rows) {
if (!rows.length)
return callbacks.ifError("contract does not contain my address");
prosaic_contract.store(body);
var chat_message = "(prosaic-contract:" + Buffer.from(JSON.stringify(body), 'utf8').toString('base64') + ")";
eventBus.emit("text", from_address, chat_message, ++message_counter);
callbacks.ifOk();
});
break;
case 'prosaic_contract_shared':
if (!body.title || !body.text || !body.creation_date)
return callbacks.ifError("not all contract fields submitted");
if (!ValidationUtils.isValidAddress(body.peer_address) || !ValidationUtils.isValidAddress(body.my_address))
return callbacks.ifError("either peer_address or address is not valid in contract");
if (body.hash !== prosaic_contract.getHash(body))
return callbacks.ifError("wrong contract hash");
if (!/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/.test(body.creation_date))
return callbacks.ifError("wrong contract creation date");
db.query("SELECT 1 FROM my_addresses \n\
JOIN wallet_signing_paths USING(wallet)\n\
WHERE my_addresses.address=? AND wallet_signing_paths.device_address=?",[body.my_address, from_address],
function(rows) {
if (!rows.length)
return callbacks.ifError("contract does not contain my address");
prosaic_contract.store(body);
callbacks.ifOk();
}
);
break;
case 'prosaic_contract_response':
var validation = require('./validation.js');
if (body.status !== "accepted" && body.status !== "declined")
return callbacks.ifError("wrong status supplied");
prosaic_contract.getByHash(body.hash, function(objContract){
if (!objContract)
return callbacks.ifError("wrong contract hash");
if (body.status === "accepted" && !body.signed_message)
return callbacks.ifError("response is not signed");
var processResponse = function(objSignedMessage) {
if (body.authors && body.authors.length) {
if (body.authors.length !== 1)
return callbacks.ifError("wrong number of authors received");
var author = body.authors[0];
if (author.definition && (author.address !== objectHash.getChash160(author.definition)))
return callbacks.ifError("incorrect definition received");
if (!ValidationUtils.isValidAddress(author.address) || author.address !== objContract.peer_address)
return callbacks.ifError("incorrect author address");
// this can happen when acceptor and offerer have same device in cosigners
db.query('SELECT 1 FROM my_addresses WHERE address=? \n\
UNION SELECT 1 FROM shared_addresses WHERE shared_address=?', [author.address, author.address], function(rows) {
if (rows.length)
return;
db.query("INSERT "+db.getIgnore()+" INTO peer_addresses (address, device_address, signing_paths, definition) VALUES (?, ?, ?, ?)",
[author.address, from_address, JSON.stringify(Object.keys(objSignedMessage.authors[0].authentifiers)), JSON.stringify(author.definition)],
function(res) {
if (res.affectedRows == 0)
db.query("UPDATE peer_addresses SET signing_paths=?, definition=? WHERE address=?", [JSON.stringify(Object.keys(objSignedMessage.authors[0].authentifiers)), JSON.stringify(author.definition), author.address]);
}
);
}
);
}
if (objContract.status !== 'pending')
return callbacks.ifError("contract is not active, current status: " + objContract.status);
var objDateCopy = new Date(objContract.creation_date_obj);
if (objDateCopy.setHours(objDateCopy.getHours(), objDateCopy.getMinutes(), (objDateCopy.getSeconds() + objContract.ttl * 60 * 60)|0) < Date.now())
return callbacks.ifError("contract already expired");
prosaic_contract.setField(objContract.hash, "status", body.status);
eventBus.emit("text", from_address, "contract \""+objContract.title+"\" " + body.status, ++message_counter);
eventBus.emit("prosaic_contract_response_received" + body.hash, (body.status === "accepted"), body.authors);
callbacks.ifOk();
};
if (body.signed_message) {
try{
var signedMessageJson = Buffer.from(body.signed_message, 'base64').toString('utf8');
var objSignedMessage = JSON.parse(signedMessageJson);
}
catch(e){
return callbacks.ifError("wrong signed message");
}
// if (objSignedMessage.version !== constants.version)
// return callbacks.ifError("wrong version in signed message: " + objSignedMessage.version);
signed_message.validateSignedMessage(db, objSignedMessage, objContract.peer_address, function(err) {
if (err || objSignedMessage.authors[0].address !== objContract.peer_address || objSignedMessage.signed_message != objContract.title)
return callbacks.ifError("wrong contract signature");
processResponse(objSignedMessage);
});
} else
processResponse();
});
break;
case 'prosaic_contract_update':
prosaic_contract.getByHash(body.hash, function(objContract){
if (!objContract || objContract.peer_device_address !== from_address)
return callbacks.ifError("wrong contract hash or not an owner");
if (body.field == "status") {
if (body.value !== "revoked" || objContract.status !== "pending")
return callbacks.ifError("wrong status for contract supplied");
} else
if (body.field == "unit") {
if (objContract.status !== "accepted")
return callbacks.ifError("contract was not accepted");
if (objContract.unit)
return callbacks.ifError("unit was already provided for this contract");
} else
if (body.field == "shared_address") {
if (objContract.status !== "accepted")
return callbacks.ifError("contract was not accepted");
if (objContract.shared_address)
return callbacks.ifError("shared_address was already provided for this contract");
if (!ValidationUtils.isValidAddress(body.value))
return callbacks.ifError("invalid address provided");
} else {
return callbacks.ifError("wrong field");
}
prosaic_contract.setField(objContract.hash, body.field, body.value);
callbacks.ifOk();
});
break;
case 'arbiter_contract_offer':
body.peer_device_address = from_address;
if (!body.title || !body.text || !body.creation_date || !body.arbiter_address || typeof body.me_is_payer === "undefined" || !body.my_pairing_code || !body.amount || body.amount <= 0)
return callbacks.ifError("not all contract fields submitted");
if (!ValidationUtils.isValidAddress(body.my_address) || !ValidationUtils.isValidAddress(body.peer_address) || !ValidationUtils.isValidAddress(body.arbiter_address))
return callbacks.ifError("either peer_address or address or arbiter_address is not valid in contract");
if (body.hash !== arbiter_contract.getHash(body)) {
return callbacks.ifError("wrong contract hash");
}
if (!/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/.test(body.creation_date))
return callbacks.ifError("wrong contract creation date");
var my_address = body.peer_address;
body.peer_address = body.my_address;
body.my_address = my_address;
var my_party_name = body.peer_party_name;
body.peer_party_name = body.my_party_name;
body.my_party_name = my_party_name;
body.peer_pairing_code = body.my_pairing_code; body.my_pairing_code = null;
body.peer_contact_info = body.my_contact_info; body.my_contact_info = null;
body.me_is_payer = !body.me_is_payer;
if (body.hash !== arbiter_contract.getHash(body))
throw Error("wrong contract hash after swapping me and peer");
db.query("SELECT 1 FROM my_addresses WHERE address=?", [body.my_address], function(rows) {
if (!rows.length)
return callbacks.ifError("contract does not contain my address");
arbiter_contract.store(body, function() {
eventBus.emit("arbiter_contract_offer", body.hash);
callbacks.ifOk();
});
});
break;
// to cosigners
case 'arbiter_contract_shared':
if (!body.title || !body.text || !body.creation_date || !body.arbiter_address || typeof body.me_is_payer === "undefined" || !body.peer_pairing_code || !body.amount || body.amount <= 0)
return callbacks.ifError("not all contract fields submitted");
if (!ValidationUtils.isValidAddress(body.peer_address) || !ValidationUtils.isValidAddress(body.my_address) || !ValidationUtils.isValidAddress(body.arbiter_address) )
return callbacks.ifError("either peer_address or address or arbiter_address or shared_address are not valid in contract");
if (body.hash !== arbiter_contract.getHash(body))
return callbacks.ifError("wrong contract hash");
if (!/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/.test(body.creation_date))
return callbacks.ifError("wrong contract creation date");
db.query("SELECT 1 FROM my_addresses \n\
JOIN wallet_signing_paths USING(wallet)\n\
WHERE my_addresses.address=? AND wallet_signing_paths.device_address=?",[body.my_address, from_address],
function(rows) {
if (!rows.length)
return callbacks.ifError("contract does not contain my address");
body.me_is_cosigner = true;
arbiter_contract.store(body);
callbacks.ifOk();
}
);
break;
// sent by both contract parties and your cosigners, in both directions
case 'arbiter_contract_update':
arbiter_contract.getByHash(body.hash, function(objContract){
var from_cosigner = false;
db.query("SELECT 1 FROM wallet_signing_paths WHERE device_address=?", [from_address], function(rows) {
if (rows.length)
from_cosigner = true;
if (!objContract || (from_address !== objContract.peer_device_address && !from_cosigner && !(from_address === objContract.arbstore_device_address && objContract.status === 'in_appeal' && body.field === 'status')))
return callbacks.ifError("wrong contract hash or not an owner");
if (body.field === "status") {
var isOK = false;
switch (objContract.status) {
case "pending":
if (body.value === "revoked" || body.value === "accepted")
isOK = true;
break;
case "paid":
if (body.value === "in_dispute" || body.value === "cancelled" || body.value === "completed")
isOK = true;
break;
case "dispute_resolved":
if (body.value === "in_appeal")
isOK = true;
break;
case "in_appeal":
if (objContract.arbstore_device_address === from_address && (body.value === 'appeal_approved' || body.value === 'appeal_declined'))
isOK = true;
break;
}
if (!isOK)
return callbacks.ifError("wrong status for contract supplied");
} else
if (body.field === "unit") {
if (objContract.status !== "accepted")
return callbacks.ifError("contract was not accepted");
if (objContract.unit)
return callbacks.ifError("unit was already provided for this contract");
arbiter_contract.setField(objContract.hash, "status", "signed", null, true);
} else
if (body.field === "shared_address") {
if (objContract.status !== "accepted")
return callbacks.ifError("contract was not accepted");
if (objContract.shared_address)
return callbacks.ifError("shared_address was already provided for this contract");
if (!ValidationUtils.isValidAddress(body.value))
return callbacks.ifError("invalid address provided");
} else {
return callbacks.ifError("wrong field");
}
arbiter_contract.setField(objContract.hash, body.field, body.value, function(objContract) {
eventBus.emit("arbiter_contract_update", objContract, body.field, body.value);
callbacks.ifOk();
}, from_cosigner);
});
});
break;
// arbstore sends to arbiter's wallet
case 'arbiter_dispute_request':
if (!body.contract_hash || !body.my_address || !body.peer_address || body.me_is_payer === undefined || !body.my_pairing_code || !body.peer_pairing_code
|| !body.encrypted_contract || !body.unit || !body.amount || body.asset === undefined || !body.arbiter_address || !body.service_fee_asset)
return callbacks.ifError("wrong dispute request");
var contractContent = device.decryptPackage(body.encrypted_contract);
if (!contractContent || !contractContent.creation_date || !contractContent.title || !contractContent.text)
return callbacks.ifError("wrong contract content");
if (conf.bLight)
network.requestHistoryFor([body.unit], [], function(){});
body.contract_content = contractContent;
body.arbstore_device_address = from_address;
arbiter_contract.insertDispute(body, function(res) {
if (res.affectedRows == 0) {
return callbacks.ifError("can't insert dispute request into db");
}
var objDispute = {
contract_hash: body.contract_hash,
title: contractContent.title,
service_fee_asset: body.service_fee_asset
};
var chat_message = "(arbiter-dispute:" + Buffer.from(JSON.stringify(objDispute), 'utf8').toString('base64') + ")";
eventBus.emit("text", from_address, chat_message, ++message_counter);
callbacks.ifOk();
});
break;
// sent by peer
case 'arbiter_contract_response':
var validation = require('./validation.js');
if (body.status !== "accepted" && body.status !== "declined")
return callbacks.ifError("wrong status supplied");
arbiter_contract.getByHash(body.hash, function(objContract){
if (!objContract)
return callbacks.ifError("wrong contract hash");
if (body.status === "accepted" && !body.signed_message)
return callbacks.ifError("response is not signed");
var processResponse = function(objSignedMessage) {
if (body.authors && body.authors.length) {
if (body.authors.length !== 1)
return callbacks.ifError("wrong number of authors received");
var author = body.authors[0];
if (author.definition && (author.address !== objectHash.getChash160(author.definition)))
return callbacks.ifError("incorrect definition received");
if (!ValidationUtils.isValidAddress(author.address) || author.address !== objContract.peer_address)
return callbacks.ifError("incorrect author address");
// this can happen when acceptor and offerer have same device in cosigners
db.query('SELECT 1 FROM my_addresses WHERE address=? \n\
UNION SELECT 1 FROM shared_addresses WHERE shared_address=?', [author.address, author.address], function(rows) {
if (rows.length)
return;
db.query("INSERT "+db.getIgnore()+" INTO peer_addresses (address, device_address, signing_paths, definition) VALUES (?, ?, ?, ?)",
[author.address, from_address, JSON.stringify(Object.keys(objSignedMessage.authors[0].authentifiers)), JSON.stringify(author.definition)],
function(res) {
if (res.affectedRows == 0)
db.query("UPDATE peer_addresses SET signing_paths=?, definition=? WHERE address=?", [JSON.stringify(Object.keys(objSignedMessage.authors[0].authentifiers)), JSON.stringify(author.definition), author.address]);
}
);
}
);
}
var isAllowed = objContract.status === "pending" || (objContract.status === 'accepted' && body.status === 'accepted');
if (!isAllowed)
return callbacks.ifError("contract is not active, current status: " + objContract.status);
var objDateCopy = new Date(objContract.creation_date_obj);
if (objDateCopy.setHours(objDateCopy.getHours(), objDateCopy.getMinutes(), (objDateCopy.getSeconds() + objContract.ttl * 60 * 60)|0) < Date.now())
return callbacks.ifError("contract already expired");
if (body.my_pairing_code)
arbiter_contract.setField(objContract.hash, "peer_pairing_code", body.my_pairing_code);
if (body.my_contact_info)
arbiter_contract.setField(objContract.hash, "peer_contact_info", body.my_contact_info);
arbiter_contract.setField(objContract.hash, "status", body.status, function(objContract){
eventBus.emit("arbiter_contract_response_received", objContract);
});
callbacks.ifOk();
};
if (body.signed_message) {
try{
var signedMessageJson = Buffer.from(body.signed_message, 'base64').toString('utf8');
var objSignedMessage = JSON.parse(signedMessageJson);
}
catch(e){
return callbacks.ifError("wrong signed message");
}
signed_message.validateSignedMessage(db, objSignedMessage, objContract.peer_address, function(err) {
if (err || objSignedMessage.authors[0].address !== objContract.peer_address || objSignedMessage.signed_message != objContract.title)
return callbacks.ifError("wrong contract signature");
processResponse(objSignedMessage);
});
} else
processResponse();
});
break;
default:
callbacks.ifError("unknnown subject: "+subject);
}
});
}
var handledChainsCache = {};
function handlePrivatePaymentChains(ws, body, from_address, callbacks){
var arrChains = body.chains;
if (!ValidationUtils.isNonemptyArray(arrChains))
return callbacks.ifError("no chains found");
var cache_key = objectHash.getBase64Hash(arrChains);
if (handledChainsCache[cache_key]) {
eventBus.emit('all_private_payments_handled', from_address);
eventBus.emit('all_private_payments_handled-' + arrChains[0][0].unit);
return callbacks.ifOk();
}
profiler.increment();
if (conf.bLight)
network.requestUnfinishedPastUnitsOfPrivateChains(arrChains); // it'll work in the background
var assocValidatedByKey = {};
var bParsingComplete = false;
var cancelAllKeys = function(){
for (var key in assocValidatedByKey)
eventBus.removeAllListeners(key);
};
var current_message_counter = ++message_counter;
var checkIfAllValidated = function(){
if (!assocValidatedByKey) // duplicate call - ignore
return console.log('duplicate call of checkIfAllValidated');
for (var key in assocValidatedByKey)
if (!assocValidatedByKey[key])
return console.log('not all private payments validated yet');
eventBus.emit('all_private_payments_handled', from_address);
eventBus.emit('all_private_payments_handled-' + arrChains[0][0].unit);
assocValidatedByKey = null; // to avoid duplicate calls
if (!body.forwarded){
if (from_address) emitNewPrivatePaymentReceived(from_address, arrChains, current_message_counter);
// note, this forwarding won't work if the user closes the wallet before validation of the private chains
var arrUnits = arrChains.map(function(arrPrivateElements){ return arrPrivateElements[0].unit; });
db.query("SELECT address FROM unit_authors WHERE unit IN(?)", [arrUnits], function(rows){
var arrAuthorAddresses = rows.map(function(row){ return row.address; });
// if the addresses are not shared, it doesn't forward anything
forwardPrivateChainsToOtherMembersOfSharedAddresses(arrChains, arrAuthorAddresses, from_address, true);
});
}
profiler.print();
};
async.eachSeries(
arrChains,
function(arrPrivateElements, cb){ // validate each chain individually
var objHeadPrivateElement = arrPrivateElements[0];
if (!!objHeadPrivateElement.payload.denomination !== ValidationUtils.isNonnegativeInteger(objHeadPrivateElement.output_index))
return cb("divisibility doesn't match presence of output_index");
var output_index = objHeadPrivateElement.payload.denomination ? objHeadPrivateElement.output_index : -1;
var json_payload_hash = objectHash.getBase64Hash(objHeadPrivateElement.payload, true);
var key = 'private_payment_validated-'+objHeadPrivateElement.unit+'-'+json_payload_hash+'-'+output_index;
assocValidatedByKey[key] = false;
network.handleOnlinePrivatePayment(ws, arrPrivateElements, true, {
ifError: function(error){
console.log("handleOnlinePrivatePayment error: "+error);
cb("an error"); // do not leak error message to the hub
},
ifValidationError: function(unit, error){
console.log("handleOnlinePrivatePayment validation error: "+error);
cb("an error"); // do not leak error message to the hub
},
ifAccepted: function(unit){
console.log("handleOnlinePrivatePayment accepted");
assocValidatedByKey[key] = true;
cb(); // do not leak unit info to the hub
},
// this is the most likely outcome for light clients
ifQueued: function(){
console.log("handleOnlinePrivatePayment queued, will wait for "+key);
eventBus.once(key, function(bValid){
if (!bValid)
return cancelAllKeys();
assocValidatedByKey[key] = true;
if (bParsingComplete)
checkIfAllValidated();
else
console.log('parsing incomplete yet');
});
cb();
}
});
},
function(err){
bParsingComplete = true;
if (err){
cancelAllKeys();
return callbacks.ifError(err);
}
checkIfAllValidated();
handledChainsCache[cache_key] = true;
callbacks.ifOk();
// forward the chains to other members of output addresses
if (!body.forwarded)
forwardPrivateChainsToOtherMembersOfOutputAddresses(arrChains, true);
}
);
}
function forwardPrivateChainsToOtherMembersOfOutputAddresses(arrChains, bForwarded, conn, onSaved){
console.log("forwardPrivateChainsToOtherMembersOfOutputAddresses", arrChains);
var assocOutputAddresses = {};
arrChains.forEach(function(arrPrivateElements){
var objHeadPrivateElement = arrPrivateElements[0];
var payload = objHeadPrivateElement.payload;
payload.outputs.forEach(function(output){
if (output.address)
assocOutputAddresses[output.address] = true;
});
if (objHeadPrivateElement.output && objHeadPrivateElement.output.address)
assocOutputAddresses[objHeadPrivateElement.output.address] = true;
});
var arrOutputAddresses = Object.keys(assocOutputAddresses);
console.log("output addresses", arrOutputAddresses);
conn = conn || db;
if (!onSaved)
onSaved = function(){};
readWalletsByAddresses(conn, arrOutputAddresses, function(arrWallets){
if (arrWallets.length === 0){
// breadcrumbs.add("forwardPrivateChainsToOtherMembersOfOutputAddresses: " + JSON.stringify(arrChains)); // remove in livenet
// eventBus.emit('nonfatal_error', "not my wallet? output addresses: "+arrOutputAddresses.join(', '), new Error());
// throw Error("not my wallet? output addresses: "+arrOutputAddresses.join(', '));
}
var arrFuncs = [];
if (arrWallets.length > 0)
arrFuncs.push(function(cb){
walletDefinedByKeys.forwardPrivateChainsToOtherMembersOfWallets(arrChains, arrWallets, bForwarded, conn, cb);
});
arrFuncs.push(function(cb){
walletDefinedByAddresses.forwardPrivateChainsToOtherMembersOfAddresses(arrChains, arrOutputAddresses, bForwarded, conn, cb);
});
async.series(arrFuncs, onSaved);
});
}
function readWalletsByAddresses(conn, arrAddresses, handleWallets){
conn.query("SELECT DISTINCT wallet FROM my_addresses WHERE address IN(?)", [arrAddresses], function(rows){
var arrWallets = rows.map(function(row){ return row.wallet; });
conn.query("SELECT DISTINCT address FROM shared_address_signing_paths WHERE shared_address IN(?)", [arrAddresses], function(rows){
if (rows.length === 0)
return handleWallets(arrWallets);
var arrNewAddresses = rows.map(function(row){ return row.address; });
readWalletsByAddresses(conn, arrNewAddresses, function(arrNewWallets){
handleWallets(_.union(arrWallets, arrNewWallets));
});
});
});
}
// event emitted in two cases:
// 1. if I received private payloads via direct connection, not through a hub
// 2. (not true any more) received private payload from anywhere, didn't handle it immediately, saved and handled later
eventBus.on("new_direct_private_chains", forwardPrivateChainsToOtherMembersOfOutputAddresses);
function emitNewPrivatePaymentReceived(payer_device_address, arrChains, message_counter){
console.log('emitNewPrivatePaymentReceived');
walletGeneral.readMyAddresses(function(arrAddresses){
var assocAmountsByAsset = {};
var assocMyReceivingAddresses = {};
var units = [];
arrChains.forEach(function(arrPrivateElements){
var objHeadPrivateElement = arrPrivateElements[0];
units.push(objHeadPrivateElement.unit);
var payload = objHeadPrivateElement.payload;
var asset = payload.asset || 'base';
if (!assocAmountsByAsset[asset])
assocAmountsByAsset[asset] = 0;
payload.outputs.forEach(function(output){
if (output.address && arrAddresses.indexOf(output.address) >= 0){
assocAmountsByAsset[asset] += output.amount;
assocMyReceivingAddresses[output.address] = true;
}
});
// indivisible
var output = objHeadPrivateElement.output;
if (output && output.address && arrAddresses.indexOf(output.address) >= 0){
assocAmountsByAsset[asset] += payload.outputs[objHeadPrivateElement.output_index].amount;
assocMyReceivingAddresses[output.address] = true;
}
});
console.log('assocAmountsByAsset', assocAmountsByAsset);
var arrMyReceivingAddresses = Object.keys(assocMyReceivingAddresses);
if (arrMyReceivingAddresses.length === 0)
return;
// skip notification if the payment was from our adddress
db.query("SELECT 1 FROM unit_authors JOIN my_addresses USING(address) WHERE unit IN(?)\n\
UNION SELECT 1 FROM unit_authors JOIN shared_addresses ON unit_authors.address=shared_addresses.shared_address WHERE unit IN(?)", [units, units],
function(rows) {
if (rows.length)
return;
db.query("SELECT 1 FROM shared_addresses WHERE shared_address IN(?)", [arrMyReceivingAddresses], function(rows){
var emit = function(address_type) {
for (var asset in assocAmountsByAsset)
if (assocAmountsByAsset[asset])
eventBus.emit('received_payment', payer_device_address, assocAmountsByAsset[asset], asset, message_counter, address_type);
}
var bToSharedAddress = (rows.length > 0);
if (!bToSharedAddress) {
db.query("SELECT 1 FROM my_watched_addresses WHERE address IN(?)", [arrMyReceivingAddresses], function(rows){
if (rows.length > 0)
emit('watched');
else
emit('main');
});
} else
emit('shared');
});
}
);
});
}
function emitNewPublicPaymentReceived(payer_device_address, objUnit){ // current_message_counter unused
walletGeneral.readMyAddresses(function(arrAddresses){
db.query("SELECT shared_address FROM shared_addresses", function(rows){
var arrSharedAddresses = rows.map(function(row){ return row.shared_address; });
var assocAmountsByAsset = {};
objUnit.messages.forEach(function(message){
if (message.app !== 'payment' || !message.payload)
return;
var payload = message.payload;
var asset = payload.asset || 'base';
if (!assocAmountsByAsset[asset])
assocAmountsByAsset[asset] = {main: 0, shared: 0};
payload.outputs.forEach(function(output){
if (output.address && arrAddresses.indexOf(output.address) >= 0){
var bShared = (arrSharedAddresses.indexOf(output.address) >= 0);
assocAmountsByAsset[asset][bShared ? 'shared' : 'main'] += output.amount;
}
});
});
for (var asset in assocAmountsByAsset){
var amounts = assocAmountsByAsset[asset];
for (var type in amounts)
if (amounts[type])
eventBus.emit('received_payment', payer_device_address, amounts[type], asset, ++message_counter, type);
}
});
});
}
function findAddress(address, signing_path, callbacks, fallback_remote_device_address){
db.query(
"SELECT wallet, account, is_change, address_index, full_approval_date, device_address \n\
FROM my_addresses JOIN wallets USING(wallet) JOIN wallet_signing_paths USING(wallet) \n\
WHERE address=? AND signing_path=?",
[address, signing_path],
function(rows){
if (rows.length > 1)
throw Error("more than 1 address found");
if (rows.length === 1){
var row = rows[0];
if (!row.full_approval_date)
return callbacks.ifError("wallet of address "+address+" not approved");