-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (120 loc) · 4.82 KB
/
index.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var bitcoin = require('bitcoinjs-lib')
var crypto = require('crypto')
var affirm = require('affirm.js')
var base58 = require('bs58')
var fixed = require('mangler').fixed
var sinful = require('sinful-math.js')
module.exports = (function () {
var bitcoinutil = {}
bitcoinutil.isValidBitcoinAddress = function (address) {
var buffer = new Buffer(base58.decode(address))
var payload = buffer.slice(0, -4)
var checksum = buffer.slice(-4)
var newChecksum = sha256x2(payload).slice(0, 4)
for (var i = 0; i < newChecksum.length; ++i) {
if (newChecksum[i] !== checksum[i]) return false
}
return true
}
function sha256x2(buffer) {
buffer = crypto.createHash('sha256').update(buffer).digest()
return crypto.createHash('sha256').update(buffer).digest()
}
function getNetwork(network) {
return bitcoin.networks[network || 'bitcoin']
}
function inferNetworkFromPrivateKey(privateKeyWIF) {
var networkName = privateKeyWIF[0] === 'K' || privateKeyWIF[0] === 'L' ? 'bitcoin' : 'testnet'
return getNetwork(networkName)
}
function inferNetworkFromAddress(address) {
var networkName = address[0] === '1' ? 'bitcoin' : 'testnet'
return getNetwork(networkName)
}
bitcoinutil.toAddress = function (publicKey, network) {
network = getNetwork(network)
return bitcoin.ECPair.fromPublicKeyBuffer(fromHex(publicKey), network).getAddress()
}
bitcoinutil.addressFromPrivateKey = function (privateKey) {
var network = inferNetworkFromPrivateKey(privateKey)
var key = bitcoin.ECPair.fromWIF(privateKey, network)
return makeAddress(key)
}
function makeAddress(key) {
return {
address : key.getAddress(),
privateKey: key.toWIF(),
publicKey : toHex(key.getPublicKeyBuffer())
}
}
bitcoinutil.makeRandom = function (networkName) {
var network = getNetwork(networkName)
var key = bitcoin.ECPair.makeRandom()
key.network = network
return makeAddress(key)
}
bitcoinutil.getPublicKey = function (privateKey) {
var network = inferNetworkFromPrivateKey(privateKey)
var myPrivateKey = bitcoin.ECPair.fromWIF(privateKey, network)
return toHex(myPrivateKey.getPublicKeyBuffer())
}
function toHex(buffer) {
return buffer.toString('hex')
}
function fromHex(hex) {
return new Buffer(hex, 'hex')
}
bitcoinutil.getMultisigAddress = function (n, publickeys, networkName) {
affirm(networkName, 'Please provide network. possible values are "bitcoin" and "testnet".')
var network = getNetwork(networkName)
publickeys = Buffer.isBuffer(publickeys[0]) ? publickeys : publickeys.sort();
var addressBuffer = publickeys.map(function (address) {
return Buffer.isBuffer(address) ? address : fromHex(address)
})
var multisig = bitcoin.script.multisigOutput(n, addressBuffer)
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(multisig))
var multisigAddress = bitcoin.address.fromOutputScript(scriptPubKey, network)
return { address: multisigAddress, redeem: toHex(multisig) }
}
bitcoinutil.sign = function (txhex, privateKeyWIF, redeemHex, isIncomplete) {
affirm(txhex, "txhex is required")
affirm(privateKeyWIF, "privateKey is required")
var network = inferNetworkFromPrivateKey(privateKeyWIF)
var tx = bitcoin.Transaction.fromHex(txhex)
var txb = bitcoin.TransactionBuilder.fromTransaction(tx, network)
var redeem = redeemHex && fromHex(redeemHex)
var ecKey = bitcoin.ECPair.fromWIF(privateKeyWIF, network)
txb.inputs.forEach(function (input, index) {
txb.sign(index, ecKey, redeem)
})
return isIncomplete ? txb.buildIncomplete().toHex() : txb.build().toHex();
}
bitcoinutil.signMessage = function (privateKeyWIF, message) {
var network = inferNetworkFromPrivateKey(privateKeyWIF)
var key = bitcoin.ECPair.fromWIF(privateKeyWIF, network)
return bitcoin.message.sign(key, message, network).toString("base64")
}
bitcoinutil.verifyMessage = function (address, signature, message) {
var network = inferNetworkFromAddress(address)
return bitcoin.message.verify(address, signature, message, network)
}
bitcoinutil.hash160 = function (input) {
return bitcoin.crypto.hash160(new Buffer(input, "utf8")).toString('hex')
}
bitcoinutil.hash256 = function (input) {
return bitcoin.crypto.hash256(new Buffer(input, "utf8")).toString('hex')
}
bitcoinutil.satoshify = function (btc) {
// btc = fixed(btc)
return Math.round(sinful.mul(btc , 100000000))
}
bitcoinutil.btcfy = function (satoshi) {
satoshi = Math.round(satoshi)
return sinful.div(satoshi , 100000000)
}
bitcoinutil.getTxIdFromHex = function (tx) {
var transaction = bitcoin.Transaction.fromHex(tx)
return transaction.getId()
}
return bitcoinutil
})()