-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (69 loc) · 2.96 KB
/
index.js
File metadata and controls
91 lines (69 loc) · 2.96 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Provides different ways to generate addresses
*/
const bitcoin = require('bitcoinjs-lib');
const isCompressed = (node) => (node.keyPair && node.keyPair.compressed) ? true : node.compressed;
export const pubKeyToP2PKH = (pubKey, network) => {
const scriptPubKey = bitcoin.script.pubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
return bitcoin.address.fromOutputScript(scriptPubKey, network)
}
export const pubKeyToP2SHP2WPKH = (pubKey, network) => {
const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey)),
scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript));
return bitcoin.address.fromOutputScript(scriptPubKey, network);
}
export const pubKeyToP2WPKH = (pubKey, network) => {
const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey))
return bitcoin.address.fromOutputScript(scriptPubKey, network)
}
export const getAllPossibleAddressesFromPubKey = (pubKey, network) => [
pubKeyToP2PKH(pubKey, network),
pubKeyToP2SHP2WPKH(pubKey, network),
pubKeyToP2WPKH(pubKey, network)
]
export const addressFunctionP2PKH = (node) => {
const pubKey = node.getPublicKeyBuffer();
return pubKeyToP2PKH(pubKey, node.getNetwork());
}
export const addressFunctionP2SHP2WPKH = (node) => {
if (!isCompressed(node)) {
throw('Cannot create P2SH-P2WPKH address from uncompressed key');
}
const pubKey = node.getPublicKeyBuffer();
return pubKeyToP2SHP2WPKH(pubKey, node.getNetwork());
}
export const addressFunctionP2WPKH = (node) => {
if (!isCompressed(node)) {
throw('Cannot create P2WPKH address from uncompressed key');
}
const pubKey = node.getPublicKeyBuffer();
return pubKeyToP2WPKH(pubKey, node.getNetwork());
}
export const signInputFunctionP2PKH = (sendTx, i, node) => {
sendTx.sign(i, node);
}
export const signInputFunctionP2SHP2WPKH = (sendTx, i, node, inputValue) => {
var pubKey = node.getPublicKeyBuffer();
var pubKeyHash = bitcoin.crypto.hash160(pubKey);
var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
sendTx.sign(i, node, redeemScript, null, inputValue);
}
export const signInputFunctionP2WPKH = (sendTx, i, node, inputValue) => {
sendTx.sign(i, node, null, null, inputValue);
}
export const addInputFunctionP2PKH = (sendTx, input, sequence) => {
sendTx.addInput(input.hash, input.index, sequence);
}
export const addInputFunctionP2SHP2WPKH = (sendTx, input, sequence) => {
sendTx.addInput(input.hash, input.index, sequence);
}
export const addInputFunctionP2WPKH = (sendTx, input, sequence, node) => {
const redeemScript = getP2WPKHRedeemScript(node);
sendTx.addInput(input.hash, input.index, sequence, redeemScript);
}
const getP2WPKHRedeemScript = (node) => {
const pubKey = node.getPublicKeyBuffer();
const pubKeyHash = bitcoin.crypto.hash160(pubKey);
const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
return redeemScript;
}