More wrappers.

This commit is contained in:
Florian Dold 2015-12-14 08:26:38 +01:00
parent 9da386ed63
commit de570d5594
3 changed files with 391 additions and 76 deletions

View File

@ -26,15 +26,26 @@ var emsc = {
amount_add: getEmsc('TALER_amount_add', 'void', ['number', 'number', 'number']), amount_add: getEmsc('TALER_amount_add', 'void', ['number', 'number', 'number']),
amount_subtract: getEmsc('TALER_amount_subtract', 'void', ['number', 'number', 'number']), amount_subtract: getEmsc('TALER_amount_subtract', 'void', ['number', 'number', 'number']),
amount_normalize: getEmsc('TALER_amount_normalize', 'void', ['number']), amount_normalize: getEmsc('TALER_amount_normalize', 'void', ['number']),
amount_cmp: getEmsc('TALER_amount_cmp', 'number', ['number', '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']),
string_to_data: getEmsc('GNUNET_STRINGS_string_to_data', 'void', ['number', 'number', 'number', 'number'])
}; };
var emscAlloc = { var emscAlloc = {
get_amount: getEmsc('TALER_WRALL_get_amount', 'number', ['number', 'number', 'number', 'string']), get_amount: getEmsc('TALER_WRALL_get_amount', 'number', ['number', 'number', 'number', 'string']),
eddsa_key_create: getEmsc('GNUNET_CRYPTO_eddsa_key_create', 'number'), 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']), 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']), 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']),
malloc: (size) => Module._malloc(size), malloc: (size) => Module._malloc(size),
}; };
var SignaturePurpose;
(function (SignaturePurpose) {
})(SignaturePurpose || (SignaturePurpose = {}));
class ArenaObject { class ArenaObject {
constructor(arena) { constructor(arena) {
this.nativePtr = 0; this.nativePtr = 0;
@ -72,6 +83,14 @@ class Amount extends ArenaObject {
emsc.free(this.nativePtr); emsc.free(this.nativePtr);
} }
} }
toNbo(a) {
let x = new AmountNbo(null, a);
emsc.amount_hton(x.nativePtr, this.nativePtr);
return x;
}
fromNbo(nbo) {
emsc.amount_ntoh(this.nativePtr, nbo.nativePtr);
}
get value() { get value() {
return emsc.get_value(this.nativePtr); return emsc.get_value(this.nativePtr);
} }
@ -120,60 +139,151 @@ class Amount extends ArenaObject {
emsc.amount_normalize(this.nativePtr); emsc.amount_normalize(this.nativePtr);
} }
} }
class SizedArenaObject extends ArenaObject { class PackedArenaObject extends ArenaObject {
constructor(init, a) {
super(a);
if (init === null || init === undefined) {
this.nativePtr = emscAlloc.malloc(this.size());
}
else {
this.nativePtr = init;
}
}
encode() { encode() {
var d = emscAlloc.data_to_string_alloc(this.nativePtr, this.size); var d = emscAlloc.data_to_string_alloc(this.nativePtr, this.size());
var s = Module.Pointer_stringify(d); var s = Module.Pointer_stringify(d);
emsc.free(d); emsc.free(d);
return s; return s;
} }
decode(s) {
// We need to get the javascript string
// to the emscripten heap first.
// XXX: Does this work with multi-bytes code points?
let hstr = emscAlloc.malloc(s.length + 1);
Module.writeStringToMemory(s, hstr);
let res = emsc.string_to_data(hstr, s.length, this.nativePtr, this.size());
if (res < 1) {
throw { error: "wrong encoding" };
} }
class EddsaPrivateKey extends SizedArenaObject { }
destroy() {
emsc.free(this.nativePtr);
this.nativePtr = 0;
}
hash() {
var x = new HashCode();
emsc.hash(this.nativePtr, this.size(), x.nativePtr);
return x;
}
}
class AmountNbo extends PackedArenaObject {
size() { return 24; }
}
class EddsaPrivateKey extends PackedArenaObject {
static create(a) { static create(a) {
let k = new EddsaPrivateKey(a); let p = emscAlloc.eddsa_key_create();
k.nativePtr = emscAlloc.eddsa_key_create(); return new EddsaPrivateKey(p, a);
return k;
}
get size() {
return 32;
}
destroy() {
// TODO
} }
size() { return 32; }
getPublicKey() { getPublicKey() {
let pk = new EddsaPublicKey(this.arena); let p = emscAlloc.eddsa_public_key_from_private(this.nativePtr);
pk.nativePtr = emscAlloc.eddsa_public_key_from_private(this.nativePtr); return new EddsaPublicKey(p, this.arena);
return pk;
} }
} }
class EddsaPublicKey extends SizedArenaObject { class EddsaPublicKey extends PackedArenaObject {
destroy() { size() { return 32; }
// TODO
}
get size() {
return 32;
}
} }
class RsaBlindingKey extends ArenaObject { class RsaBlindingKey extends ArenaObject {
destroy() { destroy() {
// TODO // TODO
} }
} }
class HashCode extends ArenaObject { class HashCode extends PackedArenaObject {
destroy() { size() { return 64; }
// TODO }
class ByteArray extends PackedArenaObject {
constructor(desiredSize, init, a) {
super(init, a);
if (init === undefined || init === null) {
this.nativePtr = emscAlloc.malloc(desiredSize);
}
else {
this.nativePtr = init;
}
this.allocatedSize = desiredSize;
}
size() { return this.allocatedSize; }
}
class EccSignaturePurpose extends PackedArenaObject {
constructor(purpose, payload, a) {
this.payload = payload;
let p = emscAlloc.purpose_create(purpose, payload.nativePtr, payload.size());
super(p, a);
}
size() { return this.payload.size() + 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: "Member not set", key: name };
}
totalSize += this.members[name].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, a);
return x;
}
set(name, value) {
let typemap = {};
for (let f of this.fieldTypes()) {
typemap[f[0]] = f[1];
}
if (!(name in typemap)) {
throw { error: "Key not found", key: name };
}
if (!(value instanceof typemap[name])) {
throw { error: "Wrong type", key: name };
}
// TODO: check type!
this.members[name] = value;
} }
} }
class ByteArray extends ArenaObject { class WithdrawRequestPS extends SignatureStruct {
destroy() { purpose() { return undefined; }
// TODO fieldTypes() {
return [
["reserve_pub", EddsaPublicKey],
["amount_with_fee", Amount],
["withdraw_fee", Amount],
["h_denomination_pub", HashCode],
["h_coin_envelope", HashCode]];
} }
} }
class RsaPublicKey extends ArenaObject { class RsaPublicKey extends ArenaObject {
destroy() { destroy() {
// TODO emsc.rsa_public_key_free(this.nativePtr);
this.nativePtr = 0;
} }
} }
function rsaBlind(hashCode, blindingKey, pkey, arena) { function rsaBlind(hashCode, blindingKey, pkey, arena) {
return null; 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;
} }

