diff options
Diffstat (limited to 'src/nostr.js')
| -rw-r--r-- | src/nostr.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/nostr.js b/src/nostr.js index 731c076..4b7f5c3 100644 --- a/src/nostr.js +++ b/src/nostr.js @@ -9,7 +9,8 @@ // Sync is on by default (relay.coinos.io) but can be turned off or pointed at // other relays in Settings; the preference lives in localStorage. -import * as nip06 from 'nostr-tools/nip06'; +import { HDKey } from '@scure/bip32'; +import { seedSync } from './seed.js'; import * as nip44 from 'nostr-tools/nip44'; import * as nip04 from 'nostr-tools/nip04'; import { getPublicKey, finalizeEvent, generateSecretKey, verifyEvent } from 'nostr-tools/pure'; @@ -650,13 +651,21 @@ export { getPublicKey, finalizeEvent, generateSecretKey }; // The nostr identity a seed carries (NostrSync#load's key, without the // wallet) — so the account switcher can put a face on every account on the -// device, not just the one that's open. Memoized: nip06 stretches the seed. +// device, not just the one that's open. Memoized: the seed is stretched. +// NIP-06: m/44'/1237'/<account>'/0/0 of the BIP-39 seed — what nostr-tools' +// privateKeyFromSeedWords derives, from the seed the wallet already stretched +// (seed.js) instead of stretching the words a second time. +export function nostrKeyFromSeed(mnemonic, passphrase = '', accountIndex = 0) { + const sk = HDKey.fromMasterSeed(seedSync(mnemonic, passphrase || '')).derive(`m/44'/1237'/${accountIndex || 0}'/0/0`).privateKey; + if (!sk) throw new Error('could not derive private key'); + return sk; +} const _seedPks = new Map(); export function seedPubkey(mnemonic, passphrase = '', accountIndex = 0) { const k = `${mnemonic}\n${passphrase}\n${accountIndex}`; if (_seedPks.has(k)) return _seedPks.get(k); let pk = null; - try { pk = getPublicKey(nip06.privateKeyFromSeedWords(mnemonic, passphrase || undefined, accountIndex || 0)); } catch {} + try { pk = getPublicKey(nostrKeyFromSeed(mnemonic, passphrase, accountIndex)); } catch {} _seedPks.set(k, pk); return pk; } @@ -839,8 +848,8 @@ export class NostrSync { load(mnemonic, passphrase = '', accountIndex = 0) { // a derived sibling wallet (same seed, higher BIP84 account) gets its own - // nostr identity and sync slots — nip06 shifts by account index - this.sk = nip06.privateKeyFromSeedWords(mnemonic, passphrase || undefined, accountIndex || 0); + // nostr identity and sync slots — NIP-06 shifts by account index + this.sk = nostrKeyFromSeed(mnemonic, passphrase, accountIndex); this.pk = getPublicKey(this.sk); this.ck = nip44.getConversationKey(this.sk, this.pk); } |
