-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-pathjoin.js
509 lines (375 loc) · 15.1 KB
/
test-pathjoin.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
/////////////////////////////////////
// Proof-of-concept implementation for PathJoin
// Author: Pedro Moreno-Sanchez
// Email: [email protected]
//
// Note: This implementation is insecure! Do not use it with your credit!
// This implementation has been written purely to evaluate the feasibility of PathJoin.
//Installation instructions:
// 1. Install NodeJS and the Node Package Manager (npm). Most Linux distros have a package for NodeJS, but make sure you have version 0.12.0 or higher.
// 2. Use npm to install Babel globally: npm install -g babel
// 3. Use npm to install RippleAPI: npm install ripple-lib
// 4. Use npm to install all other dependencies required in this script
// 5. Copy the index.js file into node_modules/elliptic/lib/elliptic/eddsa/ (it contains the implementation of distributed signature algorithm)
// 6. Fill the fields in the script with the wallets in your experiment.
// 7. You can run the example with : babel-node test-pathjoin.js
'use strict';
const elliptic = require('elliptic');
const Ed25519 = elliptic.eddsa('ed25519');
const hashjs = require('hash.js');
const RippleAPI = require('ripple-lib').RippleAPI;
const keypairs = require('ripple-keypairs');
const binary = require('ripple-binary-codec');
const assert = require('assert');
var addressCodec = require('ripple-address-codec');
const api = new RippleAPI({server: 'wss://s1.ripple.com:443'}); //Connect to a Ripple server
const instructions = {maxLedgerVersionOffset: 5, fee: '0.012'}; //Set the configuration
//Users input wallets for the input shared wallet (Win)
// You need to create a fresh wallet for each of the users
// Current test has only 5 users. It can easily be extended for more users
const wala_in_add = '';
const wala_in_secret = '';
const walb_in_add = '';
const walb_in_secret = '';
const walc_in_add = '';
const walc_in_secret = '';
const wald_in_add = '';
const wald_in_secret = '';
const wale_in_add = '';
const wale_in_secret = '';
//Users input wallets for the output shared (Wout)
// You need to create a fresh wallet for each of the users
// Current test has only 5 users. It can easily be extended for more users
const wala_out_add = '';
const wala_out_secret = '';
const walb_out_add = '';
const walb_out_secret = '';
const walc_out_add = '';
const walc_out_secret = '';
const wald_out_add = '';
const wald_out_secret = '';
const wale_out_add = '';
const wale_out_secret = '';
//Users input wallets
// You need to set these wallets whith those ones where users have
//their IOU to be mixed
const ain_add = '';
const ain_secret = '';
const bin_add = '';
const bin_secret = '';
const cin_add = '';
const cin_secret = '';
const din_add = '';
const din_secret = '';
const ein_add = '';
const ein_secret = '';
//Users output wallets
//You need to set these wallets whith those ones where users will receive
//their mixed IOU
const aout_add = '';
const aout_secret = '';
const bout_add = '';
const bout_secret = '';
const cout_add = '';
const cout_secret = '';
const dout_add = '';
const dout_secret = '';
const eout_add = '';
const eout_secret = '';
//Gateway wallet
const gw_add = '';
///////
//Variables
const w_in = 1 // To notify functions to do operations regarding input shared wallet
const w_out = 2 // To notify functions to do operations regarding output shared wallet
//Include here the shared input wallet. This can be created with calculateAddress function
const win_add = '';
//Include here the shared output wallet. This can be created with calculateAddress function
const wout_add = '';
//////////
//Transactions structures
//Payment in XRP
const payment = {
source: {
address: '', //source wallet
maxAmount: {
value: '', //source amount
currency: 'XRP'
}
},
destination: {
address: , //destination wallet
amount: {
value: '', //destination amount (most of the cases is the same as source amount)
currency: 'XRP'
}
}
};
const settings = {
"defaultRipple" : true
};
//Create a trust line
const trustline = {
"currency": "", //curenncy (3 letters)
"counterparty": "", //counterparty in the credit link
"limit": "0", //upper limit in the credit link
"qualityIn": 1.0, // change these values to set fees for the transaction
"qualityOut": 1.0,
"ripplingDisabled": true, //Disabled rippling by default (see paper for details)
"frozen": false, //See Ripple documentation for details about this field
"memos": [ //Memo field (can be set to empty)
{
"type": "PathShuffletest",
"format": "plain/text",
"data": "texted data"
}
]
};
//Create a IOU payment
const payment_path = {
"source": {
"address": '', //Source wallet
"maxAmount": {
"value": "", //Source amount
"currency": "", //Source currency
"counterparty": "" //First wallet in the payment path
}
},
"destination": {
"address": "", //Destionation wallet
"amount": {
"value": "", //Destionation amount (the same as source amount if no fees)
"currency": "", //Destination currency (the same as source currency)
"counterparty": "" //The wallet before the destination in payment path
}
},
"paths": '' //Path field as obtained from getpaths function in ripple-lib (see https://github.com/ripple/ripple-lib/blob/develop/docs/index.md)
};
//Structure to call getpaths in ripple-lib (see https://github.com/ripple/ripple-lib/blob/develop/docs/index.md)
const pathfind = {
"source": {
"address": "" //source wallet
},
"destination": {
"address": "", //destination wallet
"amount": {
"currency": "PSH", //destination currency
"value" : "41", //destination amount
"counterparty" : "" //last hop before destination in the payment path
}
}
};
//////Auxiliary functions
function hash(message) {
return hashjs.sha512().update(message).digest().slice(0, 32);
}
function bytesToHex(a) {
return a.map(function(byteValue) {
const hex = byteValue.toString(16).toUpperCase();
return hex.length > 1 ? hex : '0' + hex;
}).join('');
}
function quit(message) {
console.log(message);
process.exit(0);
}
function fail(message) {
console.error(message);
console.log("Disconnecting");
api.disconnect();
process.exit(1);
}
//Generate a fresh key from a secret string
function generateKey(secret_param) {
const priv = keypairs.deriveKeypair(secret_param).privateKey;
const raw = keypairs.myHexToBytes(priv).slice(1);
const key = Ed25519.keyFromSecret(raw);
return key;
}
//Calculate the id of a shared wallet
function calculateAddress(wallet_id) {
if (wallet_id == w_in) {
const secret1 = wala_in_secret;
const secret2 = walb_in_secret;
const secret3 = walc_in_secret;
const secret4 = wald_in_secret;
const secret5 = wale_in_secret;
const key1 = generateKey(secret1);
const key2 = generateKey(secret2);
const key3 = generateKey(secret3);
const key4 = generateKey(secret4);
const key5 = generateKey(secret5);
var key_all_pub = key1.pub().add(key2.pub().add(key3.pub().add(key4.pub().add(key5.pub()))));
var key_total = Ed25519.keyFromPublic(key_all_pub);
var my_address = keypairs.deriveAddress('ED' + bytesToHex(key_total.pubBytes()));
console.log("address for shared wallet is");
console.log(my_address);
}
else if (wallet_id == w_out) {
const secret1 = wala_out_secret;
const secret2 = walb_out_secret;
const secret3 = walc_out_secret;
const secret4 = wald_out_secret;
const secret5 = wale_out_secret;
const key1 = generateKey(secret1);
const key2 = generateKey(secret2);
const key3 = generateKey(secret3);
const key4 = generateKey(secret4);
const key5 = generateKey(secret5);
var key_all_pub = key1.pub().add(key2.pub().add(key3.pub().add(key4.pub().add(key5.pub()))));
var key_total = Ed25519.keyFromPublic(key_all_pub);
var my_address = keypairs.deriveAddress('ED' + bytesToHex(key_total.pubBytes()));
console.log("address for shared wallet is");
console.log(my_address);
}
}
//Compute the signature on a transaction involving a shared wallet
function computeSignature(wallet_id, txJSON) {
if (wallet_id == w_in) {
console.log ("Signing a transaction for Wallet in");
const random_1 = Ed25519.computeR(Math.random().toString());
const random_2 = Ed25519.computeR(Math.random().toString());
const random_3 = Ed25519.computeR(Math.random().toString());
const random_4 = Ed25519.computeR(Math.random().toString());
const random_5 = Ed25519.computeR(Math.random().toString());
var random_all = random_1.R.add(random_2.R.add(random_3.R.add(random_4.R.add(random_5.R))));
const secret1 = wala_in_secret;
const secret2 = walb_in_secret;
const secret3 = walc_in_secret;
const secret4 = wald_in_secret;
const secret5 = wale_in_secret;
var key1 = generateKey(secret1);
var key2 = generateKey(secret2);
var key3 = generateKey(secret3);
var key4 = generateKey(secret4);
var key5 = generateKey(secret5);
var key_all_pub = key1.pub().add(key2.pub().add(key3.pub().add(key4.pub().add(key5.pub()))));
var key_total = Ed25519.keyFromPublic(key_all_pub);
txJSON.SigningPubKey = 'ED' + bytesToHex(key_total.pubBytes());
var signingData = keypairs.myHexToBytes(binary.encodeForSigning(txJSON));
var my_address = keypairs.deriveAddress('ED' + bytesToHex(key_total.pubBytes()));
console.log("Win address is");
console.log(my_address);
var share_1 = Ed25519.sign_share(signingData, key1.priv(), random_1.r, random_all, key_total.pubBytes());
var share_2 = Ed25519.sign_share(signingData, key2.priv(), random_2.r, random_all, key_total.pubBytes());
var share_3 = Ed25519.sign_share(signingData, key3.priv(), random_3.r, random_all, key_total.pubBytes());
var share_4 = Ed25519.sign_share(signingData, key4.priv(), random_4.r, random_all, key_total.pubBytes());
var share_5 = Ed25519.sign_share(signingData, key5.priv(), random_5.r, random_all, key_total.pubBytes());
var total_s = Ed25519.add_shares(share_1, Ed25519.add_shares(share_2, Ed25519.add_shares(share_3, Ed25519.add_shares(share_4, share_5))));
var signature = Ed25519.encodeSignature(random_all, total_s);
console.log("Signature verification");
console.log(Ed25519.verify(signingData, signature, key_total));
return signature;
}
else if (wallet_id == w_out) {
console.log ("Signing a transaction for Wallet out");
const random_1 = Ed25519.computeR(Math.random().toString());
const random_2 = Ed25519.computeR(Math.random().toString());
const random_3 = Ed25519.computeR(Math.random().toString());
const random_4 = Ed25519.computeR(Math.random().toString());
const random_5 = Ed25519.computeR(Math.random().toString());
var random_all = random_1.R.add(random_2.R.add(random_3.R.add(random_4.R.add(random_5.R))));
const secret1 = wala_out_secret;
const secret2 = walb_out_secret;
const secret3 = walc_out_secret;
const secret4 = wald_out_secret;
const secret5 = wale_out_secret;
const key1 = generateKey(secret1);
const key2 = generateKey(secret2);
const key3 = generateKey(secret3);
const key4 = generateKey(secret4);
const key5 = generateKey(secret5);
var key_all_pub = key1.pub().add(key2.pub().add(key3.pub().add(key4.pub().add(key5.pub()))));
var key_total = Ed25519.keyFromPublic(key_all_pub);
txJSON.SigningPubKey = 'ED' + bytesToHex(key_total.pubBytes());
const signingData = keypairs.myHexToBytes(binary.encodeForSigning(txJSON));
var my_address = keypairs.deriveAddress('ED' + bytesToHex(key_total.pubBytes()));
console.log("Wout address is");
console.log(my_address);
const share_1 = Ed25519.sign_share(signingData, key1.priv(), random_1.r, random_all, key_total.pubBytes());
const share_2 = Ed25519.sign_share(signingData, key2.priv(), random_2.r, random_all, key_total.pubBytes());
const share_3 = Ed25519.sign_share(signingData, key3.priv(), random_3.r, random_all, key_total.pubBytes());
const share_4 = Ed25519.sign_share(signingData, key4.priv(), random_4.r, random_all, key_total.pubBytes());
const share_5 = Ed25519.sign_share(signingData, key5.priv(), random_5.r, random_all, key_total.pubBytes());
var total_s = Ed25519.add_shares(share_1, Ed25519.add_shares(share_2, Ed25519.add_shares(share_3, Ed25519.add_shares(share_4, share_5))));
console.log(total_s);
const signature = Ed25519.encodeSignature(random_all, total_s);
console.log("Signature verification");
console.log(Ed25519.verify(signingData, signature, key_total));
return signature;
}
}
//Wrapper function to sign a transaction using a shared wallet
function wrapper_sign(wallet_id, txJSON) {
const tx = JSON.parse(txJSON);
tx.TxnSignature = computeSignature(wallet_id, tx);
const serialized = binary.encode(tx);
return {
signedTransaction: serialized,
id: serialized
};
}
///////Main function
//First connect to the server
api.connect().then(() => {
//Useful ripple-lib calls to track the state of the network
/* console.log('Connected...');
return api.getSettings(wtwo_add).then(settings => {
console.log(settings);
console.log(JSON.stringify(settings, null, 2));
quit("Done!");
});
*/
/* console.log('Connected...');
return api.getBalances(wout_add).then(balances => {
console.log(JSON.stringify(balances, null, 2));
quit("Done!");
});
*/
/* console.log('Connected...');
return api.getTrustlines(win_add).then(trustlines =>{
console.log(JSON.stringify(trustlines, null, 2));
quit("Done!");
});
*/
/*
console.log('Connected...');
return api.getPaths(pathfind)
.then(paths => {
console.log(paths)
quit("Done!");
});
*/
////Use this piece of code to submit a transaction with a non-shared wallet
//return api.preparePayment(wallet, payment, instructions).then(prepared => {
//api.submit(api.sign(prepared.txJSON, ein_secret)["signedTransaction"]).then(quit, fail);
//});
////Use this piece of code to set a link with a non-shared wallet
//return api.prepareTrustline(wallet, trustline).then(prepared => {
//api.submit(api.sign(prepared.txJSON, ein_secret)["signedTransaction"]).then(quit, fail);
//});
////Use this piece of code to submit a transaction with a shared wallet
return api.preparePayment(win_add, payment_path, instructions).then(prepared => {
console.log('Payment transaction prepared...');
console.log('Transaction');
console.log(prepared.txJSON);
const signedTransaction = wrapper_sign(w_in, prepared.txJSON);
console.log('Payment transaction signed...');
console.log(binary.decode(signedTransaction["signedTransaction"]));
console.log("signature is");
console.log(signedTransaction["signedTransaction"]);
api.submit(signedTransaction["signedTransaction"]).then(quit, fail);
});
////Use this piece of code to set a credit link with a shared wallet
/*return api.prepareTrustline(wallet, trustline).then(prepared => {
console.log('Payment transaction prepared...');
console.log('Transaction');
console.log(prepared.txJSON);
const signedTransaction = wrapper_sign(w_in, prepared.txJSON);
console.log('Payment transaction signed...');
console.log(binary.decode(signedTransaction["signedTransaction"]));
console.log("signature is");
console.log(signedTransaction["signedTransaction"]);
api.submit(signedTransaction["signedTransaction"]).then(quit, fail);
});*/
}).catch(fail);