2015-12-04 22:47:44 +01:00
|
|
|
/*
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
This file is part of TALER
|
|
|
|
Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors)
|
2015-12-04 22:47:44 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
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.
|
2015-12-04 22:47:44 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
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.
|
2015-12-04 22:47:44 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
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/>
|
|
|
|
*/
|
2015-12-07 23:39:25 +01:00
|
|
|
"use strict";
|
2015-12-13 23:47:30 +01:00
|
|
|
// Size of a native pointer.
|
2015-12-13 18:10:33 +01:00
|
|
|
const PTR_SIZE = 4;
|
2015-12-14 16:54:47 +01:00
|
|
|
const GNUNET_OK = 1;
|
|
|
|
const GNUNET_YES = 1;
|
|
|
|
const GNUNET_NO = 0;
|
|
|
|
const GNUNET_SYSERR = -1;
|
2015-12-16 00:38:36 +01:00
|
|
|
let getEmsc = (...args) => Module.cwrap.apply(null, args);
|
2015-12-13 23:47:30 +01:00
|
|
|
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']),
|
2015-12-16 00:38:36 +01:00
|
|
|
amount_add: getEmsc('TALER_amount_add', 'number', ['number', 'number', 'number']),
|
|
|
|
amount_subtract: getEmsc('TALER_amount_subtract', 'number', ['number', 'number', 'number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
amount_normalize: getEmsc('TALER_amount_normalize', 'void', ['number']),
|
2015-12-17 22:56:24 +01:00
|
|
|
amount_get_zero: getEmsc('TALER_amount_get_zero', 'number', ['string', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
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']),
|
2015-12-16 00:38:36 +01:00
|
|
|
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']),
|
2015-12-14 16:54:47 +01:00
|
|
|
eddsa_sign: getEmsc('GNUNET_CRYPTO_eddsa_sign', 'number', ['number', 'number', 'number']),
|
|
|
|
hash_create_random: getEmsc('GNUNET_CRYPTO_hash_create_random', 'void', ['number', 'number']),
|
2015-12-16 00:38:36 +01:00
|
|
|
rsa_blinding_key_destroy: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_free', 'void', ['number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
};
|
|
|
|
var emscAlloc = {
|
|
|
|
get_amount: getEmsc('TALER_WRALL_get_amount', 'number', ['number', 'number', 'number', 'string']),
|
2015-12-16 00:38:36 +01:00
|
|
|
eddsa_key_create: getEmsc('GNUNET_CRYPTO_eddsa_key_create', 'number', []),
|
2015-12-13 23:47:30 +01:00
|
|
|
eddsa_public_key_from_private: getEmsc('TALER_WRALL_eddsa_public_key_from_private', 'number', ['number']),
|
2015-12-14 05:36:06 +01:00
|
|
|
data_to_string_alloc: getEmsc('GNUNET_STRINGS_data_to_string_alloc', 'number', ['number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
purpose_create: getEmsc('TALER_WRALL_purpose_create', 'number', ['number', 'number', 'number']),
|
|
|
|
rsa_blind: getEmsc('GNUNET_CRYPTO_rsa_blind', 'number', ['number', 'number', 'number', 'number']),
|
2015-12-16 00:38:36 +01:00
|
|
|
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']),
|
2015-12-14 08:54:59 +01:00
|
|
|
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']),
|
2015-12-16 00:38:36 +01:00
|
|
|
rsa_signature_decode: getEmsc('GNUNET_CRYPTO_rsa_signature_decode', 'number', ['number', 'number']),
|
2015-12-14 16:54:47 +01:00
|
|
|
rsa_public_key_encode: getEmsc('GNUNET_CRYPTO_rsa_public_key_encode', 'number', ['number', 'number']),
|
2015-12-16 05:53:55 +01:00
|
|
|
rsa_unblind: getEmsc('GNUNET_CRYPTO_rsa_unblind', 'number', ['number', 'number', 'number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
malloc: (size) => Module._malloc(size),
|
|
|
|
};
|
2015-12-14 08:26:38 +01:00
|
|
|
var SignaturePurpose;
|
|
|
|
(function (SignaturePurpose) {
|
2015-12-14 16:54:47 +01:00
|
|
|
SignaturePurpose[SignaturePurpose["RESERVE_WITHDRAW"] = 1200] = "RESERVE_WITHDRAW";
|
2015-12-17 22:56:24 +01:00
|
|
|
SignaturePurpose[SignaturePurpose["WALLET_COIN_DEPOSIT"] = 1201] = "WALLET_COIN_DEPOSIT";
|
2015-12-14 08:26:38 +01:00
|
|
|
})(SignaturePurpose || (SignaturePurpose = {}));
|
2015-12-14 16:54:47 +01:00
|
|
|
var RandomQuality;
|
|
|
|
(function (RandomQuality) {
|
|
|
|
RandomQuality[RandomQuality["WEAK"] = 0] = "WEAK";
|
|
|
|
RandomQuality[RandomQuality["STRONG"] = 1] = "STRONG";
|
|
|
|
RandomQuality[RandomQuality["NONCE"] = 2] = "NONCE";
|
|
|
|
})(RandomQuality || (RandomQuality = {}));
|
2015-12-13 23:47:30 +01:00
|
|
|
class ArenaObject {
|
|
|
|
constructor(arena) {
|
2015-12-14 16:54:47 +01:00
|
|
|
this.nativePtr = null;
|
2015-12-16 05:53:55 +01:00
|
|
|
if (!arena) {
|
|
|
|
if (arenaStack.length == 0) {
|
|
|
|
throw Error("No arena available");
|
|
|
|
}
|
|
|
|
arena = arenaStack[arenaStack.length - 1];
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
arena.put(this);
|
|
|
|
this.arena = arena;
|
|
|
|
}
|
2015-12-16 00:38:36 +01:00
|
|
|
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();
|
|
|
|
}
|
2015-12-04 22:47:44 +01:00
|
|
|
}
|
2015-12-16 05:53:55 +01:00
|
|
|
class DefaultArena {
|
2015-12-13 23:47:30 +01:00
|
|
|
constructor() {
|
|
|
|
this.heap = [];
|
|
|
|
}
|
|
|
|
put(obj) {
|
|
|
|
this.heap.push(obj);
|
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
destroy() {
|
2015-12-16 05:53:55 +01:00
|
|
|
for (let obj of this.heap) {
|
|
|
|
obj.destroy();
|
|
|
|
}
|
|
|
|
this.heap = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Arena that destroys all its objects once control has returned to the message
|
|
|
|
* loop and a small interval has passed.
|
|
|
|
*/
|
|
|
|
class SyncArena extends DefaultArena {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let me = this;
|
|
|
|
this.timer = new Worker('background/timerThread.js');
|
2015-12-17 22:56:24 +01:00
|
|
|
this.timer.onmessage = () => {
|
2015-12-16 05:53:55 +01:00
|
|
|
this.destroy();
|
|
|
|
};
|
|
|
|
//this.timer.postMessage({interval: 50});
|
|
|
|
}
|
|
|
|
destroy() {
|
|
|
|
super.destroy();
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-04 22:47:44 +01:00
|
|
|
}
|
2015-12-16 05:53:55 +01:00
|
|
|
let arenaStack = [];
|
|
|
|
arenaStack.push(new SyncArena());
|
2015-12-13 23:47:30 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
static getZero(currency, a) {
|
|
|
|
let am = new Amount(null, a);
|
|
|
|
let r = emsc.amount_get_zero(currency, am.getNative());
|
|
|
|
if (r != GNUNET_OK) {
|
|
|
|
throw Error("invalid currency");
|
|
|
|
}
|
|
|
|
return am;
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
toNbo(a) {
|
2015-12-14 09:39:02 +01:00
|
|
|
let x = new AmountNbo(a);
|
|
|
|
x.alloc();
|
2015-12-14 08:26:38 +01:00
|
|
|
emsc.amount_hton(x.nativePtr, this.nativePtr);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
fromNbo(nbo) {
|
|
|
|
emsc.amount_ntoh(this.nativePtr, nbo.nativePtr);
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
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) {
|
2015-12-14 11:26:55 +01:00
|
|
|
// this = this - a
|
|
|
|
let res = emsc.amount_subtract(this.nativePtr, this.nativePtr, a.nativePtr);
|
2015-12-13 23:47:30 +01:00
|
|
|
if (res == 0) {
|
|
|
|
// Underflow
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (res > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-14 11:26:55 +01:00
|
|
|
throw Error("Incompatible currencies");
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
cmp(a) {
|
|
|
|
return emsc.amount_cmp(this.nativePtr, a.nativePtr);
|
|
|
|
}
|
|
|
|
normalize() {
|
|
|
|
emsc.amount_normalize(this.nativePtr);
|
|
|
|
}
|
2015-12-07 23:39:25 +01:00
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
class PackedArenaObject extends ArenaObject {
|
2015-12-14 09:39:02 +01:00
|
|
|
constructor(a) {
|
2015-12-14 08:26:38 +01:00
|
|
|
super(a);
|
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
toCrock() {
|
2015-12-14 08:26:38 +01:00
|
|
|
var d = emscAlloc.data_to_string_alloc(this.nativePtr, this.size());
|
2015-12-14 05:36:06 +01:00
|
|
|
var s = Module.Pointer_stringify(d);
|
|
|
|
emsc.free(d);
|
|
|
|
return s;
|
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
loadCrock(s) {
|
2015-12-14 09:39:02 +01:00
|
|
|
this.alloc();
|
2015-12-14 08:26:38 +01:00
|
|
|
// We need to get the javascript string
|
|
|
|
// to the emscripten heap first.
|
2015-12-14 09:39:02 +01:00
|
|
|
let buf = ByteArray.fromString(s);
|
|
|
|
let res = emsc.string_to_data(buf.nativePtr, s.length, this.nativePtr, this.size());
|
|
|
|
buf.destroy();
|
2015-12-14 08:26:38 +01:00
|
|
|
if (res < 1) {
|
|
|
|
throw { error: "wrong encoding" };
|
|
|
|
}
|
2015-12-14 05:36:06 +01:00
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
alloc() {
|
|
|
|
if (this.nativePtr === null) {
|
|
|
|
this.nativePtr = emscAlloc.malloc(this.size());
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
destroy() {
|
2015-12-14 08:26:38 +01:00
|
|
|
emsc.free(this.nativePtr);
|
|
|
|
this.nativePtr = 0;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
hash() {
|
|
|
|
var x = new HashCode();
|
2015-12-14 16:54:47 +01:00
|
|
|
x.alloc();
|
2015-12-14 08:26:38 +01:00
|
|
|
emsc.hash(this.nativePtr, this.size(), x.nativePtr);
|
|
|
|
return x;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
class AmountNbo extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 24;
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
class EddsaPrivateKey extends PackedArenaObject {
|
|
|
|
static create(a) {
|
2015-12-14 09:39:02 +01:00
|
|
|
let obj = new EddsaPrivateKey(a);
|
|
|
|
obj.nativePtr = emscAlloc.eddsa_key_create();
|
|
|
|
return obj;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 32;
|
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
getPublicKey(a) {
|
|
|
|
let obj = new EddsaPublicKey(a);
|
|
|
|
obj.nativePtr = emscAlloc.eddsa_public_key_from_private(this.nativePtr);
|
|
|
|
return obj;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
mixinStatic(EddsaPrivateKey, fromCrock);
|
|
|
|
function fromCrock(s) {
|
|
|
|
let x = new this();
|
|
|
|
x.alloc();
|
|
|
|
x.loadCrock(s);
|
|
|
|
return x;
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
function mixin(obj, method, name) {
|
|
|
|
if (!name) {
|
|
|
|
name = method.name;
|
2015-12-14 08:54:59 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
if (!name) {
|
|
|
|
throw Error("Mixin needs a name.");
|
|
|
|
}
|
|
|
|
console.log("mixing in", name, "into", obj.name);
|
|
|
|
obj.prototype[method.name] = method;
|
|
|
|
}
|
|
|
|
function mixinStatic(obj, method, name) {
|
|
|
|
if (!name) {
|
|
|
|
name = method.name;
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
throw Error("Mixin needs a name.");
|
|
|
|
}
|
|
|
|
console.log("mixing in", name, "into", obj.name);
|
|
|
|
obj[method.name] = method;
|
|
|
|
}
|
|
|
|
class EddsaPublicKey extends PackedArenaObject {
|
|
|
|
size() {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mixinStatic(EddsaPublicKey, fromCrock);
|
|
|
|
function makeFromCrock(decodeFn) {
|
|
|
|
function fromCrock(s, a) {
|
2015-12-17 13:30:34 +01:00
|
|
|
let obj = new this(a);
|
|
|
|
let buf = ByteArray.fromCrock(s);
|
2015-12-17 22:56:24 +01:00
|
|
|
obj.setNative(decodeFn(buf.getNative(), buf.size()));
|
2015-12-17 13:30:34 +01:00
|
|
|
buf.destroy();
|
|
|
|
return obj;
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
return fromCrock;
|
|
|
|
}
|
|
|
|
function makeToCrock(encodeFn) {
|
|
|
|
function toCrock() {
|
2015-12-14 08:54:59 +01:00
|
|
|
let ptr = emscAlloc.malloc(PTR_SIZE);
|
|
|
|
let size = emscAlloc.rsa_blinding_key_encode(this.nativePtr, ptr);
|
|
|
|
let res = new ByteArray(size, Module.getValue(ptr, '*'));
|
2015-12-14 16:54:47 +01:00
|
|
|
let s = res.toCrock();
|
2015-12-14 08:54:59 +01:00
|
|
|
emsc.free(ptr);
|
|
|
|
res.destroy();
|
|
|
|
return s;
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
return toCrock;
|
|
|
|
}
|
|
|
|
class RsaBlindingKey extends ArenaObject {
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
|
|
|
this.toCrock = makeToCrock(emscAlloc.rsa_blinding_key_encode);
|
|
|
|
}
|
|
|
|
static create(len, a) {
|
|
|
|
let o = new RsaBlindingKey(a);
|
|
|
|
o.nativePtr = emscAlloc.rsa_blinding_key_create(len);
|
|
|
|
return o;
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
destroy() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
mixinStatic(RsaBlindingKey, makeFromCrock(emscAlloc.rsa_blinding_key_decode));
|
2015-12-14 08:26:38 +01:00
|
|
|
class HashCode extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 64;
|
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
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);
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
mixinStatic(HashCode, fromCrock);
|
2015-12-14 08:26:38 +01:00
|
|
|
class ByteArray extends PackedArenaObject {
|
|
|
|
constructor(desiredSize, init, a) {
|
2015-12-14 09:39:02 +01:00
|
|
|
super(a);
|
2015-12-14 08:26:38 +01:00
|
|
|
if (init === undefined || init === null) {
|
|
|
|
this.nativePtr = emscAlloc.malloc(desiredSize);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.nativePtr = init;
|
|
|
|
}
|
|
|
|
this.allocatedSize = desiredSize;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return this.allocatedSize;
|
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
static fromString(s, a) {
|
|
|
|
let hstr = emscAlloc.malloc(s.length + 1);
|
|
|
|
Module.writeStringToMemory(s, hstr);
|
|
|
|
return new ByteArray(s.length, hstr, a);
|
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
class EccSignaturePurpose extends PackedArenaObject {
|
|
|
|
constructor(purpose, payload, a) {
|
2015-12-14 09:39:02 +01:00
|
|
|
super(a);
|
|
|
|
this.nativePtr = emscAlloc.purpose_create(purpose, payload.nativePtr, payload.size());
|
2015-12-14 16:54:47 +01:00
|
|
|
this.payloadSize = payload.size();
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return this.payloadSize + 8;
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
class SignatureStruct {
|
2015-12-16 10:45:16 +01:00
|
|
|
constructor(x) {
|
2015-12-14 08:26:38 +01:00
|
|
|
this.members = {};
|
2015-12-16 10:45:16 +01:00
|
|
|
for (let k in x) {
|
2015-12-16 10:53:29 +01:00
|
|
|
this.set(k, x[k]);
|
2015-12-16 10:45:16 +01:00
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
toPurpose(a) {
|
|
|
|
let totalSize = 0;
|
|
|
|
for (let f of this.fieldTypes()) {
|
|
|
|
let name = f[0];
|
|
|
|
let member = this.members[name];
|
|
|
|
if (!member) {
|
2015-12-14 11:26:55 +01:00
|
|
|
throw Error(format("Member {0} not set", name));
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
totalSize += member.size();
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
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);
|
2015-12-17 22:56:24 +01:00
|
|
|
return new EccSignaturePurpose(this.purpose(), ba);
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
set(name, value) {
|
|
|
|
let typemap = {};
|
|
|
|
for (let f of this.fieldTypes()) {
|
|
|
|
typemap[f[0]] = f[1];
|
|
|
|
}
|
|
|
|
if (!(name in typemap)) {
|
2015-12-14 16:54:47 +01:00
|
|
|
throw Error(format("Key {0} not found", name));
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
if (!(value instanceof typemap[name])) {
|
2015-12-14 16:54:47 +01:00
|
|
|
throw Error(format("Wrong type for {0}", name));
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
this.members[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class WithdrawRequestPS extends SignatureStruct {
|
2015-12-16 10:45:16 +01:00
|
|
|
constructor(w) {
|
|
|
|
super(w);
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
purpose() {
|
|
|
|
return SignaturePurpose.RESERVE_WITHDRAW;
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
fieldTypes() {
|
|
|
|
return [
|
|
|
|
["reserve_pub", EddsaPublicKey],
|
2015-12-14 16:54:47 +01:00
|
|
|
["amount_with_fee", AmountNbo],
|
|
|
|
["withdraw_fee", AmountNbo],
|
2015-12-14 08:26:38 +01:00
|
|
|
["h_denomination_pub", HashCode],
|
2015-12-17 22:56:24 +01:00
|
|
|
["h_coin_envelope", HashCode]
|
|
|
|
];
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
class AbsoluteTimeNbo extends PackedArenaObject {
|
|
|
|
static fromTalerString(s) {
|
|
|
|
throw Error();
|
|
|
|
}
|
|
|
|
size() {
|
|
|
|
return 8;
|
|
|
|
}
|
2015-12-16 00:38:36 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
class UInt64 extends PackedArenaObject {
|
|
|
|
static fromNumber(n) {
|
|
|
|
throw Error();
|
|
|
|
}
|
|
|
|
size() {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class DepositRequestPS extends SignatureStruct {
|
|
|
|
constructor(w) {
|
|
|
|
super(w);
|
|
|
|
}
|
|
|
|
purpose() {
|
|
|
|
return SignaturePurpose.WALLET_COIN_DEPOSIT;
|
|
|
|
}
|
|
|
|
fieldTypes() {
|
|
|
|
return [
|
|
|
|
["h_contract", HashCode],
|
|
|
|
["h_wire", HashCode],
|
|
|
|
["timestamp", AbsoluteTimeNbo],
|
|
|
|
["refund_deadline", AbsoluteTimeNbo],
|
|
|
|
["transaction_id", UInt64],
|
|
|
|
["amount_with_fee", AmountNbo],
|
|
|
|
["deposit_fee", AmountNbo],
|
|
|
|
["merchant", EddsaPublicKey],
|
|
|
|
["coin_pub", EddsaPublicKey],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function makeEncode(encodeFn) {
|
|
|
|
function encode(arena) {
|
|
|
|
let ptr = emscAlloc.malloc(PTR_SIZE);
|
|
|
|
let len = encodeFn(this.getNative(), ptr);
|
|
|
|
let res = new ByteArray(len, null, arena);
|
|
|
|
res.setNative(Module.getValue(ptr, '*'));
|
|
|
|
emsc.free(ptr);
|
|
|
|
return res;
|
2015-12-14 08:54:59 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
return encode;
|
|
|
|
}
|
|
|
|
class RsaPublicKey extends ArenaObject {
|
2015-12-14 16:54:47 +01:00
|
|
|
toCrock() {
|
|
|
|
return this.encode().toCrock();
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
destroy() {
|
2015-12-14 08:26:38 +01:00
|
|
|
emsc.rsa_public_key_free(this.nativePtr);
|
|
|
|
this.nativePtr = 0;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
mixinStatic(RsaPublicKey, makeFromCrock(emscAlloc.rsa_public_key_decode));
|
|
|
|
mixin(RsaPublicKey, makeEncode(emscAlloc.rsa_public_key_encode));
|
2015-12-14 09:39:02 +01:00
|
|
|
class EddsaSignature extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 64;
|
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
}
|
2015-12-16 00:38:36 +01:00
|
|
|
class RsaSignature extends ArenaObject {
|
|
|
|
destroy() {
|
|
|
|
emsc.rsa_signature_free(this.getNative());
|
|
|
|
this.setNative(0);
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
mixinStatic(RsaSignature, makeFromCrock(emscAlloc.rsa_signature_decode));
|
|
|
|
mixin(RsaSignature, makeEncode(emscAlloc.rsa_signature_encode));
|
2015-12-13 23:47:30 +01:00
|
|
|
function rsaBlind(hashCode, blindingKey, pkey, arena) {
|
2015-12-14 08:26:38 +01:00
|
|
|
let ptr = emscAlloc.malloc(PTR_SIZE);
|
|
|
|
let s = emscAlloc.rsa_blind(hashCode.nativePtr, blindingKey.nativePtr, pkey.nativePtr, ptr);
|
2015-12-17 22:56:24 +01:00
|
|
|
return new ByteArray(s, Module.getValue(ptr, '*'), arena);
|
2015-12-13 18:10:33 +01:00
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
function eddsaSign(purpose, priv, a) {
|
2015-12-14 16:54:47 +01:00
|
|
|
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;
|
2015-12-14 09:39:02 +01:00
|
|
|
}
|
2015-12-16 05:53:55 +01:00
|
|
|
function rsaUnblind(sig, bk, pk, a) {
|
|
|
|
let x = new RsaSignature(a);
|
|
|
|
x.nativePtr = emscAlloc.rsa_unblind(sig.nativePtr, bk.nativePtr, pk.nativePtr);
|
|
|
|
return x;
|
2015-12-16 00:38:36 +01:00
|
|
|
}
|