This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.ts
142 lines (123 loc) · 3.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { keccak256 } from "ethereum-cryptography/keccak";
import { createHmac } from "crypto";
import secp256k1 from "secp256k1";
export type HDKey = {
privateKey: Buffer;
publicKey: Uint8Array;
chainCode: Uint8Array;
};
const HARDENED_OFFSET = 0x80000000 as const;
const MASTER_SECRET = Buffer.from("Bitcoin seed", "utf8");
export function createAccountGeneratorFromSeedAndPath(
seedBuffer: Uint8Array,
hdPath: string[]
) {
const parent = createAccountFromSeed(seedBuffer);
const path = deriveFromPath(hdPath, parent);
return (index: number) => {
return deriveFromIndex(index, path);
};
}
export const uncompressedPublicKeyToAddress = (
uncompressedPublicKey: Uint8Array
) => {
const address = Buffer.from(
secp256k1.publicKeyConvert(uncompressedPublicKey, false)
);
// first byte is discarded
const hash = keccak256(address.slice(1));
return hash.slice(-20); // address is the last 20
};
function createAccountFromSeed(seedBuffer: Uint8Array): HDKey {
const I = createHmac("sha512", MASTER_SECRET).update(seedBuffer).digest();
const privateKey = I.slice(0, 32);
const chainCode = I.slice(32);
const publicKey = makePublicKey(privateKey);
return {
privateKey,
chainCode,
publicKey
};
}
function deriveFromPath(fullPath: string[], child: HDKey): HDKey {
fullPath.forEach(function (c, i) {
if (i === 0) {
if (!/^[mM]{1}/.test(c)) {
throw new Error('Path must start with "m" or "M"');
}
return;
}
const hardened = c.length > 1 && c[c.length - 1] === "'";
let childIndex = parseInt(c, 10);
if (childIndex >= HARDENED_OFFSET) throw new Error("Invalid index");
if (hardened) childIndex += HARDENED_OFFSET;
child = deriveChild(
childIndex,
hardened,
child.privateKey,
child.publicKey,
child.chainCode
);
});
return child;
}
function deriveFromIndex(index: number, child: HDKey) {
if (index >= HARDENED_OFFSET) throw new Error("Invalid index");
return deriveChild(
index,
false,
child.privateKey,
child.publicKey,
child.chainCode
);
}
function makePublicKey(privateKey: Uint8Array) {
return secp256k1.publicKeyCreate(privateKey);
}
/**
* A buffer of size 4 that can be reused as long as all changes are consumed
* within the same event loop.
*/
const SHARED_BUFFER_4 = Buffer.allocUnsafe(4);
function deriveChild(
index: number,
isHardened: boolean,
privateKey: Buffer,
publicKey: Uint8Array,
chainCode: Uint8Array
): {
privateKey: Buffer;
publicKey: Uint8Array;
chainCode: Uint8Array;
} {
const indexBuffer = SHARED_BUFFER_4;
indexBuffer.writeUInt32BE(index, 0);
let data: Buffer;
const privateKeyLength = privateKey.length;
if (isHardened) {
// Hardened child
// privateKeyLength + 1 (BUFFER_ZERO.length) + 4 (indexBuffer.length)
const dataLength = privateKeyLength + 1 + 4;
data = Buffer.concat(
[Buffer.allocUnsafe(1).fill(0), privateKey, indexBuffer],
dataLength
);
} else {
// Normal child
data = Buffer.concat([publicKey, indexBuffer], publicKey.length + 4);
}
const I = createHmac("sha512", chainCode).update(data).digest();
const IL = I.slice(0, 32);
try {
const privK = Buffer.allocUnsafe(privateKeyLength);
privateKey.copy(privK, 0, 0, privateKeyLength);
const newPrivK = secp256k1.privateKeyTweakAdd(privK, IL);
return {
privateKey: Buffer.from(newPrivK),
publicKey: makePublicKey(newPrivK),
chainCode: I.slice(32)
};
} catch {
return deriveChild(index + 1, isHardened, privateKey, publicKey, chainCode);
}
}