forked from PsychicCat/monero-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
129 lines (110 loc) · 3.72 KB
/
example.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
// Author: Interchained/Shopglobal https://github.com/shopglobal
// Connect with us on telegram https://t.me/electronero
// Year: 2018
// Contents: Nodejs Example for educational purposes. Not for production use.
// Do not use this exact example for production!
// This example is parts of a secure project, published for educational purposes.
// Security is important! Be safe!
// establish a connection with neroWallet
var neroWallet = require('./lib/wallet');
var Wallet = new neroWallet();
// examples
// checkers for timing
// integer checkers
var checkBalance = 0;
// boolean checkers
var serveAddress = false;
var serveBalance = false;
// default balance, address
var balance = 0;
var address = '';
function serveAddressFunction(){
Wallet.address().then((result) => {
if(result){
serveAddress = true
address = result.address;
addressText = "Electronero Address: " + address + '\n';
if(serveAddress == true){
// do something !
console.log(address);
console.log("Address Trigger: "+serveAddress);
console.log(addressText);
}
}
});
}
function serveBalanceFunction(){
Wallet.balance().then((result) => {
if(result){
// checker should equal 3
checkBalance++
balance = result.balance;
balanceText = "Electronero Balance: " + balance +" ETNX" + '\n';
if(checkBalance == 3){
// trugger balance actions at this stage
serveBalance = true;
console.log("Balance Counter: "+checkBalance);
console.log(balanceText);
}
if(serveBalance = true){
// log balance, or serve it, store it, move it, great..
console.log("Balance Trigger: " +serveBalance);
console.log("Electronero Balance: "+balance);
// do something else !
serveAddressFunction();
}
}
});
}
function storeBalanceFunction(){
Wallet.store().then((result) => {
if(result){
// checker should equal 2
checkBalance++
if(checkBalance == 2){
console.log("Balance Counter: "+checkBalance);
// blockchain was scanned, then balance was stored, let's serve it
// since we scanned and stored, lets run the serve balance function above
serveBalanceFunction()
}
}
});
}
function balanceFunction(){
Wallet.rescan().then((result) => {
if(result){
// checker should equal 1
checkBalance++
if(checkBalance == 1){
console.log("First to log"+"\n"+"Balance Counter: "+checkBalance);
// I want to serve balance, so I will rescan first, then store, then serve balance
// since we scanned, lets run the store function above
storeBalanceFunction()
}
}
});
}
function openWalletFunction(){
// remember to adjust this parameter in production, this is for example purposes
// the below command would open a passwordless Electronero wallet cache string called 'nero_wallet'
// REMEMBER TO NOT USE THIS IN PRODUCTION BY ANY MEANS. THIS IS AN EDUCATIONAL EXAMPLE
Wallet.open_wallet('nero_wallet', '').then((result) => {
if (result)
{
// ok we opened the wallet, so let's run the balance function and start scanning first
balanceFunction()
console.log(result);
}
});
}
function startFunction(){
// on initial run, this will create wallet if not exists
// remember to adjust this parameter in production, this is for example purposes
// the below command would generate a passwordless Electronero wallet cache string called 'nero_wallet'
// REMEMBER TO NOT USE THIS IN PRODUCTION BY ANY MEANS. THIS IS AN EDUCATIONAL EXAMPLE
Wallet.create_wallet('nero_wallet').then(function(result){
console.log(result);
});
// now that we created a wallet, lets open it
openWalletFunction();
}startFunction(); // deploy with startFunction. Since node is asynchronous this function will run first...