Use published schnorrkel-js (not embedded) (#320)

This commit is contained in:
Jaco Greeff
2019-02-18 11:48:53 +01:00
committed by GitHub
parent 44b44e338b
commit 830c98d03b
7 changed files with 7 additions and 295 deletions
+1
View File
@@ -29,6 +29,7 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.3.1",
"@polkadot/schnorrkel-js": "^0.1.2-1",
"@polkadot/util": "^0.34.19",
"@types/bip39": "^2.4.2",
"@types/webassembly-js-api": "^0.0.2",
@@ -6,4 +6,4 @@ import '@polkadot/util/polyfill';
import './polyfill';
// export * from '@parity/schnorrkel-js';
export * from './schnorrkel_js';
export * from '@polkadot/schnorrkel-js';
@@ -1,9 +0,0 @@
/* tslint:disable */
export function sign(arg0: Uint8Array, arg1: Uint8Array, arg2: Uint8Array): Uint8Array;
export function verify(arg0: Uint8Array, arg1: Uint8Array, arg2: Uint8Array): boolean;
export function secret_from_seed(arg0: Uint8Array): Uint8Array;
export function keypair_from_seed(arg0: Uint8Array): Uint8Array;
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
@@ -1,263 +0,0 @@
/* tslint:disable */
let wasm; const wasmImports = {};
let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm(arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8Memory().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function getArrayU8FromWasm(ptr, len) {
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
}
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}
return cachedGlobalArgumentPtr;
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
/**
* Sign a message
*
* The combination of both public and private key must be provided.
* This is effectively equivalent to a keypair.
*
* * public: UIntArray with 32 element
* * private: UIntArray with 64 element
* * message: Arbitrary length UIntArray
*
* * returned vector is the signature consisting of 64 bytes.
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @param {Uint8Array} arg2
* @returns {Uint8Array}
*/
module.exports.sign = function(arg0, arg1, arg2) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm(arg1);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm(arg2);
const len2 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.sign(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
wasm.__wbindgen_free(ptr2, len2 * 1);
}
};
/**
* Verify a message and its corresponding against a public key;
*
* * signature: UIntArray with 64 element
* * message: Arbitrary length UIntArray
* * pubkey: UIntArray with 32 element
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @param {Uint8Array} arg2
* @returns {boolean}
*/
module.exports.verify = function(arg0, arg1, arg2) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm(arg1);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm(arg2);
const len2 = WASM_VECTOR_LEN;
try {
return (wasm.verify(ptr0, len0, ptr1, len1, ptr2, len2)) !== 0;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
wasm.__wbindgen_free(ptr2, len2 * 1);
}
};
/**
* Generate a secret key (aka. private key) from a seed phrase.
*
* * seed: UIntArray with 32 element
*
* returned vector is the private key consisting of 64 bytes.
* @param {Uint8Array} arg0
* @returns {Uint8Array}
*/
module.exports.secret_from_seed = function(arg0) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.secret_from_seed(retptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
};
/**
* Generate a key pair. .
*
* * seed: UIntArray with 32 element
*
* returned vector is the concatenation of first the private key (64 bytes)
* followed by the public key (32) bytes.
* @param {Uint8Array} arg0
* @returns {Uint8Array}
*/
module.exports.keypair_from_seed = function(arg0) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.keypair_from_seed(retptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
};
const { u8aToString } = require('@polkadot/util');
// let cachedTextDecoder = new TextDecoder('utf-8');
function getStringFromWasm(ptr, len) {
return u8aToString(getUint8Memory().subarray(ptr, ptr + len));
}
const heap = new Array(32);
heap.fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
wasmImports.__wbg_new_886f15c1b20b061b = function(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
return addHeapObject(new Function(varg0));
};
function getObject(idx) { return heap[idx]; }
wasmImports.__wbg_call_a2b503e0ee1234e4 = function(arg0, arg1) {
return addHeapObject(getObject(arg0).call(getObject(arg1)));
};
wasmImports.__wbg_self_ddd2d80076091e5f = function(arg0) {
return addHeapObject(getObject(arg0).self);
};
wasmImports.__wbg_crypto_4b7669ff1793d881 = function(arg0) {
return addHeapObject(getObject(arg0).crypto);
};
wasmImports.__wbg_getRandomValues_6de85818bd2ad699 = function(arg0) {
return addHeapObject(getObject(arg0).getRandomValues);
};
wasmImports.__wbg_getRandomValues_95cef5eed1acafda = function(arg0, arg1, arg2) {
let varg1 = getArrayU8FromWasm(arg1, arg2);
getObject(arg0).getRandomValues(varg1);
};
wasmImports.__wbg_require_86edd37cfda5f13d = function(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
throw new Error(`Invalid require from WASM for ${varg0}`);
};
wasmImports.__wbg_randomFillSync_571502126f344d60 = function(arg0, arg1, arg2) {
let varg1 = getArrayU8FromWasm(arg1, arg2);
getObject(arg0).randomFillSync(varg1);
};
function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}
wasmImports.__wbindgen_object_drop_ref = function(i) { dropObject(i); };
wasmImports.__wbindgen_is_undefined = function(idx) {
return getObject(idx) === undefined ? 1 : 0;
};
wasmImports.__wbindgen_jsval_eq = function(a, b) {
return getObject(a) === getObject(b) ? 1 : 0;
};
const createPromise = require('./schnorrkel_js_bg');
const wasmPromise = createPromise(wasmImports).catch(() => null);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
@@ -1,20 +0,0 @@
const bytes = require('./schnorrkel_js_wasm');
module.exports = async function createExportPromise (wasmImports) {
const imports = {
'./schnorrkel_js': wasmImports
};
if (!WebAssembly) {
return null;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
return null;
}
}
File diff suppressed because one or more lines are too long
+5
View File
@@ -1410,6 +1410,11 @@
typedoc-plugin-no-inherit "^1.1.6"
typescript "^3.3.3"
"@polkadot/schnorrkel-js@^0.1.2-1":
version "0.1.2-1"
resolved "https://registry.yarnpkg.com/@polkadot/schnorrkel-js/-/schnorrkel-js-0.1.2-1.tgz#90e4e8bca54157513cf5192f4581491bcbf5c44e"
integrity sha512-Y6cJWOOJyQ6TPzWzfgPrZlWzcR89X2tOgLIrCXOyEE72+CFiBfyAQDqG2jBzxolEqqN/Fz0AUdxqPT45BzionQ==
"@polkadot/ts@^0.1.52":
version "0.1.52"
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.52.tgz#6fb09e494d244dad4b3a9ca581bfe13beec66565"