View File

@ -46,7 +46,25 @@ var emsc = {
['number']), ['number']),
amount_cmp: getEmsc('TALER_amount_cmp', amount_cmp: getEmsc('TALER_amount_cmp',
'number', 'number',
['number', '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']),
string_to_data: getEmsc('GNUNET_STRINGS_string_to_data',
'void',
['number', 'number', 'number', 'number'])
}; };
var emscAlloc = { var emscAlloc = {
@ -61,9 +79,20 @@ var emscAlloc = {
data_to_string_alloc: getEmsc('GNUNET_STRINGS_data_to_string_alloc', data_to_string_alloc: getEmsc('GNUNET_STRINGS_data_to_string_alloc',
'number', 'number',
['number', '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']),
malloc: (size: number) => Module._malloc(size), malloc: (size: number) => Module._malloc(size),
}; };
enum SignaturePurpose {
}
abstract class ArenaObject { abstract class ArenaObject {
nativePtr: number; nativePtr: number;
arena: Arena; arena: Arena;
@ -114,6 +143,16 @@ class Amount extends ArenaObject {
} }
} }
toNbo(a?: Arena): AmountNbo {
let x = new AmountNbo(null, a);
emsc.amount_hton(x.nativePtr, this.nativePtr);
return x;
}
fromNbo(nbo: AmountNbo): void {
emsc.amount_ntoh(this.nativePtr, nbo.nativePtr);
}
get value() { get value() {
return emsc.get_value(this.nativePtr); return emsc.get_value(this.nativePtr);
} }
@ -172,42 +211,71 @@ class Amount extends ArenaObject {
abstract class PackedArenaObject extends ArenaObject { abstract class PackedArenaObject extends ArenaObject {
size: number; abstract size(): number;
constructor(init?: number, a?: Arena) {
super(a);
if (init === null || init === undefined) {
this.nativePtr = emscAlloc.malloc(this.size());
} else {
this.nativePtr = init;
}
}
encode(): string { encode(): string {
var d = emscAlloc.data_to_string_alloc(this.nativePtr, this.size); var d = emscAlloc.data_to_string_alloc(this.nativePtr, this.size());
var s = Module.Pointer_stringify(d); var s = Module.Pointer_stringify(d);
emsc.free(d); emsc.free(d);
return s; return s;
} }
decode(s: string) {
// We need to get the javascript string
// to the emscripten heap first.
// XXX: Does this work with multi-bytes code points?
let hstr = emscAlloc.malloc(s.length + 1);
Module.writeStringToMemory(s, hstr);
let res = emsc.string_to_data(hstr, s.length, this.nativePtr, this.size());
if (res < 1) {
throw {error: "wrong encoding"};
}
}
destroy() {
emsc.free(this.nativePtr);
this.nativePtr = 0;
}
hash(): HashCode {
var x = new HashCode();
emsc.hash(this.nativePtr, this.size(), x.nativePtr);
return x;
}
}
class AmountNbo extends PackedArenaObject {
size() { return 24; }
} }
class EddsaPrivateKey extends PackedArenaObject { class EddsaPrivateKey extends PackedArenaObject {
static create(a?: Arena): EddsaPrivateKey { static create(a?: Arena): EddsaPrivateKey {
let k = new EddsaPrivateKey(a); let p = emscAlloc.eddsa_key_create();
k.nativePtr = emscAlloc.eddsa_key_create(); return new EddsaPrivateKey(p, a);
return k;
} }
get size() { return 32; } size() { return 32; }
getPublicKey(): EddsaPublicKey { getPublicKey(): EddsaPublicKey {
let pk = new EddsaPublicKey(this.arena); let p = emscAlloc.eddsa_public_key_from_private(this.nativePtr);
pk.nativePtr = emscAlloc.eddsa_public_key_from_private(this.nativePtr); return new EddsaPublicKey(p, this.arena);
return pk;
} }
} }
class EddsaPublicKey extends PackedArenaObject { class EddsaPublicKey extends PackedArenaObject {
destroy() { size() { return 32; }
// TODO
}
get size() {
return 32;
}
} }
@ -218,32 +286,110 @@ class RsaBlindingKey extends ArenaObject {
} }
class HashCode extends ArenaObject { class HashCode extends PackedArenaObject {
destroy() { size() { return 64; }
// TODO
}
} }
class ByteArray extends PackedArenaObject { class ByteArray extends PackedArenaObject {
destroy() { private allocatedSize: number;
// TODO size() { return this.allocatedSize; }
constructor(desiredSize: number, init: number, a?: Arena) {
super(init, a);
if (init === undefined || init === null) {
this.nativePtr = emscAlloc.malloc(desiredSize);
} else {
this.nativePtr = init;
}
this.allocatedSize = desiredSize;
}
}
class EccSignaturePurpose extends PackedArenaObject {
size() { return this.payload.size() + 8; }
payload: PackedArenaObject;
constructor(purpose: SignaturePurpose, payload: PackedArenaObject, a?: Arena) {
this.payload = payload;
let p = emscAlloc.purpose_create(purpose, payload.nativePtr, payload.size());
super(p, a);
}
}
abstract class SignatureStruct {
abstract fieldTypes(): Array<any>;
abstract purpose(): SignaturePurpose;
private members: any = {};
toPurpose(a?: Arena): EccSignaturePurpose {
let totalSize = 0;
for (let f of this.fieldTypes()) {
let name = f[0];
let member = this.members[name];
if (!member) {
throw {error: "Member not set", key: name};
}
totalSize += this.members[name].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, a);
return x;
}
set(name: string, value: any) {
let typemap: any = {}
for (let f of this.fieldTypes()) {
typemap[f[0]] = f[1];
}
if (!(name in typemap)) {
throw {error: "Key not found", key: name};
}
if (!(value instanceof typemap[name])) {
throw {error: "Wrong type", key: name};
}
// TODO: check type!
this.members[name] = value;
}
}
class WithdrawRequestPS extends SignatureStruct {
purpose() { return undefined; }
fieldTypes() {
return [
["reserve_pub", EddsaPublicKey],
["amount_with_fee", Amount],
["withdraw_fee", Amount],
["h_denomination_pub", HashCode],
["h_coin_envelope", HashCode]];
} }
} }
class RsaPublicKey extends ArenaObject { class RsaPublicKey extends ArenaObject {
destroy() { destroy() {
// TODO emsc.rsa_public_key_free(this.nativePtr);
this.nativePtr = 0;
} }
} }
function rsaBlind(hashCode: HashCode, function rsaBlind(hashCode: HashCode,
blindingKey: RsaBlindingKey, blindingKey: RsaBlindingKey,
pkey: RsaPublicKey, pkey: EddsaPublicKey,
arena?: Arena): ByteArray arena?: Arena): ByteArray
{ {
return null; 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;
} }

File diff suppressed because one or more lines are too long