|
| 1 | +/** |
| 2 | + * Derive raw private keys from a BIP-39 mnemonic. |
| 3 | + * |
| 4 | + * OWS (@open-wallet-standard/core) does not expose private keys — it returns |
| 5 | + * mnemonics from exportWallet. Callers that need raw keys derive them here. |
| 6 | + * |
| 7 | + * EVM: BIP-44 secp256k1, path m/44'/60'/0'/0/<index> |
| 8 | + * Solana: SLIP-0010 ed25519, path m/44'/501'/<index>'/0' (Phantom convention) |
| 9 | + */ |
| 10 | + |
| 11 | +import { Buffer } from "node:buffer"; |
| 12 | +import { mnemonicToSeedSync, validateMnemonic } from "@scure/bip39"; |
| 13 | +import { wordlist } from "@scure/bip39/wordlists/english"; |
| 14 | +import { HDKey } from "@scure/bip32"; |
| 15 | +import { hmac } from "@noble/hashes/hmac.js"; |
| 16 | +import { sha512 } from "@noble/hashes/sha512.js"; |
| 17 | +import { Keypair } from "@solana/web3.js"; |
| 18 | + |
| 19 | +const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 20 | + |
| 21 | +function toHex(bytes) { |
| 22 | + return Buffer.from(bytes).toString("hex"); |
| 23 | +} |
| 24 | + |
| 25 | +function bytesToBase58(bytes) { |
| 26 | + let num = 0n; |
| 27 | + for (const b of bytes) num = num * 256n + BigInt(b); |
| 28 | + let out = ""; |
| 29 | + while (num > 0n) { |
| 30 | + const rem = Number(num % 58n); |
| 31 | + out = BASE58_ALPHABET[rem] + out; |
| 32 | + num = num / 58n; |
| 33 | + } |
| 34 | + for (const b of bytes) { |
| 35 | + if (b === 0) out = "1" + out; |
| 36 | + else break; |
| 37 | + } |
| 38 | + return out; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * SLIP-0010 ed25519 derivation. Only hardened steps are valid. |
| 43 | + * @param {Uint8Array} seed - BIP-39 seed (64 bytes) |
| 44 | + * @param {number[]} pathIndices - hardened indices already OR'd with 0x80000000 |
| 45 | + * @returns {Uint8Array} 32-byte private seed (input to Keypair.fromSeed) |
| 46 | + */ |
| 47 | +function deriveEd25519(seed, pathIndices) { |
| 48 | + const masterKey = new TextEncoder().encode("ed25519 seed"); |
| 49 | + let I = hmac(sha512, masterKey, seed); |
| 50 | + let key = I.slice(0, 32); |
| 51 | + let chain = I.slice(32); |
| 52 | + |
| 53 | + for (const idx of pathIndices) { |
| 54 | + const data = new Uint8Array(1 + 32 + 4); |
| 55 | + data[0] = 0x00; |
| 56 | + data.set(key, 1); |
| 57 | + data[33] = (idx >>> 24) & 0xff; |
| 58 | + data[34] = (idx >>> 16) & 0xff; |
| 59 | + data[35] = (idx >>> 8) & 0xff; |
| 60 | + data[36] = idx & 0xff; |
| 61 | + I = hmac(sha512, chain, data); |
| 62 | + key = I.slice(0, 32); |
| 63 | + chain = I.slice(32); |
| 64 | + } |
| 65 | + return key; |
| 66 | +} |
| 67 | + |
| 68 | +const HARDENED = 0x80000000; |
| 69 | + |
| 70 | +export function deriveEvmKey(mnemonic, index = 0) { |
| 71 | + if (!validateMnemonic(mnemonic, wordlist)) { |
| 72 | + throw new Error("Invalid mnemonic"); |
| 73 | + } |
| 74 | + const seed = mnemonicToSeedSync(mnemonic); |
| 75 | + const node = HDKey.fromMasterSeed(seed).derive(`m/44'/60'/0'/0/${index}`); |
| 76 | + if (!node.privateKey) throw new Error("EVM derivation produced no private key"); |
| 77 | + return { |
| 78 | + privateKey: "0x" + toHex(node.privateKey), |
| 79 | + address: null, // viem could derive; left null to avoid extra dep at this layer |
| 80 | + path: `m/44'/60'/0'/0/${index}`, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +export function deriveSolanaKey(mnemonic, index = 0) { |
| 85 | + if (!validateMnemonic(mnemonic, wordlist)) { |
| 86 | + throw new Error("Invalid mnemonic"); |
| 87 | + } |
| 88 | + const seed = mnemonicToSeedSync(mnemonic); |
| 89 | + const path = [44 | HARDENED, 501 | HARDENED, index | HARDENED, 0 | HARDENED]; |
| 90 | + const privSeed = deriveEd25519(seed, path); |
| 91 | + const kp = Keypair.fromSeed(privSeed); |
| 92 | + const secret64 = kp.secretKey; // 32 priv + 32 pub |
| 93 | + return { |
| 94 | + privateKeyBase58: bytesToBase58(secret64), |
| 95 | + privateKeyHex: toHex(secret64.slice(0, 32)), |
| 96 | + address: kp.publicKey.toBase58(), |
| 97 | + path: `m/44'/501'/${index}'/0'`, |
| 98 | + }; |
| 99 | +} |
0 commit comments