Compare commits

...
35 Commits
Author SHA1 Message Date
github-actions[bot] 346720f384 [CI Skip] release/stable 5.1.1
skip-checks: true
2022-03-27 07:06:01 +00:00
Jaco aeade5636a 5.1.1 (#322) 2022-03-27 10:01:06 +03:00
github-actions[bot] 9f3c8f1f74 [CI Skip] release/beta 5.0.2-7
skip-checks: true
2022-03-27 06:29:19 +00:00
Jaco 3d1e48ec4b Bump TypeScript (#321) 2022-03-27 09:23:31 +03:00
github-actions[bot] cbffca3984 [CI Skip] release/beta 5.0.2-6
skip-checks: true
2022-03-26 20:05:39 +00:00
Jaco 9d6427d9da Bump dev deps (#320) 2022-03-26 18:10:18 +02:00
github-actions[bot] 368cb6806f [CI Skip] release/beta 5.0.2-5
skip-checks: true
2022-03-26 15:44:48 +00:00
Jaco 9795eaf31c Don't run auto-init, but set input (#319)
* Don't run auto-init, but set input

* Adjust
2022-03-26 17:39:57 +02:00
github-actions[bot] b4f7b0b177 [CI Skip] release/beta 5.0.2-4
skip-checks: true
2022-03-24 11:38:15 +00:00
Jaco 44048626cd Update secp256k1 library (#318)
* Update secp256k1 library

* Adjust

* compress/uncompress

* All of them?

* Test removal of lazy section hack

* Run tests sequentially

* build:mac target

* s/sepc256k1/secp256k1/

* CHANGELOG
2022-03-24 13:31:57 +02:00
github-actions[bot] 7fb8a1cb93 [CI Skip] release/beta 5.0.2-3
skip-checks: true
2022-03-23 13:26:55 +00:00
Jaco 2d544e7bc3 Check instantiate as function (#317)
* Check instantiate as function

* Rename wbg
2022-03-23 15:20:10 +02:00
github-actions[bot] 0f0a8131c5 [CI Skip] release/beta 5.0.2-2
skip-checks: true
2022-03-23 12:00:49 +00:00
Jaco f7e6acbf9c Add initNone (#316)
* Add initNone

* Add force to explicit call
2022-03-23 13:54:11 +02:00
github-actions[bot] eb445e79e2 [CI Skip] release/beta 5.0.2-1
skip-checks: true
2022-03-23 05:21:23 +00:00
Jaco ddee3dfd25 Bump dev (#314) 2022-03-23 07:15:15 +02:00
github-actions[bot] 8b7e6b90a1 [CI Skip] release/beta 5.0.2-0
skip-checks: true
2022-03-19 08:57:25 +00:00
Jaco dd571dd35f Update dev deps to latest util (#313) 2022-03-19 10:52:05 +02:00
Jaco c4e4414bbf FAQ link 2022-03-19 10:43:05 +02:00
github-actions[bot] 221b50e905 [CI Skip] release/stable 5.0.1
skip-checks: true
2022-03-19 07:42:08 +00:00
Jaco 42598dc36b 5.0.1 (#312) 2022-03-19 09:37:11 +02:00
github-actions[bot] ea9d986a48 [CI Skip] release/beta 4.6.2-6
skip-checks: true
2022-03-19 07:25:36 +00:00
Jaco 64b10dd4ad Deps & CHANGELOG (#311) 2022-03-19 09:20:33 +02:00
github-actions[bot] 729b46ee5f [CI Skip] release/beta 4.6.2-5
skip-checks: true
2022-03-19 07:08:10 +00:00
Jaco 0dfb491e9d Reproduction for secp256k1 asm.js failure (#310)
* Reproduction for secp256k1 asm.js failure

* Make test output more readable

* Additional test cleanups

* Bump build deps

* Adjust output sizing flags

* Bump wasm-bindgen

* secp256k1 HACK

* Update packages/wasm-crypto/src/bundle.ts

* Update packages/wasm-crypto/src/bundle.ts

* Update packages/wasm-crypto/test/all/index.cjs
2022-03-19 09:02:52 +02:00
github-actions[bot] 63585ff3a7 [CI Skip] release/beta 4.6.2-4
skip-checks: true
2022-03-18 12:46:20 +00:00
Jaco 698e62d70a Adjust init to allow for asm/wasm or combo (#309)
* Additional known secp256k1 test

* Fix test

* typo

* ... one more typo

* Adjust init to allow for asm/wasm or combo

* Cleanup export, no internal cjs

* Adjust sideEffects

* CHANGELOG
2022-03-18 14:40:59 +02:00
github-actions[bot] ab9ca9f49d [CI Skip] release/beta 4.6.2-3
skip-checks: true
2022-03-18 12:00:14 +00:00
Jaco 257080e1f9 Additional known secp256k1 test (#308)
* Additional known secp256k1 test

* Fix test

* typo

* ... one more typo
2022-03-18 13:54:52 +02:00
github-actions[bot] 068c83b9fb [CI Skip] release/beta 4.6.2-2
skip-checks: true
2022-03-17 05:42:55 +00:00
Jaco cb86c48bc4 Bump @polkadot/dev (#306)
* Bump @polkadot/dev

* trigger
2022-03-17 07:36:16 +02:00
github-actions[bot] f0ceed5454 [CI Skip] release/beta 4.6.2-1
skip-checks: true
2022-03-16 08:04:19 +00:00
Jaco f0e4b29d73 Bump dev deps (#305) 2022-03-16 09:58:59 +02:00
github-actions[bot] 4d23c75fa4 [CI Skip] release/beta 4.6.2-0
skip-checks: true
2022-03-12 10:12:21 +00:00
Jaco 0ae73cbad3 Bump common (#303) 2022-03-12 12:07:27 +02:00
35 changed files with 658 additions and 692 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
6 7
3.2.2 3.2.2
3.2.3 3.2.3
+24
View File
@@ -1,5 +1,29 @@
# CHANGELOG # CHANGELOG
## 5.1.1 Mar 27, 2022
Changes:
- Swap from `libsecp256k1` to `secp256k1` (this aligns with the Substrate use)
- Adjust `wasm-crypto/init*` to also export `initWasm(): Promise<void>` (optional manual init)
- Allow for `wasm-crypto/initNone` with no defined Wasm or Asm interfaces
- Fix initialization on React Native with only ASM
## 5.0.1 Mar 19, 2022
- **Breaking change** For users of React Native, you are now required to add `import '@polkadot/wasm-crypto/initOnlyAsm'` at your project top-level to ensure that asm.js is initialized. (Or alternatively `import '@polkadot/wasm-crypto/initWasmAsm'` to future-proof when WASM does become available)
- **Breaking change** For users who used to map the `data` and `empty` of the internal `wasm-crypto-{wasm, asmjs}` packages in their bundlers, swap to one of the `@polkadot/wasm-crypto/init*` top-level imports to set the type of interfaces you would prefer. A full writeup of the rationale and other options can be found [in the FAQ](https://polkadot.js.org/docs/util-crypto/FAQ#i-dont-have-wasm-available-in-my-environment)
Changes:
- Add (optional) `@polkadot/wasm-crypto/init{OnlyAsm, OnlyWasm, WasmAsm}` to allow specific interface types
- Add work-around for lazy secp256k1 init in asm.js environments
- Optimize asm.js output size
- Use latest `wasm-bindgen`, `binaryen` & `wabt` packages in build
- Additional workaround for Vite bundling
## 4.6.1 Mar 12, 2022 ## 4.6.1 Mar 12, 2022
Changes: Changes:
+1 -1
View File
@@ -1,4 +1,4 @@
376 Jaco 4.6.1 (#302) 394 Jaco 5.1.1 (#322)
1 Evgeny Fixed type (#121) 1 Evgeny Fixed type (#121)
1 Shunfan Zhou Support sr25519 agreement (#209) 1 Shunfan Zhou Support sr25519 agreement (#209)
1 Steve Degosserie Expose Schnorrkel's VRF capabilities (#170) 1 Steve Degosserie Expose Schnorrkel's VRF capabilities (#170)
+6 -5
View File
@@ -10,13 +10,14 @@
"url": "https://github.com/polkadot-js/wasm.git" "url": "https://github.com/polkadot-js/wasm.git"
}, },
"sideEffects": false, "sideEffects": false,
"version": "4.6.1", "version": "5.1.1",
"workspaces": [ "workspaces": [
"packages/*" "packages/*"
], ],
"scripts": { "scripts": {
"build": "yarn build:wasm", "build": "yarn build:wasm",
"build:js": "./scripts/build-js.sh", "build:js": "./scripts/build-js.sh",
"build:mac": "PATH=\"/opt/homebrew/opt/llvm/bin:$PATH\" CC=/opt/homebrew/opt/llvm/bin/clang AR=/opt/homebrew/opt/llvm/bin/llvm-ar yarn build",
"build:release": "polkadot-ci-ghact-build", "build:release": "polkadot-ci-ghact-build",
"build:rollup": "polkadot-exec-rollup --config", "build:rollup": "polkadot-exec-rollup --config",
"build:wasm": "./scripts/build.sh", "build:wasm": "./scripts/build.sh",
@@ -27,15 +28,15 @@
"test:wasm:js": "yarn test:wasm:js:jest && yarn test:wasm:js:node", "test:wasm:js": "yarn test:wasm:js:jest && yarn test:wasm:js:node",
"test:wasm:js:jest": "polkadot-dev-run-test ./test/jest.spec.js", "test:wasm:js:jest": "polkadot-dev-run-test ./test/jest.spec.js",
"test:wasm:js:node": "cd packages/wasm-crypto && node ./test/wasm.cjs && node ./test/asm.cjs", "test:wasm:js:node": "cd packages/wasm-crypto && node ./test/wasm.cjs && node ./test/asm.cjs",
"test:wasm:rust": "cd packages/wasm-crypto && RUST_BACKTRACE=full cargo test --release -- --nocapture" "test:wasm:rust": "cd packages/wasm-crypto && RUST_BACKTRACE=full cargo test --release -- --test-threads=1 --nocapture"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.17.5", "@babel/core": "^7.17.8",
"@polkadot/dev": "^0.65.87", "@polkadot/dev": "^0.65.101",
"fflate": "^0.7.3", "fflate": "^0.7.3",
"override-require": "^1.1.1" "override-require": "^1.1.1"
}, },
"resolutions": { "resolutions": {
"typescript": "^4.6.2" "typescript": "^4.6.3"
} }
} }
+4 -6
View File
@@ -17,15 +17,13 @@
}, },
"sideEffects": false, "sideEffects": false,
"type": "module", "type": "module",
"version": "4.6.1", "version": "5.1.1",
"browser": "empty.js", "main": "index.js",
"main": "empty.js",
"react-native": "data.js",
"dependencies": { "dependencies": {
"@babel/runtime": "^7.17.2" "@babel/runtime": "^7.17.8"
}, },
"devDependencies": { "devDependencies": {
"@polkadot/util": "^8.4.1" "@polkadot/util": "^8.6.1"
}, },
"peerDependencies": { "peerDependencies": {
"@polkadot/util": "*" "@polkadot/util": "*"
@@ -1,6 +1,5 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import { asmJsInit as _asmJsInit } from './cjs/data.js'; export { asmJsInit } from './cjs/data.js';
export { packageInfo } from './packageInfo';
export const asmJsInit = _asmJsInit;
@@ -1,4 +1,6 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
export const asmJsInit = null; import './detectPackage';
export * from './bundle';
@@ -3,4 +3,4 @@
// Do not edit, auto-generated by @polkadot/dev // Do not edit, auto-generated by @polkadot/dev
export const packageInfo = { name: '@polkadot/wasm-crypto-asmjs', path: 'auto', type: 'auto', version: '4.6.1' }; export const packageInfo = { name: '@polkadot/wasm-crypto-asmjs', path: 'auto', type: 'auto', version: '5.1.1' };
+4 -6
View File
@@ -17,15 +17,13 @@
}, },
"sideEffects": false, "sideEffects": false,
"type": "module", "type": "module",
"version": "4.6.1", "version": "5.1.1",
"browser": "data.js", "main": "index.js",
"main": "data.js",
"react-native": "data.js",
"dependencies": { "dependencies": {
"@babel/runtime": "^7.17.2" "@babel/runtime": "^7.17.8"
}, },
"devDependencies": { "devDependencies": {
"@polkadot/util": "^8.4.1" "@polkadot/util": "^8.6.1"
}, },
"peerDependencies": { "peerDependencies": {
"@polkadot/util": "*" "@polkadot/util": "*"
@@ -1,8 +1,10 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import { bytes, sizeUncompressed } from './cjs/bytes';
import { base64Decode } from './base64'; import { base64Decode } from './base64';
import { bytes, sizeUncompressed } from './bytes';
import { unzlibSync } from './fflate'; import { unzlibSync } from './fflate';
export { packageInfo } from './packageInfo';
export const wasmBytes = unzlibSync(base64Decode(bytes), new Uint8Array(sizeUncompressed)); export const wasmBytes = unzlibSync(base64Decode(bytes), new Uint8Array(sizeUncompressed));
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bytes as _bytes, sizeCompressed as _sizeCompressed, sizeUncompressed as _sizeUncompressed } from './cjs/bytes';
export const bytes = _bytes;
export const sizeCompressed = _sizeCompressed;
export const sizeUncompressed = _sizeUncompressed;
@@ -1,8 +1,6 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
export const bytes = null; import './detectPackage';
export const sizeCompressed = 0; export * from './bundle';
export const sizeUncompressed = 0;
+1 -1
View File
@@ -3,4 +3,4 @@
// Do not edit, auto-generated by @polkadot/dev // Do not edit, auto-generated by @polkadot/dev
export const packageInfo = { name: '@polkadot/wasm-crypto-wasm', path: 'auto', type: 'auto', version: '4.6.1' }; export const packageInfo = { name: '@polkadot/wasm-crypto-wasm', path: 'auto', type: 'auto', version: '5.1.1' };
+70 -238
View File
@@ -52,12 +52,6 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@@ -83,16 +77,7 @@ dependencies = [
"block-padding", "block-padding",
"byte-tools", "byte-tools",
"byteorder", "byteorder",
"generic-array 0.12.3", "generic-array",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array 0.14.4",
] ]
[[package]] [[package]]
@@ -122,6 +107,12 @@ version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
[[package]]
name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
@@ -149,15 +140,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "cpufeatures"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "crunchy" name = "crunchy"
version = "0.2.2" version = "0.2.2"
@@ -170,20 +152,10 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" checksum = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5"
dependencies = [ dependencies = [
"generic-array 0.12.3", "generic-array",
"subtle 1.0.0", "subtle 1.0.0",
] ]
[[package]]
name = "crypto-mac"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab"
dependencies = [
"generic-array 0.14.4",
"subtle 2.2.3",
]
[[package]] [[package]]
name = "curve25519-dalek" name = "curve25519-dalek"
version = "2.1.0" version = "2.1.0"
@@ -191,8 +163,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d85653f070353a16313d0046f173f70d1aadd5b42600a14de626f0dfb3473a5" checksum = "5d85653f070353a16313d0046f173f70d1aadd5b42600a14de626f0dfb3473a5"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"digest 0.8.1", "digest",
"rand_core 0.5.1", "rand_core",
"subtle 2.2.3", "subtle 2.2.3",
"zeroize", "zeroize",
] ]
@@ -203,16 +175,7 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
dependencies = [ dependencies = [
"generic-array 0.12.3", "generic-array",
]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array 0.14.4",
] ]
[[package]] [[package]]
@@ -232,9 +195,9 @@ checksum = "21a8a37f4e8b35af971e6db5e3897e7a6344caa3f92f6544f88125a1f5f0035a"
dependencies = [ dependencies = [
"curve25519-dalek", "curve25519-dalek",
"ed25519", "ed25519",
"rand 0.7.3", "rand",
"serde", "serde",
"sha2 0.8.2", "sha2",
"zeroize", "zeroize",
] ]
@@ -275,16 +238,6 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "generic-array"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
dependencies = [
"typenum",
"version_check",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.14" version = "0.1.14"
@@ -341,29 +294,8 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695" checksum = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695"
dependencies = [ dependencies = [
"crypto-mac 0.7.0", "crypto-mac",
"digest 0.8.1", "digest",
]
[[package]]
name = "hmac"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "126888268dcc288495a26bf004b38c5fdbb31682f992c84ceb046a1f0fe38840"
dependencies = [
"crypto-mac 0.8.0",
"digest 0.9.0",
]
[[package]]
name = "hmac-drbg"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17ea0a1394df5b6574da6e0c1ade9e78868c9fb0a4e5ef4428e32da4676b85b1"
dependencies = [
"digest 0.9.0",
"generic-array 0.14.4",
"hmac 0.8.1",
] ]
[[package]] [[package]]
@@ -393,55 +325,6 @@ version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
[[package]]
name = "libsecp256k1"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0452aac8bab02242429380e9b2f94ea20cea2b37e2c1777a1358799bbe97f37"
dependencies = [
"arrayref",
"base64",
"digest 0.9.0",
"hmac-drbg",
"lazy_static",
"libsecp256k1-core",
"libsecp256k1-gen-ecmult",
"libsecp256k1-gen-genmult",
"rand 0.8.4",
"serde",
"sha2 0.9.8",
"typenum",
]
[[package]]
name = "libsecp256k1-core"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451"
dependencies = [
"crunchy",
"digest 0.9.0",
"subtle 2.2.3",
]
[[package]]
name = "libsecp256k1-gen-ecmult"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809"
dependencies = [
"libsecp256k1-core",
]
[[package]]
name = "libsecp256k1-gen-genmult"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c"
dependencies = [
"libsecp256k1-core",
]
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.3.4" version = "0.3.4"
@@ -474,7 +357,7 @@ checksum = "c6feca46f4fa3443a01769d768727f10c10a20fdb65e52dc16a81f0c8269bb78"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"keccak", "keccak",
"rand_core 0.5.1", "rand_core",
"zeroize", "zeroize",
] ]
@@ -514,12 +397,6 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.10.2" version = "0.10.2"
@@ -551,7 +428,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "006c038a43a45995a9670da19e67600114740e8511d4333bf97a56e66a7542d9" checksum = "006c038a43a45995a9670da19e67600114740e8511d4333bf97a56e66a7542d9"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"crypto-mac 0.7.0", "crypto-mac",
] ]
[[package]] [[package]]
@@ -568,9 +445,9 @@ checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.19" version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
@@ -592,22 +469,11 @@ checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [ dependencies = [
"getrandom 0.1.14", "getrandom 0.1.14",
"libc", "libc",
"rand_chacha 0.2.2", "rand_chacha",
"rand_core 0.5.1", "rand_core",
"rand_hc", "rand_hc",
] ]
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
]
[[package]] [[package]]
name = "rand_chacha" name = "rand_chacha"
version = "0.2.2" version = "0.2.2"
@@ -615,17 +481,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [ dependencies = [
"ppv-lite86", "ppv-lite86",
"rand_core 0.5.1", "rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.3",
] ]
[[package]] [[package]]
@@ -637,22 +493,13 @@ dependencies = [
"getrandom 0.1.14", "getrandom 0.1.14",
] ]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom 0.2.3",
]
[[package]] [[package]]
name = "rand_hc" name = "rand_hc"
version = "0.2.0" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [ dependencies = [
"rand_core 0.5.1", "rand_core",
] ]
[[package]] [[package]]
@@ -684,9 +531,9 @@ dependencies = [
"curve25519-dalek", "curve25519-dalek",
"getrandom 0.1.14", "getrandom 0.1.14",
"merlin", "merlin",
"rand 0.7.3", "rand",
"rand_core 0.5.1", "rand_core",
"sha2 0.8.2", "sha2",
"subtle 2.2.3", "subtle 2.2.3",
"zeroize", "zeroize",
] ]
@@ -705,9 +552,27 @@ checksum = "656c79d0e90d0ab28ac86bf3c3d10bfbbac91450d3f190113b4e76d9fec3cfdd"
dependencies = [ dependencies = [
"byte-tools", "byte-tools",
"byteorder", "byteorder",
"hmac 0.7.1", "hmac",
"pbkdf2", "pbkdf2",
"sha2 0.8.2", "sha2",
]
[[package]]
name = "secp256k1"
version = "0.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c42e6f1735c5f00f51e43e28d6634141f2bcad10931b2609ddd74a86d751260"
dependencies = [
"secp256k1-sys",
]
[[package]]
name = "secp256k1-sys"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957da2573cde917463ece3570eab4a0b3f19de6f1646cde62e6fd3868f566036"
dependencies = [
"cc",
] ]
[[package]] [[package]]
@@ -715,20 +580,6 @@ name = "serde"
version = "1.0.114" version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
@@ -736,23 +587,10 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
dependencies = [ dependencies = [
"block-buffer 0.7.3", "block-buffer",
"digest 0.8.1", "digest",
"fake-simd", "fake-simd",
"opaque-debug 0.2.3", "opaque-debug",
]
[[package]]
name = "sha2"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b69f9a4c9740d74c5baa3fd2e547f9525fa8088a8a958e0ca2409a514e33f5fa"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 1.0.0",
"cpufeatures",
"digest 0.9.0",
"opaque-debug 0.3.0",
] ]
[[package]] [[package]]
@@ -781,9 +619,9 @@ checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.38" version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4" checksum = "ea297be220d52398dcc07ce15a209fce436d361735ac1db700cab3b6cdfb9f54"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -809,12 +647,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0165e045cc2ae1660270ca65e1676dbaab60feb0f91b10f7d0665e9b47e31f2" checksum = "b0165e045cc2ae1660270ca65e1676dbaab60feb0f91b10f7d0665e9b47e31f2"
dependencies = [ dependencies = [
"failure", "failure",
"hmac 0.7.1", "hmac",
"once_cell", "once_cell",
"pbkdf2", "pbkdf2",
"rand 0.7.3", "rand",
"rustc-hash", "rustc-hash",
"sha2 0.8.2", "sha2",
"unicode-normalization", "unicode-normalization",
] ]
@@ -839,7 +677,7 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" checksum = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56"
dependencies = [ dependencies = [
"rand 0.7.3", "rand",
] ]
[[package]] [[package]]
@@ -863,12 +701,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"
@@ -891,14 +723,14 @@ dependencies = [
"ed25519-dalek", "ed25519-dalek",
"getrandom 0.2.3", "getrandom 0.2.3",
"hex-literal", "hex-literal",
"hmac 0.7.1", "hmac",
"libsecp256k1",
"merlin", "merlin",
"pbkdf2", "pbkdf2",
"rand 0.7.3", "rand",
"schnorrkel", "schnorrkel",
"scrypt", "scrypt",
"sha2 0.8.2", "secp256k1",
"sha2",
"tiny-bip39", "tiny-bip39",
"tiny-keccak", "tiny-keccak",
"twox-hash", "twox-hash",
@@ -908,19 +740,19 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.65" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3edbcc9536ab7eababcc6d2374a0b7bfe13a2b6d562c5e07f370456b1a8f33d" checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 1.0.0",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.65" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ed2fb8c84bfad20ea66b26a3743f3e7ba8735a69fe7d95118c33ec8fc1244d" checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"lazy_static", "lazy_static",
@@ -933,9 +765,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.65" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb071268b031a64d92fc6cf691715ca5a40950694d8f683c5bb43db7c730929e" checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@@ -943,9 +775,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.65" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf592c807080719d1ff2f245a687cbadb3ed28b2077ed7084b47aba8b691f2c6" checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -956,9 +788,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.65" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b6c0220ded549d63860c78c38f3bcc558d1ca3f4efa74942c536ddbbb55e87" checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2"
[[package]] [[package]]
name = "wee_alloc" name = "wee_alloc"
+2 -2
View File
@@ -22,7 +22,7 @@ curve25519-dalek = { version = "2.1.0", default-features = false }
ed25519-dalek = { version = "1.0.0-pre.4", features = [] } ed25519-dalek = { version = "1.0.0-pre.4", features = [] }
getrandom = { version="0.2.3", features = ["js"] } getrandom = { version="0.2.3", features = ["js"] }
hmac = "0.7.0" hmac = "0.7.0"
libsecp256k1 = { version = "0.7.0", default-features = false, features=["hmac", "lazy-static-context"] } secp256k1 = { version = "0.21.2", default-features = false, features = ["recovery", "global-context"] }
merlin = { version = "2.0", default-features = false } merlin = { version = "2.0", default-features = false }
pbkdf2 = { version = "0.3.0", default-features = false } pbkdf2 = { version = "0.3.0", default-features = false }
rand = { version="0.7.3", default-features = false, features = ["wasm-bindgen"] } rand = { version="0.7.3", default-features = false, features = ["wasm-bindgen"] }
@@ -32,7 +32,7 @@ sha2 = "0.8.1"
tiny-bip39 = { version = "0.7", default-features = false } tiny-bip39 = { version = "0.7", default-features = false }
tiny-keccak = { version = "2.0.1", features = ["keccak"] } tiny-keccak = { version = "2.0.1", features = ["keccak"] }
twox-hash = "1.5.0" twox-hash = "1.5.0"
wasm-bindgen = "=0.2.65" wasm-bindgen = "=0.2.79"
wee_alloc = "0.4.3" wee_alloc = "0.4.3"
[dev-dependencies] [dev-dependencies]
+15 -7
View File
@@ -17,19 +17,27 @@
}, },
"sideEffects": [ "sideEffects": [
"./detectPackage.js", "./detectPackage.js",
"./detectPackage.cjs" "./detectPackage.cjs",
"./initNone.js",
"./initNone.cjs",
"./initOnlyAsm.js",
"./initOnlyAsm.cjs",
"./initOnlyWasm.js",
"./initOnlyWasm.cjs",
"./initWasmAsm.js",
"./initWasmAsm.cjs"
], ],
"type": "module", "type": "module",
"version": "4.6.1", "version": "5.1.1",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"@babel/runtime": "^7.17.2", "@babel/runtime": "^7.17.8",
"@polkadot/wasm-crypto-asmjs": "^4.6.1", "@polkadot/wasm-crypto-asmjs": "^5.1.1",
"@polkadot/wasm-crypto-wasm": "^4.6.1" "@polkadot/wasm-crypto-wasm": "^5.1.1"
}, },
"devDependencies": { "devDependencies": {
"@polkadot/util": "^8.4.1", "@polkadot/util": "^8.6.1",
"@polkadot/x-randomvalues": "^8.4.1" "@polkadot/x-randomvalues": "^8.6.1"
}, },
"peerDependencies": { "peerDependencies": {
"@polkadot/util": "*", "@polkadot/util": "*",
+28 -34
View File
@@ -3,7 +3,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { AsmCreator, WasmCryptoInstance } from './types'; import type { WasmCryptoInstance } from './types';
import { assert, stringToU8a, u8aToString } from '@polkadot/util'; import { assert, stringToU8a, u8aToString } from '@polkadot/util';
@@ -12,56 +12,50 @@ type PopFirst<T extends unknown[]> =
? N ? N
: []; : [];
let wasm: WasmCryptoInstance | null = null; interface Bridge {
let cachegetInt32: Int32Array | null = null; cachegetInt32: Int32Array | null;
let cachegetUint8: Uint8Array | null = null; cachegetUint8: Uint8Array | null;
type: 'wasm' | 'asm';
export async function initWasm (wasmBytes: Uint8Array | null, asmFn: AsmCreator | null, wbg: Record<string, WebAssembly.ImportValue>): Promise<void> { wasm: WasmCryptoInstance | null;
try { wasmPromise: Promise<void> | null;
assert(typeof WebAssembly !== 'undefined' && wasmBytes && wasmBytes.length, 'WebAssembly is not available in your environment'); wasmPromiseFn: null | (() => Promise<void>);
const source = await WebAssembly.instantiate(wasmBytes, { wbg });
wasm = source.instance.exports as unknown as WasmCryptoInstance;
} catch (error) {
// if we have a valid supplied asm.js, return that
if (asmFn) {
wasm = asmFn(wbg);
} else {
console.error('FATAL: Unable to initialize @polkadot/wasm-crypto');
console.error(error);
wasm = null;
}
}
} }
export const __bridge: Bridge = {
cachegetInt32: null,
cachegetUint8: null,
type: 'wasm',
wasm: null,
wasmPromise: null,
wasmPromiseFn: null
};
export function withWasm <T, F extends (wasm: WasmCryptoInstance, ...params: never[]) => T> (fn: F): (...params: PopFirst<Parameters<F>>) => ReturnType<F> { export 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> => { return (...params: PopFirst<Parameters<F>>): ReturnType<F> => {
assert(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.'); 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.');
return fn(wasm, ...params) as ReturnType<F>; return fn(__bridge.wasm, ...params) as ReturnType<F>;
}; };
} }
export function getWasm (): WasmCryptoInstance { export function getWasm (): WasmCryptoInstance {
return wasm!; return __bridge.wasm!;
} }
export function getInt32 (): Int32Array { export function getInt32 (): Int32Array {
if (cachegetInt32 === null || cachegetInt32.buffer !== wasm!.memory.buffer) { if (__bridge.cachegetInt32 === null || __bridge.cachegetInt32.buffer !== __bridge.wasm!.memory.buffer) {
cachegetInt32 = new Int32Array(wasm!.memory.buffer); __bridge.cachegetInt32 = new Int32Array(__bridge.wasm!.memory.buffer);
} }
return cachegetInt32; return __bridge.cachegetInt32;
} }
export function getUint8 (): Uint8Array { export function getUint8 (): Uint8Array {
if (cachegetUint8 === null || cachegetUint8.buffer !== wasm!.memory.buffer) { if (__bridge.cachegetUint8 === null || __bridge.cachegetUint8.buffer !== __bridge.wasm!.memory.buffer) {
cachegetUint8 = new Uint8Array(wasm!.memory.buffer); __bridge.cachegetUint8 = new Uint8Array(__bridge.wasm!.memory.buffer);
} }
return cachegetUint8; return __bridge.cachegetUint8;
} }
export function getU8a (ptr: number, len: number): Uint8Array { export function getU8a (ptr: number, len: number): Uint8Array {
@@ -73,7 +67,7 @@ export function getString (ptr: number, len: number): string {
} }
export function allocU8a (arg: Uint8Array): [number, number] { export function allocU8a (arg: Uint8Array): [number, number] {
const ptr = wasm!.__wbindgen_malloc(arg.length * 1); const ptr = __bridge.wasm!.__wbindgen_malloc(arg.length * 1);
getUint8().set(arg, ptr / 1); getUint8().set(arg, ptr / 1);
@@ -89,7 +83,7 @@ export function resultU8a (): Uint8Array {
const r1 = getInt32()[8 / 4 + 1]; const r1 = getInt32()[8 / 4 + 1];
const ret = getU8a(r0, r1).slice(); const ret = getU8a(r0, r1).slice();
wasm!.__wbindgen_free(r0, r1 * 1); __bridge.wasm!.__wbindgen_free(r0, r1 * 1);
return ret; return ret;
} }
+18 -9
View File
@@ -1,16 +1,11 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import { asmJsInit } from '@polkadot/wasm-crypto-asmjs'; import { __bridge, allocString, allocU8a, getWasm, resultString, resultU8a, withWasm } from './bridge';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm'; import { initWasm } from './init';
import { allocString, allocU8a, getWasm, initWasm, resultString, resultU8a, withWasm } from './bridge';
import * as imports from './imports';
export { packageInfo } from './packageInfo'; export { packageInfo } from './packageInfo';
const wasmPromise = initWasm(wasmBytes, asmJsInit, imports).catch(() => null);
export const bip39Generate = withWasm((wasm, words: 12 | 15 | 18 | 21 | 24): string => { export const bip39Generate = withWasm((wasm, words: 12 | 15 | 18 | 21 | 24): string => {
wasm.ext_bip39_generate(8, words); wasm.ext_bip39_generate(8, words);
@@ -207,6 +202,20 @@ export function isReady (): boolean {
return !!getWasm(); return !!getWasm();
} }
export function waitReady (): Promise<boolean> { export async function waitReady (): Promise<boolean> {
return wasmPromise.then(() => isReady()); try {
if (!__bridge.wasmPromise) {
if (!__bridge.wasmPromiseFn) {
__bridge.wasmPromiseFn = initWasm;
}
__bridge.wasmPromise = __bridge.wasmPromiseFn();
}
await __bridge.wasmPromise;
return isReady();
} catch {
return false;
}
} }
+44
View File
@@ -0,0 +1,44 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AsmCreator, WasmCryptoInstance } from './types';
import { assert } from '@polkadot/util';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
import { __bridge } from './bridge';
import * as wbg from './imports';
async function createPromise (wasmBytes: Uint8Array | null, asmFn: AsmCreator | null): Promise<void> {
try {
assert(typeof WebAssembly === 'object' && typeof WebAssembly.instantiate === 'function' && wasmBytes && wasmBytes.length, 'WebAssembly is not available in your environment');
const source = await WebAssembly.instantiate(wasmBytes, { wbg });
__bridge.wasm = (source.instance.exports as unknown as WasmCryptoInstance);
} catch (error) {
// if we have a valid supplied asm.js, return that
if (asmFn) {
__bridge.type = 'asm';
__bridge.wasm = asmFn(wbg);
} else {
console.error(`FATAL: Unable to initialize @polkadot/wasm-crypto:: ${(error as Error).message}`);
__bridge.wasm = null;
}
}
}
export function setWasmPromise (wasmBytes: Uint8Array | null, asmFn: AsmCreator | null): Promise<void> {
__bridge.wasmPromise = createPromise(wasmBytes, asmFn);
return __bridge.wasmPromise;
}
export function setWasmPromiseFn (fn: () => Promise<void>): void {
__bridge.wasmPromiseFn = fn;
}
export function initWasm (): Promise<void> {
return setWasmPromise(wasmBytes, null);
}
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AsmCreator } from './types';
import { __bridge } from './bridge';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function setWasmPromise (_wasmBytes: Uint8Array | null, _asmFn: AsmCreator | null): Promise<void> {
__bridge.wasmPromise = Promise.resolve();
return __bridge.wasmPromise;
}
export function setWasmPromiseFn (fn: () => Promise<void>): void {
__bridge.wasmPromiseFn = fn;
}
export function setWasmOnlyPromise (): Promise<void> {
return setWasmPromise(null, null);
}
export function initWasm (): Promise<void> {
return setWasmOnlyPromise();
}
setWasmPromiseFn(initWasm);
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
import { setWasmPromise, setWasmPromiseFn } from './init';
export function initWasm (): Promise<void> {
return setWasmPromise(null, asmJsInit);
}
setWasmPromiseFn(initWasm);
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { initWasm as baseInitWasm, setWasmPromiseFn } from './init';
export function initWasm (): Promise<void> {
return baseInitWasm();
}
setWasmPromiseFn(initWasm);
+13
View File
@@ -0,0 +1,13 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
import { setWasmPromise, setWasmPromiseFn } from './init';
export function initWasm (): Promise<void> {
return setWasmPromise(wasmBytes, asmJsInit);
}
setWasmPromiseFn(initWasm);
+1 -1
View File
@@ -3,4 +3,4 @@
// Do not edit, auto-generated by @polkadot/dev // Do not edit, auto-generated by @polkadot/dev
export const packageInfo = { name: '@polkadot/wasm-crypto', path: 'auto', type: 'auto', version: '4.6.1' }; export const packageInfo = { name: '@polkadot/wasm-crypto', path: 'auto', type: 'auto', version: '5.1.1' };
+39 -35
View File
@@ -1,22 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
use libsecp256k1::{Message, PublicKey, RecoveryId, SecretKey, Signature, recover, sign}; use secp256k1::{ecdsa::{RecoverableSignature, RecoveryId}, Message, PublicKey, SecretKey, SECP256K1};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
pub fn ext_secp_pub_compress(pubkey: &[u8]) -> Vec<u8> { pub fn ext_secp_pub_compress(pubkey: &[u8]) -> Vec<u8> {
match PublicKey::parse_slice(&pubkey, None) { match PublicKey::from_slice(&pubkey) {
Ok(p) => p
.serialize_compressed()
.to_vec(),
_ => panic!("Invalid pubkey provided.")
}
}
#[wasm_bindgen]
pub fn ext_secp_pub_expand(pubkey: &[u8]) -> Vec<u8> {
match PublicKey::parse_slice(&pubkey, None) {
Ok(p) => p Ok(p) => p
.serialize() .serialize()
.to_vec(), .to_vec(),
@@ -25,44 +15,58 @@ pub fn ext_secp_pub_expand(pubkey: &[u8]) -> Vec<u8> {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn ext_secp_from_seed(seed: &[u8]) -> Vec<u8> { pub fn ext_secp_pub_expand(pubkey: &[u8]) -> Vec<u8> {
match SecretKey::parse_slice(seed) { match PublicKey::from_slice(&pubkey) {
Ok(s) => { Ok(p) => p
let mut res = vec![]; .serialize_uncompressed()
let pubkey = PublicKey::from_secret_key(&s); .to_vec(),
_ => panic!("Invalid pubkey provided.")
res.extend_from_slice(&s.serialize());
res.extend_from_slice(&pubkey.serialize_compressed());
res
},
_ => panic!("Incalid seed provided.")
} }
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn ext_secp_recover(hash: &[u8], sig: &[u8], rec: u8) -> Vec<u8> { pub fn ext_secp_from_seed(seed: &[u8]) -> Vec<u8> {
match (Message::parse_slice(hash), Signature::parse_standard_slice(sig), RecoveryId::parse(rec)) { match SecretKey::from_slice(seed) {
(Ok(m), Ok(s), Ok(r)) => Ok(s) => {
match recover(&m, &s, &r) { let mut res = vec![];
let pubkey = PublicKey::from_secret_key(SECP256K1, &s);
res.extend_from_slice(&s.serialize_secret());
res.extend_from_slice(&pubkey.serialize());
res
},
_ => panic!("Invalid seed provided.")
}
}
#[wasm_bindgen]
pub fn ext_secp_recover(hash: &[u8], sig: &[u8], rec: i32) -> Vec<u8> {
match RecoveryId::from_i32(rec) {
Ok(r) => match (Message::from_slice(hash), RecoverableSignature::from_compact(&sig, r)) {
(Ok(m), Ok(s)) => match s.recover(&m) {
Ok(k) => k Ok(k) => k
.serialize_compressed() .serialize()
.to_vec(), .to_vec(),
_ => panic!("Invalid message provided.") _ => panic!("Unable to recover.")
}, },
_ => panic!("Invalid signature provided.")
},
_ => panic!("Invalid recovery data provided.") _ => panic!("Invalid recovery data provided.")
} }
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn ext_secp_sign(hash: &[u8], seckey: &[u8]) -> Vec<u8> { pub fn ext_secp_sign(hash: &[u8], seckey: &[u8]) -> Vec<u8> {
match (Message::parse_slice(hash), SecretKey::parse_slice(seckey)) { match (Message::from_slice(hash), SecretKey::from_slice(seckey)) {
(Ok(m), Ok(s)) => { (Ok(m), Ok(s)) => {
let mut res = vec![]; let mut res = vec![];
let (sig, rec) = sign(&m, &s); let (rec, sig) = SECP256K1
.sign_ecdsa_recoverable(&m, &s)
.serialize_compact();
res.extend_from_slice(&sig.serialize()); res.extend_from_slice(&sig);
res.push(rec.into()); res.push(rec.to_i32() as u8);
res res
}, },
@@ -114,7 +118,7 @@ pub mod tests {
#[test] #[test]
fn can_sign() { fn can_sign() {
// JS expectiation - doesn't match? // JS expectation - doesn't match?
// let expected = hex!("df92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba8c4efa521361678d9b8582744d77aa4b8d886d7380b7808a683174afad9c4700300"); // let expected = hex!("df92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba8c4efa521361678d9b8582744d77aa4b8d886d7380b7808a683174afad9c4700300");
let expected = hex!("df92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01"); let expected = hex!("df92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01");
let seckey = hex!("4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae"); let seckey = hex!("4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae");
+12 -4
View File
@@ -1,8 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
const rootIncl = ['@polkadot/wasm-crypto-asmjs', '@polkadot/wasm-crypto-wasm']; const rootIncl = ['@polkadot/wasm-crypto', '@polkadot/wasm-crypto-asmjs', '@polkadot/wasm-crypto-wasm'];
const detcIncl = rootIncl.map((p) => `${p}/packageInfo.cjs`); const detcIncl = [
...rootIncl.map((p) => `${p}/initOnlyAsm.cjs`),
...rootIncl.map((p) => `${p}/initOnlyWasm.cjs`),
...rootIncl.map((p) => `${p}/packageInfo.cjs`)
];
require('override-require')( require('override-require')(
(request) => (request) =>
@@ -10,6 +14,10 @@ require('override-require')(
detcIncl.some((p) => request.endsWith(p)), detcIncl.some((p) => request.endsWith(p)),
(request) => (request) =>
rootIncl.some((p) => request.endsWith(p)) rootIncl.some((p) => request.endsWith(p))
? require(`${rootIncl.find((p) => request.endsWith(p))}/build/data.cjs`) ? require(`${rootIncl.find((p) => request.endsWith(p))}/build/index.cjs`)
: require(`${detcIncl.find((p) => request.endsWith(p)).replace('packageInfo.cjs', 'build/packageInfo.cjs')}`) : require(`${detcIncl.find((p) => request.endsWith(p))
.replace('initOnlyAsm.cjs', 'build/initOnlyAsm.cjs')
.replace('initOnlyWasm.cjs', 'build/initOnlyWasm.cjs')
.replace('packageInfo.cjs', 'build/packageInfo.cjs')
}`)
); );
+21 -12
View File
@@ -10,19 +10,22 @@ const sr25519 = require('./sr25519.cjs');
const vrf = require('./vrf.cjs'); const vrf = require('./vrf.cjs');
const tests = { const tests = {
...bip39, // We place secp256k1 first, this allows the interaction with it in the
...ed25519, // hashing (specifically scrypt) test not be an issue (ASM.js only)
...hashing, // https://github.com/polkadot-js/wasm/issues/307
...secp256k1, ...secp256k1,
...ed25519,
...sr25519, ...sr25519,
...vrf ...vrf,
...bip39,
...hashing
}; };
function beforeAll () { function beforeAll () {
return wasm.waitReady(); return wasm.waitReady();
} }
function runAll () { function runAll (name) {
const failed = []; const failed = [];
let count = 0; let count = 0;
@@ -38,23 +41,29 @@ function runAll () {
console.timeEnd(name); console.timeEnd(name);
} catch (error) { } catch (error) {
console.error();
console.error(error); console.error(error);
failed.push(name); failed.push(name);
} }
}); });
if (failed.length) { if (failed.length) {
throw new Error(`Failed: ${failed.length} of ${count}: ${failed.concat(', ')}`); throw new Error(`\n*** ${name}: FAILED: ${failed.length} of ${count}: ${failed.join(', ')}`);
} }
} }
function runUnassisted () { function runUnassisted (name) {
beforeAll() console.log(`\n*** ${name}: Running tests`);
.then(() => runAll())
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
beforeAll()
.then(() => runAll(name))
.then(() => {
console.log(`\n*** ${name}: All passed`);
process.exit(0);
})
.catch((error) => {
console.error(error.message, '\n');
process.exit(-1); process.exit(-1);
}); });
} }
+25 -2
View File
@@ -21,7 +21,7 @@ function secp256k1Expand (wasm) {
assert(result === '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af', 'ERROR: secp256k1Expand does not match'); assert(result === '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af', 'ERROR: secp256k1Expand does not match');
} }
function secp256k1FromSeed (wasm) { function secp256k1FromSeed1 (wasm) {
const seckey = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae'; const seckey = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
const result = u8aToHex(wasm.secp256k1FromSeed(hexToU8a(seckey))); const result = u8aToHex(wasm.secp256k1FromSeed(hexToU8a(seckey)));
@@ -30,6 +30,28 @@ function secp256k1FromSeed (wasm) {
assert(result === '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b', 'ERROR: secp256k1FromSeed does not match'); assert(result === '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b', 'ERROR: secp256k1FromSeed does not match');
} }
function secp256k1FromSeed2 (wasm) {
// https://github.com/polkadot-js/wasm/issues/307
const seckey = new Uint8Array([
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
214, 67, 38, 103, 57, 11, 180, 114, 104, 84
]);
const expected = u8aToHex(new Uint8Array([
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
214, 67, 38, 103, 57, 11, 180, 114, 104, 84, 2,
10, 16, 145, 52, 31, 229, 102, 75, 250, 23, 130,
213, 224, 71, 121, 104, 144, 104, 201, 22, 176, 76,
179, 101, 236, 49, 83, 117, 86, 132, 217, 161
]));
const result = u8aToHex(wasm.secp256k1FromSeed(seckey));
console.log('\tRES', result);
assert(result === expected, 'ERROR: secp256k1FromSeed does not match');
}
function secp256k1Recover (wasm) { function secp256k1Recover (wasm) {
const sig = '0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff'; const sig = '0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff';
const msg = '0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9'; const msg = '0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9';
@@ -53,7 +75,8 @@ function secp256k1Sign (wasm) {
module.exports = { module.exports = {
secp256k1Compress, secp256k1Compress,
secp256k1Expand, secp256k1Expand,
secp256k1FromSeed, secp256k1FromSeed1,
secp256k1FromSeed2,
secp256k1Recover, secp256k1Recover,
secp256k1Sign secp256k1Sign
}; };
+2 -3
View File
@@ -1,10 +1,9 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors // Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
global.WebAssembly = null;
require('./alias-imports.cjs'); require('./alias-imports.cjs');
require('@polkadot/wasm-crypto/initOnlyAsm.cjs');
const { runUnassisted } = require('./all/index.cjs'); const { runUnassisted } = require('./all/index.cjs');
runUnassisted(); runUnassisted('ASM');
-2
View File
@@ -5,8 +5,6 @@
* @jest-environment jsdom * @jest-environment jsdom
*/ */
// override-require in jest.config.js
import { beforeAll, tests, wasm } from './all/index.cjs'; import { beforeAll, tests, wasm } from './all/index.cjs';
describe('wasm-crypto', () => { describe('wasm-crypto', () => {
+1 -1
View File
@@ -5,4 +5,4 @@ require('./alias-imports.cjs');
const { runUnassisted } = require('./all/index.cjs'); const { runUnassisted } = require('./all/index.cjs');
runUnassisted(); runUnassisted('WASM');
+4 -6
View File
@@ -26,7 +26,7 @@ echo "*** Converting to WASM"
# optimise # optimise
echo "*** Optimising WASM output" echo "*** Optimising WASM output"
../binaryen/bin/wasm-opt $WSM -Os -o $OPT ../binaryen/bin/wasm-opt $WSM -Oz -o $OPT
# convert wasm to base64 structure # convert wasm to base64 structure
echo "*** Packing WASM into baseX" echo "*** Packing WASM into baseX"
@@ -34,14 +34,12 @@ node ../scripts/pack-wasm-base.cjs
# build asmjs version from the input (optimised) WASM # build asmjs version from the input (optimised) WASM
echo "*** Building asm.js version" echo "*** Building asm.js version"
../binaryen/bin/wasm2js --output $ASM $OPT ../binaryen/bin/wasm2js -Oz --output $ASM $OPT
# cleanup the generated asm, converting to cjs # cleanup the generated asm, converting to cjs
sed -i -e '/import {/d' $ASM sed -i -e '/import {/d' $ASM
sed -i -e '/export var /d' $ASM sed -i -e '1,/var retasmFunc = /!d' $ASM
sed -i -e 's/{abort.*},memasmFunc/wbg, memasmFunc/g' $ASM sed -i -e 's/var retasmFunc = .*/const asmJsInit = (wbg) => asmFunc(wbg);\n\nmodule.exports = { asmJsInit };/g' $ASM
sed -i -e 's/var retasmFunc = /const asmJsInit = (wbg) => /' $ASM
echo "module.exports = { asmJsInit };" >> $ASM
# cleanups # cleanups
rm -rf wasm-crypto-asmjs/build/cjs/*-e rm -rf wasm-crypto-asmjs/build/cjs/*-e
+7 -6
View File
@@ -7,16 +7,17 @@ set -e
source ./scripts/rust-version.sh source ./scripts/rust-version.sh
BINARYEN_REPO=https://github.com/WebAssembly/binaryen # NOTE If this is bumped, bump the version in Cargo.toml as well
BINARYEN_VER=version_97
BINARYEN_ZIP=
BINDGEN_REPO=https://github.com/rustwasm/wasm-bindgen BINDGEN_REPO=https://github.com/rustwasm/wasm-bindgen
BINDGEN_VER=0.2.65 BINDGEN_VER=0.2.79
BINDGEN_ZIP= BINDGEN_ZIP=
BINARYEN_REPO=https://github.com/WebAssembly/binaryen
BINARYEN_VER=version_105
BINARYEN_ZIP=
WABT_REPO=https://github.com/WebAssembly/wabt WABT_REPO=https://github.com/WebAssembly/wabt
WABT_VER=1.0.19 WABT_VER=1.0.27
WABT_ZIP= WABT_ZIP=
unamestr=`uname` unamestr=`uname`
+2 -2
View File
@@ -4,9 +4,9 @@
"composite": true, "composite": true,
"paths": { "paths": {
"@polkadot/wasm-crypto-asmjs/packageInfo": ["wasm-crypto-asmjs/src/packageInfo"], "@polkadot/wasm-crypto-asmjs/packageInfo": ["wasm-crypto-asmjs/src/packageInfo"],
"@polkadot/wasm-crypto-asmjs": ["wasm-crypto-asmjs/src/data"], "@polkadot/wasm-crypto-asmjs": ["wasm-crypto-asmjs/src"],
"@polkadot/wasm-crypto-wasm/packageInfo": ["wasm-crypto-wasm/src/packageInfo"], "@polkadot/wasm-crypto-wasm/packageInfo": ["wasm-crypto-wasm/src/packageInfo"],
"@polkadot/wasm-crypto-wasm": ["wasm-crypto-wasm/src/data"], "@polkadot/wasm-crypto-wasm": ["wasm-crypto-wasm/src"],
"@polkadot/wasm-crypto": ["wasm-crypto/src"] "@polkadot/wasm-crypto": ["wasm-crypto/src"]
}, },
"skipLibCheck": true "skipLibCheck": true
+253 -288
View File
File diff suppressed because it is too large Load Diff