-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
143 lines (124 loc) · 4.1 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const { createConnection, Node } = require('./index');
const client = createConnection({
host: "HOST_ADDRESS",
port: 9332, // default litecoind port
user: "USER_NAME",
pass: "USER_PASS",
timeout: 30000,
ssl: false
});
const senderPK = "1ST_ACCOUNT_PRIVATE_KEY";
const senderAddress = "1ST_ACCOUNT_ADDRESS";
const alternativeSenderPK = "2ND_ACCOUNT_PRIVATE_KEY";
const destinationAddress = "2ND_ACCOUNT_ADDRESS";
const fee = 0.007;
const amount = 0.02;
const node = new Node(client, senderPK);
node.sendTransaction(senderAddress, destinationAddress, amount, fee)
.then(txid => console.log('Transaction id:', txid));
// ------------------------------- OLD ----------------------------------
// const litecoin = require('litecoin');
// const client = new litecoin.Client({
// host: "HOST_ADDRESS",
// port: 9332, // default litecoind port
// user: "USER_NAME",
// pass: "USER_PASS",
// timeout: 30000,
// ssl: false
// });
// MAIN NET
// private keys
// const account1privkey = "1ST_ACCOUNT_PRIVATE_KEY";
// const account2privkey = "2ND_ACCOUNT_PRIVATE_KEY";
// address
// const account1 = "1ST_ACCOUNT_ADDRESS";
// const account2 = "2ND_ACCOUNT_ADDRESS";
// -----
// const fee = 0.007;
// const amount = 0.02; // amount to be sent
// sendTransaction(account2, amount, account1, account1privkey, fee);
// function sendTransaction(recipient, amount, sender, senderPrivateKey) {
// const promise = new Promise(function(resolve, reject) {
// //checking balance
// client.listReceivedByAddress((err, accounts)=>{
// if (err) {
// reject(err);
// }
// let hasBalance = false;
// let balance = 0;
// for (i = 0; i < accounts.length; i++) {
// if (accounts[i].address == sender && (accounts[i].amount >= amount + fee)) {
// balance = accounts[i].amount;
// hasBalance = true;
// }
// }
// if (hasBalance) {
// resolve(balance);
// } else {
// reject("not enought funds");
// }
// });
// });
// promise.then((balance) => new Promise(function(resolve, reject) {
// //gathering unspent transactions
// client.listUnspent((err, ret) => {
// if (err) {
// reject(err);
// }
// console.log("listUnspent", ret);
// let txUnspent = [];
// let unspentAmount = 0;
// for (i = 0; i < ret.length; i++) {
// txUnspent.push({ txid: ret[i].txid, vout: ret[i].vout })
// unspentAmount += ret[i].amount;
// if (unspentAmount > amount + fee) {
// break;
// }
// }
// const rawChange = unspentAmount - amount - fee;
// const change = parseFloat(rawChange.toFixed(8));
// console.log(unspentAmount, balance, amount, fee, change);
// resolve([txUnspent, change]);
// });
// }))
// .then((txid_vOut) => new Promise(function(resolve, reject) {
// console.log('txid_vOut', txid_vOut);
// //crafting the raw transaction
// client.createRawTransaction(txid_vOut[0], { [recipient]: amount, [sender]: txid_vOut[1] }, (err, ret) => {
// if (err) {
// reject(err);
// }
// console.log('createRawTransaction', ret);
// client.decodeRawTransaction(ret, function(err, ret) {
// if (err) {
// return console.log('err', err);
// }
// console.log("tx decode", ret);
// });
// resolve(ret);
// });
// }))
// .then((rawTransaction) => new Promise(function(resolve, reject) {
// // signing the raw transaction
// client.importPrivKey(senderPrivateKey);
// client.signRawTransaction(rawTransaction, (err, ret) => {
// if (err) {
// reject(err);
// }
// console.log('signRawTransaction', ret);
// resolve(ret);
// });
// }))
// .then((signedRawTransaction) => new Promise( function(resolve, reject) {
// console.log('signedRawTransaction', signedRawTransaction);
// // submiting the signed transaction
// client.sendRawTransaction(signedRawTransaction.hex, (err, ret) => {
// if (err) {
// reject(err);
// }
// console.log('sendRawTransaction', ret);
// resolve(ret);
// });
// }))
// .then(txid => console.log("final txid", txid));
// }