-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
85 lines (55 loc) · 2.28 KB
/
index.mjs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {schnorr} from '@noble/curves/secp256k1'
import {bytesToHex} from '@noble/hashes/utils'
import {wordlist} from '@scure/bip39/wordlists/english'
import {generateMnemonic,mnemonicToSeedSync, validateMnemonic } from '@scure/bip39'
import {HDKey} from '@scure/bip32'
import {nip19} from 'nostr-tools'
import pkg from 'bitcore-lib';
const {PrivateKey} = pkg;
export function strGenerateSeedWords(){
return generateMnemonic(wordlist)
}
export function boolIsValidWords(words){
return validateMnemonic(words, wordlist)
}
function strSecKeyFromSeedWords(mnemonic, der_path, passphrase = "" ){
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
let privateKey = root.derive(der_path).privateKey
if (!privateKey) throw new Error('could not derive private key')
return bytesToHex(privateKey)
}
function getNostrPublicKey(privateKey) {
return bytesToHex(schnorr.getPublicKey(privateKey))
}
export function objNostrKeys(strSeedWord12, intAccount = 0, intChange = 0, intIndex = 0) {
if (boolIsValidWords(strSeedWord12) === false){
return {error: "Invalid seed words."}
}
if (intChange > 1 || intChange < 0 ){
return {error: "intChain is either 0 or 1."}
}
let objOut = {}
let strDerivationPath = `m/44'/1237'/${intAccount}'/${intChange}/${intIndex}`
objOut.nsecHex = strSecKeyFromSeedWords(strSeedWord12, strDerivationPath)
objOut.npubHex = getNostrPublicKey(objOut.nsecHex)
objOut.nsec = nip19.nsecEncode(objOut.nsecHex)
objOut.npub = nip19.npubEncode(objOut.npubHex)
objOut.strDerivationPath = strDerivationPath
return objOut
}
export function objCoinKeys(strSeedWord12, intCoin = 0, intAccount = 0, intChange = 0, intIndex = 0) {
if (boolIsValidWords(strSeedWord12) === false){
return {error: "Invalid seed words."}
}
if (intChange > 1 || intChange < 0 ){
return {error: "intChain is either 0 or 1."}
}
let objOut = {}
let strDerivationPath = `m/44'/${intCoin}'/${intAccount}'/${intChange}/${intIndex}`
objOut.nsecHex = strSecKeyFromSeedWords(strSeedWord12, strDerivationPath)
let privateKeyCoin = new PrivateKey(objOut.nsecHex)
objOut.strCoinPrivateKeyWIF = privateKeyCoin.toWIF()
objOut.strCoinAddress = privateKeyCoin.toAddress().toString()
objOut.strDerivationPath = strDerivationPath
return objOut
}