-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.js
49 lines (34 loc) · 1.13 KB
/
wallet.js
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
//Hd wallet as specified by BIP-39 and BIP-32
const bitcoin = require('bitcoinjs-lib')
const bip32 = require('bip32')
const bip39 = require('bip39')
const fs = require('fs')
// which bitcoin network to use , regtest here
const network = bitcoin.networks.regtest
//derivation paths contains information about key structure as well what child level there is
const path = 'm/44\'/1\'/0\'/0'
let mnemonicWords = bip39.generateMnemonic()
let seed = bip39.mnemonicToSeedSync(mnemonicWords)
let root = bip32.fromSeed(seed,network)
let account = root.derivePath(path)
let node = account.derive(1).derive(0)
let address = bitcoin.payments.p2pkh({
pubkey:node.publicKey,
network:network
}).address
let fullWallet ={
mnemonic: mnemonicWords,
seed:seed.toString('hex'),
pubkey:node.publicKey.toString('hex'),
address:address
}
// write seed and mnemonic to a file as well as other info to a file
fullWallet = JSON.stringify(fullWallet)
fs.writeFile("wallet.json",fullWallet,(err)=>{
if(err){
console.log(err)
}
})
//TODO
//Restore wallet from mnemonic code word
// const RestorWallet=(mnemonic)=>{}