Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
370a529668 | ||
|
|
b7486f4f3b | ||
|
|
ff4a877ef9 | ||
|
|
e0f741db37 | ||
|
|
3107861322 | ||
|
|
7eff345003 | ||
|
|
00f7da2136 | ||
|
|
150a143163 | ||
|
|
f31ec881ef | ||
|
|
0b838d2055 | ||
|
|
b68aefe64d | ||
|
|
9340c296ee | ||
|
|
132ed20d18 | ||
|
|
2927c37776 | ||
|
|
ac63788ecd | ||
|
|
0a64c9d329 | ||
|
|
81c9f3aa33 | ||
|
|
6fae33074a | ||
|
|
679b282061 | ||
|
|
396b4cb598 | ||
|
|
8b67b82047 | ||
|
|
da7d771898 | ||
|
|
42b5942886 | ||
|
|
d848c42bd3 | ||
|
|
e9d034928f | ||
|
|
2f042819ac | ||
|
|
2177d2f2cc | ||
|
|
4d974e4d7f | ||
|
|
cb05a1782a | ||
|
|
5f7cb7641a | ||
|
|
ff62f24cd4 | ||
|
|
d45dfb7dd2 | ||
|
|
1d4de873cb | ||
|
|
edfc1f0610 | ||
|
|
a662a23d19 | ||
|
|
7cff1c8736 | ||
|
|
6bfdebcff5 | ||
|
|
5b3158cb15 | ||
|
|
baa3439646 | ||
|
|
ac040bd62b | ||
|
|
e0a9e33623 | ||
|
|
b29f959dda | ||
|
|
7a5ec9d347 | ||
|
|
28ddad0549 | ||
|
|
615746a323 | ||
|
|
e49d5a3219 | ||
|
|
a31f7cac0d | ||
|
|
9b65fe6c86 | ||
|
|
3b5dcd41d9 | ||
|
|
f4db1385fc | ||
|
|
1b844254fe | ||
|
|
bf924e5da0 | ||
|
|
da37be391e | ||
|
|
2a44054387 | ||
|
|
4c85c4d336 | ||
|
|
03030ae93f | ||
|
|
ed438e8877 | ||
|
|
e7700787b2 | ||
|
|
6cccb7b9b0 | ||
|
|
f8678ed024 | ||
|
|
9b48e3dd36 | ||
|
|
f5db698705 | ||
|
|
2a24642220 | ||
|
|
5cd8ea6926 | ||
|
|
633b33430a | ||
|
|
341e57eeb5 | ||
|
|
2a9b79f086 | ||
|
|
fc5a7bf9ce | ||
|
|
fbb57dc9ca | ||
|
|
2358984605 | ||
|
|
28ae0b8963 | ||
|
|
4e316440b9 | ||
|
|
f9c65c8fb2 | ||
|
|
d3c5d1a617 | ||
|
|
b57cdaef14 | ||
|
|
1f2ad4ea36 | ||
|
|
4c3537d869 | ||
|
|
6f1ff9f276 |
@@ -0,0 +1 @@
|
||||
6.2.3
|
||||
+10
-1
@@ -1,4 +1,4 @@
|
||||
7
|
||||
8
|
||||
|
||||
3.2.2
|
||||
3.2.3
|
||||
@@ -6,3 +6,12 @@
|
||||
|
||||
4.0.2
|
||||
4.1.2
|
||||
|
||||
6.1.2
|
||||
6.1.3
|
||||
6.1.4
|
||||
6.1.5
|
||||
6.2.2
|
||||
|
||||
|
||||
6.2.3
|
||||
|
||||
+3
-1
@@ -8,7 +8,9 @@ module.exports = {
|
||||
ignorePatterns: [
|
||||
...base.ignorePatterns,
|
||||
'**/target/*',
|
||||
'packages/bytes/*'
|
||||
'**/build-wasm/*',
|
||||
'**/deno/*.ts',
|
||||
'**/test/*.ts'
|
||||
],
|
||||
parserOptions: {
|
||||
...base.parserOptions,
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<!--
|
||||
|
||||
For general support, howto, coding and bundling questions, please
|
||||
use the Substrate & Polkadot StackExchange at
|
||||
|
||||
https://substrate.stackexchange.com/
|
||||
|
||||
and get other ecosystem developers involved. This issues in this
|
||||
repository are meant for the tracking of feature requests and bug
|
||||
reports.
|
||||
|
||||
While all issues are looked at non-bug and non-features would take
|
||||
quite a bit longer to get to and may yield less than satisfactory
|
||||
responses in this format.
|
||||
|
||||
Additionally, please ensure you have done a search on the existing
|
||||
and closed issues before logging a new request. This saves time on
|
||||
all sides.
|
||||
|
||||
-->
|
||||
|
||||
|
||||
* **I'm submitting a ...**
|
||||
|
||||
<!---
|
||||
|
||||
REQUIRED:
|
||||
|
||||
Classify the type of report your are submitting
|
||||
|
||||
-->
|
||||
|
||||
- [ ] Bug report
|
||||
- [ ] Feature request
|
||||
- [ ] Support request
|
||||
- [ ] Other
|
||||
|
||||
|
||||
* **What is the current behavior and expected behavior?**
|
||||
|
||||
<!---
|
||||
|
||||
REQUIRED:
|
||||
|
||||
If you're describing a bug, tell us what should happen. If you're
|
||||
suggesting a change/improvement, tell us how it should work.
|
||||
|
||||
-->
|
||||
|
||||
|
||||
* **What is the motivation for changing the behavior?**
|
||||
|
||||
<!---
|
||||
|
||||
OPTIONAL:
|
||||
|
||||
Suggest a motivation for the request or ideas how to implement the
|
||||
addition or change
|
||||
|
||||
-->
|
||||
|
||||
|
||||
* **Please tell us about your environment:**
|
||||
|
||||
<!---
|
||||
|
||||
REQUIRED:
|
||||
|
||||
Include as many relevant details about the environment in which you
|
||||
experienced the issue. Also ensure that you have tested against the
|
||||
latest stable releases if you believe this to be a bug
|
||||
|
||||
-->
|
||||
|
||||
- Version:
|
||||
- Environment:
|
||||
|
||||
- [ ] Node.js
|
||||
- [ ] Browser
|
||||
- [ ] Other (limited support for other environments)
|
||||
|
||||
- Language:
|
||||
|
||||
- [ ] JavaScript
|
||||
- [ ] TypeScript (include tsc --version)
|
||||
- [ ] Other
|
||||
@@ -13,7 +13,10 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: ${{ matrix.step }}
|
||||
if: always()
|
||||
run: |
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
./scripts/install-build-deps.sh
|
||||
if [ "${{ matrix.step }}" != "lint" ]; then
|
||||
./scripts/install-build-deps.sh
|
||||
fi
|
||||
yarn ${{ matrix.step }}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
name: deno
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
pr:
|
||||
strategy:
|
||||
matrix:
|
||||
step: ['check', 'build']
|
||||
name: ${{ matrix.step }}
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
YARN_ENABLE_SCRIPTS: false
|
||||
WITH_DENO: true
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: denoland/setup-deno@v1
|
||||
with:
|
||||
deno-version: vx.x.x
|
||||
- name: ${{ matrix.step }}
|
||||
if: always()
|
||||
run: |
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
if [ "${{ matrix.step }}" != "check" ]; then
|
||||
./scripts/install-build-deps.sh
|
||||
fi
|
||||
yarn deno:setup
|
||||
yarn deno:${{ matrix.step }}
|
||||
@@ -3,6 +3,7 @@ bindgen/
|
||||
build/
|
||||
build-cjs/
|
||||
build-esm/
|
||||
build-deno/
|
||||
build-docs/
|
||||
build-wasm/
|
||||
bytes/
|
||||
|
||||
@@ -1,5 +1,57 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 6.2.3 Jul 7, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Optimize WASM init with pre-allocated buffers
|
||||
- Additional platform-specific tests
|
||||
|
||||
|
||||
## 6.2.2 Jul 4, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Protect against potential low-level double-sign leak in dalek-ed25519 (Don't use provided input pubKey, see https://github.com/MystenLabs/ed25519-unsafe-libs)
|
||||
|
||||
|
||||
## 6.2.1 Jul 1, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Add missing `peerDependencies` to `wasm-crypto` (`bridge` requirement)
|
||||
- Adjust `WebAssembly.{Memory, ModuleImports}` usage to cater for non-dom TS
|
||||
|
||||
|
||||
## 6.1.5 Jun 23, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust build outputs for Deno targets
|
||||
|
||||
|
||||
## 6.1.4 Jun 22, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust build outputs for Deno targets
|
||||
|
||||
|
||||
## 6.1.3 Jun 21, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Fix bundle publish (from dev bump)
|
||||
|
||||
|
||||
## 6.1.2 Jun 21, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust assert usage in all internal non-test code
|
||||
- Additional comments where missing
|
||||
|
||||
|
||||
## 6.1.1 May 13, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
417 Jaco 6.1.1 (#350)
|
||||
457 Jaco 6.2.3 (w/ dev publish fix) (#391)
|
||||
1 Evgeny Fixed type (#121)
|
||||
1 Shunfan Zhou Support sr25519 agreement (#209)
|
||||
1 Steve Degosserie Expose Schnorrkel's VRF capabilities (#170)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// auto-generated via deno/setup.ts, do not edit
|
||||
|
||||
export * as wasmBridge from 'https://deno.land/x/polkadot/wasm-bridge/mod.ts';
|
||||
export * as wasmCrypto from 'https://deno.land/x/polkadot/wasm-crypto/mod.ts';
|
||||
export * as wasmCryptoAsmjs from 'https://deno.land/x/polkadot/wasm-crypto-asmjs/mod.ts';
|
||||
export * as wasmCryptoInit from 'https://deno.land/x/polkadot/wasm-crypto-init/mod.ts';
|
||||
export * as wasmCryptoWasm from 'https://deno.land/x/polkadot/wasm-crypto-wasm/mod.ts';
|
||||
export * as wasmUtil from 'https://deno.land/x/polkadot/wasm-util/mod.ts';
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"imports": {
|
||||
"crypto": "https://deno.land/std/node/crypto.ts",
|
||||
"@polkadot/util": "https://deno.land/x/polkadot/common/mod.ts",
|
||||
"https://deno.land/x/polkadot/wasm-crypto-init/": "../packages/wasm-crypto-init/build-deno/",
|
||||
"https://deno.land/x/polkadot/wasm-bridge/": "../packages/wasm-bridge/build-deno/",
|
||||
"https://deno.land/x/polkadot/wasm-crypto-wasm/": "../packages/wasm-crypto-wasm/build-deno/",
|
||||
"https://deno.land/x/polkadot/wasm-crypto/": "../packages/wasm-crypto/build-deno/",
|
||||
"https://deno.land/x/polkadot/wasm-crypto-asmjs/": "../packages/wasm-crypto-asmjs/build-deno/",
|
||||
"https://deno.land/x/polkadot/wasm-util/": "../packages/wasm-util/build-deno/"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
import { stringCamelCase } from 'https://deno.land/x/polkadot/util/mod.ts';
|
||||
|
||||
const exports: string[] = [];
|
||||
const imports: Record<string, string> = {
|
||||
'@polkadot/util': 'https://deno.land/x/polkadot/common/mod.ts',
|
||||
crypto: 'https://deno.land/std/node/crypto.ts'
|
||||
};
|
||||
|
||||
for await (const entry of Deno.readDir('packages')) {
|
||||
if (entry.isDirectory) {
|
||||
try {
|
||||
await Deno.readTextFile(`packages/${entry.name}/src/mod.ts`);
|
||||
|
||||
exports.push(`export * as ${stringCamelCase(entry.name)} from 'https://deno.land/x/polkadot/${entry.name}/mod.ts';`);
|
||||
|
||||
imports[`https://deno.land/x/polkadot/${entry.name}/`] = `../packages/${entry.name}/build-deno/`;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Deno.writeTextFile('deno/check.ts', `// Copyright 2019-${new Date().getFullYear()} @polkadot/wasm authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\n// auto-generated via deno/setup.ts, do not edit\n\n${exports.sort().join('\n')}\n`);
|
||||
|
||||
await Deno.writeTextFile('deno/import_map.json', `${JSON.stringify({ imports }, null, 2)}\n`);
|
||||
+8
-4
@@ -10,7 +10,7 @@
|
||||
"url": "https://github.com/polkadot-js/wasm.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
@@ -22,21 +22,25 @@
|
||||
"build:rollup": "polkadot-exec-rollup --config",
|
||||
"build:wasm": "./scripts/build.sh",
|
||||
"clean": "./scripts/clean.sh",
|
||||
"deno:build": "yarn build",
|
||||
"deno:check": "yarn build:js && deno check --import-map=deno/import_map.json deno/check.ts",
|
||||
"deno:setup": "deno run --import-map=deno/import_map.json --allow-read --allow-write deno/setup.ts",
|
||||
"lint": "polkadot-dev-run-lint",
|
||||
"postinstall": "polkadot-dev-yarn-only",
|
||||
"test": "yarn test:wasm-crypto:rust",
|
||||
"test:wasm-crypto:deno": "deno test --allow-read --import-map=deno/import_map.json packages/wasm-crypto/test/deno.ts",
|
||||
"test:wasm-crypto:js": "yarn test:wasm-crypto:js:jest && yarn test:wasm-crypto:js:node",
|
||||
"test:wasm-crypto:js:jest": "polkadot-dev-run-test ./test/jest.spec.js",
|
||||
"test:wasm-crypto:js:node": "cd packages/wasm-crypto && node ./test/wasm.cjs && node ./test/asm.cjs",
|
||||
"test:wasm-crypto:rust": "cd packages/wasm-crypto && RUST_BACKTRACE=full cargo test --release -- --test-threads=1 --nocapture"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.17.10",
|
||||
"@polkadot/dev": "^0.66.21",
|
||||
"@babel/core": "^7.18.6",
|
||||
"@polkadot/dev": "^0.67.57",
|
||||
"fflate": "^0.7.3",
|
||||
"override-require": "^1.1.1"
|
||||
},
|
||||
"resolutions": {
|
||||
"typescript": "^4.6.4"
|
||||
"typescript": "^4.7.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
# @polkadot/wasm-bridge
|
||||
|
||||
A re-usable bridge between JS & WASM interfaces. It is used internally by `@polkadot/wasm-crypto`.
|
||||
|
||||
@@ -20,14 +20,14 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9"
|
||||
"@babel/runtime": "^7.18.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^9.1.1",
|
||||
"@polkadot/x-randomvalues": "^9.1.1"
|
||||
"@polkadot/util": "^9.7.2",
|
||||
"@polkadot/x-randomvalues": "^9.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "*",
|
||||
|
||||
@@ -1,14 +1,27 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-bridge authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// A number of functions are "unsafe" and purposefully so - it is
|
||||
// assumed that where the bridge is used, it is correctly wrapped
|
||||
// in a safeguard (see withWasm in the wasm-crypto package) which
|
||||
// then ensures that the internal wasm instance here is available
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
import type { BridgeBase, InitFn, InitPromise, WasmBaseInstance } from './types';
|
||||
import type { BridgeBase, InitFn, InitPromise, WasmBaseInstance, WasmImports } from './types';
|
||||
|
||||
import { stringToU8a, u8aToString } from '@polkadot/util';
|
||||
|
||||
import { Wbg } from './wbg';
|
||||
|
||||
/**
|
||||
* @name Bridge
|
||||
* @description
|
||||
* Creates a bridge between the JS and WASM environments.
|
||||
*
|
||||
* For any bridge it is passed an function white is then called internally at the
|
||||
* time of initialization. This affectively implements the layer between WASM and
|
||||
* the native environment, providing all the plumbing needed for the Wbg classes.
|
||||
*/
|
||||
export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
#cachegetInt32: Int32Array | null;
|
||||
#cachegetUint8: Uint8Array | null;
|
||||
@@ -18,7 +31,7 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
#wasm: C | null;
|
||||
#wasmError: string | null;
|
||||
#wasmPromise: InitPromise<C> | null;
|
||||
#wbg: WebAssembly.ModuleImports;
|
||||
#wbg: WasmImports;
|
||||
#type: 'asm' | 'wasm' | 'none';
|
||||
|
||||
constructor (createWasm: InitFn<C>) {
|
||||
@@ -36,22 +49,22 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
this.#wbg = { ...new Wbg(this) };
|
||||
}
|
||||
|
||||
/** @description Returns the init error */
|
||||
get error (): string | null {
|
||||
return this.#wasmError;
|
||||
}
|
||||
|
||||
/** @description Returns the init type */
|
||||
get type (): 'asm' | 'wasm' | 'none' {
|
||||
return this.#type;
|
||||
}
|
||||
|
||||
get wbg (): WebAssembly.ModuleImports {
|
||||
return this.#wbg;
|
||||
}
|
||||
|
||||
/** @description Returns the created wasm interface */
|
||||
get wasm (): C | null {
|
||||
return this.#wasm;
|
||||
}
|
||||
|
||||
/** @description Performs the wasm initialization */
|
||||
async init (createWasm?: InitFn<C>): Promise<C | null> {
|
||||
if (!this.#wasmPromise || createWasm) {
|
||||
this.#wasmPromise = (createWasm || this.#createWasm)(this.#wbg);
|
||||
@@ -66,10 +79,18 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return this.#wasm;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Gets an object from the heap
|
||||
*/
|
||||
getObject (idx: number): unknown {
|
||||
return this.#heap[idx];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Removes an object from the heap
|
||||
*/
|
||||
dropObject (idx: number) {
|
||||
if (idx < 36) {
|
||||
return;
|
||||
@@ -79,6 +100,10 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
this.#heapNext = idx;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieves and removes an object to the heap
|
||||
*/
|
||||
takeObject (idx: number): unknown {
|
||||
const ret = this.getObject(idx);
|
||||
|
||||
@@ -87,6 +112,10 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Adds an object to the heap
|
||||
*/
|
||||
addObject (obj: unknown): number {
|
||||
if (this.#heapNext === this.#heap.length) {
|
||||
this.#heap.push(this.#heap.length + 1);
|
||||
@@ -100,6 +129,10 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return idx;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieve an Int32 in the WASM interface
|
||||
*/
|
||||
getInt32 (): Int32Array {
|
||||
if (this.#cachegetInt32 === null || this.#cachegetInt32.buffer !== this.#wasm!.memory.buffer) {
|
||||
this.#cachegetInt32 = new Int32Array(this.#wasm!.memory.buffer);
|
||||
@@ -108,6 +141,10 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return this.#cachegetInt32;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieve an Uint8Array in the WASM interface
|
||||
*/
|
||||
getUint8 (): Uint8Array {
|
||||
if (this.#cachegetUint8 === null || this.#cachegetUint8.buffer !== this.#wasm!.memory.buffer) {
|
||||
this.#cachegetUint8 = new Uint8Array(this.#wasm!.memory.buffer);
|
||||
@@ -116,14 +153,26 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return this.#cachegetUint8;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieves an Uint8Array in the WASM interface
|
||||
*/
|
||||
getU8a (ptr: number, len: number): Uint8Array {
|
||||
return this.getUint8().subarray(ptr / 1, ptr / 1 + len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieves a string in the WASM interface
|
||||
*/
|
||||
getString (ptr: number, len: number): string {
|
||||
return u8aToString(this.getU8a(ptr, len));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Allocates an Uint8Array in the WASM interface
|
||||
*/
|
||||
allocU8a (arg: Uint8Array): [number, number] {
|
||||
const ptr = this.#wasm!.__wbindgen_malloc(arg.length * 1);
|
||||
|
||||
@@ -132,10 +181,18 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return [ptr, arg.length];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Allocates a string in the WASM interface
|
||||
*/
|
||||
allocString (arg: string): [number, number] {
|
||||
return this.allocU8a(stringToU8a(arg));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieves an Uint8Array from the WASM interface
|
||||
*/
|
||||
resultU8a (): Uint8Array {
|
||||
const r0 = this.getInt32()[8 / 4 + 0];
|
||||
const r1 = this.getInt32()[8 / 4 + 1];
|
||||
@@ -146,6 +203,10 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Retrieve a string from the WASM interface
|
||||
*/
|
||||
resultString (): string {
|
||||
return u8aToString(this.resultU8a());
|
||||
}
|
||||
|
||||
@@ -3,4 +3,3 @@
|
||||
|
||||
export * from './bridge';
|
||||
export * from './init';
|
||||
export * from './wbg';
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-bundle authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { InitFn, InitPromise, InitResult, WasmBaseInstance } from './types';
|
||||
import type { InitFn, InitPromise, InitResult, WasmBaseInstance, WasmImports } from './types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmBytes: null | Uint8Array, asmFn: null | ((wbg: WebAssembly.ModuleImports) => C)): InitFn<C> {
|
||||
return async (wbg: WebAssembly.ModuleImports): InitPromise<C> => {
|
||||
/**
|
||||
* @name createWasmFn
|
||||
* @description
|
||||
* Create a WASM (or ASM.js) creator interface based on the supplied information.
|
||||
*
|
||||
* It will attempt to create a WASM interface first and if this fails or is not available in
|
||||
* the environment, will fallback to attempting to create an ASM.js interface.
|
||||
*/
|
||||
export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmBytes: null | Uint8Array, asmFn: null | ((wbg: WasmImports) => C)): InitFn<C> {
|
||||
return async (wbg: WasmImports): InitPromise<C> => {
|
||||
const result: InitResult<C> = {
|
||||
error: null,
|
||||
type: 'none',
|
||||
@@ -14,7 +20,11 @@ export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmByt
|
||||
};
|
||||
|
||||
try {
|
||||
assert(typeof WebAssembly === 'object' && typeof WebAssembly.instantiate === 'function' && wasmBytes && wasmBytes.length, 'WebAssembly is not available in your environment');
|
||||
if (!wasmBytes || !wasmBytes.length) {
|
||||
throw new Error('No WebAssembly provided for initialization');
|
||||
} else if (typeof WebAssembly !== 'object' || typeof WebAssembly.instantiate !== 'function') {
|
||||
throw new Error('WebAssembly is not available in your environment');
|
||||
}
|
||||
|
||||
const source = await WebAssembly.instantiate(wasmBytes, { wbg });
|
||||
|
||||
@@ -22,7 +32,7 @@ export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmByt
|
||||
result.type = 'wasm';
|
||||
} catch (error) {
|
||||
// if we have a valid supplied asm.js, return that
|
||||
if (asmFn) {
|
||||
if (typeof asmFn === 'function') {
|
||||
result.wasm = asmFn(wbg);
|
||||
result.type = 'asm';
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-bridge authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './index';
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/wasm-bridge', path: 'auto', type: 'auto', version: '6.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/wasm-bridge', path: 'auto', type: 'auto', version: '6.2.3' };
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-bridge authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable camelcase */
|
||||
// Use non-strong types instead of WasmImports which may not
|
||||
// be available as part of the TS environment types (it needs dom)
|
||||
export type WasmImports = Record<string, (...args: never[]) => unknown>;
|
||||
|
||||
// Use non-strong types instead of WebAssembly.Memory which may not
|
||||
// be available as part of the TS environment types (it needs dom)
|
||||
export type WasmMemory = { buffer: ArrayBuffer };
|
||||
|
||||
export declare interface InitResult<C extends WasmBaseInstance> {
|
||||
error: string | null;
|
||||
@@ -11,11 +17,9 @@ export declare interface InitResult<C extends WasmBaseInstance> {
|
||||
|
||||
export type InitPromise <C extends WasmBaseInstance> = Promise<InitResult<C>>;
|
||||
|
||||
export type InitFn <C extends WasmBaseInstance> = (wbg: WebAssembly.ModuleImports) => InitPromise<C>;
|
||||
export type InitFn <C extends WasmBaseInstance> = (wbg: WasmImports) => InitPromise<C>;
|
||||
|
||||
export interface BridgeBase<C extends WasmBaseInstance> extends InitResult<C> {
|
||||
wbg: WebAssembly.ModuleImports;
|
||||
|
||||
init (createWasm?: InitFn<C>): Promise<C | null>;
|
||||
getObject (idx: number): unknown;
|
||||
dropObject (idx: number): void;
|
||||
@@ -32,7 +36,7 @@ export interface BridgeBase<C extends WasmBaseInstance> extends InitResult<C> {
|
||||
}
|
||||
|
||||
export interface WasmBaseInstance {
|
||||
memory: WebAssembly.Memory;
|
||||
memory: WasmMemory;
|
||||
|
||||
// wbindgen functions (required and used internally)
|
||||
|
||||
|
||||
@@ -8,6 +8,17 @@ import { getRandomValues } from '@polkadot/x-randomvalues';
|
||||
const DEFAULT_CRYPTO = { getRandomValues };
|
||||
const DEFAULT_SELF = { crypto: DEFAULT_CRYPTO };
|
||||
|
||||
/**
|
||||
* @name Wbg
|
||||
* @description
|
||||
* This defines the internal interfaces that wasm-bindgen used to communicate
|
||||
* with the host layer. None of these functions are available to the user, rather
|
||||
* they are called internally from the WASM code itself.
|
||||
*
|
||||
* The interfaces here are exposed in the imports on the created WASM interfaces.
|
||||
*
|
||||
* Internally the implementation does a thin layer into the supplied bridge.
|
||||
*/
|
||||
export class Wbg<C extends WasmBaseInstance> {
|
||||
#bridge: BridgeBase<C>;
|
||||
|
||||
@@ -15,41 +26,50 @@ export class Wbg<C extends WasmBaseInstance> {
|
||||
this.#bridge = bridge;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
abort = (): never => {
|
||||
throw new Error('abort');
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
__wbindgen_is_undefined = (idx: number): boolean => {
|
||||
return this.#bridge.getObject(idx) === undefined;
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
__wbindgen_throw = (ptr: number, len: number): boolean => {
|
||||
throw new Error(this.#bridge.getString(ptr, len));
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
__wbg_self_1b7a39e3a92c949c = (): number => {
|
||||
return this.#bridge.addObject(DEFAULT_SELF);
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
__wbg_require_604837428532a733 = (ptr: number, len: number): never => {
|
||||
throw new Error(`Unable to require ${this.#bridge.getString(ptr, len)}`);
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
__wbg_crypto_968f1772287e2df0 = (_idx: number): number => {
|
||||
return this.#bridge.addObject(DEFAULT_CRYPTO);
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
__wbg_getRandomValues_a3d34b4fee3c2869 = (_idx: number): number => {
|
||||
return this.#bridge.addObject(DEFAULT_CRYPTO.getRandomValues);
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
__wbg_getRandomValues_f5e14ab7ac8e995d = (_arg0: number, ptr: number, len: number): void => {
|
||||
DEFAULT_CRYPTO.getRandomValues(this.#bridge.getU8a(ptr, len));
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
__wbg_randomFillSync_d5bd2d655fdf256a = (_idx: number, _ptr: number, _len: number): never => {
|
||||
throw new Error('randomFillsync is not available');
|
||||
@@ -57,11 +77,8 @@ export class Wbg<C extends WasmBaseInstance> {
|
||||
// getObject(idx).randomFillSync(getU8a(ptr, len));
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
__wbindgen_object_drop_ref = (idx: number): void => {
|
||||
this.#bridge.takeObject(idx);
|
||||
};
|
||||
}
|
||||
|
||||
export function initWbg <C extends WasmBaseInstance> (bridge: BridgeBase<C>): Wbg<C> {
|
||||
return new Wbg(bridge);
|
||||
}
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
## @polkadot/wasm-crypto-asmjs
|
||||
|
||||
Asm.js build outputs.
|
||||
|
||||
@@ -17,13 +17,13 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9"
|
||||
"@babel/runtime": "^7.18.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^9.1.1"
|
||||
"@polkadot/util": "^9.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "*"
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export declare const asmJsInit: null;
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const asmJsInit = null;
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './index';
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto-asmjs', path: 'auto', type: 'auto', version: '6.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto-asmjs', path: 'auto', type: 'auto', version: '6.2.3' };
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
## @polkadot/wasm-crypto-init
|
||||
|
||||
Internal `@polkadot/wasm-crypto` initialization functions for Browser, Node & RN.
|
||||
|
||||
@@ -20,20 +20,22 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"browser": "wasm.js",
|
||||
"main": "wasm.js",
|
||||
"react-native": "asm.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/wasm-bridge": "6.1.1",
|
||||
"@polkadot/wasm-crypto-asmjs": "6.1.1",
|
||||
"@polkadot/wasm-crypto-wasm": "6.1.1"
|
||||
"@babel/runtime": "^7.18.6",
|
||||
"@polkadot/wasm-bridge": "6.2.3",
|
||||
"@polkadot/wasm-crypto-asmjs": "6.2.3",
|
||||
"@polkadot/wasm-crypto-wasm": "6.2.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^9.1.1"
|
||||
"@polkadot/util": "^9.7.2",
|
||||
"@polkadot/x-randomvalues": "^9.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "*"
|
||||
"@polkadot/util": "*",
|
||||
"@polkadot/x-randomvalues": "*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,9 @@ import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
/**
|
||||
* @name createWasm
|
||||
* @description
|
||||
* Creates an interface using only ASM.js
|
||||
*/
|
||||
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', null, asmJsInit);
|
||||
|
||||
@@ -10,4 +10,9 @@ import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
/**
|
||||
* @name createWasm
|
||||
* @description
|
||||
* Creates an interface using WASM and a fallback ASM.js
|
||||
*/
|
||||
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', wasmBytes, asmJsInit);
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-init authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './wasm';
|
||||
@@ -8,4 +8,9 @@ import { createWasmFn } from '@polkadot/wasm-bridge';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
/**
|
||||
* @name createWasm
|
||||
* @description
|
||||
* Creates an interface using no WASM and no ASM.js
|
||||
*/
|
||||
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', null, null);
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto-init', path: 'auto', type: 'auto', version: '6.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto-init', path: 'auto', type: 'auto', version: '6.2.3' };
|
||||
|
||||
@@ -9,4 +9,9 @@ import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
/**
|
||||
* @name createWasm
|
||||
* @description
|
||||
* Creates an interface using only WASM
|
||||
*/
|
||||
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', wasmBytes, null);
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
## @polkadot/wasm-crypto-wasm
|
||||
|
||||
Wasm build outputs.
|
||||
|
||||
@@ -17,14 +17,14 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/wasm-util": "6.1.1"
|
||||
"@babel/runtime": "^7.18.6",
|
||||
"@polkadot/wasm-util": "6.2.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^9.1.1"
|
||||
"@polkadot/util": "^9.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "*"
|
||||
|
||||
@@ -3,8 +3,17 @@
|
||||
|
||||
import { base64Decode, unzlibSync } from '@polkadot/wasm-util';
|
||||
|
||||
import { bytes, sizeUncompressed } from './cjs/bytes';
|
||||
import { bytes, lenIn, lenOut } from './cjs/bytes.js';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
export const wasmBytes = unzlibSync(base64Decode(bytes), new Uint8Array(sizeUncompressed));
|
||||
/**
|
||||
* @name wasmBytes
|
||||
* @description
|
||||
* The decoded WASM interface as exposed by this package.
|
||||
*
|
||||
* The build process will output into cjs/* into a compressed base64 format.
|
||||
* Upon loading the exposed bytes will be decoded and decompressed form this
|
||||
* specific format and returned.
|
||||
*/
|
||||
export const wasmBytes = unzlibSync(base64Decode(bytes, new Uint8Array(lenIn)), new Uint8Array(lenOut));
|
||||
|
||||
+5
-3
@@ -1,6 +1,8 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export declare const bytes = '';
|
||||
export declare const sizeCompressed = 0;
|
||||
export declare const sizeUncompressed = 0;
|
||||
export declare const bytes: string;
|
||||
|
||||
export declare const lenIn: number;
|
||||
|
||||
export declare const lenOut: number;
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
|
||||
module.exports.bytes = '';
|
||||
|
||||
module.exports.sizeCompressed = 0;
|
||||
module.exports.lenIn = 0;
|
||||
|
||||
module.exports.sizeUncompressed = 0;
|
||||
module.exports.lenOut = 0;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export declare const bytes: string;
|
||||
|
||||
export declare const lenIn: number;
|
||||
|
||||
export declare const lenOut: number;
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const bytes = '';
|
||||
|
||||
export const lenIn = 0;
|
||||
|
||||
export const lenOut = 0;
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './index';
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto-wasm', path: 'auto', type: 'auto', version: '6.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto-wasm', path: 'auto', type: 'auto', version: '6.2.3' };
|
||||
|
||||
@@ -11,21 +11,19 @@ Install the package (also requires `@polkadot/util` for `TextEncoder` polyfills
|
||||
Use it -
|
||||
|
||||
```js
|
||||
const { u8aToHex } = require('@polkadot/util');
|
||||
const { bip39Generate, bip39ToSeed, waitReady } = require('@polkadot/wasm-crypto');
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { bip39Generate, bip39ToSeed, waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
async function main () {
|
||||
// first wait until the WASM has been loaded (async init)
|
||||
await waitReady();
|
||||
// first wait until the WASM has been loaded (async init)
|
||||
await waitReady();
|
||||
|
||||
// generate phrase
|
||||
const phrase = bip39Generate(12);
|
||||
// generate phrase
|
||||
const phrase = bip39Generate(12);
|
||||
|
||||
// get ed25519 seed from phrase
|
||||
const seed = bip39ToSeed(phrase, '');
|
||||
// get ed25519 seed from phrase
|
||||
const seed = bip39ToSeed(phrase, '');
|
||||
|
||||
// display
|
||||
console.log('phrase:', phrase);
|
||||
console.log('seed:', u8aToHex(seed));
|
||||
}
|
||||
// display
|
||||
console.log('phrase:', phrase);
|
||||
console.log('seed:', u8aToHex(seed));
|
||||
```
|
||||
|
||||
@@ -28,20 +28,22 @@
|
||||
"./initWasmAsm.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/wasm-bridge": "6.1.1",
|
||||
"@polkadot/wasm-crypto-asmjs": "6.1.1",
|
||||
"@polkadot/wasm-crypto-init": "6.1.1",
|
||||
"@polkadot/wasm-crypto-wasm": "6.1.1",
|
||||
"@polkadot/wasm-util": "6.1.1"
|
||||
"@babel/runtime": "^7.18.6",
|
||||
"@polkadot/wasm-bridge": "6.2.3",
|
||||
"@polkadot/wasm-crypto-asmjs": "6.2.3",
|
||||
"@polkadot/wasm-crypto-init": "6.2.3",
|
||||
"@polkadot/wasm-crypto-wasm": "6.2.3",
|
||||
"@polkadot/wasm-util": "6.2.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^9.1.1"
|
||||
"@polkadot/util": "^9.7.2",
|
||||
"@polkadot/x-randomvalues": "^9.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "*"
|
||||
"@polkadot/util": "*",
|
||||
"@polkadot/x-randomvalues": "*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,21 +3,38 @@
|
||||
|
||||
import type { WasmCryptoInstance } from '@polkadot/wasm-crypto-init/types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { bridge, initBridge } from './init';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
export { bridge };
|
||||
|
||||
// Removes the first parameter (expected as WasmCryptoInstance) and leaves the
|
||||
// rest of the parameters in-tack. This allows us to dynamically create a function
|
||||
// return from the withWasm helper
|
||||
type PopFirst<T extends unknown[]> =
|
||||
T extends [WasmCryptoInstance, ...infer N]
|
||||
? N
|
||||
: [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description
|
||||
* This create an extenal interface function from the signature, all the while checking
|
||||
* the actual bridge wasm interface to ensure it has been initialized.
|
||||
*
|
||||
* This means that we can call it
|
||||
*
|
||||
* withWasm(wasm: WasmCryptoInstance, a: number, b: string) => Uint8Array
|
||||
*
|
||||
* and in this case it will create an interface function with the signarure
|
||||
*
|
||||
* (a: number, b: string) => Uint8Array
|
||||
*/
|
||||
function withWasm <T, F extends (wasm: WasmCryptoInstance, ...params: never[]) => T> (fn: F): (...params: PopFirst<Parameters<F>>) => ReturnType<F> {
|
||||
return (...params: PopFirst<Parameters<F>>): ReturnType<F> => {
|
||||
assert(bridge.wasm, 'The WASM interface has not been initialized. Ensure that you wait for the initialization Promise with waitReady() from @polkadot/wasm-crypto (or cryptoWaitReady() from @polkadot/util-crypto) before attempting to use WASM-only interfaces.');
|
||||
if (!bridge.wasm) {
|
||||
throw new Error('The WASM interface has not been initialized. Ensure that you wait for the initialization Promise with waitReady() from @polkadot/wasm-crypto (or cryptoWaitReady() from @polkadot/util-crypto) before attempting to use WASM-only interfaces.');
|
||||
}
|
||||
|
||||
return fn(bridge.wasm, ...params) as ReturnType<F>;
|
||||
};
|
||||
|
||||
@@ -7,8 +7,19 @@ import type { WasmCryptoInstance } from '@polkadot/wasm-crypto-init/types';
|
||||
import { Bridge } from '@polkadot/wasm-bridge';
|
||||
import { createWasm } from '@polkadot/wasm-crypto-init';
|
||||
|
||||
/**
|
||||
* @name bridge
|
||||
* @description
|
||||
* The JS <-> WASM bridge that is in operation. For the specific package
|
||||
* it is a global, i.e. all operations happens on this specific bridge
|
||||
*/
|
||||
export const bridge = new Bridge<WasmCryptoInstance>(createWasm);
|
||||
|
||||
/**
|
||||
* @name initBridge
|
||||
* @description
|
||||
* Creates a new bridge interface with the (optional) initialization function
|
||||
*/
|
||||
export async function initBridge (createWasm?: InitFn<WasmCryptoInstance>): Promise<WasmCryptoInstance | null> {
|
||||
return bridge.init(createWasm);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,15 @@ import { createWasm } from '@polkadot/wasm-crypto-init/none';
|
||||
|
||||
import { initBridge } from './init';
|
||||
|
||||
/**
|
||||
* @name initWasm
|
||||
* @description
|
||||
* For historic purposes and for tighter control on init, specifically performing
|
||||
* a WASM initialization with no interface whatsoever (no WASM, no ASM.js)
|
||||
*
|
||||
* Generally should not be used unless you want explicit control over which
|
||||
* interfaces are initialized.
|
||||
*/
|
||||
export async function initWasm (): Promise<void> {
|
||||
await initBridge(createWasm);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,15 @@ import { createWasm } from '@polkadot/wasm-crypto-init/asm';
|
||||
|
||||
import { initBridge } from './init';
|
||||
|
||||
/**
|
||||
* @name initWasm
|
||||
* @description
|
||||
* For historic purposes and for tighter control on init, specifically performing
|
||||
* a WASM initialization with only ASM.js
|
||||
*
|
||||
* Generally should not be used unless you want explicit control over which
|
||||
* interfaces are initialized.
|
||||
*/
|
||||
export async function initWasm (): Promise<void> {
|
||||
await initBridge(createWasm);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,15 @@ import { createWasm } from '@polkadot/wasm-crypto-init/wasm';
|
||||
|
||||
import { initBridge } from './init';
|
||||
|
||||
/**
|
||||
* @name initWasm
|
||||
* @description
|
||||
* For historic purposes and for tighter control on init, specifically performing
|
||||
* a WASM initialization with only WASM (generally the default for most platforms)
|
||||
*
|
||||
* Generally should not be used unless you want explicit control over which
|
||||
* interfaces are initialized.
|
||||
*/
|
||||
export async function initWasm (): Promise<void> {
|
||||
await initBridge(createWasm);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,15 @@ import { createWasm } from '@polkadot/wasm-crypto-init/both';
|
||||
|
||||
import { initBridge } from './init';
|
||||
|
||||
/**
|
||||
* @name initWasm
|
||||
* @description
|
||||
* For historic purposes and for tighter control on init, specifically performing
|
||||
* a WASM initialization with ASM and an ASM.js fallback
|
||||
*
|
||||
* Generally should not be used unless you want explicit control over which
|
||||
* interfaces are initialized.
|
||||
*/
|
||||
export async function initWasm (): Promise<void> {
|
||||
await initBridge(createWasm);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './index';
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto', path: 'auto', type: 'auto', version: '6.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/wasm-crypto', path: 'auto', type: 'auto', version: '6.2.3' };
|
||||
|
||||
@@ -6,15 +6,14 @@ use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature, Signer as _, Verif
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// Keypair helper function
|
||||
fn new_from_parts(pubkey: &[u8], seckey: &[u8]) -> Keypair {
|
||||
let mut pair = vec![];
|
||||
fn new_from_seed(seed: &[u8]) -> Keypair {
|
||||
match SecretKey::from_bytes(seed) {
|
||||
Ok(secret) => {
|
||||
let public: PublicKey = (&secret).into();
|
||||
|
||||
pair.extend_from_slice(seckey);
|
||||
pair.extend_from_slice(pubkey);
|
||||
|
||||
match Keypair::from_bytes(&pair) {
|
||||
Ok(p) => p,
|
||||
_ => panic!("Provided pair is invalid.")
|
||||
Keypair { secret: secret, public: public }
|
||||
},
|
||||
_ => panic!("Invalid seed provided.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,16 +25,9 @@ fn new_from_parts(pubkey: &[u8], seckey: &[u8]) -> Keypair {
|
||||
/// followed by the public key (32) bytes, as the full secret keys.
|
||||
#[wasm_bindgen]
|
||||
pub fn ext_ed_from_seed(seed: &[u8]) -> Vec<u8> {
|
||||
match &SecretKey::from_bytes(seed) {
|
||||
Ok(s) => {
|
||||
let pubkey: PublicKey = s.into();
|
||||
|
||||
new_from_parts(pubkey.as_bytes(), seed)
|
||||
.to_bytes()
|
||||
.to_vec()
|
||||
},
|
||||
_ => panic!("Invalid seed provided.")
|
||||
}
|
||||
new_from_seed(seed)
|
||||
.to_bytes()
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
/// Sign a message
|
||||
@@ -43,14 +35,16 @@ pub fn ext_ed_from_seed(seed: &[u8]) -> Vec<u8> {
|
||||
/// The combination of both public and private key must be provided.
|
||||
/// This is effectively equivalent to a keypair.
|
||||
///
|
||||
/// * pubkey: UIntArray with 32 element
|
||||
/// * _: UIntArray with 32 element (was pubkey, now ignored)
|
||||
/// * private: UIntArray with 64 element
|
||||
/// * message: Arbitrary length UIntArray
|
||||
///
|
||||
/// * returned vector is the signature consisting of 64 bytes.
|
||||
#[wasm_bindgen]
|
||||
pub fn ext_ed_sign(pubkey: &[u8], seckey: &[u8], message: &[u8]) -> Vec<u8> {
|
||||
new_from_parts(pubkey, seckey)
|
||||
pub fn ext_ed_sign(_: &[u8], seckey: &[u8], message: &[u8]) -> Vec<u8> {
|
||||
// https://github.com/MystenLabs/ed25519-unsafe-libs
|
||||
// we never use the provided pubkey
|
||||
new_from_seed(seckey)
|
||||
.sign(message)
|
||||
.to_bytes()
|
||||
.to_vec()
|
||||
|
||||
@@ -3,13 +3,12 @@
|
||||
|
||||
const { assert } = require('@polkadot/util');
|
||||
|
||||
const wasm = require('../../build');
|
||||
const bip39 = require('./bip39.cjs');
|
||||
const ed25519 = require('./ed25519.cjs');
|
||||
const hashing = require('./hashing.cjs');
|
||||
const secp256k1 = require('./secp256k1.cjs');
|
||||
const sr25519 = require('./sr25519.cjs');
|
||||
const vrf = require('./vrf.cjs');
|
||||
const bip39 = require('./bip39.js');
|
||||
const ed25519 = require('./ed25519.js');
|
||||
const hashing = require('./hashing.js');
|
||||
const secp256k1 = require('./secp256k1.js');
|
||||
const sr25519 = require('./sr25519.js');
|
||||
const vrf = require('./vrf.js');
|
||||
|
||||
const tests = {
|
||||
// We place secp256k1 first, this allows the interaction with it in the
|
||||
@@ -23,7 +22,7 @@ const tests = {
|
||||
...hashing
|
||||
};
|
||||
|
||||
async function beforeAll (name) {
|
||||
async function beforeAll (name, wasm) {
|
||||
const result = await wasm.waitReady();
|
||||
|
||||
console.log(`*** waitReady()=${result} for ${wasm.bridge.type}`);
|
||||
@@ -33,7 +32,7 @@ async function beforeAll (name) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function runAll (name) {
|
||||
function runAll (name, wasm) {
|
||||
const failed = [];
|
||||
let count = 0;
|
||||
|
||||
@@ -63,11 +62,11 @@ function runAll (name) {
|
||||
}
|
||||
}
|
||||
|
||||
function runUnassisted (name) {
|
||||
function runUnassisted (name, wasm) {
|
||||
console.log(`\n*** ${name}: Running tests`);
|
||||
|
||||
beforeAll(name)
|
||||
.then(() => runAll(name))
|
||||
beforeAll(name, wasm)
|
||||
.then(() => runAll(name, wasm))
|
||||
.then(() => {
|
||||
console.log(`\n*** ${name}: All passed`);
|
||||
process.exit(0);
|
||||
@@ -82,6 +81,5 @@ module.exports = {
|
||||
beforeAll,
|
||||
runAll,
|
||||
runUnassisted,
|
||||
tests,
|
||||
wasm
|
||||
tests
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
{ "type": "commonjs" }
|
||||
@@ -4,6 +4,7 @@
|
||||
require('./alias-imports.cjs');
|
||||
require('@polkadot/wasm-crypto/initOnlyAsm');
|
||||
|
||||
const { runUnassisted } = require('./all/index.cjs');
|
||||
const wasm = require('../build');
|
||||
const { runUnassisted } = require('./all/index.js');
|
||||
|
||||
runUnassisted('ASM');
|
||||
runUnassisted('ASM', wasm);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createRequire } from 'https://deno.land/std@0.107.0/node/module.ts';
|
||||
|
||||
import * as wasm from '../build-deno/mod.ts';
|
||||
|
||||
declare const globalThis: Record<string, unknown>;
|
||||
declare const require: (name: string) => any;
|
||||
|
||||
globalThis.require = createRequire(import.meta.url);
|
||||
|
||||
const exp = require('./all/index.js');
|
||||
|
||||
await exp.beforeAll('wasm', wasm);
|
||||
|
||||
Object.keys(exp.tests).forEach((name) => {
|
||||
Deno.test(name, () => exp.tests[name](wasm));
|
||||
});
|
||||
@@ -5,11 +5,14 @@
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { beforeAll as beforeAllFn, tests, wasm } from './all/index.cjs';
|
||||
import * as wasm from '../build';
|
||||
import { beforeAll as beforeAllFn, tests } from './all/index.js';
|
||||
|
||||
globalThis.wasm = wasm;
|
||||
|
||||
describe('wasm-crypto', () => {
|
||||
beforeAll(async () => {
|
||||
await beforeAllFn('wasm');
|
||||
await beforeAllFn('wasm', wasm);
|
||||
});
|
||||
|
||||
Object.keys(tests).forEach((name) => {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
require('./alias-imports.cjs');
|
||||
|
||||
const { runUnassisted } = require('./all/index.cjs');
|
||||
const wasm = require('../build');
|
||||
const { runUnassisted } = require('./all/index.js');
|
||||
|
||||
runUnassisted('WASM');
|
||||
runUnassisted('WASM', wasm);
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"exclude": [
|
||||
"**/test/*.ts"
|
||||
],
|
||||
"references": [
|
||||
{ "path": "../wasm-bridge/tsconfig.build.json" },
|
||||
{ "path": "../wasm-crypto-init/tsconfig.build.json" },
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
## @polkadot/wasm-util
|
||||
|
||||
Wasm utility functions, used internally in `@polkadot/wasm-crypto`.
|
||||
|
||||
@@ -20,13 +20,13 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "6.1.1",
|
||||
"version": "6.2.3",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9"
|
||||
"@babel/runtime": "^7.18.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^9.1.1"
|
||||
"@polkadot/util": "^9.7.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "*"
|
||||
|
||||
@@ -1,22 +1,37 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
const chr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
const map: Record<string, number> = {};
|
||||
|
||||
export function base64Decode (data: string): Uint8Array {
|
||||
const bytes = [];
|
||||
for (let i = 0; i < chr.length; i++) {
|
||||
map[chr[i]] = i;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name base64Decode
|
||||
* @description
|
||||
* A base64Decoding function that operates in all environments. Unlike decoding
|
||||
* from Buffer (Node.js only) or atob (browser-only) this implementation is
|
||||
* slightly slower, but it is platform independent.
|
||||
*
|
||||
* For our usage, since we have access to the static final size (where used), we
|
||||
* decode to a specified output buffer.
|
||||
*/
|
||||
export function base64Decode (data: string, out: Uint8Array): Uint8Array {
|
||||
let byte = 0;
|
||||
let bits = 0;
|
||||
let pos = -1;
|
||||
|
||||
for (let i = 0; i < data.length && data[i] !== '='; i++) {
|
||||
// each character represents 6 bits
|
||||
byte = (byte << 6) | chars.indexOf(data[i]);
|
||||
byte = (byte << 6) | map[data[i]];
|
||||
|
||||
// each byte needs to contain 8 bits
|
||||
if ((bits += 6) >= 8) {
|
||||
bytes.push((byte >>> (bits -= 8)) & 0xff);
|
||||
out[++pos] = (byte >>> (bits -= 8)) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
return Uint8Array.from(bytes);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2019-2022 @polkadot/wasm-util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './index';
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/wasm-util', path: 'auto', type: 'auto', version: '6.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/wasm-util', path: 'auto', type: 'auto', version: '6.2.3' };
|
||||
|
||||
@@ -10,6 +10,8 @@ source ../scripts/rust-version.sh
|
||||
WSM=$PKG_NAME/build-wasm/wasm_bg.wasm
|
||||
OPT=$PKG_NAME/build-wasm/wasm_opt.wasm
|
||||
ASM=$PKG_NAME-asmjs/build/cjs/data.js
|
||||
DENO_DIR=$PKG_NAME-asmjs/build-deno/deno
|
||||
DENO_ASM=$DENO_DIR/data.js
|
||||
|
||||
# build new via wasm-pack
|
||||
echo "*** Building Rust sources"
|
||||
@@ -36,11 +38,22 @@ node ../scripts/pack-wasm-base.cjs
|
||||
echo "*** Building asm.js version"
|
||||
../binaryen/bin/wasm2js -Oz --output $ASM $OPT
|
||||
|
||||
# copy the deno version
|
||||
mkdir -p $DENO_DIR
|
||||
cp $ASM $DENO_ASM
|
||||
|
||||
# cleanup the generated asm, converting to cjs
|
||||
sed -i -e '/import {/d' $ASM
|
||||
sed -i -e '1,/var retasmFunc = /!d' $ASM
|
||||
sed -i -e 's/var retasmFunc = .*/const asmJsInit = (wbg) => asmFunc(wbg);\n\nmodule.exports = { asmJsInit };/g' $ASM
|
||||
|
||||
# same as the cjs version, this time for deno
|
||||
sed -i -e '/import {/d' $DENO_ASM
|
||||
sed -i -e '1,/var retasmFunc = /!d' $DENO_ASM
|
||||
sed -i -e 's/var retasmFunc = .*/export const asmJsInit = (wbg) => asmFunc(wbg);/g' $DENO_ASM
|
||||
|
||||
# cleanups
|
||||
rm -rf $PKG_NAME-asmjs/build/cjs/*-e
|
||||
rm -rf $PKG_NAME-asmjs/build-deno/deno/*-e
|
||||
rm -rf $PKG_NAME-wasm/build/cjs/*-e
|
||||
rm -rf $PKG_NAME-wasm/build-deno/deno/*-e
|
||||
|
||||
+17
-11
@@ -13,18 +13,24 @@ cd packages
|
||||
|
||||
PKG_NAME=wasm-crypto ../scripts/build-wasm.sh
|
||||
|
||||
yarn test:wasm-crypto:js
|
||||
if [ -z "$WITH_DENO" ]; then
|
||||
yarn test:wasm-crypto:js
|
||||
else
|
||||
yarn test:wasm-crypto:deno
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "*** wasm-crypto"
|
||||
ls -alR wasm-crypto/build
|
||||
echo ""
|
||||
echo "*** wasm-crypto-asmjs"
|
||||
ls -alR wasm-crypto-asmjs/build
|
||||
echo ""
|
||||
echo "*** wasm-crypto-wasm"
|
||||
ls -alR wasm-crypto-wasm/build
|
||||
# echo ""
|
||||
# echo "*** wasm-crypto"
|
||||
# ls -alR wasm-crypto/build
|
||||
# echo ""
|
||||
# echo "*** wasm-crypto-asmjs"
|
||||
# ls -alR wasm-crypto-asmjs/build
|
||||
# echo ""
|
||||
# echo "*** wasm-crypto-wasm"
|
||||
# ls -alR wasm-crypto-wasm/build
|
||||
|
||||
cd ..
|
||||
|
||||
yarn build:rollup
|
||||
if [ -z "$WITH_DENO" ]; then
|
||||
yarn build:rollup
|
||||
fi
|
||||
|
||||
@@ -48,12 +48,12 @@ if [ ! -d "binaryen" ]; then
|
||||
echo "*** Downloading binaryen"
|
||||
curl -L $BINARYEN_REPO/releases/download/$BINARYEN_VER/$BINARYEN_ZIP.tar.gz | tar xz
|
||||
mv binaryen-$BINARYEN_VER binaryen
|
||||
ls -alR binaryen
|
||||
# ls -alR binaryen
|
||||
fi
|
||||
|
||||
if [ ! -d "bindgen" ]; then
|
||||
echo "*** Downloading bindgen"
|
||||
curl -L $BINDGEN_REPO/releases/download/$BINDGEN_VER/$BINDGEN_ZIP.tar.gz | tar xz
|
||||
mv $BINDGEN_ZIP bindgen
|
||||
ls -alR bindgen
|
||||
# ls -alR bindgen
|
||||
fi
|
||||
|
||||
@@ -3,21 +3,37 @@
|
||||
|
||||
const fflate = require('fflate/node');
|
||||
const fs = require('fs');
|
||||
const mkdirp = require('mkdirp');
|
||||
const { formatNumber } = require('@polkadot/util');
|
||||
|
||||
const data = fs.readFileSync(`./${process.env.PKG_NAME}/build-wasm/wasm_opt.wasm`);
|
||||
const compressed = Buffer.from(fflate.zlibSync(data, { level: 9 }));
|
||||
const denoDir = `./${process.env.PKG_NAME}-wasm/build-deno/deno`;
|
||||
|
||||
console.log(`*** Compressed WASM: ${formatNumber(data.length)} -> ${formatNumber(compressed.length)} (${(100 * compressed.length / data.length).toFixed(2)}%)`);
|
||||
|
||||
mkdirp.sync(denoDir);
|
||||
|
||||
fs.writeFileSync(`./${process.env.PKG_NAME}-wasm/build/cjs/bytes.js`, `// Copyright 2019-${new Date().getFullYear()} @polkadot/${process.env.PKG_NAME}-wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Generated as part of the build, do not edit
|
||||
|
||||
const sizeCompressed = ${compressed.length};
|
||||
const sizeUncompressed = ${data.length};
|
||||
const lenIn = ${compressed.length};
|
||||
const lenOut = ${data.length};
|
||||
const bytes = '${compressed.toString('base64')}';
|
||||
|
||||
module.exports = { bytes, sizeCompressed, sizeUncompressed };
|
||||
module.exports = { bytes, lenIn, lenOut };
|
||||
`);
|
||||
|
||||
fs.writeFileSync(`${denoDir}/bytes.js`, `// Copyright 2019-${new Date().getFullYear()} @polkadot/${process.env.PKG_NAME}-wasm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Generated as part of the build, do not edit
|
||||
|
||||
export const lenIn = ${compressed.length};
|
||||
|
||||
export const lenOut = ${data.length};
|
||||
|
||||
export const bytes = '${compressed.toString('base64')}';
|
||||
`);
|
||||
|
||||
Reference in New Issue
Block a user