Compare commits

...
88 Commits
Author SHA1 Message Date
Github Actions aceaa90466 [CI Skip] release/stable 3.2.4
skip-checks: true
2021-02-24 16:14:52 +00:00
Jaco Greeff 63622edcf4 Cleanup implicit dependencies, perform base64 decoding using base64-js (#178)
* Cleanup implicit dependencies, perform base64 decoding using base64-js

* base64 decoding

* lint

* remove extra call

* adjust
2021-02-24 17:09:06 +01:00
Github Actions 4a1c0563cb [CI Skip] release/stable 3.2.3
skip-checks: true
2021-02-16 10:55:04 +00:00
Jaco Greeff b34181f104 3.2.3 (#176) 2021-02-16 11:48:28 +01:00
Github Actions 141a067b85 [CI Skip] release/stable 3.2.2
skip-checks: true
2021-01-24 11:00:54 +00:00
Jaco Greeff 4c48d91f8e 3.2.2 (#175) 2021-01-24 11:55:09 +01:00
Github Actions 9158d7c066 [CI Skip] release/stable 3.2.1
skip-checks: true
2021-01-22 08:49:35 +00:00
Jaco Greeff 135affe3e3 3.2 (#174) 2021-01-22 09:44:18 +01:00
Github Actions 1460ce9301 [CI Skip] release/beta 3.1.2-6
skip-checks: true
2021-01-21 12:04:54 +00:00
Jaco Greeff 8510120701 Bumps (#173)
* Bumps

* Update fflate imports
2021-01-21 12:59:06 +01:00
Github Actions 1e5b33413f [CI Skip] release/beta 3.1.2-5
skip-checks: true
2021-01-21 11:34:19 +00:00
Jaco Greeff 16d1b28f77 Cleanups (#172) 2021-01-21 12:28:11 +01:00
Github Actions 33a54be279 [CI Skip] release/beta 3.1.2-4
skip-checks: true
2021-01-20 11:13:40 +00:00
Jaco Greeff e6bc31ac5d Adjust vrf_{sign, verify} with one variant (#171) 2021-01-20 12:08:27 +01:00
Github Actions a7f8342458 [CI Skip] release/beta 3.1.2-3
skip-checks: true
2021-01-17 15:22:02 +00:00
Steve DegosserieandGithub Actions b1752ad001 Expose Schnorrkel's VRF capabilities (#170)
* Expose vrf_sign_extra function from Schnorrkel

* Document function, code cleanup

* Added vrf_sign, vrf_verify, vrf_verify_extra functions.

* Expose VRF functions to js

Co-authored-by: Github Actions <action@github.com>
2021-01-17 16:16:14 +01:00
Github Actions 28e3f6018d [CI Skip] release/beta 3.1.2-2
skip-checks: true
2021-01-03 10:25:39 +00:00
Jaco Greeff 1bb6db86f7 2021 (#167) 2021-01-03 11:20:26 +01:00
Github Actions 571cdabc78 [CI Skip] release/beta 3.1.2-1
skip-checks: true
2021-01-02 16:58:01 +00:00
Jaco Greeff 11c6315409 Bump dev/util deps (#166)
* Bump dev/util deps

* Move dev-only deps
2021-01-02 17:51:35 +01:00
Github Actions 3a1740a5a2 [CI Skip] release/beta 3.1.2-0
skip-checks: true
2020-12-22 09:20:51 +00:00
Jaco Greeff c725e8ee03 Bump deps (#165)
* Bump deps

* rewrite
2020-12-22 10:15:44 +01:00
Github Actions 6650a8e160 [CI Skip] release/stable 3.1.1
skip-checks: true
2020-12-19 13:12:57 +00:00
Jaco Greeff a3d1878453 3.1 (#163) 2020-12-19 14:07:09 +01:00
Github Actions 73b2109666 [CI Skip] release/beta 3.0.2-12
skip-checks: true
2020-12-19 10:31:03 +00:00
Jaco Greeff 3c13f266df Bundle unzlibSync (no worker requirement, stop-gap) (#162)
* Bundle unzlibSync (no worker requirement, stop-gap)

* Bump deps

* Pass uncompressedSize

* mjs fixes
2020-12-19 11:24:59 +01:00
Github Actions d9a4572c7c [CI Skip] release/beta 3.0.2-11
skip-checks: true
2020-12-18 10:10:13 +00:00
Jaco Greeff f6187ca00b Bump dev 2020-12-18 11:04:29 +01:00
Github Actions c0193ba4ad [CI Skip] release/beta 3.0.2-10
skip-checks: true
2020-12-18 09:44:19 +00:00
Jaco Greeff a96c6337a0 WP4 compat :( 2020-12-18 10:39:13 +01:00
Github Actions ba926e920d [CI Skip] release/beta 3.0.2-9
skip-checks: true
2020-12-18 09:22:16 +00:00
Jaco Greeff 29d1ac009a detectPackage test 2020-12-18 10:17:04 +01:00
Github Actions 74c7e05fed [CI Skip] release/beta 3.0.2-8
skip-checks: true
2020-12-17 17:27:18 +00:00
Jaco Greeff 8618b57ee8 __dirname fix 2020-12-17 18:22:13 +01:00
Github Actions bdf7fb55fb [CI Skip] release/beta 3.0.2-7
skip-checks: true
2020-12-17 16:43:10 +00:00
Jaco Greeff e7389c2f15 Remove non-working import.meta wrapper 2020-12-17 17:37:12 +01:00
Github Actions f176d0b733 [CI Skip] release/beta 3.0.2-6
skip-checks: true
2020-12-17 16:24:43 +00:00
Jaco Greeff 81101b69a3 detectPackage protection? 2020-12-17 17:18:57 +01:00
Github Actions 4625944ee4 [CI Skip] release/beta 3.0.2-5
skip-checks: true
2020-12-17 13:02:40 +00:00
Jaco Greeff 15d1166637 Mark detectPackage as sideEffect 2020-12-17 13:57:32 +01:00
Github Actions 40f2990895 [CI Skip] release/beta 3.0.2-4
skip-checks: true
2020-12-17 11:33:59 +00:00
Jaco Greeff 123bb2a09c detectPackage function 2020-12-17 12:28:28 +01:00
Github Actions f863cb418e [CI Skip] release/beta 3.0.2-3
skip-checks: true
2020-12-17 10:41:54 +00:00
Jaco Greeff 0510eb869a Dedupe 2020-12-17 11:36:38 +01:00
Github Actions 16226a1919 [CI Skip] release/beta 3.0.2-2
skip-checks: true
2020-12-16 14:42:41 +00:00
Jaco Greeff 836fa8fb5f Fix detectPackage import 2020-12-16 15:37:44 +01:00
Github Actions 97f02c2dc5 [CI Skip] release/beta 3.0.2-1
skip-checks: true
2020-12-16 14:22:43 +00:00
Jaco Greeff a3ed8293f8 esm detectPackage (#161) 2020-12-16 15:17:47 +01:00
Github Actions b12b73df85 [CI Skip] release/beta 3.0.2-0
skip-checks: true
2020-12-15 15:10:59 +00:00
Jaco Greeff f01a9b187e Single instance package detection (#160)
* Single instance package detection

* s/require/request/

* Internal isReady

* Revert unneeded from-build alias
2020-12-15 16:04:22 +01:00
Github Actions 7898d95844 [CI Skip] release/stable 3.0.1
skip-checks: true
2020-12-13 09:14:12 +00:00
Jaco Greeff 2c4f8bbe4e 3.0 (#158)
* 3.0

* CHANGELOG

* Rename content packages

* final dev bump
2020-12-13 10:09:24 +01:00
Github Actions ef1caeb94e [CI Skip] release/beta 2.0.2-16
skip-checks: true
2020-12-12 12:14:43 +00:00
Jaco Greeff 8837ec9386 Use lz4 on the WASM stream (#159)
* Use lz4 on the WASM stream

* Allow fflate tree-shaking

* Rewrite deps
2020-12-12 13:09:47 +01:00
Github Actions 653aa56f12 [CI Skip] release/beta 2.0.2-15
skip-checks: true
2020-12-11 22:14:28 +00:00
Jaco Greeff 9ece350f21 Bump dev 2020-12-11 23:08:40 +01:00
Github Actions bd6435053d [CI Skip] release/beta 2.0.2-14
skip-checks: true
2020-12-11 16:32:30 +00:00
Jaco Greeff 9c5c22ddbe Bump common x-randomvalues import 2020-12-11 17:27:28 +01:00
Github Actions 77622d8408 [CI Skip] release/beta 2.0.2-13
skip-checks: true
2020-12-11 15:51:42 +00:00
Jaco Greeff 3e1249ca2c Allow esm builds (#157) 2020-12-11 16:45:56 +01:00
Github Actions 9c09490f62 [CI Skip] release/beta 2.0.2-12
skip-checks: true
2020-12-11 08:38:04 +00:00
Jaco Greeff c46c08d296 Bump dev 2020-12-11 09:32:35 +01:00
Github Actions f4d4bb90f0 [CI Skip] release/beta 2.0.2-11
skip-checks: true
2020-12-10 16:04:44 +00:00
Jaco Greeff 45b51adfc7 Build esm (don't use) (#156) 2020-12-10 16:59:50 +01:00
Github Actions aa084bf11b [CI Skip] release/beta 2.0.2-10
skip-checks: true
2020-12-10 14:04:13 +00:00
Jaco Greeff 0a1aec2acf Add missing @babel/runtime dep 2020-12-10 14:59:19 +01:00
Github Actions 463626d75c [CI Skip] release/beta 2.0.2-9
skip-checks: true
2020-12-10 13:51:09 +00:00
Jaco Greeff dc4203fe7c Bump dev 2020-12-10 14:45:49 +01:00
Github Actions 0fa386b93a [CI Skip] release/beta 2.0.2-8
skip-checks: true
2020-12-10 13:13:47 +00:00
Jaco Greeff 5c563bb95f Split bytes packages for easier override (#155) 2020-12-10 14:07:11 +01:00
Github Actions 4a10b7b066 [CI Skip] release/beta 2.0.2-7
skip-checks: true
2020-12-09 17:38:08 +00:00
Jaco Greeff c31c8537be Babel config update (#154) 2020-12-09 18:32:56 +01:00
Github Actions 7aebad00eb [CI Skip] release/beta 2.0.2-6
skip-checks: true
2020-12-09 15:17:51 +00:00
Jaco Greeff 7af6734e26 Fix sr25519 soft derive signature (#153) 2020-12-09 16:11:56 +01:00
Github Actions edcdd48830 [CI Skip] release/beta 2.0.2-5
skip-checks: true
2020-12-09 14:57:56 +00:00
Jaco Greeff ad4ac2e101 Convert to TypeScript sources (#152) 2020-12-09 15:52:04 +01:00
Github Actions 2a44e076c2 [CI Skip] release/beta 2.0.2-4
skip-checks: true
2020-12-08 10:30:08 +00:00
Jaco Greeff 9ed88fe53a Remove --fix on CI (#151) 2020-12-08 11:25:18 +01:00
Github Actions 856bf68abd [CI Skip] release/beta 2.0.2-3
skip-checks: true
2020-12-07 14:05:32 +00:00
Jaco Greeff b36ad6fdf2 Bump dev (#150)
* Bump dev

* Rebump
2020-12-07 14:59:44 +01:00
Github Actions 78d56fa82e [CI Skip] release/beta 2.0.2-2
skip-checks: true
2020-12-05 09:57:02 +00:00
Jaco Greeff 294d30cb3b Bump deps (minor) (#149) 2020-12-05 10:51:49 +01:00
Github Actions 3185b23a97 [CI Skip] release/beta 2.0.2-1
skip-checks: true
2020-12-01 17:52:56 +00:00
Jaco Greeff a8df6878b8 Bump berry (#148)
* Bump berry

* rewrite
2020-12-01 18:33:20 +01:00
Github Actions 148b1c1f98 [CI Skip] release/beta 2.0.2-0
skip-checks: true
2020-11-24 11:46:09 +00:00
Jaco Greeff 355b225428 Bump dev deps (#147) 2020-11-24 12:41:05 +01:00
Jaco Greeff 7e0cfe73f1 Bump dev (#146)
* Bump dev

* Update babel config
2020-11-11 11:28:47 +01:00
Jaco Greeff 618d5ad6d6 Adjust CC excludes (#145) 2020-11-08 10:10:36 +01:00
69 changed files with 3370 additions and 2281 deletions
+4
View File
@@ -1 +1,5 @@
4
3.2.2
3.2.3
3.2.4
+1
View File
@@ -2,3 +2,4 @@ exclude_patterns:
- "**/*.spec.js"
- "**/*.spec.ts"
- "docs/**/*.js"
- "**/test/**/*.js"
+5 -3
View File
@@ -1,7 +1,7 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
const base = require('@polkadot/dev/config/eslint');
const base = require('@polkadot/dev/config/eslint.cjs');
module.exports = {
...base,
@@ -13,7 +13,9 @@ module.exports = {
'**/binaryen/*',
'**/build/*',
'**/coverage/*',
'**/node_modules/*'
'**/node_modules/*',
'**/pkg/*',
'**/target/*'
],
parserOptions: {
...base.parserOptions,
+1
View File
@@ -2,6 +2,7 @@ binaryen/
bindgen/
build/
build-docs/
bytes/
coverage/
docs/.vuepress/dist/
node_modules/
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
-55
View File
File diff suppressed because one or more lines are too long
+55
View File
File diff suppressed because one or more lines are too long
+1 -3
View File
@@ -9,7 +9,5 @@ plugins:
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
spec: "@yarnpkg/plugin-version"
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
yarnPath: .yarn/releases/yarn-2.3.3.cjs
yarnPath: .yarn/releases/yarn-2.4.0.cjs
+55
View File
@@ -1,5 +1,60 @@
# CHANGELOG
## 3.2.4 Feb 24, 2021
Changes:
- Cleanup implicit dependencies, perform base64 decoding using base64-js
## 3.2.3 Feb 16, 2021
Changes:
- Change package detect import to use `.js` source, not `.json`
## 3.2.2 Jan 24, 2021
Changes:
- Remove `module` field in `package.json`
## 3.2.1 Jan 22, 2021
Contributed:
- Expose sr25519 `ext_vrf_{sign, verify}` methods (Thanks to https://github.com/stiiifff)
Changes:
- Add explicit `.editorconfig` for Rust sources
- Remove unused code bundles (`vrf_{sign,verify}_extra`, `secp256k1` with tests)
- Test run wrapper cleanups
## 3.1.1 Dec 19, 2020
Changes:
- Single-instance package detection
- Adjust WASM inflate with localized methods
## 3.0.1 Dec 13, 2020
**Important** This assumes `@polkadot/util` 5+ peerDependencies
**Important** While this package contains no external interface changes, it doe now compile and ship both cjs and esm modules. This means that modern bundlers will have more information for tree-shaking available. Additionally when using Node with `.mjs` extensions, the esm version imports will be used on recent Node versions. Adding an export map, as here, may have some impacts so a major semver bump is advised. There may be (small) dragons hiding in the dark corners...
Changes:
- Build and expose both cjs and esm via exports map
- Split wasm and asmjs files into the `@polkadot/wasm-crypto-{wasm,asmjs}` packages
- Add fflate WASM compression, with 180K raw savings (37K over-the-wire)
- Convert the package to full TypeScript sources
## 2.0.1 Nov 8, 2020
Changes:
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
module.exports = require('@polkadot/dev/config/babel');
module.exports = require('@polkadot/dev/config/babel-config-cjs.cjs');
+9 -4
View File
@@ -1,14 +1,19 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
const config = require('@polkadot/dev/config/jest');
const config = require('@polkadot/dev/config/jest.cjs');
module.exports = Object.assign({}, config, {
moduleNameMapper: {
'@polkadot/wasm-(crypto)(.*)$': '<rootDir>/packages/wasm-$1/src/$2'
'@polkadot/wasm-crypto-asmjs(.*)$': '<rootDir>/packages/wasm-crypto-asmjs/build/$1',
'@polkadot/wasm-crypto-wasm(.*)$': '<rootDir>/packages/wasm-crypto-wasm/build/$1',
// eslint-disable-next-line sort-keys
'@polkadot/wasm-crypto(.*)$': '<rootDir>/packages/wasm-crypto/src/$1'
},
modulePathIgnorePatterns: [
'<rootDir>/packages/wasm-crypto-asmjs/build',
'<rootDir>/packages/wasm-crypto-wasm/build',
'<rootDir>/packages/wasm-crypto/build'
],
resolver: '@polkadot/dev/config/jest-resolver'
resolver: '@polkadot/dev/config/jest-resolver.cjs'
});
+6 -4
View File
@@ -7,7 +7,7 @@
"packages/*"
],
"resolutions": {
"typescript": "^4.0.5"
"typescript": "^4.1.5"
},
"scripts": {
"build": "yarn build:wasm",
@@ -23,8 +23,10 @@
"test:wasm:rust": "cd packages/wasm-crypto && RUST_BACKTRACE=full cargo test --release -- --nocapture"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@polkadot/dev": "^0.59.19"
"@babel/core": "^7.13.1",
"@polkadot/dev": "^0.61.27",
"fflate": "^0.6.4",
"override-require": "^1.1.1"
},
"version": "2.0.1"
"version": "3.2.4"
}
+18
View File
@@ -0,0 +1,18 @@
{
"name": "@polkadot/wasm-crypto-asmjs",
"version": "3.2.4",
"description": "Asm.js content for wasm-crypto",
"browser": "empty.js",
"main": "empty.js",
"react-native": "data.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
"license": "Apache-2.0",
"bugs": "https://github.com/polkadot-js/wasm/issues",
"homepage": "https://github.com/polkadot-js/wasm",
"dependencies": {
"@babel/runtime": "^7.13.7"
}
}
+4
View File
@@ -0,0 +1,4 @@
// Copyright 2019-2021 @polkadot/wasm-crypto-asmjs authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const asmJsInit = null;
+4
View File
@@ -0,0 +1,4 @@
// Copyright 2019-2021 @polkadot/wasm-crypto-asmjs authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const asmJsInit = null;
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Auto-generated by @polkadot/dev, do not edit
export const packageInfo = { name: '@polkadot/wasm-crypto-asmjs', version: '3.2.4' };
+18
View File
@@ -0,0 +1,18 @@
{
"name": "@polkadot/wasm-crypto-wasm",
"version": "3.2.4",
"description": "Wasm content for wasm-crypto",
"browser": "data.js",
"main": "data.js",
"react-native": "data.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
"license": "Apache-2.0",
"bugs": "https://github.com/polkadot-js/wasm/issues",
"homepage": "https://github.com/polkadot-js/wasm",
"dependencies": {
"@babel/runtime": "^7.13.7"
}
}
+85
View File
@@ -0,0 +1,85 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// MIT License
//
// Copyright (c) 2014 Jameson Little
//
// https://github.com/beatgammit/base64-js/blob/88957c9943c7e2a0f03cdf73e71d579e433627d3/index.js
// This only contains the toByteArray function (no encoding)
//
// Only tweaks make here are some TS adjustments (we use strict null checks), the code is otherwise as-is with
// only the single required function provided
const CODE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const lookup: string[] = [];
const revLookup: number[] = [];
for (let i = 0; i < CODE.length; ++i) {
lookup[i] = CODE[i];
revLookup[CODE.charCodeAt(i)] = i;
}
// Support decoding URL-safe base64 strings, as Node.js does.
// See: https://en.wikipedia.org/wiki/Base64#URL_applications
revLookup['-'.charCodeAt(0)] = 62;
revLookup['_'.charCodeAt(0)] = 63;
function getLens (b64: string): [number, number] {
const len = b64.length;
if (len % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4');
}
// Trim off extra bytes after placeholder bytes are found
// See: https://github.com/beatgammit/base64-js/issues/42
const validLen = b64.indexOf('=');
return validLen === -1
? [len, 0]
: [validLen, 4 - (validLen % 4)];
}
export function toByteArray (b64: string): Uint8Array {
const [validLen, placeHoldersLen] = getLens(b64);
const arr = new Uint8Array(((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen);
let curByte = 0;
let i;
// if there are placeholders, only get up to the last complete 4 chars
const len = placeHoldersLen > 0
? validLen - 4
: validLen;
for (i = 0; i < len; i += 4) {
const tmp =
(revLookup[b64.charCodeAt(i)] << 18) |
(revLookup[b64.charCodeAt(i + 1)] << 12) |
(revLookup[b64.charCodeAt(i + 2)] << 6) |
revLookup[b64.charCodeAt(i + 3)];
arr[curByte++] = (tmp >> 16) & 0xFF;
arr[curByte++] = (tmp >> 8) & 0xFF;
arr[curByte++] = tmp & 0xFF;
}
if (placeHoldersLen === 2) {
const tmp =
(revLookup[b64.charCodeAt(i)] << 2) |
(revLookup[b64.charCodeAt(i + 1)] >> 4);
arr[curByte++] = tmp & 0xFF;
} else if (placeHoldersLen === 1) {
const tmp =
(revLookup[b64.charCodeAt(i)] << 10) |
(revLookup[b64.charCodeAt(i + 1)] << 4) |
(revLookup[b64.charCodeAt(i + 2)] >> 2);
arr[curByte++] = (tmp >> 8) & 0xFF;
arr[curByte++] = tmp & 0xFF;
}
return arr;
}
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2019-2021 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const bytes = '';
export const sizeCompressed = 0;
export const sizeUncompressed = 0;
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2019-2021 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { toByteArray } from './base64';
import { bytes, sizeUncompressed } from './bytes';
import { unzlibSync } from './fflate';
export const wasmBytes = unzlibSync(toByteArray(bytes), new Uint8Array(sizeUncompressed));
+4
View File
@@ -0,0 +1,4 @@
// Copyright 2019-2021 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const wasmBytes = null;
+342
View File
@@ -0,0 +1,342 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// MIT License
//
// Copyright (c) 2020 Arjun Barrett
//
// Copied from https://github.com/101arrowz/fflate/blob/73c737941ec89d85cdf0ad39ee6f26c5fdc95fd7/src/index.ts
// This only contains the unzlibSync function, no compression, no async, no workers
//
// These 2 issues are addressed as a short-term, stop-gap solution
// - https://github.com/polkadot-js/api/issues/2963
// - https://github.com/101arrowz/fflate/issues/17
//
// Only tweaks make here are some TS adjustments (we use strict null checks), the code is otherwise as-is with
// only the single required function provided (compression is still being done in the build with fflate)
/* eslint-disable */
// inflate state
type InflateState = {
// lmap
l?: Uint16Array;
// dmap
d?: Uint16Array;
// lbits
m?: number;
// dbits
n?: number;
// final
f?: number;
// pos
p?: number;
// byte
b?: number;
// lstchk
i?: boolean;
};
// aliases for shorter compressed code (most minifers don't do this)
const u8 = Uint8Array, u16 = Uint16Array, u32 = Uint32Array;
// code length index map
const clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
// fixed length extra bits
const fleb = new u8([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, /* unused */ 0, 0, /* impossible */ 0]);
// fixed distance extra bits
// see fleb note
const fdeb = new u8([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, /* unused */ 0, 0]);
// get base, reverse index map from extra bits
const freb = (eb: Uint8Array, start: number) => {
const b = new u16(31);
for (let i = 0; i < 31; ++i) {
b[i] = start += 1 << eb[i - 1];
}
// numbers here are at max 18 bits
const r = new u32(b[30]);
for (let i = 1; i < 30; ++i) {
for (let j = b[i]; j < b[i + 1]; ++j) {
r[j] = ((j - b[i]) << 5) | i;
}
}
return [b, r] as const;
}
const [fl, revfl] = freb(fleb, 2);
// we can ignore the fact that the other numbers are wrong; they never happen anyway
fl[28] = 258, revfl[258] = 28;
const [fd] = freb(fdeb, 0);
// map of value to reverse (assuming 16 bits)
const rev = new u16(32768);
for (let i = 0; i < 32768; ++i) {
// reverse table algorithm from SO
let x = ((i & 0xAAAA) >>> 1) | ((i & 0x5555) << 1);
x = ((x & 0xCCCC) >>> 2) | ((x & 0x3333) << 2);
x = ((x & 0xF0F0) >>> 4) | ((x & 0x0F0F) << 4);
rev[i] = (((x & 0xFF00) >>> 8) | ((x & 0x00FF) << 8)) >>> 1;
}
// create huffman tree from u8 "map": index -> code length for code index
// mb (max bits) must be at most 15
// TODO: optimize/split up?
const hMap = ((cd: Uint8Array, mb: number, r: 0 | 1) => {
const s = cd.length;
// index
let i = 0;
// u16 "map": index -> # of codes with bit length = index
const l = new u16(mb);
// length of cd must be 288 (total # of codes)
for (; i < s; ++i) ++l[cd[i] - 1];
// u16 "map": index -> minimum code for bit length = index
const le = new u16(mb);
for (i = 0; i < mb; ++i) {
le[i] = (le[i - 1] + l[i - 1]) << 1;
}
let co: Uint16Array;
if (r) {
// u16 "map": index -> number of actual bits, symbol for code
co = new u16(1 << mb);
// bits to remove for reverser
const rvb = 15 - mb;
for (i = 0; i < s; ++i) {
// ignore 0 lengths
if (cd[i]) {
// num encoding both symbol and bits read
const sv = (i << 4) | cd[i];
// free bits
const r = mb - cd[i];
// start value
let v = le[cd[i] - 1]++ << r;
// m is end value
for (const m = v | ((1 << r) - 1); v <= m; ++v) {
// every 16 bit value starting with the code yields the same result
co[rev[v] >>> rvb] = sv;
}
}
}
} else {
co = new u16(s);
for (i = 0; i < s; ++i) co[i] = rev[le[cd[i] - 1]++] >>> (15 - cd[i]);
}
return co;
});
// fixed length tree
const flt = new u8(288);
for (let i = 0; i < 144; ++i) flt[i] = 8;
for (let i = 144; i < 256; ++i) flt[i] = 9;
for (let i = 256; i < 280; ++i) flt[i] = 7;
for (let i = 280; i < 288; ++i) flt[i] = 8;
// fixed distance tree
const fdt = new u8(32);
for (let i = 0; i < 32; ++i) fdt[i] = 5;
// fixed length map
const flrm = hMap(flt, 9, 1);
// fixed distance map
const fdrm = hMap(fdt, 5, 1);
// read d, starting at bit p and mask with m
const bits = (d: Uint8Array, p: number, m: number) => {
const o = p >>> 3;
return ((d[o] | (d[o + 1] << 8)) >>> (p & 7)) & m;
}
// read d, starting at bit p continuing for at least 16 bits
const bits16 = (d: Uint8Array, p: number) => {
const o = p >>> 3;
return ((d[o] | (d[o + 1] << 8) | (d[o + 2] << 16)) >>> (p & 7));
}
// get end of byte
const shft = (p: number) => (p >>> 3) + (p & 7 && 1);
// typed array slice - allows garbage collector to free original reference,
// while being more compatible than .slice
const slc = <T extends Uint8Array | Uint16Array | Uint32Array>(v: T, s: number, e?: number): T => {
if (s == null || s < 0) s = 0;
if (e == null || e > v.length) e = v.length;
// can't use .constructor in case user-supplied
const n = new (v instanceof u16 ? u16 : v instanceof u32 ? u32 : u8)(e - s) as T;
n.set(v.subarray(s, e));
return n;
}
// find max of array
const max = (a: Uint8Array | number[]) => {
let m = a[0];
for (let i = 1; i < a.length; ++i) {
if (a[i] > m) m = a[i];
}
return m;
};
// expands raw DEFLATE data
const inflt = (dat: Uint8Array, buf?: Uint8Array, st?: InflateState) => {
const noSt = !st || st.i;
if (!st) st = {};
// source length
const sl = dat.length;
// have to estimate size
const noBuf = !buf || !noSt;
// Assumes roughly 33% compression ratio average
if (!buf) buf = new u8(sl * 3);
// ensure buffer can fit at least l elements
const cbuf = (l: number) => {
let bl = buf!.length;
// need to increase size to fit
if (l > bl) {
// Double or set to necessary, whichever is greater
const nbuf = new u8(Math.max(bl << 1, l));
nbuf.set(buf!);
buf = nbuf;
}
};
// last chunk bitpos bytes
let final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n;
if (final && !lm) return buf;
// total bits
const tbts = sl << 3;
do {
if (!lm) {
// BFINAL - this is only 1 when last chunk is next
st.f = final = bits(dat, pos, 1);
// type: 0 = no compression, 1 = fixed huffman, 2 = dynamic huffman
const type = bits(dat, pos + 1, 3);
pos += 3;
if (!type) {
// go to end of byte boundary
const s = shft(pos) + 4, l = dat[s - 4] | (dat[s - 3] << 8), t = s + l;
if (t > sl) {
if (noSt) throw 'unexpected EOF';
break;
}
// ensure size
if (noBuf) cbuf(bt + l);
// Copy over uncompressed data
buf.set(dat.subarray(s, t), bt);
// Get new bitpos, update byte count
st.b = bt += l, st.p = pos = t << 3;
continue;
}
else if (type == 1) lm = flrm, dm = fdrm, lbt = 9, dbt = 5;
else if (type == 2) {
// literal lengths
const hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4;
const tl = hLit + bits(dat, pos + 5, 31) + 1;
pos += 14;
// length+distance tree
const ldt = new u8(tl);
// code length tree
const clt = new u8(19);
for (let i = 0; i < hcLen; ++i) {
// use index map to get real code
clt[clim[i]] = bits(dat, pos + i * 3, 7);
}
pos += hcLen * 3;
// code lengths bits
const clb = max(clt), clbmsk = (1 << clb) - 1;
if (!noSt && pos + tl * (clb + 7) > tbts) break;
// code lengths map
const clm = hMap(clt, clb, 1);
for (let i = 0; i < tl;) {
const r = clm[bits(dat, pos, clbmsk)];
// bits read
pos += r & 15;
// symbol
const s = r >>> 4;
// code length to copy
if (s < 16) {
ldt[i++] = s;
} else {
// copy count
let c = 0, n = 0;
if (s == 16) n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1];
else if (s == 17) n = 3 + bits(dat, pos, 7), pos += 3;
else if (s == 18) n = 11 + bits(dat, pos, 127), pos += 7;
while (n--) ldt[i++] = c;
}
}
// length tree distance tree
const lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
// max length bits
lbt = max(lt)
// max dist bits
dbt = max(dt);
lm = hMap(lt, lbt, 1);
dm = hMap(dt, dbt, 1);
} else throw 'invalid block type';
if (pos > tbts) throw 'unexpected EOF';
}
// Make sure the buffer can hold this + the largest possible addition
// maximum chunk size (practically, theoretically infinite) is 2^17;
if (noBuf) cbuf(bt + 131072);
const lms = (1 << lbt!) - 1, dms = (1 << dbt!) - 1;
const mxa = lbt! + dbt! + 18;
while (noSt || pos + mxa < tbts) {
// bits read, code
const c = lm[bits16(dat, pos) & lms], sym = c >>> 4;
pos += c & 15;
if (pos > tbts) throw 'unexpected EOF';
if (!c) throw 'invalid length/literal';
if (sym < 256) buf[bt++] = sym;
else if (sym == 256) {
lm = undefined;
break;
}
else {
let add = sym - 254;
// no extra bits needed if less
if (sym > 264) {
// index
const i = sym - 257, b = fleb[i];
add = bits(dat, pos, (1 << b) - 1) + fl[i];
pos += b;
}
// dist
const d = dm![bits16(dat, pos) & dms], dsym = d >>> 4;
if (!d) throw 'invalid distance';
pos += d & 15;
let dt = fd[dsym];
if (dsym > 3) {
const b = fdeb[dsym];
dt += bits16(dat, pos) & ((1 << b) - 1), pos += b;
}
if (pos > tbts) throw 'unexpected EOF';
if (noBuf) cbuf(bt + 131072);
const end = bt + add;
for (; bt < end; bt += 4) {
buf[bt] = buf[bt - dt];
buf[bt + 1] = buf[bt + 1 - dt];
buf[bt + 2] = buf[bt + 2 - dt];
buf[bt + 3] = buf[bt + 3 - dt];
}
bt = end;
}
}
st.l = lm, st.p = pos, st.b = bt;
if (lm) final = 1, st.m = lbt, st.d = dm, st.n = dbt;
} while (!final)
return bt == buf.length ? buf : slc(buf, 0, bt);
}
// zlib valid
const zlv = (d: Uint8Array) => {
if ((d[0] & 15) != 8 || (d[0] >>> 4) > 7 || ((d[0] << 8 | d[1]) % 31)) throw 'invalid zlib data';
if (d[1] & 32) throw 'invalid zlib data: preset dictionaries not supported';
}
/**
* Expands Zlib data
* @param data The data to decompress
* @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
* @returns The decompressed version of the data
*/
export function unzlibSync(data: Uint8Array, out?: Uint8Array) {
return inflt((zlv(data), data.subarray(2, -4)), out);
}
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Auto-generated by @polkadot/dev, do not edit
export const packageInfo = { name: '@polkadot/wasm-crypto-wasm', version: '3.2.4' };
+1
View File
@@ -662,6 +662,7 @@ dependencies = [
"ed25519-dalek",
"hex-literal",
"hmac",
"merlin",
"pbkdf2",
"rand",
"schnorrkel",
+1
View File
@@ -20,6 +20,7 @@ byteorder = "1.3.1"
ed25519-dalek = { version = "1.0.0-pre.4", features = [] }
hmac = "0.7.0"
# libsecp256k1 = "0.2.2"
merlin = { version = "2.0", default-features = false }
pbkdf2 = { version = "0.3.0", default-features = false }
schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated", "u64_backend"] }
scrypt = { version = "0.2", default-features = false }
+12 -9
View File
@@ -1,17 +1,20 @@
{
"name": "@polkadot/wasm-crypto",
"version": "2.0.1",
"version": "3.2.4",
"author": "Jaco Greeff <jacogr@gmail.com>",
"react-native": {
"./wasm/asm-empty.js": "./wasm/asm.js"
},
"main": "index.js",
"types": "index.d.ts",
"sideEffects": [
"./detectPackage.js",
"./detectPackage.mjs"
],
"dependencies": {
"@babel/runtime": "^7.13.7",
"@polkadot/wasm-crypto-asmjs": "^3.2.4",
"@polkadot/wasm-crypto-wasm": "^3.2.4"
},
"devDependencies": {
"@polkadot/dev": "^0.59.19",
"@polkadot/util": "^4.0.0-7",
"@polkadot/x-randomvalues": "^4.0.0-7",
"override-require": "^1.1.1"
"@polkadot/util": "^5.7.1",
"@polkadot/x-randomvalues": "^5.7.1"
},
"peerDependencies": {
"@polkadot/util": "*",
-103
View File
@@ -1,103 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
const { assert, stringToU8a, u8aToString } = require('@polkadot/util');
let wasm;
let cachegetInt32 = null;
let cachegetUint8 = null;
async function initWasm (wasmBytes, asmFn, wbg) {
try {
assert(typeof WebAssembly !== 'undefined' && wasmBytes && wasmBytes.length, 'WebAssembly is not available in your environment');
const source = await WebAssembly.instantiate(wasmBytes, { wbg });
wasm = source.instance.exports;
} catch (error) {
// if we have a valid supplied asm.js, return that
if (asmFn) {
wasm = asmFn(wbg);
} else {
console.error('ERROR: Unable to initialize @polkadot/wasm-crypto');
console.error(error);
wasm = null;
}
}
}
function withWasm (fn) {
return (...params) => {
assert(wasm, '@polkadot/wasm-crypto has not been initialized. Ensure that you wait for the initialization Promise with waitReady() (or cryptoWaitReady() from @polkadot/util-crypto) before attempting to use WASM-only interfaces.');
return fn(wasm, ...params);
};
}
function getWasm () {
return wasm;
}
function getInt32 () {
if (cachegetInt32 === null || cachegetInt32.buffer !== wasm.memory.buffer) {
cachegetInt32 = new Int32Array(wasm.memory.buffer);
}
return cachegetInt32;
}
function getUint8 () {
if (cachegetUint8 === null || cachegetUint8.buffer !== wasm.memory.buffer) {
cachegetUint8 = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8;
}
function getU8a (ptr, len) {
return getUint8().subarray(ptr / 1, ptr / 1 + len);
}
function getString (ptr, len) {
return u8aToString(getU8a(ptr, len));
}
function allocU8a (arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8().set(arg, ptr / 1);
return [ptr, arg.length];
}
function allocString (arg) {
return allocU8a(stringToU8a(arg));
}
function resultU8a () {
const r0 = getInt32()[8 / 4 + 0];
const r1 = getInt32()[8 / 4 + 1];
const ret = getU8a(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return ret;
}
function resultString () {
return u8aToString(resultU8a());
}
module.exports = {
allocString,
allocU8a,
getInt32,
getString,
getU8a,
getWasm,
initWasm,
resultString,
resultU8a,
withWasm
};
+96
View File
@@ -0,0 +1,96 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { AsmCreator, WasmCryptoInstance } from './types';
import { assert, stringToU8a, u8aToString } from '@polkadot/util';
let wasm: WasmCryptoInstance | null = null;
let cachegetInt32: Int32Array | null = null;
let cachegetUint8: Uint8Array | null = null;
export async function initWasm (wasmBytes: Uint8Array | null, asmFn: AsmCreator | null, wbg: Record<string, WebAssembly.ImportValue>): Promise<void> {
try {
assert(typeof WebAssembly !== 'undefined' && wasmBytes && wasmBytes.length, 'WebAssembly is not available in your environment');
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;
}
}
}
// FIXME We really would love to clean this up and have a sign like (wasm, ...params) => T
// Alas, TypeScript foo is not that great today, so we sadly have an extra closure here
export function withWasm <T, F extends (...params: never[]) => T> (fn: (wasm: WasmCryptoInstance) => F): F {
return ((...params: never[]): T => {
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.');
return fn(wasm)(...params);
}) as unknown as F;
}
export function getWasm (): WasmCryptoInstance {
return wasm!;
}
export function getInt32 (): Int32Array {
if (cachegetInt32 === null || cachegetInt32.buffer !== wasm!.memory.buffer) {
cachegetInt32 = new Int32Array(wasm!.memory.buffer);
}
return cachegetInt32;
}
export function getUint8 (): Uint8Array {
if (cachegetUint8 === null || cachegetUint8.buffer !== wasm!.memory.buffer) {
cachegetUint8 = new Uint8Array(wasm!.memory.buffer);
}
return cachegetUint8;
}
export function getU8a (ptr: number, len: number): Uint8Array {
return getUint8().subarray(ptr / 1, ptr / 1 + len);
}
export function getString (ptr: number, len: number): string {
return u8aToString(getU8a(ptr, len));
}
export function allocU8a (arg: Uint8Array): [number, number] {
const ptr = wasm!.__wbindgen_malloc(arg.length * 1);
getUint8().set(arg, ptr / 1);
return [ptr, arg.length];
}
export function allocString (arg: string): [number, number] {
return allocU8a(stringToU8a(arg));
}
export function resultU8a (): Uint8Array {
const r0 = getInt32()[8 / 4 + 0];
const r1 = getInt32()[8 / 4 + 1];
const ret = getU8a(r0, r1).slice();
wasm!.__wbindgen_free(r0, r1 * 1);
return ret;
}
export function resultString (): string {
return u8aToString(resultU8a());
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2017-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// For esm, this should be import.meta.url or to get the same behavior as __dirname, we can use
//
// new URL(import.meta.url).pathname)
//
// The issue is the WP4 has "some" issues with import.meta.url. So because of bundlers, we can't have
// nice things... In this case it is even worse since import.meta.url won't even make it compile, so
// there is a complete dead end with usage thereof
//
// When that is fixed, a solution is to have both .js & .mjs files, with the following content -
//
// cjs: util.detectPackage(packageInfo, () => __dirname);
// esm: detectPackage(packageInfo, () => import.meta.url);
//
// With the above we additionally need a .d.ts to just export the packageInfo
import { detectPackage } from '@polkadot/util';
import { packageInfo } from './packageInfo';
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname);
export { packageInfo };
-81
View File
@@ -1,81 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
const getRandomValues = require('@polkadot/x-randomvalues').default;
const { getString, getU8a } = require('./bridge');
const DEFAULT_SELF = { crypto: { getRandomValues } };
const heap = new Array(32).fill(undefined).concat(undefined, null, true, false);
let heapNext = heap.length;
function getObject (idx) {
return heap[idx];
}
function dropObject (idx) {
if (idx < 36) {
return;
}
heap[idx] = heapNext;
heapNext = idx;
}
function takeObject (idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
function addObject (obj) {
if (heapNext === heap.length) {
heap.push(heap.length + 1);
}
const idx = heapNext;
heapNext = heap[idx];
heap[idx] = obj;
return idx;
}
module.exports.__wbindgen_is_undefined = function (arg0) {
return getObject(arg0) === undefined;
};
module.exports.__wbg_self_1b7a39e3a92c949c = function () {
return addObject(DEFAULT_SELF);
};
module.exports.__wbg_require_604837428532a733 = function (arg0, arg1) {
throw new Error(`Unable to import ${getString(arg0, arg1)}`);
};
module.exports.__wbg_crypto_968f1772287e2df0 = function (arg0) {
return addObject(getObject(arg0).crypto);
};
module.exports.__wbg_getRandomValues_a3d34b4fee3c2869 = function (arg0) {
return addObject(getObject(arg0).getRandomValues);
};
module.exports.__wbg_getRandomValues_f5e14ab7ac8e995d = function (arg0, arg1, arg2) {
getObject(arg0).getRandomValues(getU8a(arg1, arg2));
};
module.exports.__wbg_randomFillSync_d5bd2d655fdf256a = function (arg0, arg1, arg2) {
getObject(arg0).randomFillSync(getU8a(arg1, arg2));
};
module.exports.__wbindgen_object_drop_ref = function (arg0) {
takeObject(arg0);
};
module.exports.abort = function () {
throw new Error('abort');
};
+90
View File
@@ -0,0 +1,90 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable camelcase */
import { getRandomValues } from '@polkadot/x-randomvalues';
import { getString, getU8a } from './bridge';
const DEFAULT_CRYPTO = { getRandomValues };
const DEFAULT_SELF = { crypto: DEFAULT_CRYPTO };
const heap: Array<unknown> = new Array(32).fill(undefined).concat(undefined, null, true, false);
let heapNext = heap.length;
function getObject (idx: number): unknown {
return heap[idx];
}
function dropObject (idx: number) {
if (idx < 36) {
return;
}
heap[idx] = heapNext;
heapNext = idx;
}
function takeObject (idx: number): unknown {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
function addObject (obj: unknown): number {
if (heapNext === heap.length) {
heap.push(heap.length + 1);
}
const idx = heapNext;
heapNext = heap[idx] as number;
heap[idx] = obj;
return idx;
}
export function __wbindgen_is_undefined (idx: number): boolean {
return getObject(idx) === undefined;
}
export function __wbg_self_1b7a39e3a92c949c (): number {
return addObject(DEFAULT_SELF);
}
export function __wbg_require_604837428532a733 (ptr: number, len: number): never {
throw new Error(`Unable to require ${getString(ptr, len)}`);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function __wbg_crypto_968f1772287e2df0 (_idx: number): number {
return addObject(DEFAULT_CRYPTO);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function __wbg_getRandomValues_a3d34b4fee3c2869 (_idx: number): number {
return addObject(DEFAULT_CRYPTO.getRandomValues);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function __wbg_getRandomValues_f5e14ab7ac8e995d (_arg0: number, ptr: number, len: number): void {
DEFAULT_CRYPTO.getRandomValues(getU8a(ptr, len));
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function __wbg_randomFillSync_d5bd2d655fdf256a (_idx: number, _ptr: number, _len: number): never {
throw new Error('randomFillsync is not available');
// getObject(idx).randomFillSync(getU8a(ptr, len));
}
export function __wbindgen_object_drop_ref (idx: number): void {
takeObject(idx);
}
export function abort (): never {
throw new Error('abort');
}
-31
View File
@@ -1,31 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
export function bip39Generate (words: 12 | 15 | 18 | 21 | 24): string;
export function bip39ToEntropy (phrase: string): Uint8Array;
export function bip39ToMiniSecret (phrase: string, password: string): Uint8Array;
export function bip39ToSeed (phrase: string, password: string): Uint8Array;
export function bip39Validate (phrase: string): boolean;
export function ed25519KeypairFromSeed (seed: Uint8Array): Uint8Array;
export function ed25519Sign (publicKey: Uint8Array, secretKey: Uint8Array, message: Uint8Array): Uint8Array;
export function ed25519Verify (signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean;
export function sr25519DeriveKeypairHard (pair: Uint8Array, chainCode: Uint8Array): Uint8Array;
export function sr25519DeriveKeypairSoft (pair: Uint8Array, chainCode: Uint8Array): Uint8Array;
export function sr25519DerivePublicSoft (publicKey: Uint8Array, chainCode: Uint8Array): Uint8Array;
export function sr25519KeypairFromSeed (seed: Uint8Array): Uint8Array;
export function sr25519Sign (publicKey: Uint8Array, secretKey: Uint8Array, message: Uint8Array): Uint8Array;
export function sr25519Verify (signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean;
export function blake2b (data: Uint8Array, key: Uint8Array, byteSize: number): Uint8Array;
export function keccak256 (data: Uint8Array): Uint8Array;
export function pbkdf2 (data: Uint8Array, salt: Uint8Array, rounds: number): Uint8Array;
export function scrypt (password: Uint8Array, salt: Uint8Array, log2N: number, r: number, p: number): Uint8Array;
// export function secp256k1IsRecoverable (message: Uint8Array, signature: Uint8Array): number;
// export function secp256k1Recover (message: Uint8Array, signature: Uint8Array): Uint8Array;
export function sha512 (data: Uint8Array): Uint8Array;
export function twox (data: Uint8Array, rounds: number): Uint8Array;
export function isReady (): boolean;
export function waitReady (): Promise<boolean>;
-188
View File
@@ -1,188 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
const asmFallback = require('./wasm/asm-empty');
const wasmBytes = require('./wasm/bytes');
const { allocString, allocU8a, getWasm, initWasm, resultString, resultU8a, withWasm } = require('./bridge');
const imports = require('./imports');
const wasmPromise = initWasm(wasmBytes, asmFallback, imports).catch(() => null);
module.exports.bip39Generate = withWasm((wasm, words) => {
wasm.ext_bip39_generate(8, words);
return resultString();
});
module.exports.bip39ToEntropy = withWasm((wasm, phrase) => {
const [ptr0, len0] = allocString(phrase);
wasm.ext_bip39_to_entropy(8, ptr0, len0);
return resultU8a();
});
module.exports.bip39ToMiniSecret = withWasm((wasm, phrase, password) => {
const [ptr0, len0] = allocString(phrase);
const [ptr1, len1] = allocString(password);
wasm.ext_bip39_to_mini_secret(8, ptr0, len0, ptr1, len1);
return resultU8a();
});
module.exports.bip39ToSeed = withWasm((wasm, phrase, password) => {
const [ptr0, len0] = allocString(phrase);
const [ptr1, len1] = allocString(password);
wasm.ext_bip39_to_seed(8, ptr0, len0, ptr1, len1);
return resultU8a();
});
module.exports.bip39Validate = withWasm((wasm, phrase) => {
const [ptr0, len0] = allocString(phrase);
const ret = wasm.ext_bip39_validate(ptr0, len0);
return ret !== 0;
});
module.exports.ed25519KeypairFromSeed = withWasm((wasm, seed) => {
const [ptr0, len0] = allocU8a(seed);
wasm.ext_ed_from_seed(8, ptr0, len0);
return resultU8a();
});
module.exports.ed25519Sign = withWasm((wasm, pubkey, seckey, message) => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(seckey);
const [ptr2, len2] = allocU8a(message);
wasm.ext_ed_sign(8, ptr0, len0, ptr1, len1, ptr2, len2);
return resultU8a();
});
module.exports.ed25519Verify = withWasm((wasm, signature, message, pubkey) => {
const [ptr0, len0] = allocU8a(signature);
const [ptr1, len1] = allocU8a(message);
const [ptr2, len2] = allocU8a(pubkey);
const ret = wasm.ext_ed_verify(ptr0, len0, ptr1, len1, ptr2, len2);
return ret !== 0;
});
module.exports.blake2b = withWasm((wasm, data, key, size) => {
const [ptr0, len0] = allocU8a(data);
const [ptr1, len1] = allocU8a(key);
wasm.ext_blake2b(8, ptr0, len0, ptr1, len1, size);
return resultU8a();
});
module.exports.keccak256 = withWasm((wasm, data) => {
const [ptr0, len0] = allocU8a(data);
wasm.ext_keccak256(8, ptr0, len0);
return resultU8a();
});
module.exports.pbkdf2 = withWasm((wasm, data, salt, rounds) => {
const [ptr0, len0] = allocU8a(data);
const [ptr1, len1] = allocU8a(salt);
wasm.ext_pbkdf2(8, ptr0, len0, ptr1, len1, rounds);
return resultU8a();
});
module.exports.scrypt = withWasm((wasm, password, salt, log2n, r, p) => {
const [ptr0, len0] = allocU8a(password);
const [ptr1, len1] = allocU8a(salt);
wasm.ext_scrypt(8, ptr0, len0, ptr1, len1, log2n, r, p);
return resultU8a();
});
module.exports.sha512 = withWasm((wasm, data) => {
const [ptr0, len0] = allocU8a(data);
wasm.ext_sha512(8, ptr0, len0);
return resultU8a();
});
module.exports.twox = withWasm((wasm, data, rounds) => {
const [ptr0, len0] = allocU8a(data);
wasm.ext_twox(8, ptr0, len0, rounds);
return resultU8a();
});
module.exports.sr25519DeriveKeypairHard = withWasm((wasm, pair, cc) => {
const [ptr0, len0] = allocU8a(pair);
const [ptr1, len1] = allocU8a(cc);
wasm.ext_sr_derive_keypair_hard(8, ptr0, len0, ptr1, len1);
return resultU8a();
});
module.exports.sr25519DeriveKeypairSoft = withWasm((wasm, pair, cc) => {
const [ptr0, len0] = allocU8a(pair);
const [ptr1, len1] = allocU8a(cc);
wasm.ext_sr_derive_keypair_soft(8, ptr0, len0, ptr1, len1);
return resultU8a();
});
module.exports.sr25519DerivePublicSoft = withWasm((wasm, pubkey, cc) => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(cc);
wasm.ext_sr_derive_public_soft(8, ptr0, len0, ptr1, len1);
return resultU8a();
});
module.exports.sr25519KeypairFromSeed = withWasm((wasm, seed) => {
const [ptr0, len0] = allocU8a(seed);
wasm.ext_sr_from_seed(8, ptr0, len0);
return resultU8a();
});
module.exports.sr25519Sign = withWasm((wasm, pubkey, secret, message) => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(secret);
const [ptr2, len2] = allocU8a(message);
wasm.ext_sr_sign(8, ptr0, len0, ptr1, len1, ptr2, len2);
return resultU8a();
});
module.exports.sr25519Verify = withWasm((wasm, signature, message, pubkey) => {
const [ptr0, len0] = allocU8a(signature);
const [ptr1, len1] = allocU8a(message);
const [ptr2, len2] = allocU8a(pubkey);
const ret = wasm.ext_sr_verify(ptr0, len0, ptr1, len1, ptr2, len2);
return ret !== 0;
});
module.exports.isReady = function () {
return !!getWasm();
};
module.exports.waitReady = function () {
return wasmPromise.then(() => !!getWasm());
};
+257
View File
@@ -0,0 +1,257 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import './detectPackage';
import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
import { allocString, allocU8a, getWasm, initWasm, resultString, resultU8a, withWasm } from './bridge';
import * as imports from './imports';
const wasmPromise = initWasm(wasmBytes, asmJsInit, imports).catch(() => null);
export const bip39Generate = withWasm((wasm) =>
(words: 12 | 15 | 18 | 21 | 24): string => {
wasm.ext_bip39_generate(8, words);
return resultString();
}
);
export const bip39ToEntropy = withWasm((wasm) =>
(phrase: string): Uint8Array => {
const [ptr0, len0] = allocString(phrase);
wasm.ext_bip39_to_entropy(8, ptr0, len0);
return resultU8a();
}
);
export const bip39ToMiniSecret = withWasm((wasm) =>
(phrase: string, password: string): Uint8Array => {
const [ptr0, len0] = allocString(phrase);
const [ptr1, len1] = allocString(password);
wasm.ext_bip39_to_mini_secret(8, ptr0, len0, ptr1, len1);
return resultU8a();
}
);
export const bip39ToSeed = withWasm((wasm) =>
(phrase: string, password: string): Uint8Array => {
const [ptr0, len0] = allocString(phrase);
const [ptr1, len1] = allocString(password);
wasm.ext_bip39_to_seed(8, ptr0, len0, ptr1, len1);
return resultU8a();
}
);
export const bip39Validate = withWasm((wasm) =>
(phrase: string): boolean => {
const [ptr0, len0] = allocString(phrase);
const ret = wasm.ext_bip39_validate(ptr0, len0);
return ret !== 0;
}
);
export const ed25519KeypairFromSeed = withWasm((wasm) =>
(seed: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(seed);
wasm.ext_ed_from_seed(8, ptr0, len0);
return resultU8a();
}
);
export const ed25519Sign = withWasm((wasm) =>
(pubkey: Uint8Array, seckey: Uint8Array, message: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(seckey);
const [ptr2, len2] = allocU8a(message);
wasm.ext_ed_sign(8, ptr0, len0, ptr1, len1, ptr2, len2);
return resultU8a();
}
);
export const ed25519Verify = withWasm((wasm) =>
(signature: Uint8Array, message: Uint8Array, pubkey: Uint8Array): boolean => {
const [ptr0, len0] = allocU8a(signature);
const [ptr1, len1] = allocU8a(message);
const [ptr2, len2] = allocU8a(pubkey);
const ret = wasm.ext_ed_verify(ptr0, len0, ptr1, len1, ptr2, len2);
return ret !== 0;
}
);
export const sr25519DeriveKeypairHard = withWasm((wasm) =>
(pair: Uint8Array, cc: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(pair);
const [ptr1, len1] = allocU8a(cc);
wasm.ext_sr_derive_keypair_hard(8, ptr0, len0, ptr1, len1);
return resultU8a();
}
);
export const sr25519DeriveKeypairSoft = withWasm((wasm) =>
(pair: Uint8Array, cc: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(pair);
const [ptr1, len1] = allocU8a(cc);
wasm.ext_sr_derive_keypair_soft(8, ptr0, len0, ptr1, len1);
return resultU8a();
}
);
export const sr25519DerivePublicSoft = withWasm((wasm) =>
(pubkey: Uint8Array, cc: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(cc);
wasm.ext_sr_derive_public_soft(8, ptr0, len0, ptr1, len1);
return resultU8a();
}
);
export const sr25519KeypairFromSeed = withWasm((wasm) =>
(seed: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(seed);
wasm.ext_sr_from_seed(8, ptr0, len0);
return resultU8a();
}
);
export const sr25519Sign = withWasm((wasm) =>
(pubkey: Uint8Array, secret: Uint8Array, message: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(secret);
const [ptr2, len2] = allocU8a(message);
wasm.ext_sr_sign(8, ptr0, len0, ptr1, len1, ptr2, len2);
return resultU8a();
}
);
export const sr25519Verify = withWasm((wasm) =>
(signature: Uint8Array, message: Uint8Array, pubkey: Uint8Array): boolean => {
const [ptr0, len0] = allocU8a(signature);
const [ptr1, len1] = allocU8a(message);
const [ptr2, len2] = allocU8a(pubkey);
const ret = wasm.ext_sr_verify(ptr0, len0, ptr1, len1, ptr2, len2);
return ret !== 0;
}
);
export const vrfSign = withWasm((wasm) =>
(secret: Uint8Array, context: Uint8Array, message: Uint8Array, extra: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(secret);
const [ptr1, len1] = allocU8a(context);
const [ptr2, len2] = allocU8a(message);
const [ptr3, len3] = allocU8a(extra);
wasm.ext_vrf_sign(8, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3);
return resultU8a();
}
);
export const vrfVerify = withWasm((wasm) =>
(pubkey: Uint8Array, context: Uint8Array, message: Uint8Array, extra: Uint8Array, outAndProof: Uint8Array): boolean => {
const [ptr0, len0] = allocU8a(pubkey);
const [ptr1, len1] = allocU8a(context);
const [ptr2, len2] = allocU8a(message);
const [ptr3, len3] = allocU8a(extra);
const [ptr4, len4] = allocU8a(outAndProof);
const ret = wasm.ext_vrf_verify(ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4);
return ret !== 0;
}
);
export const blake2b = withWasm((wasm) =>
(data: Uint8Array, key: Uint8Array, size: number): Uint8Array => {
const [ptr0, len0] = allocU8a(data);
const [ptr1, len1] = allocU8a(key);
wasm.ext_blake2b(8, ptr0, len0, ptr1, len1, size);
return resultU8a();
}
);
export const keccak256 = withWasm((wasm) =>
(data: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(data);
wasm.ext_keccak256(8, ptr0, len0);
return resultU8a();
}
);
export const pbkdf2 = withWasm((wasm) =>
(data: Uint8Array, salt: Uint8Array, rounds: number): Uint8Array => {
const [ptr0, len0] = allocU8a(data);
const [ptr1, len1] = allocU8a(salt);
wasm.ext_pbkdf2(8, ptr0, len0, ptr1, len1, rounds);
return resultU8a();
}
);
export const scrypt = withWasm((wasm) =>
(password: Uint8Array, salt: Uint8Array, log2n: number, r: number, p: number): Uint8Array => {
const [ptr0, len0] = allocU8a(password);
const [ptr1, len1] = allocU8a(salt);
wasm.ext_scrypt(8, ptr0, len0, ptr1, len1, log2n, r, p);
return resultU8a();
}
);
export const sha512 = withWasm((wasm) =>
(data: Uint8Array): Uint8Array => {
const [ptr0, len0] = allocU8a(data);
wasm.ext_sha512(8, ptr0, len0);
return resultU8a();
}
);
export const twox = withWasm((wasm) =>
(data: Uint8Array, rounds: number) => {
const [ptr0, len0] = allocU8a(data);
wasm.ext_twox(8, ptr0, len0, rounds);
return resultU8a();
}
);
export function isReady (): boolean {
return !!getWasm();
}
export function waitReady (): Promise<boolean> {
return wasmPromise.then(() => isReady());
}
+11
View File
@@ -0,0 +1,11 @@
// Copyright 2017-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Be able to import json in TS
// https://stackoverflow.com/questions/49996456/importing-json-file-in-typescript
declare module '*.json' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const value: any;
export default value;
}
+4 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Use `wee_alloc` as the global allocator.
@@ -16,3 +16,6 @@ pub mod hashing;
#[path = "rs/sr25519.rs"]
pub mod sr25519;
#[path = "rs/vrf.rs"]
pub mod vrf;
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Auto-generated by @polkadot/dev, do not edit
export const packageInfo = { name: '@polkadot/wasm-crypto', version: '3.2.4' };
+10
View File
@@ -0,0 +1,10 @@
root = true
[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=100
insert_final_newline=true
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
use bip39::{Mnemonic, MnemonicType, Language, Seed};
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
use std::convert::TryFrom;
+1 -83
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
use blake2_rfc::blake2b::blake2b;
@@ -7,7 +7,6 @@ use hmac::Hmac;
use pbkdf2::pbkdf2;
use scrypt::{ScryptParams, scrypt};
use sha2::{Digest, Sha512};
// use secp256k1;
use tiny_keccak::{Hasher, Keccak};
use twox_hash::XxHash;
use wasm_bindgen::prelude::*;
@@ -95,58 +94,6 @@ pub fn ext_scrypt(password: &[u8], salt: &[u8], log2_n: u8, r: u32, p: u32) -> V
}
}
// /// Checks for the recoverability of a signature from message
// ///
// /// * message: The message
// /// * signature: The signature
// ///
// /// Returns 0 on success, or a specific error code
// #[wasm_bindgen]
// pub fn ext_secp256k1_is_recoverable(message: &[u8], signature: &[u8]) -> u32 {
// let rs = match secp256k1::Signature::parse_slice(&signature[0..64]) {
// Ok(rs) => rs,
// _ => return 1
// };
// let v = match secp256k1::RecoveryId::parse(if signature[64] > 26 { signature[64] - 27 } else { signature[64] } as u8) {
// Ok(v) => v,
// _ => return 2
// };
// let mut msg = [0u8; 32];
// msg.copy_from_slice(&message);
// match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) {
// Ok(_) => 0,
// _ => 3
// }
// }
// /// Recovers the secp256k1 signature from the input
// ///
// /// * message: The message
// /// * signature: The signature
// ///
// /// Returns a vector with the recovered public key
// #[wasm_bindgen]
// pub fn ext_secp256k1_recover(message: &[u8], signature: &[u8]) -> Vec<u8> {
// let rs = match secp256k1::Signature::parse_slice(&signature[0..64]) {
// Ok(rs) => rs,
// _ => panic!("Unable to recover r,s")
// };
// let v = match secp256k1::RecoveryId::parse(if signature[64] > 26 { signature[64] - 27 } else { signature[64] } as u8) {
// Ok(v) => v,
// _ => panic!("Unable to recover v")
// };
// let mut msg = [0u8; 32];
// msg.copy_from_slice(&message);
// match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) {
// Ok(pk) => return pk.serialize()[1..65].to_vec(),
// _ => panic!("Unable to recover publickey")
// }
// }
/// sha512 hash for the specified input
///
/// * data: Arbitrary data to be hashed
@@ -186,24 +133,6 @@ pub mod tests {
use hex_literal::hex;
use super::*;
// // Constructs the message that Ethereum RPCs `personal_sign` and `eth_sign` would sign.
// fn ethereum_signable_message(data: &[u8]) -> Vec<u8> {
// let prefix = b"Pay DOTs to the Polkadot account:";
// let mut l = prefix.len() + data.len();
// let mut rev = Vec::new();
// let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
// while l > 0 {
// rev.push(b'0' + (l % 10) as u8);
// l /= 10;
// }
// v.extend(rev.into_iter().rev());
// v.extend_from_slice(&prefix[..]);
// v.extend_from_slice(data);
// v
// }
#[test]
fn can_blake2b() {
let data = b"abc";
@@ -245,17 +174,6 @@ pub mod tests {
assert_eq!(hash[..], expected[..]);
}
// #[test]
// fn can_secp256k1_real_eth_sig() {
// let sig = hex!("7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c");
// let pk = hex!("DF67EC7EAe23D2459694685257b6FC59d1BAA1FE");
// let data = [42, 0, 0, 0, 0, 0, 0, 0];
// let msg = ext_keccak256(&ethereum_signable_message(&data));
// let result = ext_keccak256(&ext_secp256k1_recover(&msg[..], &sig[..]));
// assert_eq!(result[12..], pk[..]);
// }
#[test]
fn can_sha512() {
let data = b"hello world";
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Copyright 2019 Paritytech via https://github.com/paritytech/schnorrkel-js/
+153
View File
@@ -0,0 +1,153 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Copyright 2019 Paritytech via https://github.com/paritytech/schnorrkel-js/
//
// Originally developed (as a fork) in https://github.com/polkadot-js/schnorrkel-js/
// which was adpated from the initial https://github.com/paritytech/schnorrkel-js/
// forked at commit eff430ddc3090f56317c80654208b8298ef7ab3f
use merlin::Transcript;
use schnorrkel::{signing_context, vrf::{VRFOutput, VRFProof}, PublicKey, SecretKey};
use wasm_bindgen::prelude::*;
/// Size of VRF output, bytes
pub const VRF_OUTPUT_SIZE: usize = 32;
/// Size of VRF proof, bytes
pub const VRF_PROOF_SIZE: usize = 64;
/// Size of VRF raw output, bytes
pub const VRF_RAW_OUTPUT_SIZE: usize = 16;
/// Size of VRF limit, bytes
pub const VRF_THRESHOLD_SIZE: usize = 16;
/// Size of the final full output
pub const VRF_RESULT_SIZE: usize = VRF_OUTPUT_SIZE + VRF_PROOF_SIZE;
pub fn create_transcript(extra: &[u8]) -> Transcript {
let mut transcript = Transcript::new(b"VRF");
// if is important that we don't append when empty, since the
// append in merging encodes length information
if extra.len() > 0 {
transcript.append_message(b"", extra);
}
transcript
}
// We don't use the non-extra vrf_sign and vrf_verify internally since in sr25519 they only wrap -
//
// https://github.com/w3f/schnorrkel/blob/8fa2ad3e9fbf0b652c724df6a87a4b3c5500f759/src/vrf.rs#L660
/// Run a Random Verifiable Function (VRF) on one single input
/// (message) transcript, and an extra message transcript,
/// producing the output signature and corresponding short proof.
///
/// * secret: UIntArray with 64 element
/// * context: Arbitrary length UIntArray
/// * message: Arbitrary length UIntArray
/// * extra: Arbitrary length UIntArray
///
/// * returned vector is the 32-byte output (signature) and 64-byte proof.
#[wasm_bindgen]
pub fn ext_vrf_sign(secret: &[u8], context: &[u8], message: &[u8], extra: &[u8]) -> Vec<u8> {
let keypair = SecretKey::from_ed25519_bytes(secret).unwrap().to_keypair();
let transcript = create_transcript(extra);
let (io, proof, _) = keypair.vrf_sign_extra(signing_context(context).bytes(message), transcript);
let mut result: [u8; VRF_RESULT_SIZE] = [0u8; VRF_RESULT_SIZE];
result[..VRF_OUTPUT_SIZE].copy_from_slice(io.as_output_bytes());
result[VRF_OUTPUT_SIZE..].copy_from_slice(&proof.to_bytes());
result.to_vec()
}
/// Verify VRF proof for one single input transcript, and an extra message transcript,
/// and corresponding output.
///
/// * pubkey: UIntArray with 32 element
/// * context: Arbitrary length UIntArray
/// * message: Arbitrary length UIntArray
/// * extra: Arbitrary length UIntArray
/// * out_and_proof: 96-byte output & proof array from the ext_vrf_sign function.
#[wasm_bindgen]
pub fn ext_vrf_verify(pubkey: &[u8], context: &[u8], message: &[u8], extra: &[u8], out_and_proof: &[u8]) -> bool {
match (
PublicKey::from_bytes(pubkey),
VRFOutput::from_bytes(&out_and_proof[..VRF_OUTPUT_SIZE]),
VRFProof::from_bytes(&out_and_proof[VRF_OUTPUT_SIZE..VRF_RESULT_SIZE])
) {
(Ok(public), Ok(out), Ok(proof)) => {
public
.vrf_verify_extra(
signing_context(context).bytes(message),
&out,
&proof,
create_transcript(extra)
)
.is_ok()
}
_ => false,
}
}
#[cfg(test)]
pub mod tests {
extern crate rand;
extern crate schnorrkel;
use super::*;
use crate::sr25519::ext_sr_from_seed;
use schnorrkel::{KEYPAIR_LENGTH, SECRET_KEY_LENGTH};
fn generate_random_seed() -> Vec<u8> {
(0..32).map(|_| rand::random::<u8>()).collect()
}
#[test]
fn vrf_sign_extra_and_verify() {
let seed = generate_random_seed();
let keypair = ext_sr_from_seed(seed.as_slice());
let private = &keypair[0..SECRET_KEY_LENGTH];
let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
let context = b"my VRF context";
let message = b"this is a message";
let extra = b"this is an extra";
// Perform multiple vrf_sign_extra calls w/ same context, message, extra args
let out_and_proof1 = ext_vrf_sign(private, context, message, extra);
let out_and_proof2 = ext_vrf_sign(private, context, message, extra);
// Basic size checks
assert!(out_and_proof1.len() == VRF_RESULT_SIZE);
assert!(out_and_proof2.len() == VRF_RESULT_SIZE);
// Given the same context, message & extra, output should be deterministic
let out1 = &out_and_proof1[..VRF_OUTPUT_SIZE];
let out2 = &out_and_proof2[..VRF_OUTPUT_SIZE];
assert_eq!(out1, out2);
// But proof is non-deterministic
let proof1 = &out_and_proof1[VRF_OUTPUT_SIZE..];
let proof2 = &out_and_proof2[VRF_OUTPUT_SIZE..];
assert_ne!(proof1, proof2);
// VRF outputs can verified w/ original context, message & extra args
assert!(ext_vrf_verify(
public,
context,
message,
extra,
&out_and_proof1
));
assert!(ext_vrf_verify(
public,
context,
message,
extra,
&out_and_proof2
));
}
}
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable camelcase */
@@ -32,4 +32,8 @@ export interface WasmCryptoInstance {
ext_sr_from_seed(a: number, b: number, c: number): void;
ext_sr_sign(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
ext_sr_verify(a: number, b: number, c: number, d: number, e: number, f: number): number;
ext_vrf_sign(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number): void;
ext_vrf_verify(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): number;
}
export type AsmCreator = (wbg: Record<string, WebAssembly.ImportValue>) => WasmCryptoInstance;
@@ -1,4 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
module.exports = null;
-6
View File
@@ -1,6 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Stub replaced at build time
module.exports = require('./asm-empty');
@@ -1,4 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
module.exports = null;
-6
View File
@@ -1,6 +0,0 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Stub replaced at build time
module.exports = require('./bytes-empty');
@@ -0,0 +1,12 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
require('override-require')(
(request) =>
request.endsWith('@polkadot/wasm-crypto-asmjs') ||
request.endsWith('@polkadot/wasm-crypto-wasm'),
(request) =>
request.endsWith('@polkadot/wasm-crypto-asmjs')
? require('../../wasm-crypto-asmjs/build/data')
: require('../../wasm-crypto-wasm/build/empty')
);
+1 -2
View File
@@ -1,6 +1,5 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// @ts-check
/* eslint-disable camelcase */
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable camelcase */
+1 -13
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable camelcase */
@@ -29,17 +29,6 @@ function pbkdf2Hash (wasm) {
assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match');
}
// function secp256k1Recover (wasm) {
// const pk = u8aToHex(wasm.keccak256(wasm.secp256k1Recover(
// hexToU8a('0x2bbfa926ee947dfac384d981f8913b4e0992e52d7b6711e0ec3d9f8b0f752648'),
// hexToU8a('0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c')
// )));
// console.log('\tPUB', pk);
// assert(pk === '0x91d366769d44a7c7690428bfdf67ec7eae23d2459694685257b6fc59d1baa1fe', 'ERROR: secp256k1Recover does not match');
// }
function scryptHash (wasm) {
const hash = u8aToHex(wasm.scrypt(stringToU8a('password'), stringToU8a('salt'), 14, 8, 1));
@@ -71,7 +60,6 @@ module.exports = {
keccak256,
pbkdf2Hash,
scryptHash,
// secp256k1Recover,
sha512Hash,
twoxHash
};
+11 -10
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
const wasm = require('../../build/index');
@@ -6,12 +6,14 @@ const bip39 = require('./bip39');
const ed25519 = require('./ed25519');
const hashing = require('./hashing');
const sr25519 = require('./sr25519');
const vrf = require('./vrf');
const tests = {
...bip39,
...ed25519,
...hashing,
...sr25519
...sr25519,
...vrf
};
function beforeAll () {
@@ -45,15 +47,14 @@ function runAll () {
}
function runUnassisted () {
(async () => {
await beforeAll();
beforeAll()
.then(() => runAll())
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
runAll();
})().catch((error) => {
console.error(error);
process.exit(-1);
});
process.exit(-1);
});
}
module.exports = {
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable camelcase */
+56
View File
@@ -0,0 +1,56 @@
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable camelcase */
const crypto = require('crypto');
const { assert, stringToU8a, u8aToHex } = require('@polkadot/util');
function extractKeys (pair) {
return [pair, pair.slice(64), pair.slice(0, 64)];
}
function randomPair (wasm) {
return extractKeys(wasm.sr25519KeypairFromSeed(crypto.randomBytes(32)));
}
function vrf_signAndVerify_compat (wasm) {
const [, pk, sk] = randomPair(wasm);
const outAndProof = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), new Uint8Array());
const isValid = wasm.vrfVerify(pk, stringToU8a('my VRF context'), stringToU8a('this is a message'), new Uint8Array(), outAndProof);
console.log('\tVRF', u8aToHex(outAndProof));
console.log('\tRES', isValid);
assert(isValid, 'ERROR: Unable to verify VRF output & proof');
}
function vrf_signAndVerify (wasm) {
const [, pk, sk] = randomPair(wasm);
const outAndProof = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
const isValid = wasm.vrfVerify(pk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'), outAndProof);
console.log('\tVRF', u8aToHex(outAndProof));
console.log('\tRES', isValid);
assert(isValid, 'ERROR: Unable to verify VRF extra output & proof');
}
function vrf_signAndVerify_deterministic (wasm) {
const [,, sk] = randomPair(wasm);
const outAndProof1 = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
const outAndProof2 = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
const sig1 = u8aToHex(outAndProof1.slice(0, 32));
const sig2 = u8aToHex(outAndProof2.slice(0, 32));
console.log('\tSG1', sig1);
console.log('\tSG2', sig2);
assert(sig1 === sig2, 'ERROR: VRF extra outputs are non-deterministic');
}
module.exports = {
vrf_signAndVerify,
vrf_signAndVerify_compat,
vrf_signAndVerify_deterministic
};
+2 -5
View File
@@ -1,12 +1,9 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
global.WebAssembly = null;
require('override-require')(
(request) => request.endsWith('wasm/asm-empty'),
() => require('../build/wasm/asm')
);
require('./alias-imports');
const { runUnassisted } = require('./all');
+3 -1
View File
@@ -1,10 +1,12 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* @jest-environment jsdom
*/
// override-require in jest.config.js
const { beforeAll, tests, wasm } = require('./all');
describe('wasm-crypto', () => {
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors
// Copyright 2019-2021 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
require('./alias-imports');
const { runUnassisted } = require('./all');
runUnassisted();
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Copyright 2019-2021 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
set -e
DEST=packages/wasm-crypto/build/esm
./scripts/clean-js.sh
yarn polkadot-dev-build-ts
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Copyright 2019-2020 @polkadot/wasm authors & contributors
# Copyright 2019-2021 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
@@ -8,46 +8,41 @@ set -e
# also change in install-build-deps
RUST_VER=nightly-2020-10-25
WSM=pkg/wasm_bg.wasm
OPT=pkg/wasm_opt.wasm
ASM=build/wasm/asm.js
echo "*** Building package"
# cleanup old
echo "*** Cleaning old builds"
rm -rf ./build ./pkg
mkdir -p build/wasm
# copy our package interfaces
echo "*** Copying package sources"
cp package.json build/
cp src/index.d.ts src/*.js build/
cp src/wasm/* build/wasm/
WSM=bytes/wasm_bg.wasm
OPT=bytes/wasm_opt.wasm
ASM=wasm-crypto-asmjs/build/data.js
# build new via wasm-pack
echo "*** Building WASM output"
echo "*** Building Rust sources"
cd wasm-crypto
rustup run $RUST_VER xargo build --target wasm32-unknown-unknown --release
# rustup run $RUST_VER cargo build --target wasm32-unknown-unknown --release -Z build-std=std,panic_abort
../../bindgen/wasm-bindgen target/wasm32-unknown-unknown/release/wasm.wasm --out-dir pkg --target web
cd ..
echo "*** Converting to WASM"
../bindgen/wasm-bindgen wasm-crypto/target/wasm32-unknown-unknown/release/wasm.wasm --out-dir bytes --target web
# wasm-pack build --release --scope polkadot --target web
# optimise
echo "*** Optimising WASM output"
# ../../wabt/bin/wasm-strip $WSM
../../binaryen/bin/wasm-opt $WSM -Os -o $OPT
../binaryen/bin/wasm-opt $WSM -Os -o $OPT
# convert wasm to base64 structure
echo "*** Packing WASM into baseX"
node ../../scripts/pack-wasm-base.js
node ../scripts/pack-wasm-base.js
# build asmjs version from the input (optimised) WASM
echo "*** Building asm.js version"
../../binaryen/bin/wasm2js --output $ASM $OPT
../binaryen/bin/wasm2js --output $ASM $OPT
# cleanup the generated asm, converting to cjs
sed -i -e '/import {/d' $ASM
sed -i -e '/export var /d' $ASM
sed -i -e 's/{abort.*},memasmFunc/wbg, memasmFunc/g' $ASM
sed -i -e 's/var retasmFunc = /module.exports = (wbg) => /' $ASM
rm -rf build/wasm/*-e
sed -i -e 's/var retasmFunc = /const asmJsInit = (wbg) => /' $ASM
echo "module.exports = { asmJsInit };" >> $ASM
# cleanups
rm -rf wasm-crypto-asmjs/build/*-e
rm -rf wasm-crypto-wasm/build/*-e
+17 -6
View File
@@ -1,17 +1,28 @@
#!/usr/bin/env bash
# Copyright 2019-2020 @polkadot/wasm authors & contributors
# Copyright 2019-2021 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
set -e
echo "*** Building packages"
cd packages/wasm-crypto
echo "*** Building JavaScript"
./scripts/build-js.sh
../../scripts/build-package.sh
echo "*** Building WASM"
cd packages
../scripts/build-wasm.sh
yarn test:wasm:js
ls -alR 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 ../..
cd ..
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Copyright 2019-2021 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
set -e
# cleanup old
echo "*** Cleaning old builds"
rm -rf packages/*/build packages/bytes
+4 -3
View File
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
# Copyright 2019-2020 @polkadot/wasm authors & contributors
# Copyright 2019-2021 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
set -e
echo "*** Cleaning packages"
./scripts/clean-js.sh
echo "*** Cleaning wasm"
cd packages/wasm-crypto
rm -rf build
cargo clean
cd ../..
+1 -1
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Copyright 2019-2020 @polkadot/wasm authors & contributors
# Copyright 2019-2021 @polkadot/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
+41 -4
View File
@@ -1,11 +1,48 @@
// Copyright 2019-2020 @polkadot/wasm authors & contributors
// Copyright 2019-2021 @polkadot/wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
const fs = require('fs');
const { formatNumber } = require('@polkadot/util');
const buffer = fs.readFileSync('./pkg/wasm_opt.wasm');
const A_NAME = 'wasm-crypto-asmjs';
const W_NAME = 'wasm-crypto-wasm';
fs.writeFileSync('./build/wasm/bytes.js', `// Generated as part of the build, do not edit
function hdr (package) {
return `// Copyright 2019-2021 @polkadot/${package} authors & contributors
// SPDX-License-Identifier: Apache-2.0
module.exports = Buffer.from('${buffer.toString('base64')}', 'base64');
// Generated as part of the build, do not edit
`;
}
// only one of the deps are included, here are the details for package.json -
// "fflate": "^0.4.2"
// "lz4js": "^0.2.0"
function getWasmBuffer (lib = 'fflate') {
const data = fs.readFileSync('./bytes/wasm_opt.wasm');
const compressed = lib === 'fflate'
? Buffer.from(require('fflate/node').zlibSync(data, { level: 9 }))
: Buffer.from(require('lz4js').compress(data));
console.log(`*** Compressed WASM via ${lib}: ${formatNumber(data.length)} -> ${formatNumber(compressed.length)} (${(100 * compressed.length / data.length).toFixed(2)}%)`);
return { compressed, sizeUncompressed: data.length };
}
fs.writeFileSync(`./${A_NAME}/build/data.mjs`, `${hdr(A_NAME)}
export { asmJsInit } from './data.js';
`);
fs.writeFileSync(`./${W_NAME}/build/bytes.mjs`, `${hdr(W_NAME)}
export { bytes, sizeCompressed, sizeUncompressed } from './bytes.js';
`);
const { compressed, sizeUncompressed } = getWasmBuffer();
fs.writeFileSync(`./${W_NAME}/build/bytes.js`, `${hdr(W_NAME)}
const sizeCompressed = ${compressed.length};
const sizeUncompressed = ${sizeUncompressed};
const bytes = '${compressed.toString('base64')}';
module.exports = { bytes, sizeCompressed, sizeUncompressed };
`);
+3 -1
View File
@@ -1,5 +1,5 @@
{
"extends": "@polkadot/dev/config/tsconfig",
"extends": "@polkadot/dev/config/tsconfig.json",
"exclude": [
"build/**/*",
"**/build/**/*"
@@ -7,6 +7,8 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@polkadot/wasm-crypto-asmjs": [ "packages/wasm-crypto-asmjs/src/data" ],
"@polkadot/wasm-crypto-wasm": [ "packages/wasm-crypto-wasm/src/data" ],
"@polkadot/wasm-crypto": [ "packages/wasm-crypto/src" ]
},
"skipLibCheck": true,
+1764 -1560
View File
File diff suppressed because it is too large Load Diff