-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (166 loc) · 5.71 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
const Web3 = require('web3')
const util = require('ethereumjs-util')
const EthereumTx = require('ethereumjs-tx')
const abi = require('human-standard-token-abi')
const Table = require('cli-table2')
const opn = require('opn')
const clipMonit = require('clipboard-monitor')
const notifier = require('node-notifier')
const QrCode = require('qrcode-reader')
const Jimp = require("jimp")
const screenshot = require('screenshot-desktop')
const address = {
test: '0x0000000000000000000000000000000000000000',
prod: '0xffffffffffffffffffffffffffffffffffffffff',
}
const privateKey = {
[address.test]: new Buffer('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'hex'),
[address.prod]: new Buffer('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'hex'),
}
// const web3 = new Web3(new Web3.providers.HttpProvider('https://api.myetherapi.com/eth'))
// const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/1eqvdRndI0ePBx7ciN7u'))
// const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
const web3 = new Web3(new Web3.providers.HttpProvider('https://api.mycryptoapi.com/eth'))
web3.eth.defaultAccount = address.prod
const contractAddress = '0xf85fEea2FdD81d51177F6b8F35F0e6734Ce45F5F'
const token = web3.eth.contract(abi).at(contractAddress)
const ethToSend = 3
const cmtToSend = 7500
const main = async () => {
const nonce = web3.eth.getTransactionCount(web3.eth.defaultAccount)
const gasPrices = '60'
// showBalance()
console.log(`The outgoing transaction count for your wallet address is: ${nonce}`)
const monitor = clipMonit(100)
let timer
var addressPromise = new Promise(resolve => {
let firstCopy = true
monitor.on('copy', function (data) {
//do something with the data
if (firstCopy) {
firstCopy = false
return
}
if (web3.isAddress(data.trim())) {
resolve({
address: data.trim(),
src: '剪贴板'
})
}
})
timer = setInterval(async () => {
const t = Date.now()
const capture = await screenshot()
Jimp.read(capture, function (err, image) {
if (err) {
console.error(err)
return
}
var qr = new QrCode();
qr.callback = function (err, value) {
if (value && value.result) {
const qrAddress = value.result.replace('ethereum:', '').trim()
if (web3.isAddress(qrAddress)) {
resolve({
address: qrAddress,
src: '二维码'
})
}
}
}
qr.decode(image.bitmap)
})
}, 200)
})
const addressInfo = await addressPromise
sendMoney(addressInfo)
function sendMoney ({ address: sendTo, src }) {
const sendFrom = web3.eth.defaultAccount
console.log(sendTo)
monitor.end()
clearInterval(timer)
notifier.notify({
title: `发送成功,来源:${src}`,
message: sendTo
})
const details_eth = {
"to": sendTo,
"from": sendFrom,
"value": web3.toHex(web3.toWei(ethToSend, 'ether')),
"gasLimit": 200000,
"gasPrice": gasPrices * 1000000000, // converts the gwei price to wei
"nonce": nonce,
"chainId": 1 // EIP 155 chainId - mainnet: 1, rinkeby: 4
}
const cmtData = token.transfer.getData(sendTo, web3.toHex(web3.toWei(cmtToSend, 'ether')), {
from: sendFrom
})
const details_cmt = {
"to": contractAddress,
"from": sendFrom,
"value": web3.toHex(0),
"gasLimit": 200000,
"gasPrice": gasPrices * 1000000000, // converts the gwei price to wei
"nonce": nonce + 1,
"data": cmtData,
"chainId": 1 // EIP 155 chainId - mainnet: 1, rinkeby: 4
}
const transaction_eth = new EthereumTx(details_eth)
const transaction_cmt = new EthereumTx(details_cmt)
transaction_eth.sign(Buffer.from(privateKey[sendFrom], 'hex'))
transaction_cmt.sign(Buffer.from(privateKey[sendFrom], 'hex'))
const serializedTransaction_eth = transaction_eth.serialize()
const serializedTransaction_cmt = transaction_cmt.serialize()
const t = Date.now()
// web3.eth.sendRawTransaction('0x' + serializedTransaction_eth.toString('hex'), function (error, transactionId) {
// if (!error) {
// const url = `https://etherscan.io/tx/${transactionId}`
// console.log('Time ETH: ', Date.now() - t)
// console.log(`ETH: ${url}`)
// opn(url, {
// wait: false
// })
// } else {
// console.error(error)
// }
// })
web3.eth.sendRawTransaction('0x' + serializedTransaction_cmt.toString('hex'), function (error, transactionId) {
if (!error) {
const url = `https://etherscan.io/tx/${transactionId}`
console.log('Time CMT: ', Date.now() - t)
console.log(`CMT: ${url}`)
opn(url, {
wait: false
})
} else {
console.error(error)
}
})
}
// sendMoney({
// address: address.test,
// src: '手动'
// })
}
main()
function showBalance () {
const table = new Table({
head: ['Now', 'Name', 'ETH', 'CMT', 'Address']
})
const t = Date.now()
table.push([
address.test === web3.eth.defaultAccount && '✓' || '',
'Test',
web3.fromWei(web3.eth.getBalance(address.test).toNumber(), 'ether'),
web3.fromWei(token.balanceOf.call(address.test).toString(10), 'ether'),
address.test,
])
table.push([
address.prod === web3.eth.defaultAccount && '✓' || '',
'Prod',
web3.fromWei(web3.eth.getBalance(address.prod).toNumber(), 'ether'),
web3.fromWei(token.balanceOf.call(address.prod).toString(10), 'ether'),
address.prod,
])
console.log(table.toString(), `\nuse ${Date.now() - t}ms`)
}