2015-12-13 23:47:30 +01:00
|
|
|
/*
|
2015-12-17 22:56:24 +01:00
|
|
|
This file is part of TALER
|
2015-12-25 22:42:14 +01:00
|
|
|
(C) 2015 GNUnet e.V.
|
2015-12-13 23:47:30 +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-13 23:47:30 +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-13 23:47:30 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
2016-07-07 17:59:29 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2015-12-17 22:56:24 +01:00
|
|
|
*/
|
2015-12-13 23:47:30 +01:00
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
import { AmountJson } from "./types";
|
2016-01-10 20:07:42 +01:00
|
|
|
import * as EmscWrapper from "../emscripten/emsc";
|
|
|
|
|
2016-01-05 15:42:46 +01:00
|
|
|
/**
|
|
|
|
* High-level interface to emscripten-compiled modules used
|
|
|
|
* by the wallet.
|
2016-10-12 23:30:10 +02:00
|
|
|
*
|
2016-01-05 15:42:46 +01:00
|
|
|
* @author Florian Dold
|
|
|
|
*/
|
|
|
|
|
2015-12-13 23:47:30 +01:00
|
|
|
"use strict";
|
|
|
|
|
2015-12-26 00:56:02 +01:00
|
|
|
// 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;
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
let Module = EmscWrapper.Module;
|
2015-12-26 00:56:02 +01:00
|
|
|
|
2016-09-28 23:41:34 +02:00
|
|
|
let getEmsc: EmscWrapper.EmscFunGen = (...args: any[]) => Module.cwrap.apply(
|
|
|
|
null,
|
|
|
|
args);
|
2015-12-13 23:47:30 +01:00
|
|
|
|
|
|
|
var emsc = {
|
2016-09-12 20:25:56 +02:00
|
|
|
free: (ptr: number) => Module._free(ptr),
|
2015-12-13 23:47:30 +01:00
|
|
|
get_value: getEmsc('TALER_WR_get_value',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
get_fraction: getEmsc('TALER_WR_get_fraction',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
get_currency: getEmsc('TALER_WR_get_currency',
|
2016-10-12 23:30:10 +02:00
|
|
|
'string',
|
|
|
|
['number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
amount_add: getEmsc('TALER_amount_add',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
amount_subtract: getEmsc('TALER_amount_subtract',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
amount_normalize: getEmsc('TALER_amount_normalize',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number']),
|
2015-12-17 22:56:24 +01:00
|
|
|
amount_get_zero: getEmsc('TALER_amount_get_zero',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['string', 'number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
amount_cmp: getEmsc('TALER_amount_cmp',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
amount_hton: getEmsc('TALER_amount_hton',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
amount_ntoh: getEmsc('TALER_amount_ntoh',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
hash: getEmsc('GNUNET_CRYPTO_hash',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number', 'number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
memmove: getEmsc('memmove',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
rsa_public_key_free: getEmsc('GNUNET_CRYPTO_rsa_public_key_free',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number']),
|
2015-12-16 00:38:36 +01:00
|
|
|
rsa_signature_free: getEmsc('GNUNET_CRYPTO_rsa_signature_free',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
string_to_data: getEmsc('GNUNET_STRINGS_string_to_data',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number', 'number']),
|
2015-12-14 16:54:47 +01:00
|
|
|
eddsa_sign: getEmsc('GNUNET_CRYPTO_eddsa_sign',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number']),
|
2016-02-18 22:50:17 +01:00
|
|
|
eddsa_verify: getEmsc('GNUNET_CRYPTO_eddsa_verify',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number', 'number']),
|
2015-12-14 16:54:47 +01:00
|
|
|
hash_create_random: getEmsc('GNUNET_CRYPTO_hash_create_random',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number', 'number']),
|
2015-12-16 00:38:36 +01:00
|
|
|
rsa_blinding_key_destroy: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_free',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number']),
|
2016-05-25 21:24:21 +02:00
|
|
|
random_block: getEmsc('GNUNET_CRYPTO_random_block',
|
2016-10-12 23:30:10 +02:00
|
|
|
'void',
|
|
|
|
['number', 'number', 'number']),
|
|
|
|
hash_context_abort: getEmsc('GNUNET_CRYPTO_hash_context_abort',
|
|
|
|
'void',
|
|
|
|
['number']),
|
|
|
|
hash_context_read: getEmsc('GNUNET_CRYPTO_hash_context_read',
|
|
|
|
'void',
|
|
|
|
['number', 'number', 'number']),
|
|
|
|
hash_context_finish: getEmsc('GNUNET_CRYPTO_hash_context_finish',
|
|
|
|
'void',
|
|
|
|
['number', 'number']),
|
2015-12-13 23:47:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var emscAlloc = {
|
|
|
|
get_amount: getEmsc('TALER_WRALL_get_amount',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number', 'string']),
|
2015-12-13 23:47:30 +01:00
|
|
|
eddsa_key_create: getEmsc('GNUNET_CRYPTO_eddsa_key_create',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number', []),
|
2016-10-13 02:23:24 +02:00
|
|
|
ecdsa_key_create: getEmsc('GNUNET_CRYPTO_ecdsa_key_create',
|
|
|
|
'number', []),
|
2015-12-17 22:56:24 +01:00
|
|
|
eddsa_public_key_from_private: getEmsc(
|
|
|
|
'TALER_WRALL_eddsa_public_key_from_private',
|
|
|
|
'number',
|
|
|
|
['number']),
|
2016-10-13 02:23:24 +02:00
|
|
|
ecdsa_public_key_from_private: getEmsc(
|
|
|
|
'TALER_WRALL_ecdsa_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',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
purpose_create: getEmsc('TALER_WRALL_purpose_create',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number']),
|
2015-12-14 08:26:38 +01:00
|
|
|
rsa_blind: getEmsc('GNUNET_CRYPTO_rsa_blind',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number', 'number']),
|
2015-12-14 08:54:59 +01:00
|
|
|
rsa_blinding_key_create: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_create',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number']),
|
2015-12-14 08:54:59 +01:00
|
|
|
rsa_blinding_key_encode: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_encode',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-16 00:38:36 +01:00
|
|
|
rsa_signature_encode: getEmsc('GNUNET_CRYPTO_rsa_signature_encode',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-14 08:54:59 +01:00
|
|
|
rsa_blinding_key_decode: getEmsc('GNUNET_CRYPTO_rsa_blinding_key_decode',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-14 08:54:59 +01:00
|
|
|
rsa_public_key_decode: getEmsc('GNUNET_CRYPTO_rsa_public_key_decode',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-16 00:38:36 +01:00
|
|
|
rsa_signature_decode: getEmsc('GNUNET_CRYPTO_rsa_signature_decode',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-14 16:54:47 +01:00
|
|
|
rsa_public_key_encode: getEmsc('GNUNET_CRYPTO_rsa_public_key_encode',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number']),
|
2015-12-16 05:53:55 +01:00
|
|
|
rsa_unblind: getEmsc('GNUNET_CRYPTO_rsa_unblind',
|
2016-10-12 23:30:10 +02:00
|
|
|
'number',
|
|
|
|
['number', 'number', 'number']),
|
|
|
|
hash_context_start: getEmsc('GNUNET_CRYPTO_hash_context_start',
|
|
|
|
'number',
|
|
|
|
[]),
|
2015-12-14 08:26:38 +01:00
|
|
|
malloc: (size: number) => Module._malloc(size),
|
2015-12-13 23:47:30 +01:00
|
|
|
};
|
|
|
|
|
2015-12-14 08:26:38 +01:00
|
|
|
|
2016-02-18 22:50:17 +01:00
|
|
|
export enum SignaturePurpose {
|
2015-12-17 22:56:24 +01:00
|
|
|
RESERVE_WITHDRAW = 1200,
|
|
|
|
WALLET_COIN_DEPOSIT = 1201,
|
2016-02-18 22:50:17 +01:00
|
|
|
MASTER_DENOMINATION_KEY_VALIDITY = 1025,
|
2016-10-12 23:30:10 +02:00
|
|
|
WALLET_COIN_MELT = 1202,
|
2015-12-14 16:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RandomQuality {
|
|
|
|
WEAK = 0,
|
|
|
|
STRONG = 1,
|
|
|
|
NONCE = 2
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
interface ArenaObject {
|
|
|
|
destroy(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-13 02:23:24 +02:00
|
|
|
export class HashContext implements ArenaObject {
|
2016-10-12 23:30:10 +02:00
|
|
|
private hashContextPtr: number | undefined;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.hashContextPtr = emscAlloc.hash_context_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
read(obj: PackedArenaObject): void {
|
|
|
|
if (!this.hashContextPtr) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
emsc.hash_context_read(this.hashContextPtr, obj.getNative(), obj.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
finish(h: HashCode) {
|
|
|
|
if (!this.hashContextPtr) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
h.alloc();
|
|
|
|
emsc.hash_context_finish(this.hashContextPtr, h.getNative());
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {
|
|
|
|
if (this.hashContextPtr) {
|
|
|
|
emsc.hash_context_abort(this.hashContextPtr);
|
|
|
|
}
|
|
|
|
this.hashContextPtr = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 08:26:38 +01:00
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
abstract class MallocArenaObject implements ArenaObject {
|
2016-09-14 15:55:10 +02:00
|
|
|
protected _nativePtr: number | undefined = undefined;
|
2016-10-12 23:30:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this a weak reference to the underlying memory?
|
|
|
|
*/
|
|
|
|
isWeak = false;
|
2015-12-13 23:47:30 +01:00
|
|
|
arena: Arena;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2015-12-13 23:47:30 +01:00
|
|
|
abstract destroy(): void;
|
|
|
|
|
|
|
|
constructor(arena?: Arena) {
|
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(): number {
|
|
|
|
// We want to allow latent allocation
|
|
|
|
// of native wrappers, but we never want to
|
|
|
|
// pass 'undefined' to emscripten.
|
|
|
|
if (this._nativePtr === undefined) {
|
2015-12-17 22:56:24 +01:00
|
|
|
throw Error("Native pointer not initialized");
|
2015-12-16 00:38:36 +01:00
|
|
|
}
|
|
|
|
return this._nativePtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
free() {
|
2016-10-12 23:30:10 +02:00
|
|
|
if (this.nativePtr && !this.isWeak) {
|
2015-12-16 00:38:36 +01:00
|
|
|
emsc.free(this.nativePtr);
|
2016-09-12 20:25:56 +02:00
|
|
|
this._nativePtr = undefined;
|
2015-12-16 00:38:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc(size: number) {
|
2016-09-14 16:03:17 +02:00
|
|
|
if (this._nativePtr !== undefined) {
|
2015-12-16 00:38:36 +01:00
|
|
|
throw Error("Double allocation");
|
|
|
|
}
|
|
|
|
this.nativePtr = emscAlloc.malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
setNative(n: number) {
|
|
|
|
if (n === undefined) {
|
|
|
|
throw Error("Native pointer must be a number or null");
|
|
|
|
}
|
|
|
|
this._nativePtr = n;
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
set nativePtr(v: number) {
|
2015-12-16 00:38:36 +01:00
|
|
|
this.setNative(v);
|
|
|
|
}
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
get nativePtr() {
|
2015-12-16 00:38:36 +01:00
|
|
|
return this.getNative();
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
interface Arena {
|
|
|
|
put(obj: ArenaObject): void;
|
|
|
|
destroy(): void;
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
class DefaultArena implements Arena {
|
2015-12-13 23:47:30 +01:00
|
|
|
heap: Array<ArenaObject>;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
constructor() {
|
2015-12-13 23:47:30 +01:00
|
|
|
this.heap = [];
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
put(obj: ArenaObject) {
|
2015-12-13 23:47:30 +01:00
|
|
|
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 = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
function mySetTimeout(ms: number, fn: () => void) {
|
|
|
|
// We need to use different timeouts, depending on whether
|
|
|
|
// we run in node or a web extension
|
|
|
|
if ("function" === typeof setTimeout) {
|
|
|
|
setTimeout(fn, ms);
|
|
|
|
} else {
|
|
|
|
chrome.extension.getBackgroundPage().setTimeout(fn, ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
/**
|
|
|
|
* Arena that destroys all its objects once control has returned to the message
|
|
|
|
* loop and a small interval has passed.
|
|
|
|
*/
|
|
|
|
class SyncArena extends DefaultArena {
|
2016-01-10 20:07:42 +01:00
|
|
|
private isScheduled: boolean;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
2016-01-10 20:07:42 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
pub(obj: MallocArenaObject) {
|
2016-01-10 20:07:42 +01:00
|
|
|
super.put(obj);
|
|
|
|
if (!this.isScheduled) {
|
|
|
|
this.schedule();
|
|
|
|
}
|
|
|
|
this.heap.push(obj);
|
2015-12-16 05:53:55 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
destroy() {
|
|
|
|
super.destroy();
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2016-01-10 20:07:42 +01:00
|
|
|
|
|
|
|
private schedule() {
|
|
|
|
this.isScheduled = true;
|
|
|
|
mySetTimeout(50, () => {
|
|
|
|
this.isScheduled = false;
|
|
|
|
this.destroy();
|
|
|
|
});
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
|
2015-12-16 05:53:55 +01:00
|
|
|
let arenaStack: Arena[] = [];
|
|
|
|
arenaStack.push(new SyncArena());
|
2015-12-13 23:47:30 +01:00
|
|
|
|
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
export class Amount extends MallocArenaObject {
|
2016-02-01 15:10:20 +01:00
|
|
|
constructor(args?: AmountJson, arena?: Arena) {
|
2015-12-13 23:47:30 +01:00
|
|
|
super(arena);
|
|
|
|
if (args) {
|
2015-12-17 22:56:24 +01:00
|
|
|
this.nativePtr = emscAlloc.get_amount(args.value,
|
2016-10-12 23:30:10 +02:00
|
|
|
0,
|
|
|
|
args.fraction,
|
|
|
|
args.currency);
|
2015-12-13 23:47:30 +01:00
|
|
|
} else {
|
|
|
|
this.nativePtr = emscAlloc.get_amount(0, 0, 0, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2016-09-12 20:25:56 +02:00
|
|
|
super.free();
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2016-01-05 01:10:31 +01:00
|
|
|
static getZero(currency: string, a?: Arena): Amount {
|
2016-09-12 20:25:56 +02:00
|
|
|
let am = new Amount(undefined, a);
|
2015-12-17 22:56:24 +01:00
|
|
|
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?: Arena): AmountNbo {
|
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: AmountNbo): void {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:50:17 +01:00
|
|
|
get currency(): String {
|
2015-12-13 23:47:30 +01:00
|
|
|
return emsc.get_currency(this.nativePtr);
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:50:17 +01:00
|
|
|
toJson(): AmountJson {
|
2015-12-13 23:47:30 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-02-18 22:50:17 +01:00
|
|
|
add(a: Amount) {
|
2015-12-13 23:47:30 +01:00
|
|
|
let res = emsc.amount_add(this.nativePtr, a.nativePtr, this.nativePtr);
|
|
|
|
if (res < 1) {
|
|
|
|
// Overflow
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform saturating subtraction on amounts.
|
|
|
|
*/
|
2016-02-18 22:50:17 +01:00
|
|
|
sub(a: Amount) {
|
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
|
|
|
}
|
|
|
|
|
2016-02-18 22:50:17 +01:00
|
|
|
cmp(a: Amount) {
|
2016-02-19 13:03:45 +01:00
|
|
|
// If we don't check this, the c code aborts.
|
|
|
|
if (this.currency !== a.currency) {
|
|
|
|
throw Error(`incomparable currencies (${this.currency} and ${a.currency})`);
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
return emsc.amount_cmp(this.nativePtr, a.nativePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
normalize() {
|
|
|
|
emsc.amount_normalize(this.nativePtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-28 23:41:34 +02:00
|
|
|
/**
|
|
|
|
* Count the UTF-8 characters in a JavaScript string.
|
|
|
|
*/
|
2016-10-12 23:30:10 +02:00
|
|
|
function countUtf8Bytes(str: string): number {
|
2016-09-28 23:41:34 +02:00
|
|
|
var s = str.length;
|
|
|
|
// JavaScript strings are UTF-16 arrays
|
|
|
|
for (let i = str.length - 1; i >= 0; i--) {
|
|
|
|
var code = str.charCodeAt(i);
|
|
|
|
if (code > 0x7f && code <= 0x7ff) {
|
|
|
|
// We need an extra byte in utf-8 here
|
|
|
|
s++;
|
|
|
|
} else if (code > 0x7ff && code <= 0xffff) {
|
|
|
|
// We need two extra bytes in utf-8 here
|
|
|
|
s += 2;
|
|
|
|
}
|
|
|
|
// Skip over the other surrogate
|
|
|
|
if (code >= 0xDC00 && code <= 0xDFFF) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-25 21:24:21 +02:00
|
|
|
/**
|
|
|
|
* Managed reference to a contiguous block of memory in the Emscripten heap.
|
|
|
|
* Should contain only data, not pointers.
|
|
|
|
*/
|
2016-10-12 23:30:10 +02:00
|
|
|
abstract class PackedArenaObject extends MallocArenaObject {
|
2015-12-14 08:26:38 +01:00
|
|
|
abstract size(): number;
|
|
|
|
|
2015-12-14 09:39:02 +01:00
|
|
|
constructor(a?: Arena) {
|
2015-12-14 08:26:38 +01:00
|
|
|
super(a);
|
|
|
|
}
|
2015-12-14 05:36:06 +01:00
|
|
|
|
2016-05-25 21:24:21 +02:00
|
|
|
randomize(qual: RandomQuality = RandomQuality.STRONG): void {
|
|
|
|
emsc.random_block(qual, this.nativePtr, this.size());
|
|
|
|
}
|
|
|
|
|
2015-12-14 16:54:47 +01:00
|
|
|
toCrock(): string {
|
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 08:26:38 +01:00
|
|
|
|
2015-12-25 18:48:15 +01:00
|
|
|
toJson(): any {
|
|
|
|
// Per default, the json encoding of
|
|
|
|
// packed arena objects is just the crockford encoding.
|
|
|
|
// Subclasses typically want to override this.
|
|
|
|
return this.toCrock();
|
|
|
|
}
|
|
|
|
|
2015-12-14 16:54:47 +01:00
|
|
|
loadCrock(s: string) {
|
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);
|
2015-12-17 22:56:24 +01:00
|
|
|
let res = emsc.string_to_data(buf.nativePtr,
|
2016-10-12 23:30:10 +02:00
|
|
|
s.length,
|
|
|
|
this.nativePtr,
|
|
|
|
this.size());
|
2015-12-14 09:39:02 +01:00
|
|
|
buf.destroy();
|
2015-12-14 08:26:38 +01:00
|
|
|
if (res < 1) {
|
2016-10-12 23:30:10 +02:00
|
|
|
throw { error: "wrong encoding" };
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:39:02 +01:00
|
|
|
alloc() {
|
2016-09-14 16:03:17 +02:00
|
|
|
// FIXME: should the client be allowed to call alloc multiple times?
|
2016-09-14 15:55:10 +02:00
|
|
|
if (!this._nativePtr) {
|
2015-12-14 09:39:02 +01:00
|
|
|
this.nativePtr = emscAlloc.malloc(this.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 08:26:38 +01:00
|
|
|
destroy() {
|
|
|
|
emsc.free(this.nativePtr);
|
|
|
|
this.nativePtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash(): HashCode {
|
|
|
|
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-20 20:05:06 +01:00
|
|
|
|
|
|
|
hexdump() {
|
|
|
|
let bytes: string[] = [];
|
|
|
|
for (let i = 0; i < this.size(); i++) {
|
|
|
|
let b = Module.getValue(this.getNative() + i, "i8");
|
|
|
|
b = (b + 256) % 256;
|
|
|
|
bytes.push("0".concat(b.toString(16)).slice(-2));
|
|
|
|
}
|
2016-09-12 20:25:56 +02:00
|
|
|
let lines: string[] = [];
|
2016-01-11 02:56:32 +01:00
|
|
|
for (let i = 0; i < bytes.length; i += 8) {
|
|
|
|
lines.push(bytes.slice(i, i + 8).join(","));
|
2015-12-20 20:05:06 +01:00
|
|
|
}
|
|
|
|
return lines.join("\n");
|
|
|
|
}
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class AmountNbo extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 24;
|
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
|
2015-12-25 18:48:15 +01:00
|
|
|
toJson(): any {
|
|
|
|
let a = new DefaultArena();
|
2016-09-12 20:25:56 +02:00
|
|
|
let am = new Amount(undefined, a);
|
2015-12-25 18:48:15 +01:00
|
|
|
am.fromNbo(this);
|
|
|
|
let json = am.toJson();
|
|
|
|
a.destroy();
|
|
|
|
return json;
|
|
|
|
}
|
2015-12-14 05:36:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class EddsaPrivateKey extends PackedArenaObject {
|
2015-12-13 23:47:30 +01:00
|
|
|
static create(a?: Arena): EddsaPrivateKey {
|
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-13 23:47:30 +01:00
|
|
|
|
2015-12-14 09:39:02 +01:00
|
|
|
getPublicKey(a?: Arena): EddsaPublicKey {
|
|
|
|
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
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
static fromCrock: (s: string) => EddsaPrivateKey;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
mixinStatic(EddsaPrivateKey, fromCrock);
|
2015-12-13 23:47:30 +01:00
|
|
|
|
|
|
|
|
2016-10-13 02:23:24 +02:00
|
|
|
export class EcdsaPrivateKey extends PackedArenaObject {
|
|
|
|
static create(a?: Arena): EcdsaPrivateKey {
|
|
|
|
let obj = new EcdsaPrivateKey(a);
|
|
|
|
obj.nativePtr = emscAlloc.ecdsa_key_create();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
size() {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPublicKey(a?: Arena): EcdsaPublicKey {
|
|
|
|
let obj = new EcdsaPublicKey(a);
|
|
|
|
obj.nativePtr = emscAlloc.ecdsa_public_key_from_private(this.nativePtr);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromCrock: (s: string) => EcdsaPrivateKey;
|
|
|
|
}
|
|
|
|
mixinStatic(EcdsaPrivateKey, fromCrock);
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
function fromCrock(s: string) {
|
|
|
|
let x = new this();
|
|
|
|
x.alloc();
|
|
|
|
x.loadCrock(s);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
function mixin(obj: any, method: any, name?: string) {
|
2015-12-17 22:56:24 +01:00
|
|
|
if (!name) {
|
|
|
|
name = method.name;
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
throw Error("Mixin needs a name.");
|
|
|
|
}
|
|
|
|
obj.prototype[method.name] = method;
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
function mixinStatic(obj: any, method: any, name?: string) {
|
2015-12-17 22:56:24 +01:00
|
|
|
if (!name) {
|
|
|
|
name = method.name;
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
throw Error("Mixin needs a name.");
|
|
|
|
}
|
|
|
|
obj[method.name] = method;
|
|
|
|
}
|
2015-12-17 13:30:34 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class EddsaPublicKey extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 32;
|
2015-12-14 08:54:59 +01:00
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
static fromCrock: (s: string) => EddsaPublicKey;
|
|
|
|
}
|
|
|
|
mixinStatic(EddsaPublicKey, fromCrock);
|
|
|
|
|
2016-10-13 02:23:24 +02:00
|
|
|
export class EcdsaPublicKey extends PackedArenaObject {
|
|
|
|
size() {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromCrock: (s: string) => EcdsaPublicKey;
|
|
|
|
}
|
|
|
|
mixinStatic(EddsaPublicKey, fromCrock);
|
|
|
|
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
function makeFromCrock(decodeFn: (p: number, s: number) => number) {
|
|
|
|
function fromCrock(s: string, a?: Arena) {
|
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(),
|
2016-10-12 23:30:10 +02:00
|
|
|
buf.size()));
|
2015-12-17 13:30:34 +01:00
|
|
|
buf.destroy();
|
|
|
|
return obj;
|
|
|
|
}
|
2015-12-14 08:54:59 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
return fromCrock;
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
function makeToCrock(encodeFn: (po: number,
|
2016-10-12 23:30:10 +02:00
|
|
|
ps: number) => number): () => string {
|
2015-12-17 22:56:24 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
return toCrock;
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:24:21 +02:00
|
|
|
export class RsaBlindingKeySecret extends PackedArenaObject {
|
|
|
|
size() {
|
|
|
|
return 32;
|
2015-12-17 22:56:24 +01:00
|
|
|
}
|
|
|
|
|
2016-05-25 21:24:21 +02:00
|
|
|
/**
|
|
|
|
* Create a random blinding key secret.
|
|
|
|
*/
|
|
|
|
static create(a?: Arena): RsaBlindingKeySecret {
|
|
|
|
let o = new RsaBlindingKeySecret(a);
|
|
|
|
o.alloc();
|
|
|
|
o.randomize();
|
|
|
|
return o;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2016-05-25 21:24:21 +02:00
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
static fromCrock: (s: string) => RsaBlindingKeySecret;
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
2016-05-25 21:24:21 +02:00
|
|
|
mixinStatic(RsaBlindingKeySecret, fromCrock);
|
2015-12-14 08:26:38 +01:00
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class HashCode extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromCrock: (s: string) => HashCode;
|
2015-12-14 16:54:47 +01:00
|
|
|
|
2016-05-25 21:24:21 +02:00
|
|
|
random(qual: RandomQuality = RandomQuality.STRONG) {
|
2015-12-14 16:54:47 +01:00
|
|
|
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
|
|
|
|
2015-12-13 23:47:30 +01:00
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class ByteArray extends PackedArenaObject {
|
2015-12-14 08:26:38 +01:00
|
|
|
private allocatedSize: number;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
|
|
|
size() {
|
|
|
|
return this.allocatedSize;
|
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
constructor(desiredSize: number, init?: number, a?: Arena) {
|
2015-12-14 09:39:02 +01:00
|
|
|
super(a);
|
2016-09-12 20:25:56 +02:00
|
|
|
if (init === undefined) {
|
2015-12-14 08:26:38 +01:00
|
|
|
this.nativePtr = emscAlloc.malloc(desiredSize);
|
|
|
|
} else {
|
|
|
|
this.nativePtr = init;
|
|
|
|
}
|
|
|
|
this.allocatedSize = desiredSize;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-14 09:39:02 +01:00
|
|
|
|
|
|
|
static fromString(s: string, a?: Arena): ByteArray {
|
2016-09-28 23:41:34 +02:00
|
|
|
// UTF-8 bytes, including 0-terminator
|
2016-10-12 23:30:10 +02:00
|
|
|
let terminatedByteLength = countUtf8Bytes(s) + 1;
|
2016-09-28 23:41:34 +02:00
|
|
|
let hstr = emscAlloc.malloc(terminatedByteLength);
|
|
|
|
Module.stringToUTF8(s, hstr, terminatedByteLength);
|
|
|
|
return new ByteArray(terminatedByteLength, hstr, a);
|
2015-12-14 09:39:02 +01:00
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
|
|
|
|
static fromCrock(s: string, a?: Arena): ByteArray {
|
2016-10-12 23:30:10 +02:00
|
|
|
let byteLength = countUtf8Bytes(s);
|
2016-09-29 00:29:39 +02:00
|
|
|
let hstr = emscAlloc.malloc(byteLength + 1);
|
|
|
|
Module.stringToUTF8(s, hstr, byteLength + 1);
|
2016-09-28 23:41:34 +02:00
|
|
|
let decodedLen = Math.floor((byteLength * 5) / 8);
|
2016-09-12 20:25:56 +02:00
|
|
|
let ba = new ByteArray(decodedLen, undefined, a);
|
2016-09-28 23:41:34 +02:00
|
|
|
let res = emsc.string_to_data(hstr, byteLength, ba.nativePtr, decodedLen);
|
2015-12-14 16:54:47 +01:00
|
|
|
emsc.free(hstr);
|
|
|
|
if (res != GNUNET_OK) {
|
|
|
|
throw Error("decoding failed");
|
|
|
|
}
|
|
|
|
return ba;
|
|
|
|
}
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class EccSignaturePurpose extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return this.payloadSize + 8;
|
|
|
|
}
|
|
|
|
|
2015-12-14 16:54:47 +01:00
|
|
|
payloadSize: number;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
|
|
|
constructor(purpose: SignaturePurpose,
|
2016-10-12 23:30:10 +02:00
|
|
|
payload: PackedArenaObject,
|
|
|
|
a?: Arena) {
|
2015-12-14 09:39:02 +01:00
|
|
|
super(a);
|
2015-12-17 22:56:24 +01:00
|
|
|
this.nativePtr = emscAlloc.purpose_create(purpose,
|
2016-10-12 23:30:10 +02:00
|
|
|
payload.nativePtr,
|
|
|
|
payload.size());
|
2015-12-14 16:54:47 +01:00
|
|
|
this.payloadSize = payload.size();
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-14 08:26:38 +01:00
|
|
|
abstract class SignatureStruct {
|
|
|
|
abstract fieldTypes(): Array<any>;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2015-12-14 08:26:38 +01:00
|
|
|
abstract purpose(): SignaturePurpose;
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2015-12-14 08:26:38 +01:00
|
|
|
private members: any = {};
|
2015-12-16 10:45:16 +01:00
|
|
|
|
2015-12-16 10:53:29 +01:00
|
|
|
constructor(x: { [name: string]: any }) {
|
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?: Arena): EccSignaturePurpose {
|
|
|
|
let totalSize = 0;
|
|
|
|
for (let f of this.fieldTypes()) {
|
|
|
|
let name = f[0];
|
|
|
|
let member = this.members[name];
|
|
|
|
if (!member) {
|
2016-01-10 23:52:58 +01:00
|
|
|
throw Error(`Member ${name} not set`);
|
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
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
|
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
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
|
2015-12-25 18:48:15 +01:00
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
toJson() {
|
2015-12-25 18:48:15 +01:00
|
|
|
let res: any = {};
|
|
|
|
for (let f of this.fieldTypes()) {
|
|
|
|
let name = f[0];
|
|
|
|
let member = this.members[name];
|
|
|
|
if (!member) {
|
2016-01-10 23:52:58 +01:00
|
|
|
throw Error(`Member ${name} not set`);
|
2015-12-25 18:48:15 +01:00
|
|
|
}
|
|
|
|
res[name] = member.toJson();
|
|
|
|
}
|
|
|
|
res["purpose"] = this.purpose();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-12-16 10:45:16 +01:00
|
|
|
protected set(name: string, value: PackedArenaObject) {
|
2015-12-17 22:56:24 +01:00
|
|
|
let typemap: any = {};
|
2015-12-14 08:26:38 +01:00
|
|
|
for (let f of this.fieldTypes()) {
|
|
|
|
typemap[f[0]] = f[1];
|
|
|
|
}
|
|
|
|
if (!(name in typemap)) {
|
2016-01-10 23:52:58 +01:00
|
|
|
throw Error(`Key ${name} not found`);
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
if (!(value instanceof typemap[name])) {
|
2016-01-10 23:52:58 +01:00
|
|
|
throw Error("Wrong type for ${name}");
|
2015-12-14 08:26:38 +01:00
|
|
|
}
|
|
|
|
this.members[name] = value;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-16 10:45:16 +01:00
|
|
|
// It's redundant, but more type safe.
|
2016-01-10 20:07:42 +01:00
|
|
|
export interface WithdrawRequestPS_Args {
|
2015-12-16 10:45:16 +01:00
|
|
|
reserve_pub: EddsaPublicKey;
|
|
|
|
amount_with_fee: AmountNbo;
|
|
|
|
withdraw_fee: AmountNbo;
|
|
|
|
h_denomination_pub: HashCode;
|
|
|
|
h_coin_envelope: HashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class WithdrawRequestPS extends SignatureStruct {
|
2015-12-16 10:45:16 +01:00
|
|
|
constructor(w: WithdrawRequestPS_Args) {
|
|
|
|
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 [
|
2015-12-17 22:56:24 +01:00
|
|
|
["reserve_pub", EddsaPublicKey],
|
|
|
|
["amount_with_fee", AmountNbo],
|
|
|
|
["withdraw_fee", AmountNbo],
|
|
|
|
["h_denomination_pub", HashCode],
|
|
|
|
["h_coin_envelope", HashCode]
|
|
|
|
];
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
interface RefreshMeltCoinAffirmationPS_Args {
|
|
|
|
session_hash: HashCode;
|
|
|
|
amount_with_fee: AmountNbo;
|
|
|
|
melt_fee: AmountNbo;
|
|
|
|
coin_pub: EddsaPublicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RefreshMeltCoinAffirmationPS extends SignatureStruct {
|
|
|
|
|
|
|
|
constructor(w: RefreshMeltCoinAffirmationPS_Args) {
|
|
|
|
super(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
purpose() {
|
|
|
|
return SignaturePurpose.WALLET_COIN_MELT;
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldTypes() {
|
|
|
|
return [
|
|
|
|
["session_hash", HashCode],
|
|
|
|
["amount_with_fee", AmountNbo],
|
|
|
|
["melt_fee", AmountNbo],
|
|
|
|
["coin_pub", EddsaPublicKey]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class AbsoluteTimeNbo extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
static fromTalerString(s: string): AbsoluteTimeNbo {
|
2015-12-18 01:30:22 +01:00
|
|
|
let x = new AbsoluteTimeNbo();
|
|
|
|
x.alloc();
|
|
|
|
let r = /Date\(([0-9]+)\)/;
|
|
|
|
let m = r.exec(s);
|
2016-09-14 15:55:10 +02:00
|
|
|
if (!m || m.length != 2) {
|
2015-12-18 01:30:22 +01:00
|
|
|
throw Error();
|
|
|
|
}
|
2015-12-20 20:05:06 +01:00
|
|
|
let n = parseInt(m[1]) * 1000000;
|
2015-12-18 01:30:22 +01:00
|
|
|
// XXX: This only works up to 54 bit numbers.
|
|
|
|
set64(x.getNative(), n);
|
|
|
|
return x;
|
2015-12-17 22:56:24 +01:00
|
|
|
}
|
2015-12-16 00:38:36 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 8;
|
|
|
|
}
|
2015-12-16 00:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 01:30:22 +01:00
|
|
|
// XXX: This only works up to 54 bit numbers.
|
|
|
|
function set64(p: number, n: number) {
|
|
|
|
for (let i = 0; i < 8; ++i) {
|
2015-12-20 20:05:06 +01:00
|
|
|
Module.setValue(p + (7 - i), n & 0xFF, "i8");
|
|
|
|
n = Math.floor(n / 256);
|
2015-12-18 01:30:22 +01:00
|
|
|
}
|
2016-10-12 23:30:10 +02:00
|
|
|
}
|
2015-12-18 01:30:22 +01:00
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
// XXX: This only works up to 54 bit numbers.
|
|
|
|
function set32(p: number, n: number) {
|
|
|
|
for (let i = 0; i < 4; ++i) {
|
|
|
|
Module.setValue(p + (3 - i), n & 0xFF, "i8");
|
|
|
|
n = Math.floor(n / 256);
|
|
|
|
}
|
2015-12-18 01:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class UInt64 extends PackedArenaObject {
|
2015-12-17 22:56:24 +01:00
|
|
|
static fromNumber(n: number): UInt64 {
|
2015-12-18 01:30:22 +01:00
|
|
|
let x = new UInt64();
|
|
|
|
x.alloc();
|
|
|
|
set64(x.getNative(), n);
|
|
|
|
return x;
|
2015-12-14 08:54:59 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
size() {
|
|
|
|
return 8;
|
2015-12-14 16:54:47 +01:00
|
|
|
}
|
2015-12-17 22:56:24 +01:00
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
export class UInt32 extends PackedArenaObject {
|
|
|
|
static fromNumber(n: number): UInt64 {
|
|
|
|
let x = new UInt32();
|
|
|
|
x.alloc();
|
|
|
|
set32(x.getNative(), n);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
size() {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
// It's redundant, but more type safe.
|
2016-01-10 20:07:42 +01:00
|
|
|
export interface DepositRequestPS_Args {
|
2015-12-17 22:56:24 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class DepositRequestPS extends SignatureStruct {
|
2015-12-17 22:56:24 +01:00
|
|
|
constructor(w: DepositRequestPS_Args) {
|
|
|
|
super(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
purpose() {
|
|
|
|
return SignaturePurpose.WALLET_COIN_DEPOSIT;
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
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],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:50:17 +01:00
|
|
|
export interface DenominationKeyValidityPS_args {
|
|
|
|
master: EddsaPublicKey;
|
|
|
|
start: AbsoluteTimeNbo;
|
|
|
|
expire_withdraw: AbsoluteTimeNbo;
|
|
|
|
expire_spend: AbsoluteTimeNbo;
|
|
|
|
expire_legal: AbsoluteTimeNbo;
|
|
|
|
value: AmountNbo;
|
|
|
|
fee_withdraw: AmountNbo;
|
|
|
|
fee_deposit: AmountNbo;
|
|
|
|
fee_refresh: AmountNbo;
|
2016-04-22 18:03:56 +02:00
|
|
|
fee_refund: AmountNbo;
|
2016-02-18 22:50:17 +01:00
|
|
|
denom_hash: HashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DenominationKeyValidityPS extends SignatureStruct {
|
|
|
|
constructor(w: DenominationKeyValidityPS_args) {
|
|
|
|
super(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
purpose() {
|
|
|
|
return SignaturePurpose.MASTER_DENOMINATION_KEY_VALIDITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldTypes() {
|
|
|
|
return [
|
|
|
|
["master", EddsaPublicKey],
|
|
|
|
["start", AbsoluteTimeNbo],
|
|
|
|
["expire_withdraw", AbsoluteTimeNbo],
|
|
|
|
["expire_spend", AbsoluteTimeNbo],
|
|
|
|
["expire_legal", AbsoluteTimeNbo],
|
|
|
|
["value", AmountNbo],
|
|
|
|
["fee_withdraw", AmountNbo],
|
|
|
|
["fee_deposit", AmountNbo],
|
|
|
|
["fee_refresh", AmountNbo],
|
2016-04-22 18:03:56 +02:00
|
|
|
["fee_refund", AmountNbo],
|
2016-02-18 22:50:17 +01:00
|
|
|
["denom_hash", HashCode]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
|
|
|
|
interface Encodeable {
|
|
|
|
encode(arena?: Arena): ByteArray;
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:25:56 +02:00
|
|
|
function makeEncode(encodeFn: any) {
|
2015-12-17 22:56:24 +01:00
|
|
|
function encode(arena?: Arena) {
|
2015-12-14 16:54:47 +01:00
|
|
|
let ptr = emscAlloc.malloc(PTR_SIZE);
|
2015-12-17 22:56:24 +01:00
|
|
|
let len = encodeFn(this.getNative(), ptr);
|
2016-09-12 20:25:56 +02:00
|
|
|
let res = new ByteArray(len, undefined, arena);
|
2015-12-17 22:56:24 +01:00
|
|
|
res.setNative(Module.getValue(ptr, '*'));
|
2015-12-14 16:54:47 +01:00
|
|
|
emsc.free(ptr);
|
|
|
|
return res;
|
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
return encode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
export class RsaPublicKey extends MallocArenaObject implements Encodeable {
|
2015-12-17 22:56:24 +01:00
|
|
|
static fromCrock: (s: string, a?: Arena) => RsaPublicKey;
|
|
|
|
|
|
|
|
toCrock() {
|
|
|
|
return this.encode().toCrock();
|
|
|
|
}
|
2015-12-14 16:54:47 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
destroy() {
|
|
|
|
emsc.rsa_public_key_free(this.nativePtr);
|
|
|
|
this.nativePtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
encode: (arena?: Arena) => ByteArray;
|
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-13 23:47:30 +01:00
|
|
|
|
2015-12-14 16:54:47 +01:00
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export 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-13 23:47:30 +01:00
|
|
|
|
2016-10-12 23:30:10 +02:00
|
|
|
export class RsaSignature extends MallocArenaObject implements Encodeable {
|
2015-12-17 22:56:24 +01:00
|
|
|
static fromCrock: (s: string, a?: Arena) => RsaSignature;
|
2015-12-16 00:38:36 +01:00
|
|
|
|
2015-12-17 22:56:24 +01:00
|
|
|
encode: (arena?: Arena) => ByteArray;
|
2015-12-16 00:38:36 +01:00
|
|
|
|
|
|
|
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-16 00:38:36 +01:00
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export function rsaBlind(hashCode: HashCode,
|
2016-10-12 23:30:10 +02:00
|
|
|
blindingKey: RsaBlindingKeySecret,
|
|
|
|
pkey: RsaPublicKey,
|
|
|
|
arena?: Arena): ByteArray {
|
2015-12-14 08:26:38 +01:00
|
|
|
let ptr = emscAlloc.malloc(PTR_SIZE);
|
2015-12-17 22:56:24 +01:00
|
|
|
let s = emscAlloc.rsa_blind(hashCode.nativePtr,
|
2016-10-12 23:30:10 +02:00
|
|
|
blindingKey.nativePtr,
|
|
|
|
pkey.nativePtr,
|
|
|
|
ptr);
|
2015-12-17 22:56:24 +01:00
|
|
|
return new ByteArray(s, Module.getValue(ptr, '*'), arena);
|
2015-12-13 23:47:30 +01:00
|
|
|
}
|
|
|
|
|
2015-12-14 09:39:02 +01:00
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export function eddsaSign(purpose: EccSignaturePurpose,
|
2016-10-12 23:30:10 +02:00
|
|
|
priv: EddsaPrivateKey,
|
|
|
|
a?: Arena): EddsaSignature {
|
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-17 22:56:24 +01:00
|
|
|
|
2016-02-18 22:50:17 +01:00
|
|
|
export function eddsaVerify(purposeNum: number,
|
2016-10-12 23:30:10 +02:00
|
|
|
verify: EccSignaturePurpose,
|
|
|
|
sig: EddsaSignature,
|
|
|
|
pub: EddsaPublicKey,
|
|
|
|
a?: Arena): boolean {
|
2016-02-18 22:50:17 +01:00
|
|
|
let r = emsc.eddsa_verify(purposeNum,
|
2016-10-12 23:30:10 +02:00
|
|
|
verify.nativePtr,
|
|
|
|
sig.nativePtr,
|
|
|
|
pub.nativePtr);
|
2016-02-18 22:50:17 +01:00
|
|
|
if (r === GNUNET_OK) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export function rsaUnblind(sig: RsaSignature,
|
2016-10-12 23:30:10 +02:00
|
|
|
bk: RsaBlindingKeySecret,
|
|
|
|
pk: RsaPublicKey,
|
|
|
|
a?: Arena): RsaSignature {
|
2015-12-16 05:53:55 +01:00
|
|
|
let x = new RsaSignature(a);
|
2015-12-17 22:56:24 +01:00
|
|
|
x.nativePtr = emscAlloc.rsa_unblind(sig.nativePtr,
|
2016-10-12 23:30:10 +02:00
|
|
|
bk.nativePtr,
|
|
|
|
pk.nativePtr);
|
2015-12-16 05:53:55 +01:00
|
|
|
return x;
|
2015-12-16 00:38:36 +01:00
|
|
|
}
|
2016-10-12 23:30:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
type TransferSecretP = HashCode;
|
|
|
|
|
|
|
|
export function kdf(outLength: number,
|
|
|
|
salt: PackedArenaObject,
|
|
|
|
skm: PackedArenaObject,
|
|
|
|
...contextChunks: PackedArenaObject[]): ByteArray {
|
|
|
|
const args: number[] = [];
|
|
|
|
let out = new ByteArray(outLength);
|
|
|
|
args.push(out.nativePtr, outLength);
|
|
|
|
args.push(salt.nativePtr, salt.size());
|
|
|
|
args.push(skm.nativePtr, skm.size());
|
|
|
|
for (let chunk of contextChunks) {
|
|
|
|
args.push(chunk.nativePtr, chunk.size());
|
|
|
|
}
|
|
|
|
// end terminator (it's varargs)
|
|
|
|
args.push(0);
|
|
|
|
args.push(0);
|
|
|
|
|
|
|
|
let argTypes = args.map(() => "number");
|
|
|
|
|
|
|
|
const res = Module.ccall("GNUNET_CRYPTO_kdf", "number", argTypes, args);
|
|
|
|
if (res != GNUNET_OK) {
|
|
|
|
throw Error("fatal: kdf failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface FreshCoin {
|
|
|
|
priv: EddsaPrivateKey;
|
|
|
|
blindingKey: RsaBlindingKeySecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupFreshCoin(secretSeed: TransferSecretP, coinIndex: number): FreshCoin {
|
|
|
|
let priv = new EddsaPrivateKey();
|
|
|
|
priv.isWeak = true;
|
|
|
|
let blindingKey = new RsaBlindingKeySecret();
|
|
|
|
blindingKey.isWeak = true;
|
|
|
|
|
|
|
|
let buf = kdf(priv.size() + blindingKey.size(), UInt32.fromNumber(coinIndex), ByteArray.fromString("taler-coin-derivation"));
|
|
|
|
|
|
|
|
priv.nativePtr = buf.nativePtr;
|
|
|
|
blindingKey.nativePtr = buf.nativePtr + priv.size();
|
|
|
|
|
|
|
|
return { priv, blindingKey };
|
|
|
|
}
|