@pezkuwi/util-crypto 14.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/README.md +17 -0
  2. package/package.json +45 -0
  3. package/src/address/addressToEvm.spec.ts +16 -0
  4. package/src/address/addressToEvm.ts +12 -0
  5. package/src/address/check.spec.ts +44 -0
  6. package/src/address/check.ts +34 -0
  7. package/src/address/checksum.spec.ts +45 -0
  8. package/src/address/checksum.ts +25 -0
  9. package/src/address/decode.spec.ts +138 -0
  10. package/src/address/decode.ts +41 -0
  11. package/src/address/defaults.ts +12 -0
  12. package/src/address/derive.spec.ts +26 -0
  13. package/src/address/derive.ts +36 -0
  14. package/src/address/encode.spec.ts +177 -0
  15. package/src/address/encode.ts +43 -0
  16. package/src/address/encodeDerived.spec.ts +14 -0
  17. package/src/address/encodeDerived.ts +19 -0
  18. package/src/address/encodeMulti.spec.ts +18 -0
  19. package/src/address/encodeMulti.ts +18 -0
  20. package/src/address/eq.spec.ts +45 -0
  21. package/src/address/eq.ts +24 -0
  22. package/src/address/evmToAddress.spec.ts +20 -0
  23. package/src/address/evmToAddress.ts +24 -0
  24. package/src/address/index.ts +21 -0
  25. package/src/address/is.spec.ts +113 -0
  26. package/src/address/is.ts +14 -0
  27. package/src/address/keyDerived.spec.ts +24 -0
  28. package/src/address/keyDerived.ts +22 -0
  29. package/src/address/keyMulti.spec.ts +20 -0
  30. package/src/address/keyMulti.ts +23 -0
  31. package/src/address/setSS58Format.spec.ts +21 -0
  32. package/src/address/setSS58Format.ts +20 -0
  33. package/src/address/sort.spec.ts +22 -0
  34. package/src/address/sort.ts +17 -0
  35. package/src/address/sshash.ts +12 -0
  36. package/src/address/types.ts +6 -0
  37. package/src/address/util.ts +8 -0
  38. package/src/address/validate.spec.ts +113 -0
  39. package/src/address/validate.ts +10 -0
  40. package/src/base32/bs32.ts +53 -0
  41. package/src/base32/decode.spec.ts +34 -0
  42. package/src/base32/encode.spec.ts +30 -0
  43. package/src/base32/helpers.ts +93 -0
  44. package/src/base32/index.ts +8 -0
  45. package/src/base32/is.spec.ts +32 -0
  46. package/src/base32/validate.spec.ts +44 -0
  47. package/src/base58/bs58.ts +43 -0
  48. package/src/base58/decode.spec.ts +31 -0
  49. package/src/base58/encode.spec.ts +26 -0
  50. package/src/base58/index.ts +8 -0
  51. package/src/base58/validate.spec.ts +20 -0
  52. package/src/base64/bs64.ts +43 -0
  53. package/src/base64/decode.spec.ts +42 -0
  54. package/src/base64/encode.spec.ts +14 -0
  55. package/src/base64/index.ts +10 -0
  56. package/src/base64/pad.spec.ts +14 -0
  57. package/src/base64/pad.ts +10 -0
  58. package/src/base64/trim.spec.ts +14 -0
  59. package/src/base64/trim.ts +14 -0
  60. package/src/base64/validate.spec.ts +32 -0
  61. package/src/blake2/asHex.spec.ts +57 -0
  62. package/src/blake2/asU8a.spec.ts +74 -0
  63. package/src/blake2/asU8a.ts +40 -0
  64. package/src/blake2/index.ts +8 -0
  65. package/src/bn.ts +15 -0
  66. package/src/bundle.ts +33 -0
  67. package/src/bundleInit.ts +11 -0
  68. package/src/crypto.spec.ts +18 -0
  69. package/src/crypto.ts +18 -0
  70. package/src/ed25519/deriveHard.ts +18 -0
  71. package/src/ed25519/index.ts +13 -0
  72. package/src/ed25519/pair/fromRandom.spec.ts +28 -0
  73. package/src/ed25519/pair/fromRandom.ts +25 -0
  74. package/src/ed25519/pair/fromSecret.spec.ts +33 -0
  75. package/src/ed25519/pair/fromSecret.ts +29 -0
  76. package/src/ed25519/pair/fromSeed.spec.ts +42 -0
  77. package/src/ed25519/pair/fromSeed.ts +41 -0
  78. package/src/ed25519/pair/fromString.spec.ts +17 -0
  79. package/src/ed25519/pair/fromString.ts +31 -0
  80. package/src/ed25519/sign.spec.ts +40 -0
  81. package/src/ed25519/sign.ts +38 -0
  82. package/src/ed25519/verify.spec.ts +84 -0
  83. package/src/ed25519/verify.ts +41 -0
  84. package/src/ethereum/encode.spec.ts +59 -0
  85. package/src/ethereum/encode.ts +39 -0
  86. package/src/ethereum/index.ts +6 -0
  87. package/src/ethereum/isAddress.spec.ts +34 -0
  88. package/src/ethereum/isAddress.ts +16 -0
  89. package/src/ethereum/isChecksum.ts +27 -0
  90. package/src/ethereum/isCheksum.spec.ts +30 -0
  91. package/src/hd/ethereum/index.spec.ts +54 -0
  92. package/src/hd/ethereum/index.ts +69 -0
  93. package/src/hd/index.ts +6 -0
  94. package/src/hd/ledger/derivePrivate.ts +34 -0
  95. package/src/hd/ledger/index.spec.ts +64 -0
  96. package/src/hd/ledger/index.ts +42 -0
  97. package/src/hd/ledger/master.spec.ts +19 -0
  98. package/src/hd/ledger/master.ts +26 -0
  99. package/src/hd/validatePath.spec.ts +30 -0
  100. package/src/hd/validatePath.ts +24 -0
  101. package/src/helpers.ts +38 -0
  102. package/src/hmac/index.ts +4 -0
  103. package/src/hmac/shaAsU8a.spec.ts +45 -0
  104. package/src/hmac/shaAsU8a.ts +48 -0
  105. package/src/index.ts +6 -0
  106. package/src/json/constants.ts +11 -0
  107. package/src/json/decrypt.ts +25 -0
  108. package/src/json/decryptData.ts +45 -0
  109. package/src/json/encrypt.ts +25 -0
  110. package/src/json/encryptFormat.ts +20 -0
  111. package/src/json/index.ts +7 -0
  112. package/src/json/types.ts +22 -0
  113. package/src/keccak/asHex.spec.ts +30 -0
  114. package/src/keccak/asU8a.spec.ts +56 -0
  115. package/src/keccak/asU8a.ts +45 -0
  116. package/src/keccak/index.ts +8 -0
  117. package/src/key/DeriveJunction.ts +79 -0
  118. package/src/key/extractPath.spec.ts +51 -0
  119. package/src/key/extractPath.ts +37 -0
  120. package/src/key/extractSuri.spec.ts +147 -0
  121. package/src/key/extractSuri.ts +40 -0
  122. package/src/key/fromPath.ts +28 -0
  123. package/src/key/hdkdDerive.ts +17 -0
  124. package/src/key/hdkdEcdsa.ts +8 -0
  125. package/src/key/hdkdEd25519.ts +7 -0
  126. package/src/key/hdkdSr25519.ts +14 -0
  127. package/src/key/index.ts +12 -0
  128. package/src/mnemonic/bip39.spec.ts +80 -0
  129. package/src/mnemonic/bip39.ts +127 -0
  130. package/src/mnemonic/generate.spec.ts +58 -0
  131. package/src/mnemonic/generate.ts +25 -0
  132. package/src/mnemonic/index.ts +11 -0
  133. package/src/mnemonic/toEntropy.spec.ts +36 -0
  134. package/src/mnemonic/toEntropy.ts +13 -0
  135. package/src/mnemonic/toLegacySeed.spec.ts +52 -0
  136. package/src/mnemonic/toLegacySeed.ts +39 -0
  137. package/src/mnemonic/toMiniSecret.spec.ts +67 -0
  138. package/src/mnemonic/toMiniSecret.ts +23 -0
  139. package/src/mnemonic/toMiniSecretCmp.spec.ts +64 -0
  140. package/src/mnemonic/validate.spec.ts +39 -0
  141. package/src/mnemonic/validate.ts +26 -0
  142. package/src/mnemonic/wordlists/en.ts +7 -0
  143. package/src/mnemonic/wordlists/es.ts +7 -0
  144. package/src/mnemonic/wordlists/fr.ts +7 -0
  145. package/src/mnemonic/wordlists/index.ts +11 -0
  146. package/src/mnemonic/wordlists/it.ts +7 -0
  147. package/src/mnemonic/wordlists/jp.ts +7 -0
  148. package/src/mnemonic/wordlists/ko.ts +7 -0
  149. package/src/mnemonic/wordlists/zh-s.ts +7 -0
  150. package/src/mnemonic/wordlists/zh-t.ts +7 -0
  151. package/src/mod.ts +4 -0
  152. package/src/nacl/decrypt.spec.ts +26 -0
  153. package/src/nacl/decrypt.ts +22 -0
  154. package/src/nacl/encrypt.spec.ts +20 -0
  155. package/src/nacl/encrypt.ts +31 -0
  156. package/src/nacl/index.ts +8 -0
  157. package/src/nacl/tweetnacl-secretbox-data.spec.ts +4629 -0
  158. package/src/nacl/tweetnacl-secretbox.spec.ts +161 -0
  159. package/src/nacl/tweetnacl.ts +1159 -0
  160. package/src/networks.ts +5 -0
  161. package/src/packageDetect.ts +14 -0
  162. package/src/packageInfo.ts +6 -0
  163. package/src/pbkdf2/encode.spec.ts +54 -0
  164. package/src/pbkdf2/encode.ts +29 -0
  165. package/src/pbkdf2/index.ts +4 -0
  166. package/src/random/asHex.spec.ts +38 -0
  167. package/src/random/asNumber.spec.ts +16 -0
  168. package/src/random/asNumber.ts +28 -0
  169. package/src/random/asU8a.spec.ts +36 -0
  170. package/src/random/asU8a.ts +30 -0
  171. package/src/random/index.ts +9 -0
  172. package/src/scrypt/defaults.ts +19 -0
  173. package/src/scrypt/encode.spec.ts +43 -0
  174. package/src/scrypt/encode.ts +30 -0
  175. package/src/scrypt/fromU8a.ts +44 -0
  176. package/src/scrypt/index.ts +6 -0
  177. package/src/scrypt/toU8a.ts +17 -0
  178. package/src/scrypt/types.ts +9 -0
  179. package/src/secp256k1/compress.spec.ts +47 -0
  180. package/src/secp256k1/compress.ts +21 -0
  181. package/src/secp256k1/deriveHard.ts +17 -0
  182. package/src/secp256k1/expand.spec.ts +47 -0
  183. package/src/secp256k1/expand.ts +30 -0
  184. package/src/secp256k1/hasher.spec.ts +24 -0
  185. package/src/secp256k1/hasher.ts +13 -0
  186. package/src/secp256k1/index.ts +10 -0
  187. package/src/secp256k1/pair/fromSeed.spec.ts +75 -0
  188. package/src/secp256k1/pair/fromSeed.ts +42 -0
  189. package/src/secp256k1/recover.spec.ts +35 -0
  190. package/src/secp256k1/recover.ts +36 -0
  191. package/src/secp256k1/sign.spec.ts +39 -0
  192. package/src/secp256k1/sign.ts +37 -0
  193. package/src/secp256k1/signVerify.spec.ts +94 -0
  194. package/src/secp256k1/tweakAdd.spec.ts +35 -0
  195. package/src/secp256k1/tweakAdd.ts +65 -0
  196. package/src/secp256k1/types.ts +4 -0
  197. package/src/secp256k1/verify.spec.ts +81 -0
  198. package/src/secp256k1/verify.ts +32 -0
  199. package/src/sha/asU8a.ts +30 -0
  200. package/src/sha/asU8a256.spec.ts +55 -0
  201. package/src/sha/asU8a512.spec.ts +33 -0
  202. package/src/sha/index.ts +8 -0
  203. package/src/signature/index.ts +8 -0
  204. package/src/signature/verify.spec.ts +230 -0
  205. package/src/signature/verify.ts +114 -0
  206. package/src/sr25519/agreement.spec.ts +31 -0
  207. package/src/sr25519/agreement.ts +23 -0
  208. package/src/sr25519/derive.ts +21 -0
  209. package/src/sr25519/deriveHard.ts +9 -0
  210. package/src/sr25519/derivePublic.ts +18 -0
  211. package/src/sr25519/deriveSoft.ts +9 -0
  212. package/src/sr25519/index.ts +12 -0
  213. package/src/sr25519/pair/fromSeed.spec.ts +35 -0
  214. package/src/sr25519/pair/fromSeed.ts +28 -0
  215. package/src/sr25519/pair/fromU8a.ts +23 -0
  216. package/src/sr25519/pair/testing.spec.ts +161 -0
  217. package/src/sr25519/pair/toU8a.ts +10 -0
  218. package/src/sr25519/sign.spec.ts +28 -0
  219. package/src/sr25519/sign.ts +22 -0
  220. package/src/sr25519/verify.spec.ts +42 -0
  221. package/src/sr25519/verify.ts +23 -0
  222. package/src/sr25519/vrfSign.ts +24 -0
  223. package/src/sr25519/vrfSignVerify.spec.ts +73 -0
  224. package/src/sr25519/vrfVerify.ts +25 -0
  225. package/src/test/index.ts +8 -0
  226. package/src/test/performance.ts +17 -0
  227. package/src/types.ts +33 -0
  228. package/src/xxhash/asHex.spec.ts +36 -0
  229. package/src/xxhash/asU8a.spec.ts +48 -0
  230. package/src/xxhash/asU8a.ts +45 -0
  231. package/src/xxhash/index.ts +8 -0
  232. package/src/xxhash/xxhash64.ts +155 -0
  233. package/tsconfig.build.json +18 -0
  234. package/tsconfig.spec.json +20 -0
