-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestNet1.js
More file actions
294 lines (268 loc) · 12.9 KB
/
Copy pathtestNet1.js
File metadata and controls
294 lines (268 loc) · 12.9 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
require('dotenv').config();
const bitcoin = require('bitcoinjs-lib')
const ecc = require('tiny-secp256k1')
const ecpair = require('ecpair')
const ecpairFactory = ecpair.ECPairFactory(ecc)
const config = require('./config.json')
const dayjs = require("dayjs")
const HOME_TX = "tb1q6z36pfnefqq8k3ghe8wxmnqxf5t5kvtccx03gd"
const Client = require('bitcoin-core');
const {toXOnly} = require("./bitcoinjs-lib-master/src/psbt/bip371");
const client = new Client({
host: '192.168.50.95',
network: 'testnet',
username: process.env.login,
password: process.env.passw,
port: 18332
})
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const network = bitcoin.networks.testnet
let items = new Map()
let privateConfig = []
let txsCacheMemPool = []
bitcoin.initEccLib(ecc)
function generateConfig() {
for (const conf of config) {
const keyPairC = ecpairFactory.fromPrivateKey(Buffer.from(conf, 'hex'), {compressed: true})
const keyPairU = ecpairFactory.fromPrivateKey(Buffer.from(conf, 'hex'), {compressed: false})
const p2pkhC = bitcoin.payments.p2pkh({pubkey: keyPairC.publicKey, network: network,})
privateConfig.push({privateKey: conf, address: p2pkhC.address, script: "p2pkhC"})
console.log("p2pkhC:", p2pkhC.address)
const p2pkhU = bitcoin.payments.p2pkh({pubkey: keyPairU.publicKey, network: network,})
privateConfig.push({privateKey: conf, address: p2pkhU.address, script: "p2pkhU"})
const p2wpkh = bitcoin.payments.p2wpkh({pubkey: keyPairC.publicKey, network: network,})
privateConfig.push({privateKey: conf, address: p2wpkh.address, script: "p2wpkh"})
const p2tr = bitcoin.payments.p2tr({internalPubkey: keyPairC.publicKey.slice(1, 33), network: network,})
privateConfig.push({privateKey: conf, address: p2tr.address, script: "p2tr"})
}
// privateConfig.push({
// privateKey: "0000000000000000000000000000000000000000000000000000000000000001",
// address: "39j37jPTzHCJBC6tjRb8fRmhFrGzoh6ajk",
// script: "p2pkhC"
// })
console.log("PrivateConfig:", privateConfig.length)
}
async function read() {
while (1) {
let start = Date.now()
try {
let blockchainInfo = await client.getBlockchainInfo()
let blockStats = await client.getBlockStats(blockchainInfo.blocks)
let txsMemPool = await client.getRawMempool()
let txsNewMemPool = []
let date = dayjs(Date.now()).format('DD/MM/YYYY HH:mm');
for (let txMemPool of txsMemPool) if (!txsCacheMemPool.includes(txMemPool)) txsNewMemPool.push(txMemPool)
console.log(date, "Txs:", `${txsCacheMemPool.length}+${txsNewMemPool.length}`, "block:", blockchainInfo.blocks)
txsCacheMemPool = txsMemPool
for (let txMemPool of txsNewMemPool) {
try {
let rawTx = await client.getRawTransaction(txMemPool)
let decode = await client.decodeRawTransaction(rawTx)
//console.log("decode:",JSON.stringify(decode))
for (let vout of decode.vout) {
//console.log("vout:", JSON.stringify(vout))
if (vout.scriptPubKey.addresses !== undefined) {
for (let address of vout.scriptPubKey.addresses) {
//console.log(address)
let foundConfig = equals(address)
if (foundConfig && !items.has(decode.txid)) {
items.set(decode.txid, {
tx: decode,
foundConfig: foundConfig,
vout: vout,
rawTx: rawTx,
minfeerate: blockStats.minfeerate,
rival: false,
rivalAmount: 0,
my: false,
myAmount: 0
})
}
}
}
}
} catch (e) {
console.log("error:", JSON.stringify(e))
}
}
if (items.size > 0) {
for (let txMemPool of txsNewMemPool) {
try {
//console.log("txMemPool:", JSON.stringify(txMemPool))
let rawTx = await client.getRawTransaction(txMemPool)
let decode = await client.decodeRawTransaction(rawTx)
//console.log("decode:", JSON.stringify(decode))
for (let vin of decode.vin) {
//console.log(vin)
//console.log("txid:", vin.txid)
if (items.has(vin.txid) && decode.vout[0].scriptPubKey.addresses[0] !== HOME_TX) {
let rivalAmount = 0
for (let vout of decode.vout) rivalAmount = rivalAmount + vout.value
if (rivalAmount < items.get(decode.txid).rivalAmount) rivalAmount = items.get(decode.txid).rivalAmount
items.set(vin.txid, {rival: true, rivalAmount: rivalAmount})
}
if (items.has(vin.txid) && decode.vout[0].scriptPubKey.addresses[0] === HOME_TX) {
let myAmount = 0
for (let vout of decode.vout) myAmount = myAmount + vout.value
if (myAmount < items.get(decode.txid).myAmount) myAmount = items.get(decode.txid).myAmount
items.set(vin.txid, {my: true, myAmount: myAmount})
}
}
} catch (e) {
}
}
}
for (const key of items.keys()) {
if (!txsMemPool.includes(key)) {
console.log("del key:", key)
items.delete(key)
}
}
for (const item of items.values()) {
if (!item.rival && !item.my) await createFirst(item)
if (item.rival && !item.my) await createRival(item)
if (item.rival && item.my) await createRivalMy(item)
if (!item.rival && item.my) console.log("0 myTx only")
}
} catch (e) {
console.log("error:", JSON.stringify(e))
}
let workTime = Date.now() - start
if (workTime < 15000) await delay(15000 - workTime);
}
}
function equals(address) {
for (const conf of privateConfig) if (conf.address === address) return conf
return false
}
async function createFirst(item) {
console.log("0 createFirst")
console.log("1 tx:", JSON.stringify(item.tx))
console.log("2 vout:", JSON.stringify(item.vout))
console.log("3 foundConfig:", JSON.stringify(item.foundConfig))
console.log("4 rawTx:", item.rawTx)
console.log("5 minfeerate:", item.minfeerate)
try {
let keyPair
if (item.foundConfig.script === "p2pkhC") keyPair = ecpairFactory.fromPrivateKey(Buffer.from(item.foundConfig.privateKey, 'hex'), {compressed: true})
if (item.foundConfig.script === "p2pkhU") keyPair = ecpairFactory.fromPrivateKey(Buffer.from(item.foundConfig.privateKey, 'hex'), {compressed: false})
let fee = new bitcoin.Psbt({network})
if (item.foundConfig.script === "p2pkhC" || item.foundConfig.script === "p2pkhU") {
fee.addInput({
hash: item.tx.txid,
index: item.vout.n,
nonWitnessUtxo: Buffer.from(item.rawTx, 'hex')
})
} else if (item.foundConfig.script === "p2wpkh") {
fee.addInput({
hash: item.tx.txid,
index: item.vout.n,
witnessUtxo: {
script: Buffer.from('0020' + bitcoin.crypto.sha256(item.rawTx).toString('hex'), 'hex'),
value: item.vout.value,
}
})
} else if (item.foundConfig.script === "p2tr") {
fee.addInput({
hash: item.tx.txid,
index: item.vout.n,
witnessUtxo: {
script: Buffer.from('0020' + bitcoin.crypto.sha256(item.rawTx).toString('hex'), 'hex'),
value: item.vout.value,
},
tapInternalKey: toXOnly(item.foundConfig.publicKey),
})
}
fee.addOutput({
address: HOME_TX,
value: Math.floor(item.vout.value * 100000000)
})
fee.signInput(0, keyPair)
fee.finalizeAllInputs()
let length = fee.extractTransaction().toHex().length
let sendAmount = Math.floor(item.vout.value * 100000000 - length * item.minfeerate)
await send(item, sendAmount)
} catch (e) {
console.log("error:", JSON.stringify(e))
}
}
async function createRival(item) {
console.log("0 createRival")
console.log("1 tx:", JSON.stringify(item.tx))
console.log("2 vout:", JSON.stringify(item.vout))
console.log("3 foundConfig:", JSON.stringify(item.foundConfig))
console.log("4 rawTx:", item.rawTx)
console.log("5 minfeerate:", item.minfeerate)
let sendAmount = Math.floor((item.rivalAmount - 0.000001) * 100000000)
if (sendAmount > 0) await send(item, sendAmount)
else console.log("sendAmount error:", sendAmount)
}
async function createRivalMy(item) {
console.log("0 createRivalMy")
console.log("1 tx:", JSON.stringify(item.tx))
console.log("2 vout:", JSON.stringify(item.vout))
console.log("3 foundConfig:", JSON.stringify(item.foundConfig))
console.log("4 rawTx:", item.rawTx)
console.log("5 minfeerate:", item.minfeerate)
if (item.myAmount >= item.rivalAmount) {
let sendAmount = Math.floor((item.rivalAmount - 0.000001) * 100000000)
if (sendAmount > 0) await send(item, sendAmount)
else console.log("sendAmount error:", sendAmount)
} else console.log(item.vout.value, item.myAmount, ">=", item.rivalAmount)
}
async function send(item, sendAmount) {
try {
let keyPair
if (item.foundConfig.script === "p2pkhC") keyPair = ecpairFactory.fromPrivateKey(Buffer.from(item.foundConfig.privateKey, 'hex'), {compressed: true})
if (item.foundConfig.script === "p2pkhU") keyPair = ecpairFactory.fromPrivateKey(Buffer.from(item.foundConfig.privateKey, 'hex'), {compressed: false})
console.log("6 sendAmount", sendAmount)
let psbt = new bitcoin.Psbt({network})
if (item.foundConfig.script === "p2pkhC" || item.foundConfig.script === "p2pkhU") {
psbt.addInput({
hash: item.tx.txid,
index: item.vout.n,
nonWitnessUtxo: Buffer.from(item.rawTx, 'hex')
})
} else if (item.foundConfig.script === "p2wpkh") {
psbt.addInput({
hash: item.tx.txid,
index: item.vout.n,
witnessUtxo: {
script: Buffer.from('0020' + bitcoin.crypto.sha256(item.rawTx).toString('hex'), 'hex'),
value: item.vout.value,
}
})
} else if (item.foundConfig.script === "p2tr") {
psbt.addInput({
hash: item.tx.txid,
index: item.vout.n,
witnessUtxo: {
script: Buffer.from('0020' + bitcoin.crypto.sha256(item.rawTx).toString('hex'), 'hex'),
value: item.vout.value,
},
tapInternalKey: toXOnly(item.foundConfig.publicKey),
})
}
psbt.addOutput({
address: HOME_TX,
value: sendAmount
})
psbt.signInput(0, keyPair)
psbt.finalizeAllInputs()
console.log("7 extractTransaction:", psbt.extractTransaction().toHex())
let testMempoolAccept = await client.testMempoolAccept([psbt.extractTransaction().toHex()])
console.log("8 testMempoolAccept:", JSON.stringify(testMempoolAccept))
if (testMempoolAccept[0].allowed === true) {
let sendRawTransaction = await client.sendRawTransaction(psbt.extractTransaction().toHex())
item.myAmount = sendAmount
console.log("9 sendRawTransaction:", "" + sendRawTransaction)
}
} catch (e) {
console.log("error send:", JSON.stringify(e))
}
}
generateConfig()
read().then(r => console.log(r))
// let asd = await client.getTransactionByHash('b4dd08f32be15d96b7166fd77afd18aece7480f72af6c9c7f9c5cbeb01e686fe', { extension: 'json', summary: false });
// let asd = await client.getRawMempool(true)
// let asd = await client.prioritiseTransaction("408e0f7ce93dc7a00f9af63d4e83ed3ee3fb8ac1e6667f4818ddd6a4db67efb4", 0.0, 1000000)