-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbtcpck.js
61 lines (54 loc) · 1.67 KB
/
btcpck.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
50
51
52
53
54
55
56
57
58
59
60
61
var bitcoin = require('bitcoinjs-lib')
var bip39 = require('bip39')
var buffer = require('buffer')
const bip32 = require('bip32')
function generateMnemonic(size = 128) {
return bip39.generateMnemonic(size);
}
function validateMnemonic(mnemonic) {
return bip39.validateMnemonic(mnemonic);
}
function generateWalletInfo(mnemonic, index, isTestNet = false) {
let retNode = new Object();
if (validateMnemonic(mnemonic) == false) {
retNode.status = false;
retNode.msg = "error mnemonic: " + mnemonic;
return retNode;
}
if (index == null || index < 0) {
retNode.status = false;
retNode.msg = "error index " + index;
return retNode;
}
let seedHex = bip39.mnemonicToSeedSync(mnemonic, "");
let root = bip32.fromSeed(seedHex);
let child0 = root.derivePath("m/44'/0'/" + index);
let network = bitcoin.networks.bitcoin;
networkName = "bitcoin"
if (isTestNet) {
child0 = root.derivePath("m/44'/1'/" + index);
network = bitcoin.networks.testnet;
networkName = "testnet"
}
let keyPair = bitcoin.ECPair.fromPrivateKey(child0.privateKey, {
compressed: true,
network: network
});
let walletAddress = bitcoin.payments.p2pkh({
pubkey: child0.publicKey,
network
}).address;
retNode.status = true;
retNode.msg = "success";
retNode.result = { "index": index, "address": walletAddress, "pubkey": child0.publicKey.toString("hex"), "wif": keyPair.toWIF(), "network": networkName }
return retNode
}
module.exports = {
buffer,
bitcoin,
bip39,
bip32,
generateMnemonic,
validateMnemonic,
generateWalletInfo
}