-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.js
133 lines (115 loc) · 3.95 KB
/
common.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
const xrpljs = require('xrpl-hooks');
const rbc = require('xrpl-binary-codec');
const https = require('https');
const appenv = {
hsfOVERRIDE: 1,
hsfNSDELETE: 2,
hfsOVERRIDE: 1,
hfsNSDELETE: 2,
NAMESPACE: '01EAF09326B4911554384121FF56FA8FECC215FDDE2EC35D9E59F2C53EC665A0', // sha256('evernode.org|registry')
CONFIG_PATH: process.env.CONFIG_PATH || 'hook.json',
WASM_DIR_PATH: process.env.WASM_PATH || "build",
PARAM_STATE_HOOK_KEY: '4556520100000000000000000000000000000000000000000000000000000001',
DEFINITIONS_URL: 'https://raw.githubusercontent.com/EvernodeXRPL/evernode-resources/main/definitions/definitions.json',
NETWORK: 'mainnet',
NETWORK_ID: null
}
let api = {};
const init = async (network = null) => {
const definitions = await getDefinitions();
network = network ? network : appenv.NETWORK;
const def = definitions[network];
if (!def)
throw `Invalid network: ${network}`;
appenv.NETWORK_ID = def.networkID;
api = new xrpljs.Client(def.rippledServer);
}
const getDefinitions = async () => {
return new Promise((resolve, reject) => {
https.get(appenv.DEFINITIONS_URL, res => {
let data = [];
if (res.statusCode != 200)
reject(`Error: ${res}`);
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
resolve(JSON.parse(data));
});
}).on('error', err => {
reject(`Error: ${err}`);
});
});
}
const fee = (txBlob) => {
return new Promise((resolve, reject) => {
let req = { command: 'fee' };
if (txBlob)
req['tx_blob'] = txBlob;
api.request(req).then(resp => {
resolve(resp.result.drops);
}).catch(e => {
reject(e);
});
});
};
const feeCompute = (accountSeed, txnOrg) => {
return new Promise((resolve, reject) => {
let txnToSend = { ...txnOrg };
txnToSend['SigningPubKey'] = '';
let wal = xrpljs.Wallet.fromSeed(accountSeed);
api.prepareTransaction(txnToSend, { wallet: wal }).then(txn => {
let ser = rbc.encode(txn);
fee(ser).then(fees => {
let baseDrops = fees.base_fee
delete txnToSend['SigningPubKey']
txnToSend['Fee'] = baseDrops + '';
api.prepareTransaction(txnToSend, { wallet: wal }).then(txn => {
resolve(txn);
}).catch(e => { reject(e); });
}).catch(e => { reject(e); });
}).catch(e => { reject(e); });
});
}
const feeSubmit = (seed, txn) => {
return new Promise((resolve, reject) => {
feeCompute(seed, txn).then(txn => {
api.submit(txn,
{ wallet: xrpljs.Wallet.fromSeed(seed) }).then(s => {
resolve(s);
}).catch(e => { reject(e); });
}).catch(e => { reject(e); });
});
}
const submitTxn = (seed, txn) => {
return new Promise((resolve, reject) => {
api.connect().then(() => {
feeSubmit(seed, txn).then(res => {
if (res?.result?.engine_result === 'tesSUCCESS')
resolve(res?.result?.engine_result);
else
reject(res);
}).catch(e => { reject(e); });
}).catch(e => { reject(e); });
});
}
const getHookHashes = (account) => {
return new Promise((resolve, reject) => {
api.connect().then(() => {
let req = { command: 'account_objects' };
if (account)
req['account'] = account;
api.request(req).then(resp => {
resolve(resp.result.account_objects.find(o => o.Hooks && o.Hooks.length)?.Hooks?.map(h => h?.Hook?.HookHash).filter(h => h));
}).catch(e => {
reject(e);
});
});
});
};
module.exports = {
init,
submitTxn,
getHookHashes,
appenv
}