Skip to content

Commit 9a097ac

Browse files
committed
Update hdkey to to use secp256k1 v4
1 parent b6e2440 commit 9a097ac

File tree

7 files changed

+106
-5
lines changed

7 files changed

+106
-5
lines changed

packages/ethereum-cryptography/hdkey-without-crypto-config/rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default {
1919
]
2020
}),
2121
commonjs({
22-
ignore: ["../shims/hdkey-crypto", "../secp256k1"]
22+
ignore: ["../shims/hdkey-crypto", "../shims/hdkey-secp256k1v3"]
2323
})
2424
]
2525
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require("../secp256k1");
1+
module.exports = require("../shims/hdkey-secp256k1v3");
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
import { ripemd160 } from "../ripemd160";
2+
import { sha256 } from "../sha256";
3+
14
export const createHmac = require("create-hmac");
2-
export const createHash = require("create-hash");
35
export const randomBytes = require("randombytes");
6+
7+
class Hash {
8+
private buffers: Buffer[] = [];
9+
10+
constructor(private readonly hashFunction: (msg: Buffer) => Buffer) {}
11+
12+
public update(buffer: Buffer): this {
13+
if (!Buffer.isBuffer(buffer)) {
14+
throw new Error("hdkey-crypto shim is outdated");
15+
}
16+
17+
this.buffers.push(buffer);
18+
19+
return this;
20+
}
21+
22+
public digest(param: any): Buffer {
23+
if (param) {
24+
throw new Error("hdkey-crypto shim is outdated");
25+
}
26+
27+
return this.hashFunction(Buffer.concat(this.buffers));
28+
}
29+
}
30+
31+
export const createHash = (name: string) => {
32+
if (name === "ripemd160") {
33+
return new Hash(ripemd160);
34+
}
35+
36+
if (name === "sha256") {
37+
return new Hash(sha256);
38+
}
39+
40+
throw new Error("hdkey-crypto shim is outdated");
41+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "../../shims/hdkey-secp256k1v3";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { createHmac, createHash, randomBytes } from "crypto";
1+
export { createHash, createHmac, randomBytes } from "crypto";
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as secp256k1 from "secp256k1";
2+
3+
export function privateKeyVerify(privateKey: Buffer): boolean {
4+
return secp256k1.privateKeyVerify(privateKey);
5+
}
6+
7+
export function publicKeyCreate(privateKey: Buffer, compressed = true): Buffer {
8+
return Buffer.from(secp256k1.publicKeyCreate(privateKey, compressed));
9+
}
10+
11+
export function publicKeyVerify(publicKey: Buffer): boolean {
12+
return secp256k1.publicKeyVerify(publicKey);
13+
}
14+
15+
export function publicKeyConvert(publicKey: Buffer, compressed = true): Buffer {
16+
return Buffer.from(secp256k1.publicKeyConvert(publicKey, compressed));
17+
}
18+
19+
export function privateKeyTweakAdd(publicKey: Buffer, tweak: Buffer): Buffer {
20+
return Buffer.from(
21+
secp256k1.privateKeyTweakAdd(Buffer.from(publicKey), tweak)
22+
);
23+
}
24+
25+
export function publicKeyTweakAdd(
26+
publicKey: Buffer,
27+
tweak: Buffer,
28+
compressed = true
29+
): Buffer {
30+
return Buffer.from(
31+
secp256k1.publicKeyTweakAdd(Buffer.from(publicKey), tweak, compressed)
32+
);
33+
}
34+
35+
export function sign(
36+
message: Buffer,
37+
privateKey: Buffer
38+
): { signature: Buffer; recovery: number } {
39+
const ret = secp256k1.ecdsaSign(message, privateKey);
40+
return { signature: Buffer.from(ret.signature), recovery: ret.recid };
41+
}
42+
43+
export function verify(
44+
message: Buffer,
45+
signature: Buffer,
46+
publicKey: Buffer
47+
): boolean {
48+
return secp256k1.ecdsaVerify(signature, message, publicKey);
49+
}

packages/ethereum-cryptography/test/test-vectors/hdkey.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert } from "chai";
22

33
export function createTests(HDKey: any) {
44
describe("hdkey", function() {
5-
it("Should derive correctly", function() {
5+
it("Should derive private key correctly", function() {
66
const seed =
77
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
88
const hdkey = HDKey.fromMasterSeed(Buffer.from(seed, "hex"));
@@ -13,5 +13,18 @@ export function createTests(HDKey: any) {
1313
"xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef"
1414
);
1515
});
16+
17+
it("Should derive public key correctly", function() {
18+
const seed =
19+
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
20+
const hdkey = HDKey.fromMasterSeed(Buffer.from(seed, "hex"));
21+
const expected = hdkey.derive("m/0/2147483647'/1");
22+
const parentkey = hdkey.derive("m/0/2147483647'");
23+
parentkey.wipePrivateData();
24+
25+
const childkey = parentkey.derive("m/1");
26+
27+
assert.equal(childkey.publicExtendedKey, expected.publicExtendedKey);
28+
});
1629
});
1730
}

0 commit comments

Comments
 (0)