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
|
/*
This file is part of TALER
Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors)
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
"use strict";
// Size of a native pointer.
const PTR_SIZE = 4;
const GNUNET_OK = 1;
const GNUNET_YES = 1;
const GNUNET_NO = 0;
const GNUNET_SYSERR = -1;
let getEmsc = (...args) => Module.cwrap.apply(null, args);
var emsc = {
free: (ptr) => Module._free(ptr),
get_value: getEmsc('TALER_WR_get_value', 'number', ['number']),
get_fraction: getEmsc('TALER_WR_get_fraction', 'number', ['number']),
get_currency: getEmsc('TALER_WR_get_currency', 'string', ['number']),
amount_add: getEmsc('TALER_amount_add', 'number', ['number', 'number', 'number']),
amount_subtract: getEmsc('TALER_amount_subtract', 'number', ['number', 'number', 'number']),
amount_normalize: getEmsc('TALER_amount_normalize', 'void', ['number']),
amount_cmp: getEmsc('TALER_amount_cmp', 'number', ['number', 'number']),
amount_hton: getEmsc('TALER_amount_hton', 'void', ['number', 'number']),
amount_ntoh: getEmsc('TALER_amount_ntoh', 'void', ['number', 'number']),
hash: getEmsc('GNUNET_CRYPTO_hash', 'void', ['number', 'number', 'number']),
memmove: getEmsc('memmove', 'number', ['number', 'number', 'number']),
rsa_public_key_free: getEmsc('GNUNET_CRYPTO_rsa_public_key_free', 'void', ['number']),
rsa_signature_free: getEmsc('GNUNET_CRYPTO_rsa_signature_free', 'void', ['number']),
string_to_data: getEmsc('GNUNET_STRINGS_string_to_data', 'number', ['number', 'number', 'number', 'number']),
eddsa_sign: getEmsc('GNUNET_CRYPTO_eddsa_sign', 'number', ['number', 'number', 'number']),
hash_create_random: getEmsc('GNUNET_CRYPTO_hash_create_random', 'void', ['number', 'number']),
rsa_blinding_key_destroy: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_free', 'void', ['number']),
};
var emscAlloc = {
get_amount: getEmsc('TALER_WRALL_get_amount', 'number', ['number', 'number', 'number', 'string']),
eddsa_key_create: getEmsc('GNUNET_CRYPTO_eddsa_key_create', 'number', []),
eddsa_public_key_from_private: getEmsc('TALER_WRALL_eddsa_public_key_from_private', 'number', ['number']),
data_to_string_alloc: getEmsc('GNUNET_STRINGS_data_to_string_alloc', 'number', ['number', 'number']),
purpose_create: getEmsc('TALER_WRALL_purpose_create', 'number', ['number', 'number', 'number']),
rsa_blind: getEmsc('GNUNET_CRYPTO_rsa_blind', 'number', ['number', 'number', 'number', 'number']),
rsa_blinding_key_create: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_create', 'number', ['number']),
rsa_blinding_key_encode: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_encode', 'number', ['number', 'number']),
rsa_signature_encode: getEmsc('GNUNET_CRYPTO_rsa_signature_encode', 'number', ['number', 'number']),
rsa_blinding_key_decode: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_decode', 'number', ['number', 'number']),
rsa_public_key_decode: getEmsc('GNUNET_CRYPTO_rsa_public_key_decode', 'number', ['number', 'number']),
rsa_signature_decode: getEmsc('GNUNET_CRYPTO_rsa_signature_decode', 'number', ['number', 'number']),
rsa_public_key_encode: getEmsc('GNUNET_CRYPTO_rsa_public_key_encode', 'number', ['number', 'number']),
malloc: (size) => Module._malloc(size),
};
var SignaturePurpose;
(function (SignaturePurpose) {
SignaturePurpose[SignaturePurpose["RESERVE_WITHDRAW"] = 1200] = "RESERVE_WITHDRAW";
})(SignaturePurpose || (SignaturePurpose = {}));
var RandomQuality;
(function (RandomQuality) {
RandomQuality[RandomQuality["WEAK"] = 0] = "WEAK";
RandomQuality[RandomQuality["STRONG"] = 1] = "STRONG";
RandomQuality[RandomQuality["NONCE"] = 2] = "NONCE";
})(RandomQuality || (RandomQuality = {}));
class ArenaObject {
constructor(arena) {
this.nativePtr = null;
if (!arena)
arena = defaultArena;
arena.put(this);
this.arena = arena;
}
getNative() {
// We want to allow latent allocation
// of native wrappers, but we never want to
// pass 'undefined' to emscripten.
if (this._nativePtr === undefined) {
throw Error("Native pointer not initialized");
}
return this._nativePtr;
}
free() {
if (this.nativePtr !== undefined) {
emsc.free(this.nativePtr);
this.nativePtr = undefined;
}
}
alloc(size) {
if (this.nativePtr !== undefined) {
throw Error("Double allocation");
}
this.nativePtr = emscAlloc.malloc(size);
}
setNative(n) {
if (n === undefined) {
throw Error("Native pointer must be a number or null");
}
this._nativePtr = n;
}
set nativePtr(v) {
this.setNative(v);
}
get nativePtr() {
return this.getNative();
}
}
class Arena {
constructor() {
this.heap = [];
}
put(obj) {
this.heap.push(obj);
}
destroy() {
// XXX: todo
}
}
// Arena to track allocations that do not use an explicit arena.
var defaultArena = new Arena();
class Amount extends ArenaObject {
constructor(args, arena) {
super(arena);
if (args) {
this.nativePtr = emscAlloc.get_amount(args.value, 0, args.fraction, args.currency);
}
else {
this.nativePtr = emscAlloc.get_amount(0, 0, 0, "");
}
}
destroy() {
if (this.nativePtr != 0) {
emsc.free(this.nativePtr);
}
}
toNbo(a) {
let x = new AmountNbo(a);
x.alloc();
emsc.amount_hton(x.nativePtr, this.nativePtr);
return x;
}
fromNbo(nbo) {
emsc.amount_ntoh(this.nativePtr, nbo.nativePtr);
}
get value() {
return emsc.get_value(this.nativePtr);
}
get fraction() {
return emsc.get_fraction(this.nativePtr);
}
get currency() {
return emsc.get_currency(this.nativePtr);
}
toJson() {
return {
value: emsc.get_value(this.nativePtr),
fraction: emsc.get_fraction(this.nativePtr),
currency: emsc.get_currency(this.nativePtr)
};
}
/**
* Add an amount to this amount.
*/
add(a) {
let res = emsc.amount_add(this.nativePtr, a.nativePtr, this.nativePtr);
if (res < 1) {
// Overflow
return false;
}
return true;
}
/**
* Perform saturating subtraction on amounts.
*/
sub(a) {
// this = this - a
let res = emsc.amount_subtract(this.nativePtr, this.nativePtr, a.nativePtr);
if (res == 0) {
// Underflow
return false;
}
if (res > 0) {
return true;
}
throw Error("Incompatible currencies");
}
cmp(a) {
return emsc.amount_cmp(this.nativePtr, a.nativePtr);
}
normalize() {
emsc.amount_normalize(this.nativePtr);
}
}
class PackedArenaObject extends ArenaObject {
constructor(a) {
super(a);
}
toCrock() {
var d = emscAlloc.data_to_string_alloc(this.nativePtr, this.size());
var s = Module.Pointer_stringify(d);
emsc.free(d);
return s;
}
loadCrock(s) {
this.alloc();
// We need to get the javascript string
// to the emscripten heap first.
let buf = ByteArray.fromString(s);
let res = emsc.string_to_data(buf.nativePtr, s.length, this.nativePtr, this.size());
buf.destroy();
if (res < 1) {
throw { error: "wrong encoding" };
}
}
alloc() {
if (this.nativePtr === null) {
this.nativePtr = emscAlloc.malloc(this.size());
}
}
destroy() {
emsc.free(this.nativePtr);
this.nativePtr = 0;
}
hash() {
var x = new HashCode();
x.alloc();
emsc.hash(this.nativePtr, this.size(), x.nativePtr);
return x;
}
}
class AmountNbo extends PackedArenaObject {
size() { return 24; }
}
class EddsaPrivateKey extends PackedArenaObject {
static create(a) {
let obj = new EddsaPrivateKey(a);
obj.nativePtr = emscAlloc.eddsa_key_create();
return obj;
}
size() { return 32; }
getPublicKey(a) {
let obj = new EddsaPublicKey(a);
obj.nativePtr = emscAlloc.eddsa_public_key_from_private(this.nativePtr);
return obj;
}
}
class EddsaPublicKey extends PackedArenaObject {
size() { return 32; }
}
class RsaBlindingKey extends ArenaObject {
static create(len, a) {
let o = new RsaBlindingKey(a);
o.nativePtr = emscAlloc.rsa_blinding_key_create(len);
return o;
}
toCrock() {
let ptr = emscAlloc.malloc(PTR_SIZE);
let size = emscAlloc.rsa_blinding_key_encode(this.nativePtr, ptr);
let res = new ByteArray(size, Module.getValue(ptr, '*'));
let s = res.toCrock();
emsc.free(ptr);
res.destroy();
return s;
}
destroy() {
// TODO
}
}
class HashCode extends PackedArenaObject {
size() { return 64; }
random(qualStr) {
let qual;
switch (qualStr) {
case "weak":
qual = RandomQuality.WEAK;
break;
case "strong":
case null:
case undefined:
qual = RandomQuality.STRONG;
break;
case "nonce":
qual = RandomQuality.NONCE;
break;
break;
default:
throw Error(format("unknown crypto quality: {0}", qual));
}
this.alloc();
emsc.hash_create_random(qual, this.nativePtr);
}
}
class ByteArray extends PackedArenaObject {
constructor(desiredSize, init, a) {
super(a);
if (init === undefined || init === null) {
this.nativePtr = emscAlloc.malloc(desiredSize);
}
else {
this.nativePtr = init;
}
this.allocatedSize = desiredSize;
}
size() { return this.allocatedSize; }
static fromString(s, a) {
let hstr = emscAlloc.malloc(s.length + 1);
Module.writeStringToMemory(s, hstr);
return new ByteArray(s.length, hstr, a);
}
static fromCrock(s, a) {
let hstr = emscAlloc.malloc(s.length + 1);
Module.writeStringToMemory(s, hstr);
let decodedLen = Math.floor((s.length * 5) / 8);
let ba = new ByteArray(decodedLen, null, a);
let res = emsc.string_to_data(hstr, s.length, ba.nativePtr, decodedLen);
emsc.free(hstr);
if (res != GNUNET_OK) {
throw Error("decoding failed");
}
return ba;
}
}
class EccSignaturePurpose extends PackedArenaObject {
constructor(purpose, payload, a) {
super(a);
this.nativePtr = emscAlloc.purpose_create(purpose, payload.nativePtr, payload.size());
this.payloadSize = payload.size();
}
size() { return this.payloadSize + 8; }
}
class SignatureStruct {
constructor() {
this.members = {};
}
toPurpose(a) {
let totalSize = 0;
for (let f of this.fieldTypes()) {
let name = f[0];
let member = this.members[name];
if (!member) {
throw Error(format("Member {0} not set", name));
}
totalSize += member.size();
}
let buf = emscAlloc.malloc(totalSize);
let ptr = buf;
for (let f of this.fieldTypes()) {
let name = f[0];
let member = this.members[name];
let size = member.size();
emsc.memmove(ptr, member.nativePtr, size);
ptr += size;
}
let ba = new ByteArray(totalSize, buf, a);
let x = new EccSignaturePurpose(this.purpose(), ba);
return x;
}
set(name, value) {
let typemap = {};
for (let f of this.fieldTypes()) {
typemap[f[0]] = f[1];
}
if (!(name in typemap)) {
throw Error(format("Key {0} not found", name));
}
if (!(value instanceof typemap[name])) {
throw Error(format("Wrong type for {0}", name));
}
this.members[name] = value;
}
}
class WithdrawRequestPS extends SignatureStruct {
purpose() { return SignaturePurpose.RESERVE_WITHDRAW; }
fieldTypes() {
return [
["reserve_pub", EddsaPublicKey],
["amount_with_fee", AmountNbo],
["withdraw_fee", AmountNbo],
["h_denomination_pub", HashCode],
["h_coin_envelope", HashCode]];
}
}
function encodeWith(obj, fn, arena) {
let ptr = emscAlloc.malloc(PTR_SIZE);
let len = fn(obj.getNative(), ptr);
let res = new ByteArray(len, null, arena);
res.setNative(Module.getValue(ptr, '*'));
emsc.free(ptr);
return res;
}
class RsaPublicKey extends ArenaObject {
static fromCrock(s, a) {
let obj = new RsaPublicKey(a);
let buf = ByteArray.fromCrock(s);
obj.nativePtr = emscAlloc.rsa_public_key_decode(buf.nativePtr, buf.size());
buf.destroy();
return obj;
}
toCrock() {
return this.encode().toCrock();
}
destroy() {
emsc.rsa_public_key_free(this.nativePtr);
this.nativePtr = 0;
}
encode(arena) {
let ptr = emscAlloc.malloc(PTR_SIZE);
let len = emscAlloc.rsa_public_key_encode(this.nativePtr, ptr);
let res = new ByteArray(len, Module.getValue(ptr, '*'), arena);
emsc.free(ptr);
return res;
}
}
class EddsaSignature extends PackedArenaObject {
size() { return 64; }
}
class RsaSignature extends ArenaObject {
static fromCrock(s, a) {
let obj = new this(a);
let buf = ByteArray.fromCrock(s);
obj.setNative(emscAlloc.rsa_signature_decode(buf.getNative(), buf.size()));
buf.destroy();
return obj;
}
encode(arena) {
return encodeWith(this, emscAlloc.rsa_signature_encode);
}
destroy() {
emsc.rsa_signature_free(this.getNative());
this.setNative(0);
}
}
function rsaBlind(hashCode, blindingKey, pkey, arena) {
let ptr = emscAlloc.malloc(PTR_SIZE);
let s = emscAlloc.rsa_blind(hashCode.nativePtr, blindingKey.nativePtr, pkey.nativePtr, ptr);
let res = new ByteArray(s, Module.getValue(ptr, '*'), arena);
return res;
}
function eddsaSign(purpose, priv, a) {
let sig = new EddsaSignature(a);
sig.alloc();
let res = emsc.eddsa_sign(priv.nativePtr, purpose.nativePtr, sig.nativePtr);
if (res < 1) {
throw Error("EdDSA signing failed");
}
return sig;
}
function rsaUnblind(sig, bk, pk) {
throw Error("Not implemented");
}
|