use tiny-secp256k1, updated nip06 and other utils.

This commit is contained in:
fiatjaf
2021-12-29 14:29:43 -03:00
parent faa308049f
commit 60fc0d7940
7 changed files with 61 additions and 31 deletions

View File

@@ -1,17 +1,28 @@
import createHmac from 'create-hmac'
import randomBytes from 'randombytes'
import * as bip39 from 'bip39'
import {wordlist} from 'micro-bip39/wordlists/english'
import {
generateMnemonic,
mnemonicToSeedSync,
validateMnemonic
} from 'micro-bip39'
import BIP32Factory from 'bip32'
import * as ecc from 'tiny-secp256k1'
const bip32 = BIP32Factory(ecc)
export function privateKeyFromSeed(seed) {
let hmac = createHmac('sha512', Buffer.from('Nostr seed', 'utf8'))
hmac.update(seed)
return hmac.digest().slice(0, 32).toString('hex')
let root = bip32.fromSeed(Buffer.from(seed, 'hex'))
return root.derivePath(`m/44'/1237'/0'/0'`).privateKey.toString('hex')
}
export function seedFromWords(mnemonic) {
return bip39.mnemonicToSeedSync(mnemonic)
return Buffer.from(mnemonicToSeedSync(mnemonic, wordlist)).toString('hex')
}
export function generateSeedWords() {
return bip39.entropyToMnemonic(randomBytes(16).toString('hex'))
return generateMnemonic(wordlist)
}
export function validateWords(words) {
return validateMnemonic(words, wordlist)
}