This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
243 lines (229 loc) · 8.58 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const bitcoin = require('bitcoinjs-lib')
const request = require('request')
const networks = require('./networks')
const MIN_RELAY_FEE = 1000
const DEFAULT_SAT_PER_BYTE = 10
const DERIVE_PATH = "m/49'/2'/0'"
function SegwitDepositUtils (options) {
if (!(this instanceof SegwitDepositUtils)) return new SegwitDepositUtils(options)
let self = this
self.options = Object.assign({}, options || {})
if (!self.options.insightUrl) {
self.options.insightUrl = 'https://insight.litecore.io/api/'
console.log('WARN: Using default bitcoin block explorer. It is highly suggested you set one yourself!', self.options.insightUrl)
}
if (!self.options.feePerKb) {
self.options.feePerByte = DEFAULT_SAT_PER_BYTE
}
if (!self.options.network || (self.options.network === 'mainnet')) {
self.options.network = networks.mainnet
if (!self.options.backupBroadcastUrl) {
self.options.backupBroadcastUrl = 'https://ltc1.trezor.io/api/sendtx/'
}
} else if (self.options.network === 'testnet') {
self.options.network = networks.testnet
if (!self.options.backupBroadcastUrl) {
self.options.backupBroadcastUrl = 'https://ltc1.trezor.io/api/sendtx/'
}
} else {
throw new Error('Invalid network provided ' + self.options.network)
}
// if (!self.options.password) throw new Error('SegwitDepositUtils: password required')
return self
}
SegwitDepositUtils.prototype.bip44 = function (xpub, path) {
let self = this
let node = bitcoin.HDNode.fromBase58(xpub, self.options.network)
let nodeDerivation = node.derive(0).derive(path)
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(nodeDerivation.getPublicKeyBuffer()))
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
return bitcoin.address.fromOutputScript(scriptPubKey, self.options.network)
}
SegwitDepositUtils.prototype.getPrivateKey = function (xprv, path) {
let self = this
if (!xprv) throw new Error('Xprv is null. Bad things will happen to you.')
// create the hd wallet
const node = bitcoin.HDNode.fromBase58(xprv, self.options.network)
let child = node.derivePath(DERIVE_PATH)
let nodeDerivation = child.derive(0).derive(path)
return nodeDerivation.keyPair.toWIF()
}
SegwitDepositUtils.prototype.privateToPublic = function (privateKey) {
let self = this
var keyPair = bitcoin.ECPair.fromWIF(privateKey, self.options.network)
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keyPair.getPublicKeyBuffer()))
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
return bitcoin.address.fromOutputScript(scriptPubKey, self.options.network)
}
SegwitDepositUtils.prototype.generateNewKeys = function (entropy) {
let self = this
var root = bitcoin.HDNode.fromSeedHex(entropy, self.options.network)
return {
xprv: root.toBase58(),
xpub: self.getXpubFromXprv(root.toBase58())
}
}
SegwitDepositUtils.prototype.getXpubFromXprv = function (xprv) {
let self = this
let node = bitcoin.HDNode.fromBase58(xprv, self.options.network)
let child = node.derivePath(DERIVE_PATH)
// let derivedPubKey = key.derive("m/44'/60'/0'/0").hdPublicKey
return child.neutered().toBase58()
}
SegwitDepositUtils.prototype.getBalance = function (address, done) {
let self = this
let url = self.options.insightUrl + 'addr/' + address
request.get({json: true, url: url}, function (err, response, body) {
if (!err && response.statusCode !== 200) {
return done(new Error('Unable to get balance from ' + url))
} else {
done(null, {balance: body.balance, unconfirmedBalance: body.unconfirmedBalance})
}
})
}
SegwitDepositUtils.prototype.getUTXOs = function (xpub, path, done) {
let self = this
let address = self.bip44(xpub, path)
// console.log('sweeping ', address)
let url = self.options.insightUrl + 'addr/' + address + '/utxo'
request.get({json: true, url: url}, function (err, response, body) {
if (!err && response.statusCode !== 200) {
return done(new Error('Unable to get UTXOs from ' + url + ' - status ' + response.statusCode))
} else if (body.length === 0) {
return done(new Error('Unable to get UTXOs from ' + url + ' - empty response'))
} else {
let cleanUTXOs = []
body.forEach(function (utxo) {
delete utxo['confirmations']
delete utxo['height']
delete utxo['ts']
cleanUTXOs.push(utxo)
})
if (self.options.network === networks.testnet) {
console.log('TESTNET ENABLED: Clipping UTXO length to 2 for test purposes')
cleanUTXOs = cleanUTXOs.slice(0, 2)
}
done(null, cleanUTXOs)
}
})
}
SegwitDepositUtils.prototype.getSweepTransaction = function (xprv, path, to, utxo, feePerByte) {
let self = this
const txb = new bitcoin.TransactionBuilder(self.options.network)
let totalBalance = 0
if (utxo.length === 0) {
return new Error('no UTXOs')
}
utxo.forEach(function (spendable) {
totalBalance += spendable.satoshis
txb.addInput(spendable.txid, spendable.vout) // alice1 unspent
})
if (!feePerByte) feePerByte = self.options.feePerByte
let txfee = estimateTxFee(feePerByte, utxo.length, 1, true)
if (txfee < MIN_RELAY_FEE) txfee = MIN_RELAY_FEE
if ((totalBalance - txfee) < txfee) return new Error('Balance too small to sweep!' + totalBalance + ' ' + txfee)
txb.addOutput(to, totalBalance - txfee)
let keyPair = bitcoin.HDNode.fromBase58(xprv, self.options.network).derivePath(DERIVE_PATH).derive(0).derive(path).keyPair
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keyPair.getPublicKeyBuffer()))
for (let i = 0; i < utxo.length; i++) {
txb.sign(i,
keyPair,
redeemScript,
null, // Null for simple Segwit
utxo[i].satoshis
)
}
return { signedTx: txb.build().toHex(), txid: txb.build().getId() }
}
SegwitDepositUtils.prototype.broadcastTransaction = function (txObject, done, retryUrl, originalResponse) {
let self = this
let textBody = txObject.signedTx
let url
if (retryUrl) url = retryUrl
else url = 'https://ltc1.trezor.io/api/sendtx/'
var options = {
url: url,
method: 'POST',
body: textBody
}
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
txObject.broadcasted = true
done(null, txObject)
} else {
if (url !== retryUrl) { // First broadcast attempt. Lets try again.
self.broadcastTransaction(txObject, done, self.options.backupBroadcastUrl, body)
} else {
// Second attempt failed
done(new Error('unable to broadcast. Some debug info: ' + body.toString() + ' ---- ' + originalResponse.toString()))
}
}
})
}
SegwitDepositUtils.prototype.sweepTransaction = function (xpub, xprv, path, to, feePerByte, done) {
let self = this
self.getUTXOs(xpub, path, function (err, utxo) {
if (err) return done(err)
let signedTx = self.getSweepTransaction(xprv, path, to, utxo, feePerByte)
self.broadcastTransaction(signedTx, done)
})
}
/**
* Estimate size of transaction a certain number of inputs and outputs.
* This function is based off of ledger-wallet-webtool/src/TransactionUtils.js#estimateTransactionSize
*/
function estimateTxSize (inputsCount, outputsCount, handleSegwit) {
var maxNoWitness,
maxSize,
maxWitness,
minNoWitness,
minSize,
minWitness,
varintLength
if (inputsCount < 0xfd) {
varintLength = 1
} else if (inputsCount < 0xffff) {
varintLength = 3
} else {
varintLength = 5
}
if (handleSegwit) {
minNoWitness =
varintLength + 4 + 2 + 59 * inputsCount + 1 + 31 * outputsCount + 4
maxNoWitness =
varintLength + 4 + 2 + 59 * inputsCount + 1 + 33 * outputsCount + 4
minWitness =
varintLength +
4 +
2 +
59 * inputsCount +
1 +
31 * outputsCount +
4 +
106 * inputsCount
maxWitness =
varintLength +
4 +
2 +
59 * inputsCount +
1 +
33 * outputsCount +
4 +
108 * inputsCount
minSize = (minNoWitness * 3 + minWitness) / 4
maxSize = (maxNoWitness * 3 + maxWitness) / 4
} else {
minSize = varintLength + 4 + 146 * inputsCount + 1 + 31 * outputsCount + 4
maxSize = varintLength + 4 + 148 * inputsCount + 1 + 33 * outputsCount + 4
}
return {
min: minSize,
max: maxSize
}
}
function estimateTxFee (satPerByte, inputsCount, outputsCount, handleSegwit) {
const { min, max } = estimateTxSize(inputsCount, outputsCount, handleSegwit)
const mean = Math.ceil((min + max) / 2)
return mean * satPerByte
}
module.exports = SegwitDepositUtils