@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,43 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Prefix } from './types.js';
5
+
6
+ // Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
7
+ import { u8aConcat } from '@pezkuwi/util';
8
+
9
+ import { base58Encode } from '../base58/index.js';
10
+ import { decodeAddress } from './decode.js';
11
+ import { defaults } from './defaults.js';
12
+ import { sshash } from './sshash.js';
13
+
14
+ export function encodeAddress (key: string | Uint8Array, ss58Format: Prefix = defaults.prefix): string {
15
+ // decode it, this means we can re-encode an address
16
+ const u8a = decodeAddress(key);
17
+
18
+ if ((ss58Format < 0) || (ss58Format > 16383 && !ss58Exceptions.includes(ss58Format)) || [46, 47].includes(ss58Format)) {
19
+ throw new Error('Out of range ss58Format specified');
20
+ } else if (!defaults.allowedDecodedLengths.includes(u8a.length)) {
21
+ throw new Error(`Expected a valid key to convert, with length ${defaults.allowedDecodedLengths.join(', ')}`);
22
+ }
23
+
24
+ const input = u8aConcat(
25
+ ss58Format < 64
26
+ ? [ss58Format]
27
+ : [
28
+ ((ss58Format & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000,
29
+ (ss58Format >> 8) | ((ss58Format & 0b0000_0000_0000_0011) << 6)
30
+ ],
31
+ u8a
32
+ );
33
+
34
+ return base58Encode(
35
+ u8aConcat(
36
+ input,
37
+ sshash(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1)
38
+ )
39
+ );
40
+ }
41
+
42
+ // Exceptions like 29972 (Mythos chain) which uses Ethereum style account (not ss58 Encoded)
43
+ const ss58Exceptions = [29972];
@@ -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 { encodeDerivedAddress } from './index.js';
7
+
8
+ describe('encodeDerivedAddress', (): void => {
9
+ it('creates a valid known derived address', (): void => {
10
+ expect(
11
+ encodeDerivedAddress('5GvUh7fGKsdBEh5XpypkfkGuf7j3vXLxH9BdxjxnJNVXRYi1', 0)
12
+ ).toEqual('5E5XxqPxm7QbEs6twYfp3tyjXidn4kqRrNPH4o6JK9JSLUeD');
13
+ });
14
+ });
@@ -0,0 +1,19 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '@pezkuwi/util';
5
+ import type { Prefix } from './types.js';
6
+
7
+ import { decodeAddress } from './decode.js';
8
+ import { encodeAddress } from './encode.js';
9
+ import { createKeyDerived } from './keyDerived.js';
10
+
11
+ /**
12
+ * @name encodeDerivedAddress
13
+ * @summary Creates a derived address as used in Substrate utility.
14
+ * @description
15
+ * Creates a Substrate derived address based on the input address/publicKey and the index supplied.
16
+ */
17
+ export function encodeDerivedAddress (who: string | Uint8Array, index: bigint | BN | number, ss58Format?: Prefix): string {
18
+ return encodeAddress(createKeyDerived(decodeAddress(who), index), ss58Format);
19
+ }
@@ -0,0 +1,18 @@
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 { encodeMultiAddress } from './index.js';
7
+
8
+ describe('encodeMultiAddress', (): void => {
9
+ it('creates a valid known multi address', (): void => {
10
+ expect(
11
+ encodeMultiAddress([
12
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
13
+ '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
14
+ '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'
15
+ ], 2)
16
+ ).toEqual('5DjYJStmdZ2rcqXbXGX7TW85JsrW6uG4y9MUcLq2BoPMpRA7');
17
+ });
18
+ });
@@ -0,0 +1,18 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '@pezkuwi/util';
5
+ import type { Prefix } from './types.js';
6
+
7
+ import { encodeAddress } from './encode.js';
8
+ import { createKeyMulti } from './keyMulti.js';
9
+
10
+ /**
11
+ * @name encodeMultiAddress
12
+ * @summary Creates a multisig address.
13
+ * @description
14
+ * Creates a Substrate multisig address based on the input address and the required threshold.
15
+ */
16
+ export function encodeMultiAddress (who: (string | Uint8Array)[], threshold: bigint | BN | number, ss58Format?: Prefix): string {
17
+ return encodeAddress(createKeyMulti(who, threshold), ss58Format);
18
+ }
@@ -0,0 +1,45 @@
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 { ALICE_PUBLIC_SR } from './encode.spec.js';
7
+ import { addressEq } from './index.js';
8
+
9
+ describe('addressEq', (): void => {
10
+ it('returns false with non-equal', (): void => {
11
+ expect(
12
+ addressEq(
13
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
14
+ '5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG'
15
+ )
16
+ ).toEqual(false);
17
+ });
18
+
19
+ it('returns true for equal, matching prefix', (): void => {
20
+ expect(
21
+ addressEq(
22
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
23
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
24
+ )
25
+ ).toEqual(true);
26
+ });
27
+
28
+ it('returns true for equal, non-matching prefix', (): void => {
29
+ expect(
30
+ addressEq(
31
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
32
+ '15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5'
33
+ )
34
+ ).toEqual(true);
35
+ });
36
+
37
+ it('returns true for equal, address vs publicKey', (): void => {
38
+ expect(
39
+ addressEq(
40
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
41
+ ALICE_PUBLIC_SR
42
+ )
43
+ ).toEqual(true);
44
+ });
45
+ });
@@ -0,0 +1,24 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { u8aEq } from '@pezkuwi/util';
5
+
6
+ import { decodeAddress } from './decode.js';
7
+
8
+ /**
9
+ * @name addressEq
10
+ * @summary Compares two addresses, either in ss58, Uint8Array or hex format.
11
+ * @description
12
+ * For the input values, return true is the underlying public keys do match.
13
+ * @example
14
+ * <BR>
15
+ *
16
+ * ```javascript
17
+ * import { u8aEq } from '@pezkuwi/util';
18
+ *
19
+ * u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
20
+ * ```
21
+ */
22
+ export function addressEq (a: string | Uint8Array, b: string | Uint8Array): boolean {
23
+ return u8aEq(decodeAddress(a), decodeAddress(b));
24
+ }
@@ -0,0 +1,20 @@
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 { evmToAddress } from './index.js';
7
+
8
+ describe('evmToAddress', (): void => {
9
+ it('creates a valid known SS58 address', (): void => {
10
+ expect(
11
+ evmToAddress('0xd43593c715fdd31c61141abd04a99fd6822c8558', 42, 'blake2')
12
+ ).toEqual('5FrLxJsyJ5x9n2rmxFwosFraxFCKcXZDngRLNectCn64UjtZ');
13
+ });
14
+
15
+ it('fails when length is invalid', (): void => {
16
+ expect(
17
+ () => evmToAddress('0x1234567890ABCDEF1234567890ABCDEF')
18
+ ).toThrow(/address length/);
19
+ });
20
+ });
@@ -0,0 +1,24 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { HashType } from '../secp256k1/types.js';
5
+ import type { Prefix } from './types.js';
6
+
7
+ import { u8aConcat } from '@pezkuwi/util';
8
+
9
+ import { hasher } from '../secp256k1/hasher.js';
10
+ import { encodeAddress } from './encode.js';
11
+
12
+ /**
13
+ * @name evmToAddress
14
+ * @summary Converts an EVM address to its corresponding SS58 address.
15
+ */
16
+ export function evmToAddress (evmAddress: string | Uint8Array, ss58Format?: Prefix, hashType: HashType = 'blake2'): string {
17
+ const message = u8aConcat('evm:', evmAddress);
18
+
19
+ if (message.length !== 24) {
20
+ throw new Error(`Converting ${evmAddress as string}: Invalid evm address length`);
21
+ }
22
+
23
+ return encodeAddress(hasher(hashType, message), ss58Format);
24
+ }
@@ -0,0 +1,21 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { addressToEvm } from './addressToEvm.js';
5
+ export { checkAddress } from './check.js';
6
+ export { checkAddressChecksum } from './checksum.js';
7
+ export { decodeAddress } from './decode.js';
8
+ export { deriveAddress } from './derive.js';
9
+ export { encodeAddress } from './encode.js';
10
+ export { encodeDerivedAddress } from './encodeDerived.js';
11
+ export { encodeMultiAddress } from './encodeMulti.js';
12
+ export { addressEq } from './eq.js';
13
+ export { evmToAddress } from './evmToAddress.js';
14
+ export { isAddress } from './is.js';
15
+ export { createKeyDerived } from './keyDerived.js';
16
+ export { createKeyMulti } from './keyMulti.js';
17
+ export { sortAddresses } from './sort.js';
18
+ export { validateAddress } from './validate.js';
19
+
20
+ // eslint-disable-next-line deprecation/deprecation
21
+ export { setSS58Format } from './setSS58Format.js';
@@ -0,0 +1,113 @@
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 { isAddress } from './index.js';
7
+
8
+ describe('isAddress', (): void => {
9
+ it('decodes an address', (): void => {
10
+ expect(
11
+ isAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
12
+ ).toEqual(true);
13
+ });
14
+
15
+ it('decodes the council address', (): void => {
16
+ expect(
17
+ isAddress('F3opxRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29')
18
+ ).toEqual(true);
19
+ });
20
+
21
+ it('converts a publicKey (hex) as-is', (): void => {
22
+ expect(
23
+ isAddress('0x01020304')
24
+ ).toEqual(true);
25
+ });
26
+
27
+ it('decodes a short address', (): void => {
28
+ expect(
29
+ isAddress('F7NZ')
30
+ ).toEqual(true);
31
+ });
32
+
33
+ it('decodes a 1-byte accountId (with prefix)', (): void => {
34
+ expect(
35
+ isAddress('g4b', false, 2)
36
+ ).toEqual(true);
37
+ });
38
+
39
+ it('decodes a 2-byte accountId', (): void => {
40
+ expect(
41
+ isAddress('3xygo', false, 2)
42
+ ).toEqual(true);
43
+ });
44
+
45
+ it('encodes a 4-byte address', (): void => {
46
+ expect(
47
+ isAddress('zswfoZa', false, 2)
48
+ ).toEqual(true);
49
+ });
50
+
51
+ it('decodes a 8-byte address', (): void => {
52
+ expect(
53
+ isAddress('848Gh2GcGaZia', false, 2)
54
+ ).toEqual(true);
55
+ });
56
+
57
+ it('decodes a 33-byte address', (): void => {
58
+ expect(
59
+ isAddress('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou')
60
+ ).toEqual(true);
61
+ });
62
+
63
+ it('decodes a 2-byte prefix (65)', (): void => {
64
+ expect(
65
+ isAddress('cLtA6nCDyvwKcEHH4QkZDSHMhS9s78BvUJUsKUbUAn1Jc2SCF')
66
+ ).toEqual(true);
67
+ });
68
+
69
+ it('decodes a 2-byte prefix (69)', (): void => {
70
+ expect(
71
+ isAddress('cnUaoo5wodnTVA4bnr4woSweto8hWZADUvLFXkR9Q6U7BRsbF')
72
+ ).toEqual(true);
73
+ });
74
+
75
+ it('decodes a 2-byte prefix (252)', (): void => {
76
+ expect(
77
+ isAddress('xw9Hca4RJTmBRgzJT4ieJBh7XCK9gE3NXBDSEmgGHd4TCrbnG')
78
+ ).toEqual(true);
79
+ });
80
+
81
+ it('decodes a 2-byte prefix (255)', (): void => {
82
+ expect(
83
+ isAddress('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k')
84
+ ).toEqual(true);
85
+ });
86
+
87
+ it('decodes a 2-byte prefix (ecdsa, from Substrate)', (): void => {
88
+ expect(
89
+ isAddress('4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV')
90
+ ).toEqual(true);
91
+ });
92
+
93
+ it('fails when length is invalid', (): void => {
94
+ expect(
95
+ isAddress('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b')
96
+ ).toEqual(false);
97
+ });
98
+
99
+ it('fails when the checksum does not match', (): void => {
100
+ expect(
101
+ isAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj')
102
+ ).toEqual(false);
103
+ expect(
104
+ isAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
105
+ ).toEqual(false);
106
+ });
107
+
108
+ it('fails when invalid base58 encoded address is found', (): void => {
109
+ expect(
110
+ isAddress('F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29')
111
+ ).toEqual(false);
112
+ });
113
+ });
@@ -0,0 +1,14 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Prefix } from './types.js';
5
+
6
+ import { validateAddress } from './validate.js';
7
+
8
+ export function isAddress (address?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): address is string {
9
+ try {
10
+ return validateAddress(address, ignoreChecksum, ss58Format);
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
@@ -0,0 +1,24 @@
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 { createKeyDerived } from './index.js';
7
+
8
+ describe('createKeyDerived', (): void => {
9
+ it('matches sub accounts with Rust', (): void => {
10
+ expect(
11
+ createKeyDerived(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]), 0)
12
+ ).toEqual(
13
+ new Uint8Array([234, 236, 28, 96, 177, 168, 152, 193, 71, 179, 226, 102, 179, 155, 188, 240, 90, 182, 21, 175, 47, 47, 250, 179, 178, 0, 81, 222, 70, 56, 52, 234])
14
+ );
15
+ });
16
+
17
+ it('creates a valid subkey', (): void => {
18
+ expect(
19
+ createKeyDerived('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 1)
20
+ ).toEqual(
21
+ new Uint8Array([248, 19, 86, 209, 254, 89, 84, 48, 54, 128, 166, 239, 153, 212, 143, 34, 191, 60, 210, 50, 39, 77, 122, 71, 29, 60, 247, 198, 95, 101, 246, 83])
22
+ );
23
+ });
24
+ });
@@ -0,0 +1,22 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '@pezkuwi/util';
5
+
6
+ import { bnToU8a, stringToU8a, u8aConcat } from '@pezkuwi/util';
7
+
8
+ import { blake2AsU8a } from '../blake2/asU8a.js';
9
+ import { BN_LE_16_OPTS } from '../bn.js';
10
+ import { decodeAddress } from './decode.js';
11
+
12
+ const PREFIX = stringToU8a('modlpy/utilisuba');
13
+
14
+ export function createKeyDerived (who: string | Uint8Array, index: bigint | BN | number): Uint8Array {
15
+ return blake2AsU8a(
16
+ u8aConcat(
17
+ PREFIX,
18
+ decodeAddress(who),
19
+ bnToU8a(index, BN_LE_16_OPTS)
20
+ )
21
+ );
22
+ }
@@ -0,0 +1,20 @@
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 { createKeyMulti } from './index.js';
7
+
8
+ describe('createKeyMulti', (): void => {
9
+ it('creates a valid multikey (aligning with Rust, needs sorting)', (): void => {
10
+ expect(
11
+ createKeyMulti([
12
+ new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]),
13
+ new Uint8Array([3, 0, 0, 0, 0, 0, 0, 0]),
14
+ new Uint8Array([2, 0, 0, 0, 0, 0, 0, 0])
15
+ ], 2)
16
+ ).toEqual(
17
+ new Uint8Array([67, 151, 196, 155, 179, 207, 47, 123, 90, 2, 35, 54, 162, 111, 241, 226, 88, 148, 54, 193, 252, 195, 93, 101, 16, 5, 93, 101, 186, 186, 254, 79])
18
+ );
19
+ });
20
+ });
@@ -0,0 +1,23 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BN } from '@pezkuwi/util';
5
+
6
+ import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@pezkuwi/util';
7
+
8
+ import { blake2AsU8a } from '../blake2/asU8a.js';
9
+ import { BN_LE_16_OPTS } from '../bn.js';
10
+ import { addressToU8a } from './util.js';
11
+
12
+ const PREFIX = stringToU8a('modlpy/utilisuba');
13
+
14
+ export function createKeyMulti (who: (string | Uint8Array)[], threshold: bigint | BN | number): Uint8Array {
15
+ return blake2AsU8a(
16
+ u8aConcat(
17
+ PREFIX,
18
+ compactToU8a(who.length),
19
+ ...u8aSorted(who.map(addressToU8a)),
20
+ bnToU8a(threshold, BN_LE_16_OPTS)
21
+ )
22
+ );
23
+ }
@@ -0,0 +1,21 @@
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 { encodeAddress, setSS58Format } from './index.js';
7
+
8
+ describe('setSS58Format', (): void => {
9
+ beforeEach((): void => {
10
+ // eslint-disable-next-line deprecation/deprecation
11
+ setSS58Format(2);
12
+ });
13
+
14
+ it('sets and allows encoding using', (): void => {
15
+ expect(
16
+ encodeAddress(
17
+ new Uint8Array([1])
18
+ )
19
+ ).toEqual('g4b');
20
+ });
21
+ });
@@ -0,0 +1,20 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Prefix } from './types.js';
5
+
6
+ import { logger } from '@pezkuwi/util';
7
+
8
+ import { defaults } from './defaults.js';
9
+
10
+ const l = logger('setSS58Format');
11
+
12
+ /**
13
+ * @description Sets the global SS58 format to use for address encoding
14
+ * @deprecated Use keyring.setSS58Format
15
+ */
16
+ export function setSS58Format (prefix: Prefix): void {
17
+ l.warn('Global setting of the ss58Format is deprecated and not recommended. Set format on the keyring (if used) or as part of the address encode function');
18
+
19
+ defaults.prefix = prefix;
20
+ }
@@ -0,0 +1,22 @@
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 { sortAddresses } from './index.js';
7
+
8
+ describe('sortAddresses', (): void => {
9
+ it('sorts addresses by the publicKeys', (): void => {
10
+ expect(
11
+ sortAddresses([
12
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
13
+ '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
14
+ '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'
15
+ ])
16
+ ).toEqual([
17
+ '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
18
+ '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y',
19
+ '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
20
+ ]);
21
+ });
22
+ });
@@ -0,0 +1,17 @@
1
+ // Copyright 2017-2025 @polkadot/util authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Prefix } from './types.js';
5
+
6
+ import { u8aSorted } from '@pezkuwi/util';
7
+
8
+ import { encodeAddress } from './encode.js';
9
+ import { addressToU8a } from './util.js';
10
+
11
+ export function sortAddresses (addresses: (string | Uint8Array)[], ss58Format?: Prefix): string[] {
12
+ const u8aToAddress = (u8a: Uint8Array) => encodeAddress(u8a, ss58Format);
13
+
14
+ return u8aSorted(
15
+ addresses.map(addressToU8a)
16
+ ).map(u8aToAddress);
17
+ }
@@ -0,0 +1,12 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { stringToU8a, u8aConcat } from '@pezkuwi/util';
5
+
6
+ import { blake2AsU8a } from '../blake2/asU8a.js';
7
+
8
+ const SS58_PREFIX = stringToU8a('SS58PRE');
9
+
10
+ export function sshash (key: Uint8Array): Uint8Array {
11
+ return blake2AsU8a(u8aConcat(SS58_PREFIX, key), 512);
12
+ }
@@ -0,0 +1,6 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // FIXME we really want this to map with what is in the allowedSS58 array... i.e. the
5
+ // values there. As of now, we just map to number.
6
+ export type Prefix = number;
@@ -0,0 +1,8 @@
1
+ // Copyright 2017-2025 @polkadot/util-crypto authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { decodeAddress } from './decode.js';
5
+
6
+ export function addressToU8a (who: string | Uint8Array): Uint8Array {
7
+ return decodeAddress(who);
8
+ }