Skip to content

Commit 198e556

Browse files
committed
Expose curve-secp256k1 module
1 parent 4e81642 commit 198e556

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
/*.js.map
33
/*.d.ts
44
/*.d.ts.map
5-
/pure
6-
/shims
5+
/bip39
76
/test-builds
87
/node_modules
98
/.parcel-cache

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ function ripemd160(msg: Uint8Array): Uint8Array;
8686
function blake2b(msg: Uint8Array, outputLength = 64): Uint8Array;
8787
```
8888

89+
- SHA2 (SHA256, SHA512)
90+
- keccak-256 variant of SHA3
91+
- RIPEMD160
92+
- BLAKE2b
93+
8994
```js
9095
const { sha256 } = require("ethereum-cryptography/sha256");
9196
const { keccak256, keccak224, keccak384, keccak512 } = require("ethereum-cryptography/keccak");
@@ -153,6 +158,7 @@ console.log(getRandomBytesSync(32));
153158
function getPublicKey(privateKey: Uint8Array, isCompressed?: false): Uint8Array;
154159
function getSharedSecret(privateKeyA: Uint8Array, publicKeyB: Uint8Array): Uint8Array;
155160
function sign(msgHash: Uint8Array, privateKey: Uint8Array, opts?: Options): Promise<Uint8Array>;
161+
function signSync(msgHash: Uint8Array, privateKey: Uint8Array, opts?: Options): Uint8Array;
156162
function verify(signature: Uint8Array, msgHash: Uint8Array, publicKey: Uint8Array): boolean
157163
function recoverPublicKey(msgHash: Uint8Array, signature: Uint8Array, recovery: number): Uint8Array | undefined;
158164
function utils.randomPrivateKey(): Uint8Array;
@@ -228,9 +234,10 @@ Its only difference is that it has to be be used with a named import.
228234

229235
```js
230236
const { HDKey } = require("ethereum-cryptography/hdkey");
237+
const { hexToBytes } = require("ethereum-cryptography/utils");
231238

232239
const seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
233-
const hdkey = HDKey.fromMasterSeed(Uint8Array.from(seed, "hex"));
240+
const hdkey = HDKey.fromMasterSeed(hexToBytes(seed));
234241
const childkey = hdkey.derive("m/0/2147483647'/1");
235242

236243
console.log(childkey.privateExtendedKey);
@@ -275,6 +282,11 @@ The word lists are exported as a `wordlist` variable in each of these submodules
275282

276283
## AES Encryption
277284

285+
```ts
286+
function encrypt(msg: Uint8Array, key: Uint8Array, iv: Uint8Array, mode = "aes-128-ctr", pkcs7PaddingEnabled = true): Uint8Array;
287+
function decrypt(cypherText: Uint8Array, key: Uint8Array, iv: Uint8Array, mode = "aes-128-ctr", pkcs7PaddingEnabled = true): Uint8Array
288+
```
289+
278290
The `aes` submodule contains encryption and decryption functions implementing
279291
the [Advanced Encryption Standard](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)
280292
algorithm.
@@ -341,23 +353,17 @@ Note that implementing this can mean catching all errors that can be thrown
341353
when calling on of this module's functions, and just throwing a new generic
342354
exception.
343355

344-
### Function types
345-
346-
```ts
347-
function encrypt(msg: Uint8Array, key: Uint8Array, iv: Uint8Array, mode = "aes-128-ctr", pkcs7PaddingEnabled = true): Uint8Array;
348-
function decrypt(cypherText: Uint8Array, key: Uint8Array, iv: Uint8Array, mode = "aes-128-ctr", pkcs7PaddingEnabled = true): Uint8Array
349-
```
350-
351356
### Example usage
352357

353358
```js
354359
const { encrypt } = require("ethereum-cryptography/aes");
360+
const { hexToBytes, utf8ToBytes } = require("ethereum-cryptography/utils");
355361
356362
console.log(
357363
encrypt(
358-
Uint8Array.from("message", "ascii"),
359-
Uint8Array.from("2b7e151628aed2a6abf7158809cf4f3c", "hex"),
360-
Uint8Array.from("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "hex")
364+
utf8ToBytes("message"),
365+
hexToBytes("2b7e151628aed2a6abf7158809cf4f3c"),
366+
hexToBytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
361367
)
362368
);
363369
```

src/curve-secp256k1.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { hmac } from "noble-hashes/lib/hmac";
2+
import { sha256 } from "noble-hashes/lib/sha256";
3+
import { utils as _utils } from "noble-secp256k1";
4+
export {
5+
getPublicKey,
6+
sign,
7+
signSync,
8+
verify,
9+
getSharedSecret,
10+
utils,
11+
Point,
12+
Signature,
13+
CURVE
14+
} from "noble-secp256k1";
15+
16+
// Enable sync API for noble-secp256k1
17+
_utils.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]) => {
18+
const h = hmac.create(sha256, key);
19+
messages.forEach(msg => h.update(msg));
20+
return h.digest();
21+
};

src/hdkey.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ripemd160 } from "noble-hashes/lib/ripemd160";
44
import { sha256 } from "noble-hashes/lib/sha256";
55
import { sha512 } from "noble-hashes/lib/sha512";
66
import { bytesToHex } from "noble-hashes/lib/utils";
7-
import * as secp from "noble-secp256k1";
7+
import * as secp from "./curve-secp256k1";
88
import {
99
assertBytes,
1010
concatBytes,
@@ -14,15 +14,6 @@ import {
1414
} from "./utils";
1515
const base58c = base58check(sha256);
1616

17-
// Enable sync API for noble-secp256k1
18-
secp.utils.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => {
19-
const h = hmac.create(sha256, key);
20-
for (const msg of msgs) {
21-
h.update(msg);
22-
}
23-
return h.digest();
24-
};
25-
2617
function bytesToNumber(bytes: Uint8Array): bigint {
2718
return BigInt(`0x${bytesToHex(bytes)}`);
2819
}

src/secp256k1.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import { hmac } from "noble-hashes/lib/hmac";
21
import { sha256 } from "noble-hashes/lib/sha256";
3-
import * as secp from "noble-secp256k1";
2+
import * as secp from "./curve-secp256k1";
43
import { assertBool, assertBytes, hexToBytes, toHex } from "./utils";
54

6-
// Enable sync API for noble-secp256k1
7-
secp.utils.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]) => {
8-
const h = hmac.create(sha256, key);
9-
messages.forEach(msg => h.update(msg));
10-
return h.digest();
11-
};
5+
// Legacy compatibility layer for elliptic via noble-secp256k1
6+
// Use `curve-secp256k1` module directly instead
127

138
// Copy-paste from secp256k1, maybe export it?
149
const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes));

0 commit comments

Comments
 (0)