ethereum-cryptographyy 2.1.2
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.
Potentially problematic release.
This version of ethereum-cryptographyy might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +508 -0
- package/aes.d.ts +2 -0
- package/aes.js +98 -0
- package/bip39/index.d.ts +1 -0
- package/bip39/index.js +10 -0
- package/bip39/wordlists/czech.d.ts +1 -0
- package/bip39/wordlists/czech.js +5 -0
- package/bip39/wordlists/english.d.ts +1 -0
- package/bip39/wordlists/english.js +5 -0
- package/bip39/wordlists/french.d.ts +1 -0
- package/bip39/wordlists/french.js +5 -0
- package/bip39/wordlists/italian.d.ts +1 -0
- package/bip39/wordlists/italian.js +5 -0
- package/bip39/wordlists/japanese.d.ts +1 -0
- package/bip39/wordlists/japanese.js +5 -0
- package/bip39/wordlists/korean.d.ts +1 -0
- package/bip39/wordlists/korean.js +5 -0
- package/bip39/wordlists/simplified-chinese.d.ts +1 -0
- package/bip39/wordlists/simplified-chinese.js +5 -0
- package/bip39/wordlists/spanish.d.ts +1 -0
- package/bip39/wordlists/spanish.js +5 -0
- package/bip39/wordlists/traditional-chinese.d.ts +1 -0
- package/bip39/wordlists/traditional-chinese.js +5 -0
- package/blake2b.d.ts +1 -0
- package/blake2b.js +13 -0
- package/esm/aes.js +93 -0
- package/esm/bip39/index.js +1 -0
- package/esm/bip39/wordlists/czech.js +1 -0
- package/esm/bip39/wordlists/english.js +1 -0
- package/esm/bip39/wordlists/french.js +1 -0
- package/esm/bip39/wordlists/italian.js +1 -0
- package/esm/bip39/wordlists/japanese.js +1 -0
- package/esm/bip39/wordlists/korean.js +1 -0
- package/esm/bip39/wordlists/simplified-chinese.js +1 -0
- package/esm/bip39/wordlists/spanish.js +1 -0
- package/esm/bip39/wordlists/traditional-chinese.js +1 -0
- package/esm/blake2b.js +9 -0
- package/esm/hdkey.js +1 -0
- package/esm/index.js +2 -0
- package/esm/keccak.js +10 -0
- package/esm/package.json +3 -0
- package/esm/pbkdf2.js +26 -0
- package/esm/random.js +7 -0
- package/esm/ripemd160.js +3 -0
- package/esm/scrypt.js +12 -0
- package/esm/secp256k1-compat.js +254 -0
- package/esm/secp256k1.js +1 -0
- package/esm/sha256.js +3 -0
- package/esm/sha512.js +3 -0
- package/esm/utils.js +47 -0
- package/hdkey.d.ts +1 -0
- package/hdkey.js +6 -0
- package/index.d.ts +0 -0
- package/index.js +2 -0
- package/keccak.d.ts +11 -0
- package/keccak.js +13 -0
- package/package.json +365 -0
- package/pbkdf2.d.ts +2 -0
- package/pbkdf2.js +31 -0
- package/random.d.ts +2 -0
- package/random.js +12 -0
- package/ripemd160.d.ts +1 -0
- package/ripemd160.js +6 -0
- package/scrypt.d.ts +4 -0
- package/scrypt.js +17 -0
- package/secp256k1-compat.d.ts +35 -0
- package/secp256k1-compat.js +278 -0
- package/secp256k1.d.ts +1 -0
- package/secp256k1.js +5 -0
- package/sha256.d.ts +1 -0
- package/sha256.js +6 -0
- package/sha512.d.ts +1 -0
- package/sha512.js +6 -0
- package/utils.d.ts +12 -0
- package/utils.js +63 -0
@@ -0,0 +1,278 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.contextRandomize = exports.ecdh = exports.signatureNormalize = exports.signatureImport = exports.signatureExport = exports.privateKeyTweakMul = exports.publicKeyTweakMul = exports.publicKeyTweakAdd = exports.publicKeyCombine = exports.publicKeyNegate = exports.privateKeyNegate = exports.privateKeyTweakAdd = exports.ecdsaVerify = exports.ecdsaRecover = exports.ecdsaSign = exports.publicKeyConvert = exports.publicKeyVerify = exports.publicKeyCreate = exports.privateKeyVerify = exports.createPrivateKey = exports.createPrivateKeySync = void 0;
|
4
|
+
const sha256_1 = require("@noble/hashes/sha256");
|
5
|
+
const modular_1 = require("@noble/curves/abstract/modular");
|
6
|
+
const secp256k1_js_1 = require("./secp256k1.js");
|
7
|
+
const utils_js_1 = require("./utils.js");
|
8
|
+
// Use `secp256k1` module directly.
|
9
|
+
// This is a legacy compatibility layer for the npm package `secp256k1` via noble-secp256k1
|
10
|
+
const Point = secp256k1_js_1.secp256k1.ProjectivePoint;
|
11
|
+
function hexToNumber(hex) {
|
12
|
+
if (typeof hex !== "string") {
|
13
|
+
throw new TypeError("hexToNumber: expected string, got " + typeof hex);
|
14
|
+
}
|
15
|
+
return BigInt(`0x${hex}`);
|
16
|
+
}
|
17
|
+
// Copy-paste from secp256k1, maybe export it?
|
18
|
+
const bytesToNumber = (bytes) => hexToNumber((0, utils_js_1.toHex)(bytes));
|
19
|
+
const numberToHex = (num) => num.toString(16).padStart(64, "0");
|
20
|
+
const numberToBytes = (num) => (0, utils_js_1.hexToBytes)(numberToHex(num));
|
21
|
+
const ORDER = secp256k1_js_1.secp256k1.CURVE.n;
|
22
|
+
function output(out = (len) => new Uint8Array(len), length, value) {
|
23
|
+
if (typeof out === "function") {
|
24
|
+
out = out(length);
|
25
|
+
}
|
26
|
+
(0, utils_js_1.assertBytes)(out, length);
|
27
|
+
if (value) {
|
28
|
+
out.set(value);
|
29
|
+
}
|
30
|
+
return out;
|
31
|
+
}
|
32
|
+
function getSignature(signature) {
|
33
|
+
(0, utils_js_1.assertBytes)(signature, 64);
|
34
|
+
return secp256k1_js_1.secp256k1.Signature.fromCompact(signature);
|
35
|
+
}
|
36
|
+
function createPrivateKeySync() {
|
37
|
+
return secp256k1_js_1.secp256k1.utils.randomPrivateKey();
|
38
|
+
}
|
39
|
+
exports.createPrivateKeySync = createPrivateKeySync;
|
40
|
+
async function createPrivateKey() {
|
41
|
+
return createPrivateKeySync();
|
42
|
+
}
|
43
|
+
exports.createPrivateKey = createPrivateKey;
|
44
|
+
function privateKeyVerify(privateKey) {
|
45
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
46
|
+
return secp256k1_js_1.secp256k1.utils.isValidPrivateKey(privateKey);
|
47
|
+
}
|
48
|
+
exports.privateKeyVerify = privateKeyVerify;
|
49
|
+
function publicKeyCreate(privateKey, compressed = true, out) {
|
50
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
51
|
+
(0, utils_js_1.assertBool)(compressed);
|
52
|
+
const res = secp256k1_js_1.secp256k1.getPublicKey(privateKey, compressed);
|
53
|
+
return output(out, compressed ? 33 : 65, res);
|
54
|
+
}
|
55
|
+
exports.publicKeyCreate = publicKeyCreate;
|
56
|
+
function publicKeyVerify(publicKey) {
|
57
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
58
|
+
try {
|
59
|
+
Point.fromHex(publicKey);
|
60
|
+
return true;
|
61
|
+
}
|
62
|
+
catch (e) {
|
63
|
+
return false;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
exports.publicKeyVerify = publicKeyVerify;
|
67
|
+
function publicKeyConvert(publicKey, compressed = true, out) {
|
68
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
69
|
+
(0, utils_js_1.assertBool)(compressed);
|
70
|
+
const res = Point.fromHex(publicKey).toRawBytes(compressed);
|
71
|
+
return output(out, compressed ? 33 : 65, res);
|
72
|
+
}
|
73
|
+
exports.publicKeyConvert = publicKeyConvert;
|
74
|
+
function ecdsaSign(msgHash, privateKey, options = { noncefn: undefined, data: undefined }, out) {
|
75
|
+
(0, utils_js_1.assertBytes)(msgHash, 32);
|
76
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
77
|
+
if (typeof options !== "object" || options === null) {
|
78
|
+
throw new TypeError("secp256k1.ecdsaSign: options should be object");
|
79
|
+
}
|
80
|
+
// noble-secp256k1 uses hmac instead of hmac-drbg here
|
81
|
+
if (options &&
|
82
|
+
(options.noncefn !== undefined || options.data !== undefined)) {
|
83
|
+
throw new Error("Secp256k1: noncefn && data is unsupported");
|
84
|
+
}
|
85
|
+
const sig = secp256k1_js_1.secp256k1.sign(msgHash, privateKey);
|
86
|
+
const recid = sig.recovery;
|
87
|
+
return { signature: output(out, 64, sig.toCompactRawBytes()), recid };
|
88
|
+
}
|
89
|
+
exports.ecdsaSign = ecdsaSign;
|
90
|
+
function ecdsaRecover(signature, recid, msgHash, compressed = true, out) {
|
91
|
+
(0, utils_js_1.assertBytes)(msgHash, 32);
|
92
|
+
(0, utils_js_1.assertBool)(compressed);
|
93
|
+
const sign = getSignature(signature);
|
94
|
+
const point = sign.addRecoveryBit(recid).recoverPublicKey(msgHash);
|
95
|
+
return output(out, compressed ? 33 : 65, point.toRawBytes(compressed));
|
96
|
+
}
|
97
|
+
exports.ecdsaRecover = ecdsaRecover;
|
98
|
+
function ecdsaVerify(signature, msgHash, publicKey) {
|
99
|
+
(0, utils_js_1.assertBytes)(signature, 64);
|
100
|
+
(0, utils_js_1.assertBytes)(msgHash, 32);
|
101
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
102
|
+
(0, utils_js_1.assertBytes)(signature, 64);
|
103
|
+
const r = bytesToNumber(signature.slice(0, 32));
|
104
|
+
const s = bytesToNumber(signature.slice(32, 64));
|
105
|
+
if (r >= ORDER || s >= ORDER) {
|
106
|
+
throw new Error("Cannot parse signature");
|
107
|
+
}
|
108
|
+
const pub = Point.fromHex(publicKey); // can throw error
|
109
|
+
pub; // typescript
|
110
|
+
let sig;
|
111
|
+
try {
|
112
|
+
sig = getSignature(signature);
|
113
|
+
}
|
114
|
+
catch (error) {
|
115
|
+
return false;
|
116
|
+
}
|
117
|
+
return secp256k1_js_1.secp256k1.verify(sig, msgHash, publicKey);
|
118
|
+
}
|
119
|
+
exports.ecdsaVerify = ecdsaVerify;
|
120
|
+
function privateKeyTweakAdd(privateKey, tweak) {
|
121
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
122
|
+
(0, utils_js_1.assertBytes)(tweak, 32);
|
123
|
+
let t = bytesToNumber(tweak);
|
124
|
+
if (t === 0n) {
|
125
|
+
throw new Error("Tweak must not be zero");
|
126
|
+
}
|
127
|
+
if (t >= ORDER) {
|
128
|
+
throw new Error("Tweak bigger than curve order");
|
129
|
+
}
|
130
|
+
t += bytesToNumber(privateKey);
|
131
|
+
if (t >= ORDER) {
|
132
|
+
t -= ORDER;
|
133
|
+
}
|
134
|
+
if (t === 0n) {
|
135
|
+
throw new Error("The tweak was out of range or the resulted private key is invalid");
|
136
|
+
}
|
137
|
+
privateKey.set((0, utils_js_1.hexToBytes)(numberToHex(t)));
|
138
|
+
return privateKey;
|
139
|
+
}
|
140
|
+
exports.privateKeyTweakAdd = privateKeyTweakAdd;
|
141
|
+
function privateKeyNegate(privateKey) {
|
142
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
143
|
+
const bn = (0, modular_1.mod)(-bytesToNumber(privateKey), ORDER);
|
144
|
+
privateKey.set((0, utils_js_1.hexToBytes)(numberToHex(bn)));
|
145
|
+
return privateKey;
|
146
|
+
}
|
147
|
+
exports.privateKeyNegate = privateKeyNegate;
|
148
|
+
function publicKeyNegate(publicKey, compressed = true, out) {
|
149
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
150
|
+
(0, utils_js_1.assertBool)(compressed);
|
151
|
+
const point = Point.fromHex(publicKey).negate();
|
152
|
+
return output(out, compressed ? 33 : 65, point.toRawBytes(compressed));
|
153
|
+
}
|
154
|
+
exports.publicKeyNegate = publicKeyNegate;
|
155
|
+
function publicKeyCombine(publicKeys, compressed = true, out) {
|
156
|
+
if (!Array.isArray(publicKeys) || !publicKeys.length) {
|
157
|
+
throw new TypeError(`Expected array with one or more items, not ${publicKeys}`);
|
158
|
+
}
|
159
|
+
for (const publicKey of publicKeys) {
|
160
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
161
|
+
}
|
162
|
+
(0, utils_js_1.assertBool)(compressed);
|
163
|
+
const combined = publicKeys
|
164
|
+
.map((pub) => Point.fromHex(pub))
|
165
|
+
.reduce((res, curr) => res.add(curr), Point.ZERO);
|
166
|
+
// Prohibit returning ZERO point
|
167
|
+
if (combined.equals(Point.ZERO)) {
|
168
|
+
throw new Error("Combined result must not be zero");
|
169
|
+
}
|
170
|
+
return output(out, compressed ? 33 : 65, combined.toRawBytes(compressed));
|
171
|
+
}
|
172
|
+
exports.publicKeyCombine = publicKeyCombine;
|
173
|
+
function publicKeyTweakAdd(publicKey, tweak, compressed = true, out) {
|
174
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
175
|
+
(0, utils_js_1.assertBytes)(tweak, 32);
|
176
|
+
(0, utils_js_1.assertBool)(compressed);
|
177
|
+
const p1 = Point.fromHex(publicKey);
|
178
|
+
const p2 = Point.fromPrivateKey(tweak);
|
179
|
+
const point = p1.add(p2);
|
180
|
+
if (p2.equals(Point.ZERO) || point.equals(Point.ZERO)) {
|
181
|
+
throw new Error("Tweak must not be zero");
|
182
|
+
}
|
183
|
+
return output(out, compressed ? 33 : 65, point.toRawBytes(compressed));
|
184
|
+
}
|
185
|
+
exports.publicKeyTweakAdd = publicKeyTweakAdd;
|
186
|
+
function publicKeyTweakMul(publicKey, tweak, compressed = true, out) {
|
187
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
188
|
+
(0, utils_js_1.assertBytes)(tweak, 32);
|
189
|
+
(0, utils_js_1.assertBool)(compressed);
|
190
|
+
const bn = bytesToNumber(tweak);
|
191
|
+
if (bn === 0n) {
|
192
|
+
throw new Error("Tweak must not be zero");
|
193
|
+
}
|
194
|
+
if (bn <= 1 || bn >= ORDER) {
|
195
|
+
throw new Error("Tweak is zero or bigger than curve order");
|
196
|
+
}
|
197
|
+
const point = Point.fromHex(publicKey).multiply(bn);
|
198
|
+
return output(out, compressed ? 33 : 65, point.toRawBytes(compressed));
|
199
|
+
}
|
200
|
+
exports.publicKeyTweakMul = publicKeyTweakMul;
|
201
|
+
function privateKeyTweakMul(privateKey, tweak) {
|
202
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
203
|
+
(0, utils_js_1.assertBytes)(tweak, 32);
|
204
|
+
const bn = bytesToNumber(tweak);
|
205
|
+
if (bn <= 1 || bn >= ORDER) {
|
206
|
+
throw new Error("Tweak is zero or bigger than curve order");
|
207
|
+
}
|
208
|
+
const res = (0, modular_1.mod)(bn * bytesToNumber(privateKey), ORDER);
|
209
|
+
if (res === 0n) {
|
210
|
+
throw new Error("The tweak was out of range or the resulted private key is invalid");
|
211
|
+
}
|
212
|
+
privateKey.set((0, utils_js_1.hexToBytes)(numberToHex(res)));
|
213
|
+
return privateKey;
|
214
|
+
}
|
215
|
+
exports.privateKeyTweakMul = privateKeyTweakMul;
|
216
|
+
// internal -> DER
|
217
|
+
function signatureExport(signature, out) {
|
218
|
+
const res = getSignature(signature).toDERRawBytes();
|
219
|
+
return output(out, 72, res.slice()).slice(0, res.length);
|
220
|
+
}
|
221
|
+
exports.signatureExport = signatureExport;
|
222
|
+
// DER -> internal
|
223
|
+
function signatureImport(signature, out) {
|
224
|
+
(0, utils_js_1.assertBytes)(signature);
|
225
|
+
const sig = secp256k1_js_1.secp256k1.Signature.fromDER(signature);
|
226
|
+
return output(out, 64, (0, utils_js_1.hexToBytes)(sig.toCompactHex()));
|
227
|
+
}
|
228
|
+
exports.signatureImport = signatureImport;
|
229
|
+
function signatureNormalize(signature) {
|
230
|
+
const res = getSignature(signature);
|
231
|
+
if (res.s > ORDER / 2n) {
|
232
|
+
signature.set(numberToBytes(ORDER - res.s), 32);
|
233
|
+
}
|
234
|
+
return signature;
|
235
|
+
}
|
236
|
+
exports.signatureNormalize = signatureNormalize;
|
237
|
+
function ecdh(publicKey, privateKey, options = {}, out) {
|
238
|
+
(0, utils_js_1.assertBytes)(publicKey, 33, 65);
|
239
|
+
(0, utils_js_1.assertBytes)(privateKey, 32);
|
240
|
+
if (typeof options !== "object" || options === null) {
|
241
|
+
throw new TypeError("secp256k1.ecdh: options should be object");
|
242
|
+
}
|
243
|
+
if (options.data !== undefined) {
|
244
|
+
(0, utils_js_1.assertBytes)(options.data);
|
245
|
+
}
|
246
|
+
const point = Point.fromHex(secp256k1_js_1.secp256k1.getSharedSecret(privateKey, publicKey));
|
247
|
+
if (options.hashfn === undefined) {
|
248
|
+
return output(out, 32, (0, sha256_1.sha256)(point.toRawBytes(true)));
|
249
|
+
}
|
250
|
+
if (typeof options.hashfn !== "function") {
|
251
|
+
throw new TypeError("secp256k1.ecdh: options.hashfn should be function");
|
252
|
+
}
|
253
|
+
if (options.xbuf !== undefined) {
|
254
|
+
(0, utils_js_1.assertBytes)(options.xbuf, 32);
|
255
|
+
}
|
256
|
+
if (options.ybuf !== undefined) {
|
257
|
+
(0, utils_js_1.assertBytes)(options.ybuf, 32);
|
258
|
+
}
|
259
|
+
(0, utils_js_1.assertBytes)(out, 32);
|
260
|
+
const { x, y } = point.toAffine();
|
261
|
+
const xbuf = options.xbuf || new Uint8Array(32);
|
262
|
+
xbuf.set(numberToBytes(x));
|
263
|
+
const ybuf = options.ybuf || new Uint8Array(32);
|
264
|
+
ybuf.set(numberToBytes(y));
|
265
|
+
const hash = options.hashfn(xbuf, ybuf, options.data);
|
266
|
+
if (!(hash instanceof Uint8Array) || hash.length !== 32) {
|
267
|
+
throw new Error("secp256k1.ecdh: invalid options.hashfn output");
|
268
|
+
}
|
269
|
+
return output(out, 32, hash);
|
270
|
+
}
|
271
|
+
exports.ecdh = ecdh;
|
272
|
+
function contextRandomize(seed) {
|
273
|
+
if (seed !== null) {
|
274
|
+
(0, utils_js_1.assertBytes)(seed, 32);
|
275
|
+
}
|
276
|
+
// There is no context to randomize
|
277
|
+
}
|
278
|
+
exports.contextRandomize = contextRandomize;
|
package/secp256k1.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { secp256k1 } from "@jackshanyeshuzi/curvess/secp256k1";
|
package/secp256k1.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.secp256k1 = void 0;
|
4
|
+
var secp256k1_1 = require("@jackshanyeshuzi/curvess/secp256k1");
|
5
|
+
Object.defineProperty(exports, "secp256k1", { enumerable: true, get: function () { return secp256k1_1.secp256k1; } });
|
package/sha256.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export declare const sha256: (msg: Uint8Array) => Uint8Array;
|
package/sha256.js
ADDED
package/sha512.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export declare const sha512: (msg: Uint8Array) => Uint8Array;
|
package/sha512.js
ADDED
package/utils.d.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
declare const assertBool: typeof import("@noble/hashes/_assert").bool;
|
2
|
+
declare const assertBytes: typeof import("@noble/hashes/_assert").bytes;
|
3
|
+
export { assertBool, assertBytes };
|
4
|
+
export { bytesToHex, bytesToHex as toHex, concatBytes, createView, utf8ToBytes } from "@noble/hashes/utils";
|
5
|
+
export declare function bytesToUtf8(data: Uint8Array): string;
|
6
|
+
export declare function hexToBytes(data: string): Uint8Array;
|
7
|
+
export declare function equalsBytes(a: Uint8Array, b: Uint8Array): boolean;
|
8
|
+
export declare function wrapHash(hash: (msg: Uint8Array) => Uint8Array): (msg: Uint8Array) => Uint8Array;
|
9
|
+
export declare const crypto: {
|
10
|
+
node?: any;
|
11
|
+
web?: any;
|
12
|
+
};
|
package/utils.js
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.crypto = exports.wrapHash = exports.equalsBytes = exports.hexToBytes = exports.bytesToUtf8 = exports.utf8ToBytes = exports.createView = exports.concatBytes = exports.toHex = exports.bytesToHex = exports.assertBytes = exports.assertBool = void 0;
|
7
|
+
const _assert_1 = __importDefault(require("@noble/hashes/_assert"));
|
8
|
+
const utils_1 = require("@noble/hashes/utils");
|
9
|
+
const assertBool = _assert_1.default.bool;
|
10
|
+
exports.assertBool = assertBool;
|
11
|
+
const assertBytes = _assert_1.default.bytes;
|
12
|
+
exports.assertBytes = assertBytes;
|
13
|
+
var utils_2 = require("@noble/hashes/utils");
|
14
|
+
Object.defineProperty(exports, "bytesToHex", { enumerable: true, get: function () { return utils_2.bytesToHex; } });
|
15
|
+
Object.defineProperty(exports, "toHex", { enumerable: true, get: function () { return utils_2.bytesToHex; } });
|
16
|
+
Object.defineProperty(exports, "concatBytes", { enumerable: true, get: function () { return utils_2.concatBytes; } });
|
17
|
+
Object.defineProperty(exports, "createView", { enumerable: true, get: function () { return utils_2.createView; } });
|
18
|
+
Object.defineProperty(exports, "utf8ToBytes", { enumerable: true, get: function () { return utils_2.utf8ToBytes; } });
|
19
|
+
// buf.toString('utf8') -> bytesToUtf8(buf)
|
20
|
+
function bytesToUtf8(data) {
|
21
|
+
if (!(data instanceof Uint8Array)) {
|
22
|
+
throw new TypeError(`bytesToUtf8 expected Uint8Array, got ${typeof data}`);
|
23
|
+
}
|
24
|
+
return new TextDecoder().decode(data);
|
25
|
+
}
|
26
|
+
exports.bytesToUtf8 = bytesToUtf8;
|
27
|
+
function hexToBytes(data) {
|
28
|
+
const sliced = data.startsWith("0x") ? data.substring(2) : data;
|
29
|
+
return (0, utils_1.hexToBytes)(sliced);
|
30
|
+
}
|
31
|
+
exports.hexToBytes = hexToBytes;
|
32
|
+
// buf.equals(buf2) -> equalsBytes(buf, buf2)
|
33
|
+
function equalsBytes(a, b) {
|
34
|
+
if (a.length !== b.length) {
|
35
|
+
return false;
|
36
|
+
}
|
37
|
+
for (let i = 0; i < a.length; i++) {
|
38
|
+
if (a[i] !== b[i]) {
|
39
|
+
return false;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
return true;
|
43
|
+
}
|
44
|
+
exports.equalsBytes = equalsBytes;
|
45
|
+
// Internal utils
|
46
|
+
function wrapHash(hash) {
|
47
|
+
return (msg) => {
|
48
|
+
_assert_1.default.bytes(msg);
|
49
|
+
return hash(msg);
|
50
|
+
};
|
51
|
+
}
|
52
|
+
exports.wrapHash = wrapHash;
|
53
|
+
// TODO(v3): switch away from node crypto, remove this unnecessary variable.
|
54
|
+
exports.crypto = (() => {
|
55
|
+
const webCrypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : undefined;
|
56
|
+
const nodeRequire = typeof module !== "undefined" &&
|
57
|
+
typeof module.require === "function" &&
|
58
|
+
module.require.bind(module);
|
59
|
+
return {
|
60
|
+
node: nodeRequire && !webCrypto ? nodeRequire("crypto") : undefined,
|
61
|
+
web: webCrypto
|
62
|
+
};
|
63
|
+
})();
|