-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpay.js
63 lines (62 loc) · 4.48 KB
/
pay.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
if (process.argv.length < 5)
{
console.log("Usage: node pay <source family seed> <amount xrp> <destination account>")
process.exit(1)
}
const keypairs = require('ripple-keypairs');
const secret = process.argv[2];
const address = keypairs.deriveAddress(keypairs.deriveKeypair(secret).publicKey)
const amount = BigInt(process.argv[3]) * 1000000n
const dest = process.argv[4];
const hook_account = dest;
const RippleAPI = require('ripple-lib').RippleAPI;
const fs = require('fs');
const api = new RippleAPI({server: 'ws://localhost:6005'});
api.on('error', (errorCode, errorMessage) => {console.log(errorCode + ': ' + errorMessage);});
api.on('connected', () => {console.log('connected');});
api.on('disconnected', (code) => {console.log('disconnected, code:', code);});
api.connect().then(() => {
let j = {
Account: address,
TransactionType: "Payment",
Amount: "" + amount,
Destination: dest,
Fee: "100"
}
api.prepareTransaction(j).then((x)=>
{
s = api.sign(x.txJSON, secret)
console.log(s)
api.submit(s.signedTransaction).then( response => {
console.log(response.resultCode, response.resultMessage);
let countdown = (x)=>{
if (x <= 0)
return console.log("")
process.stdout.write(x + "... ");
setTimeout(((x)=>{ return ()=>{countdown(x);} })(x-1), 1000);
};
countdown(6);
setTimeout(
((txnhash)=>{
return ()=>{
api.getTransaction(txnhash, {includeRawTransaction: true}).then(
x=>{
execs = JSON.parse(x.rawTransaction).meta.HookExecutions;
for (y in execs)
{
exec = execs[y].HookExecution;
if (exec.HookAccount == hook_account)
{
console.log("Hook Returned: ",
Buffer.from(exec.HookReturnString, 'hex').toString('utf-8'));
process.exit(0);
}
}
console.log("Could not find return from hook");
process.exit(1);
});
}
})(s.id), 6000);
}).catch (e=> { console.log(e) });
});
}).then(() => {}).catch(console.error);