@@ -0,0 +1,14 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { base64Pad } from './index.js';
7
+
8
+ describe('base64Pad', (): void => {
9
+ it('pads a utf-8 string', (): void => {
10
+ expect(
11
+ base64Pad('YWJjZA')
12
+ ).toEqual('YWJjZA==');
13
+ });
14
+ });
@@ -0,0 +1,10 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * @name base64Pad
6
+ * @description Adds padding characters for correct length
7
+ */
8
+ export function base64Pad (value: string): string {
9
+ return value.padEnd(value.length + (value.length % 4), '=');
10
+ }
@@ -0,0 +1,14 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { base64Trim } from './index.js';
7
+
8
+ describe('base64Trim', (): void => {
9
+ it('trims a utf-8 string', (): void => {
10
+ expect(
11
+ base64Trim('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
12
+ ).toEqual('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ');
13
+ });
14
+ });
@@ -0,0 +1,14 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * @name base64Trim
6
+ * @description Trims padding characters
7
+ */
8
+ export function base64Trim (value: string): string {
9
+ while (value.length && value.endsWith('=')) {
10
+ value = value.slice(0, -1);
11
+ }
12
+
13
+ return value;
14
+ }
@@ -0,0 +1,32 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { base64Validate } from './index.js';
7
+
8
+ describe('base64Validate', (): void => {
9
+ it('validates a mixed base64 utf8 string', (): void => {
10
+ expect(
11
+ () => base64Validate('aGVsbG8gd29ybGQg0J/RgNC40LLQtd^GC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
12
+ ).toThrow(/Invalid base64 character "\^" \(0x5e\) at index 30/);
13
+ });
14
+
15
+ it('validates with one extra padding character', (): void => {
16
+ expect(
17
+ base64Validate('bGlnaHQgd28=')
18
+ ).toEqual(true);
19
+ });
20
+
21
+ it('validates with two extra padding characters', (): void => {
22
+ expect(
23
+ base64Validate('bGlnaHQgdw==')
24
+ ).toEqual(true);
25
+ });
26
+
27
+ it('validates misplaced padding characters', (): void => {
28
+ expect(
29
+ () => base64Validate('bGlnaHQgdw=g=')
30
+ ).toThrow(/Invalid base64 padding sequence "=g"/);
31
+ });
32
+ });
@@ -0,0 +1,57 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { hexToU8a } from '@pezkuwi/util';
7
+ import { waitReady } from '@pezkuwi/wasm-crypto';
8
+
9
+ import { blake2AsHex } from './index.js';
10
+
11
+ describe('blake2AsHex', (): void => {
12
+ beforeEach(async (): Promise<void> => {
13
+ await waitReady();
14
+ });
15
+
16
+ for (const onlyJs of [false, true]) {
17
+ describe(`onlyJs=${(onlyJs && 'true') || 'false'}`, (): void => {
18
+ it('returns a 64-bit value (specified)', (): void => {
19
+ expect(
20
+ blake2AsHex('abc', 64, null, onlyJs)
21
+ ).toEqual('0xd8bb14d833d59559');
22
+ });
23
+
24
+ it('returns a 128-bit value (as specified)', (): void => {
25
+ expect(
26
+ blake2AsHex('abc', 128, null, onlyJs)
27
+ ).toEqual('0xcf4ab791c62b8d2b2109c90275287816');
28
+ });
29
+
30
+ it('returns a 128-bit value (as specified, with key)', (): void => {
31
+ expect(
32
+ blake2AsHex('abc', 128, new Uint8Array([1, 2]), onlyJs)
33
+ ).toEqual('0x36f3d08cda72a00ddf2be103eb5770d9');
34
+ });
35
+
36
+ it('returns a 256-bit value (default)', (): void => {
37
+ expect(
38
+ blake2AsHex('abc', undefined, null, onlyJs)
39
+ ).toEqual('0xbddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319');
40
+ });
41
+
42
+ it('returns a 512-bit value (as specified)', (): void => {
43
+ expect(
44
+ blake2AsHex('abc', 512, null, onlyJs)
45
+ ).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923');
46
+ });
47
+
48
+ it('matches with the Rust implementation', (): void => {
49
+ expect(
50
+ blake2AsHex(
51
+ hexToU8a('0x454545454545454545454545454545454545454545454545454545454545454501000000000000002481853da20b9f4322f34650fea5f240dcbfb266d02db94bfa0153c31f4a29dbdbf025dd4a69a6f4ee6e1577b251b655097e298b692cb34c18d3182cac3de0dc00000000'), 256, undefined, onlyJs
52
+ )
53
+ ).toEqual('0x1025e5db74fdaf4d2818822dccf0e1604ae9ccc62f26cecfde23448ff0248abf');
54
+ });
55
+ });
56
+ }
57
+ });
@@ -0,0 +1,74 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { hexToU8a } from '@pezkuwi/util';
7
+ import { waitReady } from '@pezkuwi/wasm-crypto';
8
+
9
+ import { perfWasm } from '../test/index.js';
10
+ import { blake2AsU8a } from './index.js';
11
+
12
+ describe('blake2AsU8a', (): void => {
13
+ beforeEach(async (): Promise<void> => {
14
+ await waitReady();
15
+ });
16
+
17
+ for (const onlyJs of [false, true]) {
18
+ describe(`onlyJs=${(onlyJs && 'true') || 'false'}`, (): void => {
19
+ it('returns a 64-bit value by default', (): void => {
20
+ expect(
21
+ blake2AsU8a('abc', undefined, undefined, onlyJs)
22
+ ).toEqual(
23
+ new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25])
24
+ );
25
+ });
26
+
27
+ it('returns a 128-bit value (as specified,)', (): void => {
28
+ expect(
29
+ blake2AsU8a('abc', 128, undefined, onlyJs)
30
+ ).toEqual(
31
+ new Uint8Array([207, 74, 183, 145, 198, 43, 141, 43, 33, 9, 201, 2, 117, 40, 120, 22])
32
+ );
33
+ });
34
+
35
+ it('returns a 256-bit value (as specified)', (): void => {
36
+ expect(
37
+ blake2AsU8a('abc', 256, undefined, onlyJs)
38
+ ).toEqual(
39
+ new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25])
40
+ );
41
+ });
42
+
43
+ it('returns a 512-bit value (as specified)', (): void => {
44
+ expect(
45
+ blake2AsU8a('abc', 512, undefined, onlyJs)
46
+ ).toEqual(
47
+ hexToU8a('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923')
48
+ );
49
+ });
50
+ });
51
+ }
52
+
53
+ it('has equivalent Wasm/Js outputs on hex inputs', (): void => {
54
+ const a = blake2AsU8a('0x123456', 256, null, false);
55
+ const b = blake2AsU8a('0x123456', 256, null, true);
56
+
57
+ expect(a).toEqual(b);
58
+ });
59
+
60
+ it('has equivalent Wasm/Js outputs with key inputs', (): void => {
61
+ const a = blake2AsU8a('0x123456', 256, new Uint8Array([1, 2]), false);
62
+ const b = blake2AsU8a('0x123456', 256, new Uint8Array([1, 2]), true);
63
+
64
+ expect(a).toEqual(b);
65
+ });
66
+
67
+ for (const bitLength of [256, 512] as const) {
68
+ describe(`bitLength=${bitLength}`, (): void => {
69
+ perfWasm(`blake2AsU8a, bitLength=${bitLength}`, 64000, (input, onlyJs) =>
70
+ blake2AsU8a(input, bitLength, null, onlyJs)
71
+ );
72
+ });
73
+ }
74
+ });
@@ -0,0 +1,40 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { blake2b as blake2bJs } from '@noble/hashes/blake2b';
5
+
6
+ import { hasBigInt, u8aToU8a } from '@pezkuwi/util';
7
+ import { blake2b, isReady } from '@pezkuwi/wasm-crypto';
8
+
9
+ import { createAsHex } from '../helpers.js';
10
+
11
+ /**
12
+ * @name blake2AsU8a
13
+ * @summary Creates a blake2b u8a from the input.
14
+ * @description
15
+ * From a `Uint8Array` input, create the blake2b and return the result as a u8a with the specified `bitLength`.
16
+ * @example
17
+ * <BR>
18
+ *
19
+ * ```javascript
20
+ * import { blake2AsU8a } from '@pezkuwi/util-crypto';
21
+ *
22
+ * blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
23
+ * ```
24
+ */
25
+ export function blake2AsU8a (data: string | Uint8Array, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
26
+ const byteLength = Math.ceil(bitLength / 8);
27
+ const u8a = u8aToU8a(data);
28
+
29
+ return !hasBigInt || (!onlyJs && isReady())
30
+ ? blake2b(u8a, u8aToU8a(key), byteLength)
31
+ : key
32
+ ? blake2bJs(u8a, { dkLen: byteLength, key })
33
+ : blake2bJs(u8a, { dkLen: byteLength });
34
+ }
35
+
36
+ /**
37
+ * @name blake2AsHex
38
+ * @description Creates a blake2b hex from the input.
39
+ */
40
+ export const blake2AsHex = /*#__PURE__*/ createAsHex(blake2AsU8a);
@@ -0,0 +1,8 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * @summary Create blake2b values with specified bitlengths
6
+ */
7
+
8
+ export { blake2AsHex, blake2AsU8a } from './asU8a.js';
package/src/bn.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export const BN_BE_OPTS = { isLe: false };
5
+ export const BN_LE_OPTS = { isLe: true };
6
+
7
+ export const BN_LE_16_OPTS = { bitLength: 16, isLe: true };
8
+
9
+ export const BN_BE_32_OPTS = { bitLength: 32, isLe: false };
10
+ export const BN_LE_32_OPTS = { bitLength: 32, isLe: true };
11
+
12
+ export const BN_BE_256_OPTS = { bitLength: 256, isLe: false };
13
+ export const BN_LE_256_OPTS = { bitLength: 256, isLe: true };
14
+
15
+ export const BN_LE_512_OPTS = { bitLength: 512, isLe: true };
package/src/bundle.ts ADDED
@@ -0,0 +1,33 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import './bundleInit.js';
5
+
6
+ // all named
7
+ export { packageInfo } from './packageInfo.js';
8
+
9
+ // all starred
10
+ export * from './address/index.js';
11
+ export * from './base32/index.js';
12
+ export * from './base58/index.js';
13
+ export * from './base64/index.js';
14
+ export * from './blake2/index.js';
15
+ export * from './crypto.js';
16
+ export * from './ed25519/index.js';
17
+ export * from './ethereum/index.js';
18
+ export * from './hd/index.js';
19
+ export * from './hmac/index.js';
20
+ export * from './json/index.js';
21
+ export * from './keccak/index.js';
22
+ export * from './key/index.js';
23
+ export * from './mnemonic/index.js';
24
+ export * from './nacl/index.js';
25
+ export * from './networks.js';
26
+ export * from './pbkdf2/index.js';
27
+ export * from './random/index.js';
28
+ export * from './scrypt/index.js';
29
+ export * from './secp256k1/index.js';
30
+ export * from './sha/index.js';
31
+ export * from './signature/index.js';
32
+ export * from './sr25519/index.js';
33
+ export * from './xxhash/index.js';
@@ -0,0 +1,11 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import '@polkadot/x-bigint/shim';
5
+
6
+ import { cryptoWaitReady } from './crypto.js';
7
+
8
+ // start init process immediately
9
+ cryptoWaitReady().catch((): void => {
10
+ // shouldn't happen, logged and caught inside cryptoWaitReady
11
+ });
@@ -0,0 +1,18 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ describe('cryptoWaitReady', (): void => {
7
+ it('should return false when it cannot initialize', async (): Promise<void> => {
8
+ const old = global.WebAssembly;
9
+
10
+ global.WebAssembly = null as unknown as typeof WebAssembly;
11
+
12
+ const { cryptoWaitReady } = await import('./crypto.js');
13
+
14
+ expect(await cryptoWaitReady()).toBe(false);
15
+
16
+ global.WebAssembly = old;
17
+ });
18
+ });
package/src/crypto.ts ADDED
@@ -0,0 +1,18 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { isReady, waitReady } from '@pezkuwi/wasm-crypto';
5
+
6
+ export const cryptoIsReady = isReady;
7
+
8
+ export function cryptoWaitReady (): Promise<boolean> {
9
+ return waitReady()
10
+ .then((): boolean => {
11
+ if (!isReady()) {
12
+ throw new Error('Unable to initialize @polkadot/util-crypto');
13
+ }
14
+
15
+ return true;
16
+ })
17
+ .catch(() => false);
18
+ }
@@ -0,0 +1,18 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { compactAddLength, isU8a, stringToU8a, u8aConcat } from '@pezkuwi/util';
5
+
6
+ import { blake2AsU8a } from '../blake2/asU8a.js';
7
+
8
+ const HDKD = compactAddLength(stringToU8a('Ed25519HDKD'));
9
+
10
+ export function ed25519DeriveHard (seed: Uint8Array, chainCode: Uint8Array): Uint8Array {
11
+ if (!isU8a(chainCode) || chainCode.length !== 32) {
12
+ throw new Error('Invalid chainCode passed to derive');
13
+ }
14
+
15
+ return blake2AsU8a(
16
+ u8aConcat(HDKD, seed, chainCode)
17
+ );
18
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * @summary Implements ed25519 operations
6
+ */
7
+ export { ed25519DeriveHard } from './deriveHard.js';
8
+ export { ed25519PairFromRandom } from './pair/fromRandom.js';
9
+ export { ed25519PairFromSecret } from './pair/fromSecret.js';
10
+ export { ed25519PairFromSeed } from './pair/fromSeed.js';
11
+ export { ed25519PairFromString } from './pair/fromString.js';
12
+ export { ed25519Sign } from './sign.js';
13
+ export { ed25519Verify } from './verify.js';
@@ -0,0 +1,28 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import type { Keypair } from '../../types.js';
7
+
8
+ import { ed25519PairFromRandom } from '../index.js';
9
+
10
+ describe('ed25519PairFromRandom', (): void => {
11
+ let keypair: Keypair;
12
+
13
+ beforeEach((): void => {
14
+ keypair = ed25519PairFromRandom();
15
+ });
16
+
17
+ it('generates a valid publicKey', (): void => {
18
+ expect(
19
+ keypair.publicKey
20
+ ).toHaveLength(32);
21
+ });
22
+
23
+ it('generates a valid secretKey', (): void => {
24
+ expect(
25
+ keypair.secretKey
26
+ ).toHaveLength(64);
27
+ });
28
+ });
@@ -0,0 +1,25 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Keypair } from '../../types.js';
5
+
6
+ import { randomAsU8a } from '../../random/index.js';
7
+ import { ed25519PairFromSeed } from './fromSeed.js';
8
+
9
+ /**
10
+ * @name ed25519PairFromRandom
11
+ * @summary Creates a new public/secret keypair.
12
+ * @description
13
+ * Returns a new generate object containing a `publicKey` & `secretKey`.
14
+ * @example
15
+ * <BR>
16
+ *
17
+ * ```javascript
18
+ * import { ed25519PairFromRandom } from '@pezkuwi/util-crypto';
19
+ *
20
+ * ed25519PairFromRandom(); // => { secretKey: [...], publicKey: [...] }
21
+ * ```
22
+ */
23
+ export function ed25519PairFromRandom (): Keypair {
24
+ return ed25519PairFromSeed(randomAsU8a());
25
+ }
@@ -0,0 +1,33 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { ed25519PairFromSecret } from '../index.js';
7
+
8
+ describe('ed25519PairFromSecret', (): void => {
9
+ const secretKey = new Uint8Array([
10
+ 18, 52, 86, 120, 144, 18, 52, 86,
11
+ 120, 144, 18, 52, 86, 120, 144, 18,
12
+ 18, 52, 86, 120, 144, 18, 52, 86,
13
+ 120, 144, 18, 52, 86, 120, 144, 18,
14
+ 180, 114, 93, 155, 165, 255, 217, 82,
15
+ 16, 250, 209, 11, 193, 10, 88, 218,
16
+ 190, 190, 41, 193, 236, 252, 1, 152,
17
+ 216, 214, 0, 41, 45, 138, 13, 53
18
+ ]);
19
+
20
+ it('generates a valid publicKey/secretKey pair', (): void => {
21
+ expect(
22
+ ed25519PairFromSecret(secretKey)
23
+ ).toEqual({
24
+ publicKey: new Uint8Array([
25
+ 180, 114, 93, 155, 165, 255, 217, 82,
26
+ 16, 250, 209, 11, 193, 10, 88, 218,
27
+ 190, 190, 41, 193, 236, 252, 1, 152,
28
+ 216, 214, 0, 41, 45, 138, 13, 53
29
+ ]),
30
+ secretKey
31
+ });
32
+ });
33
+ });
@@ -0,0 +1,29 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Keypair } from '../../types.js';
5
+
6
+ /**
7
+ * @name ed25519PairFromSecret
8
+ * @summary Creates a new public/secret keypair from a secret.
9
+ * @description
10
+ * Returns a object containing a `publicKey` & `secretKey` generated from the supplied secret.
11
+ * @example
12
+ * <BR>
13
+ *
14
+ * ```javascript
15
+ * import { ed25519PairFromSecret } from '@pezkuwi/util-crypto';
16
+ *
17
+ * ed25519PairFromSecret(...); // => { secretKey: [...], publicKey: [...] }
18
+ * ```
19
+ */
20
+ export function ed25519PairFromSecret (secretKey: Uint8Array): Keypair {
21
+ if (secretKey.length !== 64) {
22
+ throw new Error('Invalid secretKey provided');
23
+ }
24
+
25
+ return {
26
+ publicKey: secretKey.slice(32),
27
+ secretKey
28
+ };
29
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { stringToU8a } from '@pezkuwi/util';
7
+ import { waitReady } from '@pezkuwi/wasm-crypto';
8
+
9
+ import { ed25519PairFromSeed } from '../index.js';
10
+
11
+ describe('ed25519PairFromSeed', (): void => {
12
+ // NOTE: Aligned with Rust test, b"12345678901234567890123456789012"
13
+ const TEST = stringToU8a('12345678901234567890123456789012');
14
+ const RESULT = {
15
+ publicKey: new Uint8Array([
16
+ 0x2f, 0x8c, 0x61, 0x29, 0xd8, 0x16, 0xcf, 0x51,
17
+ 0xc3, 0x74, 0xbc, 0x7f, 0x08, 0xc3, 0xe6, 0x3e,
18
+ 0xd1, 0x56, 0xcf, 0x78, 0xae, 0xfb, 0x4a, 0x65,
19
+ 0x50, 0xd9, 0x7b, 0x87, 0x99, 0x79, 0x77, 0xee
20
+ ]),
21
+ secretKey: new Uint8Array([
22
+ 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50,
23
+ // public part
24
+ 0x2f, 0x8c, 0x61, 0x29, 0xd8, 0x16, 0xcf, 0x51,
25
+ 0xc3, 0x74, 0xbc, 0x7f, 0x08, 0xc3, 0xe6, 0x3e,
26
+ 0xd1, 0x56, 0xcf, 0x78, 0xae, 0xfb, 0x4a, 0x65,
27
+ 0x50, 0xd9, 0x7b, 0x87, 0x99, 0x79, 0x77, 0xee
28
+ ])
29
+ };
30
+
31
+ beforeEach(async (): Promise<void> => {
32
+ await waitReady();
33
+ });
34
+
35
+ it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
36
+ [true, false].forEach((onlyJs): void => {
37
+ expect(
38
+ ed25519PairFromSeed(TEST, onlyJs)
39
+ ).toEqual(RESULT);
40
+ });
41
+ });
42
+ });
@@ -0,0 +1,41 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Keypair } from '../../types.js';
5
+
6
+ import { ed25519 } from '@noble/curves/ed25519';
7
+
8
+ import { hasBigInt, u8aConcatStrict } from '@pezkuwi/util';
9
+ import { ed25519KeypairFromSeed, isReady } from '@pezkuwi/wasm-crypto';
10
+
11
+ /**
12
+ * @name ed25519PairFromSeed
13
+ * @summary Creates a new public/secret keypair from a seed.
14
+ * @description
15
+ * Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
16
+ * @example
17
+ * <BR>
18
+ *
19
+ * ```javascript
20
+ * import { ed25519PairFromSeed } from '@pezkuwi/util-crypto';
21
+ *
22
+ * ed25519PairFromSeed(...); // => { secretKey: [...], publicKey: [...] }
23
+ * ```
24
+ */
25
+ export function ed25519PairFromSeed (seed: Uint8Array, onlyJs?: boolean): Keypair {
26
+ if (!hasBigInt || (!onlyJs && isReady())) {
27
+ const full = ed25519KeypairFromSeed(seed);
28
+
29
+ return {
30
+ publicKey: full.slice(32),
31
+ secretKey: full.slice(0, 64)
32
+ };
33
+ }
34
+
35
+ const publicKey = ed25519.getPublicKey(seed);
36
+
37
+ return {
38
+ publicKey,
39
+ secretKey: u8aConcatStrict([seed, publicKey])
40
+ };
41
+ }
@@ -0,0 +1,17 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { ed25519PairFromString } from '../index.js';
7
+
8
+ describe('ed25519PairFromSeed', (): void => {
9
+ it('generates a valid publicKey/secretKey pair', (): void => {
10
+ expect(
11
+ ed25519PairFromString('test')
12
+ ).toEqual({
13
+ publicKey: new Uint8Array([188, 108, 179, 142, 36, 142, 76, 87, 77, 193, 147, 139, 254, 110, 196, 217, 117, 233, 167, 165, 250, 150, 247, 237, 198, 68, 129, 4, 211, 209, 136, 48]),
14
+ secretKey: new Uint8Array([146, 139, 32, 54, 105, 67, 226, 175, 209, 30, 188, 14, 174, 46, 83, 169, 59, 241, 119, 164, 252, 243, 91, 204, 100, 213, 3, 112, 78, 101, 226, 2, 188, 108, 179, 142, 36, 142, 76, 87, 77, 193, 147, 139, 254, 110, 196, 217, 117, 233, 167, 165, 250, 150, 247, 237, 198, 68, 129, 4, 211, 209, 136, 48])
15
+ });
16
+ });
17
+ });
@@ -0,0 +1,31 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Keypair } from '../../types.js';
5
+
6
+ import { stringToU8a } from '@pezkuwi/util';
7
+
8
+ import { blake2AsU8a } from '../../blake2/asU8a.js';
9
+ import { ed25519PairFromSeed } from './fromSeed.js';
10
+
11
+ /**
12
+ * @name ed25519PairFromString
13
+ * @summary Creates a new public/secret keypair from a string.
14
+ * @description
15
+ * Returns a object containing a `publicKey` & `secretKey` generated from the supplied string. The string is hashed and the value used as the input seed.
16
+ * @example
17
+ * <BR>
18
+ *
19
+ * ```javascript
20
+ * import { ed25519PairFromString } from '@pezkuwi/util-crypto';
21
+ *
22
+ * ed25519PairFromString('test'); // => { secretKey: [...], publicKey: [...] }
23
+ * ```
24
+ */
25
+ export function ed25519PairFromString (value: string): Keypair {
26
+ return ed25519PairFromSeed(
27
+ blake2AsU8a(
28
+ stringToU8a(value)
29
+ )
30
+ );
31
+ }