forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.js
2307 lines (2178 loc) · 93.3 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";
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 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 message_counter = 0;
var assocLastFailedAssetMetadataTimestamps = {};
var ASSET_METADATA_RETRY_PERIOD = 3600*1000;
function handleJustsaying(ws, subject, body){
switch (subject){
// I'm connected to a hub, received challenge
case 'hub/challenge':
var challenge = body;
device.handleChallenge(ws, challenge);
break;
// I'm connected to a hub, received a message through the hub
case 'hub/message':
var objDeviceMessage = body.message;
var message_hash = body.message_hash;
var respondWithError = function(error){
network.sendError(ws, error);
network.sendJustsaying(ws, 'hub/delete', message_hash);
};
if (!message_hash || !objDeviceMessage || !objDeviceMessage.signature || !objDeviceMessage.pubkey || !objDeviceMessage.to
|| !objDeviceMessage.encrypted_package || !objDeviceMessage.encrypted_package.dh
|| !objDeviceMessage.encrypted_package.dh.sender_ephemeral_pubkey
|| !objDeviceMessage.encrypted_package.encrypted_message
|| !objDeviceMessage.encrypted_package.iv || !objDeviceMessage.encrypted_package.authtag)
return network.sendError(ws, "missing fields");
if (objDeviceMessage.to !== device.getMyDeviceAddress())
return network.sendError(ws, "not mine");
var bOldHashIsCorrect = (message_hash === objectHash.getBase64Hash(objDeviceMessage));
if (!bOldHashIsCorrect && message_hash !== objectHash.getBase64Hash(objDeviceMessage, true))
return network.sendError(ws, "wrong hash");
try{
if (!ecdsaSig.verify(objectHash.getDeviceMessageHashToSign(objDeviceMessage), objDeviceMessage.signature, objDeviceMessage.pubkey))
return respondWithError("wrong message signature");
}
catch(e){
return respondWithError("failed to caculate message hash to sign:" + e);
}
// end of checks on the open (unencrypted) part of the message. These checks should've been made by the hub before accepting the message
// decrypt the message
var json = device.decryptPackage(objDeviceMessage.encrypted_package);
if (!json)
return respondWithError("failed to decrypt");
// who is the sender
var from_address = objectHash.getDeviceAddress(objDeviceMessage.pubkey);
// the hub couldn't mess with json.from as it was encrypted, but it could replace the objDeviceMessage.pubkey and re-sign. It'll be caught here
if (from_address !== json.from)
return respondWithError("wrong message signature");
var handleMessage = function(bIndirectCorrespondent, onDone){
handleMessageFromHub(ws, json, objDeviceMessage.pubkey, bIndirectCorrespondent, {
ifError: function(err){
respondWithError(err);
onDone();
},
ifOk: function(){
network.sendJustsaying(ws, 'hub/delete', message_hash);
onDone();
}
});
};
// serialize all messages from hub
mutex.lock(["from_hub"], function(unlock){
// check that we know this device
db.query("SELECT hub, is_indirect FROM correspondent_devices WHERE device_address=?", [from_address], function(rows){
if (rows.length > 0){
if (json.device_hub && json.device_hub !== rows[0].hub) // update correspondent's home address if necessary
db.query("UPDATE correspondent_devices SET hub=? WHERE device_address=?", [json.device_hub, from_address], function(){
handleMessage(rows[0].is_indirect, unlock);
});
else
handleMessage(rows[0].is_indirect, unlock);
}
else{ // correspondent not known
var arrSubjectsAllowedFromNoncorrespondents = ["pairing", "my_xpubkey", "wallet_fully_approved"];
if (arrSubjectsAllowedFromNoncorrespondents.indexOf(json.subject) === -1){
respondWithError("correspondent not known and not whitelisted subject");
return unlock();
}
handleMessage(false, unlock);
}
});
});
break;
// I'm connected to a hub, received a report about my undelivered inbox
case 'hub/message_box_status':
if (!ws.bLoggedIn)
return respondWithError("you are not my hub");
if (body === 'empty')
device.scheduleTempDeviceKeyRotation();
else if (body === 'has_more')
mutex.lock(["from_hub"], function(unlock){ // we'll obtain the lock after all messages are handled
setTimeout(function(){ // wait to make sure all hub/deletes finish
network.sendJustsaying(ws, 'hub/refresh');
unlock();
}, 1000)
});
break;
case 'light/have_updates':
lightWallet.refreshLightClientHistory();
break;
}
}
eventBus.on("message_from_hub", handleJustsaying);
eventBus.on("message_for_light", handleJustsaying);
// 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){
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"])){
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");
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() + objContract.ttl) < 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) {
var signedMessageJson = Buffer.from(body.signed_message, 'base64').toString('utf8');
try{
var objSignedMessage = JSON.parse(signedMessageJson);
}
catch(e){
return callbacks.ifError("wrong signed message");
}
validation.validateSignedMessage(objSignedMessage, 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;
default:
callbacks.ifError("unknnown subject: "+subject);
}
}
function handlePrivatePaymentChains(ws, body, from_address, callbacks){
var arrChains = body.chains;
if (!ValidationUtils.isNonemptyArray(arrChains))
return callbacks.ifError("no chains found");
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();
callbacks.ifOk();
// forward the chains to other members of output addresses
if (!body.forwarded)
forwardPrivateChainsToOtherMembersOfOutputAddresses(arrChains);
}
);
}
function forwardPrivateChainsToOtherMembersOfOutputAddresses(arrChains, 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, conn, cb);
});
arrFuncs.push(function(cb){
walletDefinedByAddresses.forwardPrivateChainsToOtherMembersOfAddresses(arrChains, arrOutputAddresses, 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 = {};
arrChains.forEach(function(arrPrivateElements){
var objHeadPrivateElement = arrPrivateElements[0];
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;
db.query("SELECT 1 FROM shared_addresses WHERE shared_address IN(?)", [arrMyReceivingAddresses], function(rows){
var bToSharedAddress = (rows.length > 0);
for (var asset in assocAmountsByAsset)
if (assocAmountsByAsset[asset])
eventBus.emit('received_payment', payer_device_address, assocAmountsByAsset[asset], asset, message_counter, bToSharedAddress);
});
});
}
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 === 'shared');
}
});
});
}
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");
if (row.device_address !== device.getMyDeviceAddress())
return callbacks.ifRemote(row.device_address);
var objAddress = {
address: address,
wallet: row.wallet,
account: row.account,
is_change: row.is_change,
address_index: row.address_index
};
callbacks.ifLocal(objAddress);
return;
}
db.query(
// "SELECT address, device_address, member_signing_path FROM shared_address_signing_paths WHERE shared_address=? AND signing_path=?",
// look for a prefix of the requested signing_path
"SELECT address, device_address, signing_path FROM shared_address_signing_paths \n\
WHERE shared_address=? AND signing_path=SUBSTR(?, 1, LENGTH(signing_path))",
[address, signing_path],
function(sa_rows){
if (sa_rows.length > 1)
throw Error("more than 1 member address found for shared address "+address+" and signing path "+signing_path);
if (sa_rows.length === 1) {
var objSharedAddress = sa_rows[0];
var relative_signing_path = 'r' + signing_path.substr(objSharedAddress.signing_path.length);
var bLocal = (objSharedAddress.device_address === device.getMyDeviceAddress()); // local keys
if (objSharedAddress.address === '') {
return callbacks.ifMerkle(bLocal);
} else if(objSharedAddress.address === 'secret') {
return callbacks.ifSecret();
}
return findAddress(objSharedAddress.address, relative_signing_path, callbacks, bLocal ? null : objSharedAddress.device_address);
}
db.query(
"SELECT device_address, signing_paths FROM peer_addresses WHERE address=?",
[address],
function(pa_rows) {
var candidate_addresses = [];
for (var i = 0; i < pa_rows.length; i++) {
var row = pa_rows[i];
JSON.parse(row.signing_paths).forEach(function(signing_path_candidate){
if (signing_path_candidate === signing_path)
candidate_addresses.push(row.device_address);
});
}
if (candidate_addresses.length > 1)
throw Error("more than 1 candidate device address found for peer address "+address+" and signing path "+signing_path);
if (candidate_addresses.length == 1)
return callbacks.ifRemote(candidate_addresses[0]);
if (fallback_remote_device_address)
return callbacks.ifRemote(fallback_remote_device_address);
return callbacks.ifUnknownAddress();
}
);
}
);
}
);
}
function readSharedBalance(wallet, handleBalance){
balances.readSharedBalance(wallet, function(assocBalances) {
if (conf.bLight){ // make sure we have all asset definitions available
var arrAssets = Object.keys(assocBalances).filter(function(asset){ return (asset !== 'base'); });
if (arrAssets.length === 0)
return handleBalance(assocBalances);
network.requestProofsOfJointsIfNewOrUnstable(arrAssets, function(){handleBalance(assocBalances)});
} else {
handleBalance(assocBalances);
}
});
}
function readBalance(wallet, handleBalance){
balances.readBalance(wallet, function(assocBalances) {
if (conf.bLight){ // make sure we have all asset definitions available
var arrAssets = Object.keys(assocBalances).filter(function(asset){ return (asset !== 'base'); });
if (arrAssets.length === 0)
return handleBalance(assocBalances);
network.requestProofsOfJointsIfNewOrUnstable(arrAssets, function(){handleBalance(assocBalances)});
} else {
handleBalance(assocBalances);
}
});
}
function readBalancesOnAddresses(walletId, handleBalancesOnAddresses) {
db.query("SELECT outputs.address, COALESCE(outputs.asset, 'base') as asset, sum(outputs.amount) as amount \n\
FROM outputs, my_addresses \n\
WHERE outputs.address = my_addresses.address AND my_addresses.wallet = ? AND outputs.is_spent=0 \n\
GROUP BY outputs.address, outputs.asset \n\
ORDER BY my_addresses.address_index ASC", [walletId], function(rows) {
handleBalancesOnAddresses(rows);
});
}
function readAssetMetadata(arrAssets, handleMetadata){
var sql = "SELECT asset, metadata_unit, name, suffix, decimals FROM asset_metadata";
if (arrAssets && arrAssets.length)
sql += " WHERE asset IN ("+arrAssets.map(db.escape).join(', ')+")";
db.query(sql, function(rows){
var assocAssetMetadata = {};
for (var i=0; i<rows.length; i++){
var row = rows[i];
var asset = row.asset || "base";
assocAssetMetadata[asset] = {
metadata_unit: row.metadata_unit,
decimals: row.decimals,
name: row.suffix ? row.name+'.'+row.suffix : row.name
};
}
handleMetadata(assocAssetMetadata);
// after calling the callback, try to fetch missing data about assets
if (!arrAssets)
return;
var updateAssets = conf.bLight ? network.requestProofsOfJointsIfNewOrUnstable : function(arrAssets, onDone){ onDone(); };
updateAssets(arrAssets, function(){ // make sure we have assets itself
arrAssets.forEach(function(asset){
if (assocAssetMetadata[asset] || asset === 'base' && asset === constants.BLACKBYTES_ASSET)
return;
if ((assocLastFailedAssetMetadataTimestamps[asset] || 0) > Date.now() - ASSET_METADATA_RETRY_PERIOD)
return;
fetchAssetMetadata(asset, function(err, objMetadata){
if (err)
return console.log(err);
assocAssetMetadata[asset] = {
metadata_unit: objMetadata.metadata_unit,
decimals: objMetadata.decimals,
name: objMetadata.suffix ? objMetadata.name+'.'+objMetadata.suffix : objMetadata.name
};
eventBus.emit('maybe_new_transactions');
});
});
});
});
}
function fetchAssetMetadata(asset, handleMetadata){
device.requestFromHub('hub/get_asset_metadata', asset, function(err, response){
if (err){
if (err === 'no metadata')
assocLastFailedAssetMetadataTimestamps[asset] = Date.now();
return handleMetadata("error from get_asset_metadata "+asset+": "+err);
}
var metadata_unit = response.metadata_unit;
var registry_address = response.registry_address;
var suffix = response.suffix;
if (!ValidationUtils.isStringOfLength(metadata_unit, constants.HASH_LENGTH))
return handleMetadata("bad metadata_unit: "+metadata_unit);
if (!ValidationUtils.isValidAddress(registry_address))
return handleMetadata("bad registry_address: "+registry_address);
var fetchMetadataUnit = conf.bLight
? function(onDone){
network.requestProofsOfJointsIfNewOrUnstable([metadata_unit], onDone);
}
: function(onDone){
onDone();
};
fetchMetadataUnit(function(err){
if (err)
return handleMetadata("fetchMetadataUnit failed: "+err);
storage.readJoint(db, metadata_unit, {