-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
102 lines (91 loc) · 2.94 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
// Dependencies
var express = require('express')
var app = express()
var Tx = require('ethereumjs-tx')
var request = require('request');
// Init
app.set('port', (process.env.PORT || 5002))
app.use(express.static(__dirname + '/public'))
//
// Configureable credentials
//
// privateKey and publicKey are the senders addresses
// recievingAddress is the the secondary address
// hash is the SHA-256 hash you want to anchor
//
var privateKey = new Buffer('COMPLETE', 'hex')
var publicKey = 'COMPLETE'
var recievingAddress = 'COMPLETE'
var hash = 'COMPLETE'
var blockcypherKey = 'COMPLETE'
// Get the current TX count of the sending address
request({
url: 'https://api.blockcypher.com/v1/eth/main/addrs/' + publicKey + '/balance',
method: 'GET'
}, function(error, response, body) {
if (error) {
console.log(error);
} else {
// Parse response (current accout total tx's)
var getNonce = JSON.parse(body)
// Confirm the transaction in the console
console.log("Sending sending 0.0001 ETH to 0x" + recievingAddress)
console.log("With data: " + hash)
console.log("Getting gasPrice....")
// Get the current gasPrice of the network
request({
url: 'https://api.blockcypher.com/v1/eth/main',
method: 'GET'
}, function(error, response, body) {
if (error) {
console.log(error);
} else {
// Parse response (ETH network stats)
var getGasPrice = JSON.parse(body)
console.log("Building....")
// Intigers of gasLimit & gasPrice (20% higher for priority)
// 21000 is the default gasLimit network wide currently
var gasLimit = 21000 * 1.2;
var gasPrice = Math.round(getGasPrice.high_gas_price * 1.2)
// Convert to hex for rawTx
var gasPriceHex = gasPrice.toString(16)
var gasLimitHex = gasLimit.toString(16)
// Build the raw transaction as hex
// Subtract total TX address has recieved
var rawTx = {
nonce: parseInt(getNonce.final_n_tx) - 1,
gasPrice: '0x' + gasPriceHex,
gasLimit: '0x' + gasLimitHex,
to: '0x' + recievingAddress,
value: '0x5af3107a4000', // 0.0001 ETH (Hard set)
data: '0x' + hash
}
// Sign and seralize the raw transaction
console.log("Signing....")
var tx = new Tx(rawTx)
tx.sign(privateKey)
var serTx = tx.serialize().toString('hex')
// Broadcast the transaction with BlockCypher
blockCypherBroadcast(serTx)
}
})
}
});
// Push the serialized transaction to the network - API key in URL
function blockCypherBroadcast(serTx) {
console.log("Broadcasting....")
request({
url: 'https://api.blockcypher.com/v1/eth/main/txs/push?token=' + blockcypherKey,
method: 'POST',
json: {
tx: serTx
}
}, function(error, response, body) {
if (error) {
console.log(error);
} else {
console.log("Success!")
console.log(body);
}
});
}