Skip to content

Commit 95e052d

Browse files
committed
Move more utils into noble-hashes
1 parent ea812a3 commit 95e052d

File tree

5 files changed

+20
-64
lines changed

5 files changed

+20
-64
lines changed

README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const { utf8ToBytes } = require("ethereum-cryptography/utils");
113113
sha256(utf8ToBytes("abc"))
114114

115115
// If you need hex
116-
const { toHex } = require("ethereum-cryptography/utils");
116+
const { bytesToHex as toHex } = require("ethereum-cryptography/utils");
117117
toHex(sha256(utf8ToBytes("abc")))
118118
```
119119

@@ -208,7 +208,7 @@ Note: if you've been using ethereum-cryptography v0.1, it had different API. We'
208208
This module exports a single class `HDKey`, which should be used like this:
209209

210210
```ts
211-
const { HDKey } = require("ethereum-cryptography/secp256k1");
211+
const { HDKey } = require("ethereum-cryptography/hdkey");
212212
const hdkey1 = HDKey.fromMasterSeed(seed);
213213
const hdkey2 = HDKey.fromExtendedKey(base58key);
214214
const hdkey3 = HDKey.fromJSON({ xpriv: string });
@@ -269,17 +269,6 @@ but it's backed by this package's primitives, and has built-in TypeScript types.
269269
Its only difference is that it has to be be used with a named import.
270270
The implementation is [loosely based on hdkey, which has MIT License](#LICENSE).
271271

272-
```js
273-
const { HDKey } = require("ethereum-cryptography/hdkey");
274-
const { hexToBytes } = require("ethereum-cryptography/utils");
275-
276-
const seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
277-
const hdkey = HDKey.fromMasterSeed(hexToBytes(seed));
278-
const childkey = hdkey.derive("m/0/2147483647'/1");
279-
280-
console.log(childkey.privateExtendedKey);
281-
```
282-
283272
## BIP39 Mnemonic Seed Phrase
284273

285274
```ts

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"*.d.ts.map"
2525
],
2626
"dependencies": {
27-
"micro-base": "^0.10.0",
28-
"@noble/hashes": "^0.5.6",
27+
"micro-base": "^0.10.1",
28+
"@noble/hashes": "^0.5.7",
2929
"@noble/secp256k1": "^1.3.3"
3030
},
3131
"browser": {

src/hdkey.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { hmac } from "@noble/hashes/hmac";
22
import { ripemd160 } from "@noble/hashes/ripemd160";
33
import { sha256 } from "@noble/hashes/sha256";
44
import { sha512 } from "@noble/hashes/sha512";
5-
import { bytesToHex } from "@noble/hashes/utils";
6-
import { base58check } from "micro-base";
7-
import * as secp from "./secp256k1";
85
import {
96
assertBytes,
7+
bytesToHex,
108
concatBytes,
119
createView,
1210
hexToBytes,
1311
utf8ToBytes
14-
} from "./utils";
12+
} from "@noble/hashes/utils";
13+
import { base58check } from "micro-base";
14+
import * as secp from "./secp256k1";
1515
const base58c = base58check(sha256);
1616

1717
function bytesToNumber(bytes: Uint8Array): bigint {
@@ -41,7 +41,7 @@ const hash160 = (data: Uint8Array) => ripemd160(sha256(data));
4141
const fromU32 = (data: Uint8Array) => createView(data).getUint32(0, false);
4242
const toU32 = (n: number) => {
4343
if (!Number.isSafeInteger(n) || n < 0 || n > 2 ** 32 - 1) {
44-
throw new Error(`Invalid number=${n}. Should be from 1 to 2 ** 32 - 1`);
44+
throw new Error(`Invalid number=${n}. Should be from 0 to 2 ** 32 - 1`);
4545
}
4646
const buf = new Uint8Array(4);
4747
createView(buf).setUint32(0, n, false);

src/secp256k1.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export {
1010
utils,
1111
Point,
1212
Signature,
13-
CURVE
13+
CURVE,
14+
schnorr
1415
} from "@noble/secp256k1";
1516

1617
// Enable sync API for noble-secp256k1

src/utils.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
// buf.toString('hex') -> toHex(buf)
22
import { assertBytes } from "@noble/hashes/utils";
33
export {
4+
assertBool,
45
assertBytes,
6+
bytesToHex,
57
bytesToHex as toHex,
6-
createView
8+
concatBytes,
9+
hexToBytes,
10+
createView,
11+
utf8ToBytes
712
} from "@noble/hashes/utils";
8-
// Buffer.from(hex, 'hex') -> hexToBytes(hex)
9-
export function hexToBytes(hex: string): Uint8Array {
10-
if (typeof hex !== "string") {
11-
throw new TypeError(`hexToBytes: expected string, got ${typeof hex}`);
12-
}
13-
if (hex.length % 2) {
14-
throw new Error("hexToBytes: received invalid unpadded hex");
15-
}
16-
const array = new Uint8Array(hex.length / 2);
17-
for (let i = 0; i < array.length; i++) {
18-
const j = i * 2;
19-
array[i] = Number.parseInt(hex.slice(j, j + 2), 16);
20-
}
21-
return array;
22-
}
23-
// Buffer.from(s, 'utf8') -> utf8ToBytes(s)
24-
export function utf8ToBytes(s: string) {
25-
if (typeof s !== "string") {
26-
throw new TypeError(`utf8ToBytes expected string, got ${typeof s}`);
27-
}
28-
return new TextEncoder().encode(s);
29-
}
13+
3014
// buf.toString('utf8') -> bytesToUtf8(buf)
3115
export function bytesToUtf8(data: Uint8Array): string {
3216
if (!(data instanceof Uint8Array)) {
3317
throw new TypeError(`bytesToUtf8 expected Uint8Array, got ${typeof data}`);
3418
}
3519
return new TextDecoder().decode(data);
3620
}
21+
3722
// buf.equals(buf2) -> equalsBytes(buf, buf2)
3823
export function equalsBytes(a: Uint8Array, b: Uint8Array): boolean {
3924
if (a.length !== b.length) {
@@ -46,27 +31,8 @@ export function equalsBytes(a: Uint8Array, b: Uint8Array): boolean {
4631
}
4732
return true;
4833
}
49-
// Buffer.concat([buf1, buf2]) -> concatBytes(buf1, buf2)
50-
export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
51-
if (arrays.length === 1) {
52-
return arrays[0];
53-
}
54-
const length = arrays.reduce((a, arr) => a + arr.length, 0);
55-
const result = new Uint8Array(length);
56-
for (let i = 0, pad = 0; i < arrays.length; i++) {
57-
const arr = arrays[i];
58-
result.set(arr, pad);
59-
pad += arr.length;
60-
}
61-
return result;
62-
}
63-
// Internal utils
64-
export function assertBool(b: boolean) {
65-
if (typeof b !== "boolean") {
66-
throw new Error(`Expected boolean, not ${b}`);
67-
}
68-
}
6934

35+
// Internal utils
7036
export function wrapHash(hash: (msg: Uint8Array) => Uint8Array) {
7137
return (msg: Uint8Array) => {
7238
assertBytes(msg);

0 commit comments

Comments
 (0)