-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvaults.js
More file actions
45 lines (41 loc) · 1.43 KB
/
vaults.js
File metadata and controls
45 lines (41 loc) · 1.43 KB
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
/**
* Project: Rewind Bitcoin
* Website: https://rewindbitcoin.com
*
* Author: Jose-Luis Landabaso
* Email: landabaso@gmail.com
*
* Contact Email: hello@rewindbitcoin.com
*
* License: MIT License
*
* Copyright (c) 2025 Jose-Luis Landabaso, Rewind Bitcoin
*/
import { SIGNING_MESSAGE, VAULT_PATH } from "./constants";
import { mnemonicToSeedSync, validateMnemonic } from "bip39";
import { sha256 } from "@noble/hashes/sha2";
import * as networks from "./networks";
import { BIP32Factory } from "bip32";
import { MessageFactory } from "bitcoinjs-message";
import * as secp256k1 from "@bitcoinerlab/secp256k1";
const BIP32 = BIP32Factory(secp256k1);
const MessageAPI = MessageFactory(secp256k1);
export const deriveVault = ({ mnemonic, index, network }) => {
if (!validateMnemonic(mnemonic))
throw new Error(`Invalid mnemonic ${mnemonic}`);
const vaultPath = VAULT_PATH.replace(
"<network>",
network === networks.bitcoin ? "0" : "1",
).replace("<index>", index.toString());
const masterNode = BIP32.fromSeed(mnemonicToSeedSync(mnemonic), network);
const vaultNode = masterNode.derivePath(vaultPath);
const vaultId = vaultNode.publicKey.toString("hex");
if (!vaultNode.privateKey) throw new Error("Could not generate a privateKey");
const signature = MessageAPI.sign(
SIGNING_MESSAGE,
vaultNode.privateKey,
true, // assumes compressed
);
const cipherKey = sha256(signature);
return { vaultId, cipherKey };
};