Skip to content

Commit 756d4c6

Browse files
committed
Lint
1 parent a44264f commit 756d4c6

File tree

2 files changed

+72
-61
lines changed

2 files changed

+72
-61
lines changed

src/hdkey.ts

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,55 @@ const toU32 = (n: number) => {
4545
return buf;
4646
};
4747

48-
type HDKeyOpt = {
48+
interface HDKeyOpt {
4949
versions: Versions;
5050
depth?: number;
5151
index?: number;
5252
parentFingerprint?: number;
5353
chainCode: Uint8Array;
5454
publicKey?: Uint8Array;
5555
privateKey?: Uint8Array | bigint;
56-
};
56+
}
5757

5858
export class HDKey {
59-
readonly versions: Versions;
60-
readonly depth: number = 0;
61-
readonly index: number = 0;
62-
readonly chainCode: Uint8Array | null = null;
63-
readonly parentFingerprint: number = 0;
64-
private privKey?: bigint;
65-
private privKeyBytes?: Uint8Array;
66-
private pubKey?: Uint8Array;
67-
private pubHash: Uint8Array | undefined;
59+
get fingerprint(): number {
60+
if (!this.pubHash) {
61+
throw new Error("No publicKey set!");
62+
}
63+
return fromU32(this.pubHash);
64+
}
65+
get identifier(): Uint8Array | undefined {
66+
return this.pubHash;
67+
}
68+
get pubKeyHash(): Uint8Array | undefined {
69+
return this.pubHash;
70+
}
71+
get privateKey(): Uint8Array | null {
72+
return this.privKeyBytes || null;
73+
}
74+
get publicKey(): Uint8Array | null {
75+
return this.pubKey || null;
76+
}
77+
get privateExtendedKey(): string {
78+
const priv = this.privateKey;
79+
if (!priv) {
80+
throw new Error("No private key");
81+
}
82+
return base58c.encode(
83+
this.serialize(
84+
this.versions.private,
85+
concatBytes(new Uint8Array([0]), priv)
86+
)
87+
);
88+
}
89+
get publicExtendedKey(): string {
90+
if (!this.pubKey) {
91+
throw new Error("No public key");
92+
}
93+
return base58c.encode(this.serialize(this.versions.public, this.pubKey));
94+
}
6895

69-
static fromMasterSeed(
96+
public static fromMasterSeed(
7097
seed: Uint8Array,
7198
versions: Versions = BITCOIN_VERSIONS
7299
): HDKey {
@@ -78,7 +105,7 @@ export class HDKey {
78105
});
79106
}
80107

81-
static fromExtendedKey(
108+
public static fromExtendedKey(
82109
base58key: string,
83110
versions: Versions = BITCOIN_VERSIONS
84111
): HDKey {
@@ -105,9 +132,18 @@ export class HDKey {
105132
}
106133
}
107134

108-
static fromJSON(json: { xpriv: string }): HDKey {
135+
public static fromJSON(json: { xpriv: string }): HDKey {
109136
return HDKey.fromExtendedKey(json.xpriv);
110137
}
138+
public readonly versions: Versions;
139+
public readonly depth: number = 0;
140+
public readonly index: number = 0;
141+
public readonly chainCode: Uint8Array | null = null;
142+
public readonly parentFingerprint: number = 0;
143+
private privKey?: bigint;
144+
private privKeyBytes?: Uint8Array;
145+
private pubKey?: Uint8Array;
146+
private pubHash: Uint8Array | undefined;
111147

112148
constructor(opt: HDKeyOpt) {
113149
if (!opt || typeof opt !== "object") {
@@ -118,8 +154,9 @@ export class HDKey {
118154
this.chainCode = opt.chainCode;
119155
this.index = opt.index || 0;
120156
this.parentFingerprint = opt.parentFingerprint || 0;
121-
if (opt.publicKey && opt.privateKey)
157+
if (opt.publicKey && opt.privateKey) {
122158
throw new Error("HDKey: publicKey and privateKey at same time.");
159+
}
123160
if (opt.privateKey) {
124161
if (!secp.utils.isValidPrivateKey(opt.privateKey)) {
125162
throw new Error("Invalid private key");
@@ -137,42 +174,6 @@ export class HDKey {
137174
}
138175
this.pubHash = hash160(this.pubKey);
139176
}
140-
get fingerprint(): number {
141-
if (!this.pubHash) {
142-
throw new Error("No publicKey set!");
143-
}
144-
return fromU32(this.pubHash);
145-
}
146-
get identifier(): Uint8Array | undefined {
147-
return this.pubHash;
148-
}
149-
get pubKeyHash(): Uint8Array | undefined {
150-
return this.pubHash;
151-
}
152-
get privateKey(): Uint8Array | null {
153-
return this.privKeyBytes || null;
154-
}
155-
get publicKey(): Uint8Array | null {
156-
return this.pubKey || null;
157-
}
158-
get privateExtendedKey(): string {
159-
const priv = this.privateKey;
160-
if (!priv) {
161-
throw new Error("No private key");
162-
}
163-
return base58c.encode(
164-
this.serialize(
165-
this.versions.private,
166-
concatBytes(new Uint8Array([0]), priv)
167-
)
168-
);
169-
}
170-
get publicExtendedKey(): string {
171-
if (!this.pubKey) {
172-
throw new Error("No public key");
173-
}
174-
return base58c.encode(this.serialize(this.versions.public, this.pubKey));
175-
}
176177

177178
public derive(path: string): HDKey {
178179
if (!/^[mM]'?/.test(path)) {

test/test-vectors/hdkey.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as secp from "@noble/secp256k1";
2-
import { HDKey, HARDENED_OFFSET } from "../../src/hdkey";
2+
import { HARDENED_OFFSET, HDKey } from "../../src/hdkey";
33
import { hexToBytes, toHex } from "../../src/utils";
44
import { deepStrictEqual, throws } from "./assert";
55
// https://github.com/cryptocoinjs/hdkey/blob/42637e381bdef0c8f785b14f5b66a80dad969514/test/fixtures/hdkey.json
@@ -160,7 +160,8 @@ describe("hdkey", () => {
160160

161161
describe("- privateKey", () => {
162162
it("should throw an error if incorrect key size", () => {
163-
const seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
163+
const seed =
164+
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
164165
const hdkey = HDKey.fromMasterSeed(hexToBytes(seed));
165166
// const hdkey = new HDKey.HDKey();
166167
throws(() => {
@@ -173,7 +174,8 @@ describe("hdkey", () => {
173174
describe("- publicKey", () => {
174175
it("should throw an error if incorrect key size", () => {
175176
throws(() => {
176-
const seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
177+
const seed =
178+
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
177179
const hdkey = HDKey.fromMasterSeed(hexToBytes(seed));
178180
// @ts-ignore
179181
hdkey.publicKey = new Uint8Array([1, 2, 3, 4]);
@@ -183,19 +185,25 @@ describe("hdkey", () => {
183185
it("should not throw if key is 33 bytes (compressed)", () => {
184186
const pub = secp.getPublicKey(secp.utils.randomPrivateKey(), true);
185187
deepStrictEqual(pub.length, 33);
186-
const seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
188+
const seed =
189+
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
187190
const hdkey = HDKey.fromMasterSeed(hexToBytes(seed));
188-
// @ts-ignore
189-
throws(() => { hdkey.publicKey = pub; });
191+
throws(() => {
192+
// @ts-ignore
193+
hdkey.publicKey = pub;
194+
});
190195
});
191196

192197
it("should not throw if key is 65 bytes (not compressed)", () => {
193198
const pub = secp.getPublicKey(secp.utils.randomPrivateKey(), false);
194199
deepStrictEqual(pub.length, 65);
195-
const seed = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
200+
const seed =
201+
"fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542";
196202
const hdkey = HDKey.fromMasterSeed(hexToBytes(seed));
197-
// @ts-ignore
198-
throws(() => { hdkey.publicKey = pub; });
203+
throws(() => {
204+
// @ts-ignore
205+
hdkey.publicKey = pub;
206+
});
199207
});
200208
});
201209

@@ -347,7 +355,9 @@ describe("hdkey", () => {
347355
const masterKey = HDKey.fromMasterSeed(hexToBytes(seed));
348356

349357
deepStrictEqual(!!masterKey.privateExtendedKey, true, "xpriv is truthy");
350-
throws(() => { (masterKey as any).privateKey = undefined; });
358+
throws(() => {
359+
(masterKey as any).privateKey = undefined;
360+
});
351361

352362
// throws(() => masterKey.privateExtendedKey);
353363
});

0 commit comments

Comments
 (0)