@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.
- package/README.md +17 -0
- package/package.json +45 -0
- package/src/address/addressToEvm.spec.ts +16 -0
- package/src/address/addressToEvm.ts +12 -0
- package/src/address/check.spec.ts +44 -0
- package/src/address/check.ts +34 -0
- package/src/address/checksum.spec.ts +45 -0
- package/src/address/checksum.ts +25 -0
- package/src/address/decode.spec.ts +138 -0
- package/src/address/decode.ts +41 -0
- package/src/address/defaults.ts +12 -0
- package/src/address/derive.spec.ts +26 -0
- package/src/address/derive.ts +36 -0
- package/src/address/encode.spec.ts +177 -0
- package/src/address/encode.ts +43 -0
- package/src/address/encodeDerived.spec.ts +14 -0
- package/src/address/encodeDerived.ts +19 -0
- package/src/address/encodeMulti.spec.ts +18 -0
- package/src/address/encodeMulti.ts +18 -0
- package/src/address/eq.spec.ts +45 -0
- package/src/address/eq.ts +24 -0
- package/src/address/evmToAddress.spec.ts +20 -0
- package/src/address/evmToAddress.ts +24 -0
- package/src/address/index.ts +21 -0
- package/src/address/is.spec.ts +113 -0
- package/src/address/is.ts +14 -0
- package/src/address/keyDerived.spec.ts +24 -0
- package/src/address/keyDerived.ts +22 -0
- package/src/address/keyMulti.spec.ts +20 -0
- package/src/address/keyMulti.ts +23 -0
- package/src/address/setSS58Format.spec.ts +21 -0
- package/src/address/setSS58Format.ts +20 -0
- package/src/address/sort.spec.ts +22 -0
- package/src/address/sort.ts +17 -0
- package/src/address/sshash.ts +12 -0
- package/src/address/types.ts +6 -0
- package/src/address/util.ts +8 -0
- package/src/address/validate.spec.ts +113 -0
- package/src/address/validate.ts +10 -0
- package/src/base32/bs32.ts +53 -0
- package/src/base32/decode.spec.ts +34 -0
- package/src/base32/encode.spec.ts +30 -0
- package/src/base32/helpers.ts +93 -0
- package/src/base32/index.ts +8 -0
- package/src/base32/is.spec.ts +32 -0
- package/src/base32/validate.spec.ts +44 -0
- package/src/base58/bs58.ts +43 -0
- package/src/base58/decode.spec.ts +31 -0
- package/src/base58/encode.spec.ts +26 -0
- package/src/base58/index.ts +8 -0
- package/src/base58/validate.spec.ts +20 -0
- package/src/base64/bs64.ts +43 -0
- package/src/base64/decode.spec.ts +42 -0
- package/src/base64/encode.spec.ts +14 -0
- package/src/base64/index.ts +10 -0
- package/src/base64/pad.spec.ts +14 -0
- package/src/base64/pad.ts +10 -0
- package/src/base64/trim.spec.ts +14 -0
- package/src/base64/trim.ts +14 -0
- package/src/base64/validate.spec.ts +32 -0
- package/src/blake2/asHex.spec.ts +57 -0
- package/src/blake2/asU8a.spec.ts +74 -0
- package/src/blake2/asU8a.ts +40 -0
- package/src/blake2/index.ts +8 -0
- package/src/bn.ts +15 -0
- package/src/bundle.ts +33 -0
- package/src/bundleInit.ts +11 -0
- package/src/crypto.spec.ts +18 -0
- package/src/crypto.ts +18 -0
- package/src/ed25519/deriveHard.ts +18 -0
- package/src/ed25519/index.ts +13 -0
- package/src/ed25519/pair/fromRandom.spec.ts +28 -0
- package/src/ed25519/pair/fromRandom.ts +25 -0
- package/src/ed25519/pair/fromSecret.spec.ts +33 -0
- package/src/ed25519/pair/fromSecret.ts +29 -0
- package/src/ed25519/pair/fromSeed.spec.ts +42 -0
- package/src/ed25519/pair/fromSeed.ts +41 -0
- package/src/ed25519/pair/fromString.spec.ts +17 -0
- package/src/ed25519/pair/fromString.ts +31 -0
- package/src/ed25519/sign.spec.ts +40 -0
- package/src/ed25519/sign.ts +38 -0
- package/src/ed25519/verify.spec.ts +84 -0
- package/src/ed25519/verify.ts +41 -0
- package/src/ethereum/encode.spec.ts +59 -0
- package/src/ethereum/encode.ts +39 -0
- package/src/ethereum/index.ts +6 -0
- package/src/ethereum/isAddress.spec.ts +34 -0
- package/src/ethereum/isAddress.ts +16 -0
- package/src/ethereum/isChecksum.ts +27 -0
- package/src/ethereum/isCheksum.spec.ts +30 -0
- package/src/hd/ethereum/index.spec.ts +54 -0
- package/src/hd/ethereum/index.ts +69 -0
- package/src/hd/index.ts +6 -0
- package/src/hd/ledger/derivePrivate.ts +34 -0
- package/src/hd/ledger/index.spec.ts +64 -0
- package/src/hd/ledger/index.ts +42 -0
- package/src/hd/ledger/master.spec.ts +19 -0
- package/src/hd/ledger/master.ts +26 -0
- package/src/hd/validatePath.spec.ts +30 -0
- package/src/hd/validatePath.ts +24 -0
- package/src/helpers.ts +38 -0
- package/src/hmac/index.ts +4 -0
- package/src/hmac/shaAsU8a.spec.ts +45 -0
- package/src/hmac/shaAsU8a.ts +48 -0
- package/src/index.ts +6 -0
- package/src/json/constants.ts +11 -0
- package/src/json/decrypt.ts +25 -0
- package/src/json/decryptData.ts +45 -0
- package/src/json/encrypt.ts +25 -0
- package/src/json/encryptFormat.ts +20 -0
- package/src/json/index.ts +7 -0
- package/src/json/types.ts +22 -0
- package/src/keccak/asHex.spec.ts +30 -0
- package/src/keccak/asU8a.spec.ts +56 -0
- package/src/keccak/asU8a.ts +45 -0
- package/src/keccak/index.ts +8 -0
- package/src/key/DeriveJunction.ts +79 -0
- package/src/key/extractPath.spec.ts +51 -0
- package/src/key/extractPath.ts +37 -0
- package/src/key/extractSuri.spec.ts +147 -0
- package/src/key/extractSuri.ts +40 -0
- package/src/key/fromPath.ts +28 -0
- package/src/key/hdkdDerive.ts +17 -0
- package/src/key/hdkdEcdsa.ts +8 -0
- package/src/key/hdkdEd25519.ts +7 -0
- package/src/key/hdkdSr25519.ts +14 -0
- package/src/key/index.ts +12 -0
- package/src/mnemonic/bip39.spec.ts +80 -0
- package/src/mnemonic/bip39.ts +127 -0
- package/src/mnemonic/generate.spec.ts +58 -0
- package/src/mnemonic/generate.ts +25 -0
- package/src/mnemonic/index.ts +11 -0
- package/src/mnemonic/toEntropy.spec.ts +36 -0
- package/src/mnemonic/toEntropy.ts +13 -0
- package/src/mnemonic/toLegacySeed.spec.ts +52 -0
- package/src/mnemonic/toLegacySeed.ts +39 -0
- package/src/mnemonic/toMiniSecret.spec.ts +67 -0
- package/src/mnemonic/toMiniSecret.ts +23 -0
- package/src/mnemonic/toMiniSecretCmp.spec.ts +64 -0
- package/src/mnemonic/validate.spec.ts +39 -0
- package/src/mnemonic/validate.ts +26 -0
- package/src/mnemonic/wordlists/en.ts +7 -0
- package/src/mnemonic/wordlists/es.ts +7 -0
- package/src/mnemonic/wordlists/fr.ts +7 -0
- package/src/mnemonic/wordlists/index.ts +11 -0
- package/src/mnemonic/wordlists/it.ts +7 -0
- package/src/mnemonic/wordlists/jp.ts +7 -0
- package/src/mnemonic/wordlists/ko.ts +7 -0
- package/src/mnemonic/wordlists/zh-s.ts +7 -0
- package/src/mnemonic/wordlists/zh-t.ts +7 -0
- package/src/mod.ts +4 -0
- package/src/nacl/decrypt.spec.ts +26 -0
- package/src/nacl/decrypt.ts +22 -0
- package/src/nacl/encrypt.spec.ts +20 -0
- package/src/nacl/encrypt.ts +31 -0
- package/src/nacl/index.ts +8 -0
- package/src/nacl/tweetnacl-secretbox-data.spec.ts +4629 -0
- package/src/nacl/tweetnacl-secretbox.spec.ts +161 -0
- package/src/nacl/tweetnacl.ts +1159 -0
- package/src/networks.ts +5 -0
- package/src/packageDetect.ts +14 -0
- package/src/packageInfo.ts +6 -0
- package/src/pbkdf2/encode.spec.ts +54 -0
- package/src/pbkdf2/encode.ts +29 -0
- package/src/pbkdf2/index.ts +4 -0
- package/src/random/asHex.spec.ts +38 -0
- package/src/random/asNumber.spec.ts +16 -0
- package/src/random/asNumber.ts +28 -0
- package/src/random/asU8a.spec.ts +36 -0
- package/src/random/asU8a.ts +30 -0
- package/src/random/index.ts +9 -0
- package/src/scrypt/defaults.ts +19 -0
- package/src/scrypt/encode.spec.ts +43 -0
- package/src/scrypt/encode.ts +30 -0
- package/src/scrypt/fromU8a.ts +44 -0
- package/src/scrypt/index.ts +6 -0
- package/src/scrypt/toU8a.ts +17 -0
- package/src/scrypt/types.ts +9 -0
- package/src/secp256k1/compress.spec.ts +47 -0
- package/src/secp256k1/compress.ts +21 -0
- package/src/secp256k1/deriveHard.ts +17 -0
- package/src/secp256k1/expand.spec.ts +47 -0
- package/src/secp256k1/expand.ts +30 -0
- package/src/secp256k1/hasher.spec.ts +24 -0
- package/src/secp256k1/hasher.ts +13 -0
- package/src/secp256k1/index.ts +10 -0
- package/src/secp256k1/pair/fromSeed.spec.ts +75 -0
- package/src/secp256k1/pair/fromSeed.ts +42 -0
- package/src/secp256k1/recover.spec.ts +35 -0
- package/src/secp256k1/recover.ts +36 -0
- package/src/secp256k1/sign.spec.ts +39 -0
- package/src/secp256k1/sign.ts +37 -0
- package/src/secp256k1/signVerify.spec.ts +94 -0
- package/src/secp256k1/tweakAdd.spec.ts +35 -0
- package/src/secp256k1/tweakAdd.ts +65 -0
- package/src/secp256k1/types.ts +4 -0
- package/src/secp256k1/verify.spec.ts +81 -0
- package/src/secp256k1/verify.ts +32 -0
- package/src/sha/asU8a.ts +30 -0
- package/src/sha/asU8a256.spec.ts +55 -0
- package/src/sha/asU8a512.spec.ts +33 -0
- package/src/sha/index.ts +8 -0
- package/src/signature/index.ts +8 -0
- package/src/signature/verify.spec.ts +230 -0
- package/src/signature/verify.ts +114 -0
- package/src/sr25519/agreement.spec.ts +31 -0
- package/src/sr25519/agreement.ts +23 -0
- package/src/sr25519/derive.ts +21 -0
- package/src/sr25519/deriveHard.ts +9 -0
- package/src/sr25519/derivePublic.ts +18 -0
- package/src/sr25519/deriveSoft.ts +9 -0
- package/src/sr25519/index.ts +12 -0
- package/src/sr25519/pair/fromSeed.spec.ts +35 -0
- package/src/sr25519/pair/fromSeed.ts +28 -0
- package/src/sr25519/pair/fromU8a.ts +23 -0
- package/src/sr25519/pair/testing.spec.ts +161 -0
- package/src/sr25519/pair/toU8a.ts +10 -0
- package/src/sr25519/sign.spec.ts +28 -0
- package/src/sr25519/sign.ts +22 -0
- package/src/sr25519/verify.spec.ts +42 -0
- package/src/sr25519/verify.ts +23 -0
- package/src/sr25519/vrfSign.ts +24 -0
- package/src/sr25519/vrfSignVerify.spec.ts +73 -0
- package/src/sr25519/vrfVerify.ts +25 -0
- package/src/test/index.ts +8 -0
- package/src/test/performance.ts +17 -0
- package/src/types.ts +33 -0
- package/src/xxhash/asHex.spec.ts +36 -0
- package/src/xxhash/asU8a.spec.ts +48 -0
- package/src/xxhash/asU8a.ts +45 -0
- package/src/xxhash/index.ts +8 -0
- package/src/xxhash/xxhash64.ts +155 -0
- package/tsconfig.build.json +18 -0
- package/tsconfig.spec.json +20 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { _0n, _1n } from '@pezkuwi/util';
|
|
5
|
+
import { BigInt } from '@pezkuwi/x-bigint';
|
|
6
|
+
|
|
7
|
+
// Adapted from https://github.com/pierrec/js-xxhash/blob/0504e76f3d31a21ae8528a7f590c7289c9e431d2/lib/xxhash64.js
|
|
8
|
+
//
|
|
9
|
+
// xxHash64 implementation in pure Javascript
|
|
10
|
+
// Copyright (C) 2016, Pierre Curto
|
|
11
|
+
// MIT license
|
|
12
|
+
//
|
|
13
|
+
// Changes made:
|
|
14
|
+
// - converted to TypeScript
|
|
15
|
+
// - uses native JS BigInt (no external dependencies)
|
|
16
|
+
// - support only for Uint8Array inputs
|
|
17
|
+
// - no constructor function, straight fill & digest
|
|
18
|
+
// - update code removed, only called once, no streams
|
|
19
|
+
// - inline single-use functions
|
|
20
|
+
|
|
21
|
+
interface State {
|
|
22
|
+
seed: bigint;
|
|
23
|
+
u8a: Uint8Array;
|
|
24
|
+
u8asize: number;
|
|
25
|
+
v1: bigint;
|
|
26
|
+
v2: bigint;
|
|
27
|
+
v3: bigint;
|
|
28
|
+
v4: bigint;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const P64_1 = BigInt('11400714785074694791');
|
|
32
|
+
const P64_2 = BigInt('14029467366897019727');
|
|
33
|
+
const P64_3 = BigInt('1609587929392839161');
|
|
34
|
+
const P64_4 = BigInt('9650029242287828579');
|
|
35
|
+
const P64_5 = BigInt('2870177450012600261');
|
|
36
|
+
|
|
37
|
+
// mask for a u64, all bits set
|
|
38
|
+
const U64 = BigInt('0xffffffffffffffff');
|
|
39
|
+
|
|
40
|
+
// various constants
|
|
41
|
+
const _7n = BigInt(7);
|
|
42
|
+
const _11n = BigInt(11);
|
|
43
|
+
const _12n = BigInt(12);
|
|
44
|
+
const _16n = BigInt(16);
|
|
45
|
+
const _18n = BigInt(18);
|
|
46
|
+
const _23n = BigInt(23);
|
|
47
|
+
const _27n = BigInt(27);
|
|
48
|
+
const _29n = BigInt(29);
|
|
49
|
+
const _31n = BigInt(31);
|
|
50
|
+
const _32n = BigInt(32);
|
|
51
|
+
const _33n = BigInt(33);
|
|
52
|
+
const _64n = BigInt(64);
|
|
53
|
+
const _256n = BigInt(256);
|
|
54
|
+
|
|
55
|
+
function rotl (a: bigint, b: bigint): bigint {
|
|
56
|
+
const c = a & U64;
|
|
57
|
+
|
|
58
|
+
return ((c << b) | (c >> (_64n - b))) & U64;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function fromU8a (u8a: Uint8Array, p: number, count: 2 | 4): bigint {
|
|
62
|
+
const bigints = new Array<bigint>(count);
|
|
63
|
+
let offset = 0;
|
|
64
|
+
|
|
65
|
+
for (let i = 0; i < count; i++, offset += 2) {
|
|
66
|
+
bigints[i] = BigInt(u8a[p + offset] | (u8a[p + 1 + offset] << 8));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let result = _0n;
|
|
70
|
+
|
|
71
|
+
for (let i = count - 1; i >= 0; i--) {
|
|
72
|
+
result = (result << _16n) + bigints[i];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function init (seed: bigint, input: Uint8Array): State {
|
|
79
|
+
const state = {
|
|
80
|
+
seed,
|
|
81
|
+
u8a: new Uint8Array(32),
|
|
82
|
+
u8asize: 0,
|
|
83
|
+
v1: seed + P64_1 + P64_2,
|
|
84
|
+
v2: seed + P64_2,
|
|
85
|
+
v3: seed,
|
|
86
|
+
v4: seed - P64_1
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
if (input.length < 32) {
|
|
90
|
+
state.u8a.set(input);
|
|
91
|
+
state.u8asize = input.length;
|
|
92
|
+
|
|
93
|
+
return state;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const limit = input.length - 32;
|
|
97
|
+
let p = 0;
|
|
98
|
+
|
|
99
|
+
if (limit >= 0) {
|
|
100
|
+
const adjustV = (v: bigint) =>
|
|
101
|
+
P64_1 * rotl(v + P64_2 * fromU8a(input, p, 4), _31n);
|
|
102
|
+
|
|
103
|
+
do {
|
|
104
|
+
state.v1 = adjustV(state.v1); p += 8;
|
|
105
|
+
state.v2 = adjustV(state.v2); p += 8;
|
|
106
|
+
state.v3 = adjustV(state.v3); p += 8;
|
|
107
|
+
state.v4 = adjustV(state.v4); p += 8;
|
|
108
|
+
} while (p <= limit);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (p < input.length) {
|
|
112
|
+
state.u8a.set(input.subarray(p, input.length));
|
|
113
|
+
state.u8asize = input.length - p;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return state;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function xxhash64 (input: Uint8Array, initSeed: bigint | number): Uint8Array {
|
|
120
|
+
const { seed, u8a, u8asize, v1, v2, v3, v4 } = init(BigInt(initSeed), input);
|
|
121
|
+
let p = 0;
|
|
122
|
+
let h64 = U64 & (BigInt(input.length) + (
|
|
123
|
+
input.length >= 32
|
|
124
|
+
? (((((((((rotl(v1, _1n) + rotl(v2, _7n) + rotl(v3, _12n) + rotl(v4, _18n)) ^ (P64_1 * rotl(v1 * P64_2, _31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v2 * P64_2, _31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v3 * P64_2, _31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v4 * P64_2, _31n))) * P64_1 + P64_4)
|
|
125
|
+
: (seed + P64_5)
|
|
126
|
+
));
|
|
127
|
+
|
|
128
|
+
while (p <= (u8asize - 8)) {
|
|
129
|
+
h64 = U64 & (P64_4 + P64_1 * rotl(h64 ^ (P64_1 * rotl(P64_2 * fromU8a(u8a, p, 4), _31n)), _27n));
|
|
130
|
+
p += 8;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if ((p + 4) <= u8asize) {
|
|
134
|
+
h64 = U64 & (P64_3 + P64_2 * rotl(h64 ^ (P64_1 * fromU8a(u8a, p, 2)), _23n));
|
|
135
|
+
p += 4;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
while (p < u8asize) {
|
|
139
|
+
h64 = U64 & (P64_1 * rotl(h64 ^ (P64_5 * BigInt(u8a[p++])), _11n));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
h64 = U64 & (P64_2 * (h64 ^ (h64 >> _33n)));
|
|
143
|
+
h64 = U64 & (P64_3 * (h64 ^ (h64 >> _29n)));
|
|
144
|
+
h64 = U64 & (h64 ^ (h64 >> _32n));
|
|
145
|
+
|
|
146
|
+
const result = new Uint8Array(8);
|
|
147
|
+
|
|
148
|
+
for (let i = 7; i >= 0; i--) {
|
|
149
|
+
result[i] = Number(h64 % _256n);
|
|
150
|
+
|
|
151
|
+
h64 = h64 / _256n;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "..",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"rootDir": "./src"
|
|
7
|
+
},
|
|
8
|
+
"exclude": [
|
|
9
|
+
"**/mod.ts",
|
|
10
|
+
"**/*.spec.ts"
|
|
11
|
+
],
|
|
12
|
+
"references": [
|
|
13
|
+
{ "path": "../networks/tsconfig.build.json" },
|
|
14
|
+
{ "path": "../util/tsconfig.build.json" },
|
|
15
|
+
{ "path": "../x-bigint/tsconfig.build.json" },
|
|
16
|
+
{ "path": "../x-randomvalues/tsconfig.build.json" }
|
|
17
|
+
]
|
|
18
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "..",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"emitDeclarationOnly": false,
|
|
8
|
+
"noEmit": true
|
|
9
|
+
},
|
|
10
|
+
"include": [
|
|
11
|
+
"**/*.spec.ts"
|
|
12
|
+
],
|
|
13
|
+
"references": [
|
|
14
|
+
{ "path": "../networks/tsconfig.build.json" },
|
|
15
|
+
{ "path": "../util/tsconfig.build.json" },
|
|
16
|
+
{ "path": "../util-crypto/tsconfig.build.json" },
|
|
17
|
+
{ "path": "../x-bigint/tsconfig.build.json" },
|
|
18
|
+
{ "path": "../x-randomvalues/tsconfig.build.json" }
|
|
19
|
+
]
|
|
20
|
+
}
|