Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to work with extended keys #5

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const PUBLIC_KEY_PREFIX = 'npub'
export const SECRET_KEY_PREFIX = 'nsec'
export const DERIVATION_PATH = `m/44'/1237'/0'/0/0`
export const DERIVATION_PATH = `m/44'/1237'`
36 changes: 35 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function privateKeyFromSeedWords(
}: { mnemonic: string, passphrase?: string }
): { privateKey: string } {
const root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
const { privateKey } = root.derive(DERIVATION_PATH)
const { privateKey } = root.derive(`${DERIVATION_PATH}/0'/0/0`)
if (!privateKey) {
throw new Error('could not derive private key')
}
Expand Down Expand Up @@ -55,6 +55,40 @@ export function getBech32PublicKey({ publicKey }: { publicKey: string }): { bech
}
}

export function extendedPairFromSeedWords(mnemonic: string, passphrase?: string, extendedAccountIndex = 0): {
privateExtendedKey: string,
publicExtendedKey: string
} {
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
let seed = root.derive(`${DERIVATION_PATH}/${extendedAccountIndex}'`)
let privateExtendedKey = seed.privateExtendedKey
let publicExtendedKey = seed.publicExtendedKey
if (!privateExtendedKey) throw new Error('could not derive private extended key')
return { privateExtendedKey, publicExtendedKey }
}

export function accountFromExtendedKey(base58key: string, accountIndex = 0): {
privateKey?: { hex: string, bech32: string },
publicKey: { hex: string, bech32: string }
} {
let extendedKey = HDKey.fromExtendedKey(base58key)
let version = base58key.slice(0, 4)
let child = extendedKey.deriveChild(0).deriveChild(accountIndex)
let publicKeyHex = bytesToHex(child.publicKey!.slice(1))
if (!publicKeyHex) throw new Error('could not derive public key')
let publicKeyBech32 = hexToBech32(publicKeyHex, 'npub')
if (version === 'xprv') {
let privateKeyHex = bytesToHex(child.privateKey!)
if (!privateKeyHex) throw new Error('could not derive private key')
let privateKeyBech32 = hexToBech32(privateKeyHex, 'nsec')
return {
privateKey: { hex: privateKeyHex, bech32: privateKeyBech32 },
publicKey: { hex: publicKeyHex, bech32: publicKeyBech32 }
}
}
return { publicKey: { hex: publicKeyHex, bech32: publicKeyBech32 } }
}

export function generateSeedWords(): { mnemonic: string } {
return {
mnemonic: generateMnemonic(wordlist)
Expand Down
20 changes: 20 additions & 0 deletions tests/accountFromExtendedKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest'
import { accountFromExtendedKey } from '../src/index'

describe('accountFromExtendedKey', () => {
it('should get account from extended private key', () => {
const { privateKey, publicKey } = accountFromExtendedKey('xprv9z78fizET65qsCaRr1MSutTSGk1fcKfSt1sBqmuWShtkjRJJ4WCKcSnha6EmgNzFSsyom3MWtydHyPtJtSLZQUtictVQtM2vkPcguh6TQCH')

expect(privateKey?.hex).toBe('5f29af3b9676180290e77a4efad265c4c2ff28a5302461f73597fda26bb25731')
expect(privateKey?.bech32).toBe('nsec1tu567wukwcvq9y880f8045n9cnp07299xqjxrae4jl76y6aj2ucs2mkupq')
expect(publicKey.hex).toBe('e8bcf3823669444d0b49ad45d65088635d9fd8500a75b5f20b59abefa56a144f')
expect(publicKey.bech32).toBe('npub1az708q3kd9zy6z6f44zav5ygvdwelkzspf6mtusttx47lft2z38sghk0w7')
})

it('should get account from extended public key', () => {
const { privateKey, publicKey } = accountFromExtendedKey('xpub6D6V5EX8HTe95getx2tTH2QApmrA1nPJFEnneAK813RjcDdSc3WaAF7BRNpTF7o7zXjVm3DD3VMX66jhQ7wLaZ9sS6NzyfiwfzqDZbxvpDN')

expect(publicKey.hex).toBe('e8bcf3823669444d0b49ad45d65088635d9fd8500a75b5f20b59abefa56a144f')
expect(publicKey.bech32).toBe('npub1az708q3kd9zy6z6f44zav5ygvdwelkzspf6mtusttx47lft2z38sghk0w7')
})
})
11 changes: 11 additions & 0 deletions tests/extendedPairFromSeedWords.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expect, it } from 'vitest'
import { extendedPairFromSeedWords } from '../src/index'

describe('extendedPairFromSeedWords', () => {
it('should get extended keys pair from mnemonic', () => {
const { privateExtendedKey, publicExtendedKey } = extendedPairFromSeedWords('abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about')

expect(privateExtendedKey).toBe('xprv9z78fizET65qsCaRr1MSutTSGk1fcKfSt1sBqmuWShtkjRJJ4WCKcSnha6EmgNzFSsyom3MWtydHyPtJtSLZQUtictVQtM2vkPcguh6TQCH')
expect(publicExtendedKey).toBe('xpub6D6V5EX8HTe95getx2tTH2QApmrA1nPJFEnneAK813RjcDdSc3WaAF7BRNpTF7o7zXjVm3DD3VMX66jhQ7wLaZ9sS6NzyfiwfzqDZbxvpDN')
})
})
2 changes: 1 addition & 1 deletion tests/privateKeyFromSeedWords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DERIVATION_PATH } from '../src/constants'

describe('privateKeyFromSeedWords', () => {
it('should use the NIP-06 derivation path', () => {
expect(DERIVATION_PATH).toBe(`m/44'/1237'/0'/0/0`)
expect(`${DERIVATION_PATH}/0'/0/0`).toBe(`m/44'/1237'/0'/0/0`)
})

it('should get a 64 bytes hex private key from mnemonic', () => {
Expand Down
Loading