forked from EvernodeXRPL/evernode-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsethook.js
More file actions
152 lines (129 loc) · 4.81 KB
/
sethook.js
File metadata and controls
152 lines (129 loc) · 4.81 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
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
144
145
146
147
148
149
150
151
152
const fs = require('fs');
const process = require('process');
const xrpljs = require('xrpl-hooks');
const rbc = require('xrpl-binary-codec');
const hsfOVERRIDE = 1;
// const hsfNSDELETE = 2;
// const hfsOVERRIDE = 1;
// const hfsNSDELETE = 2;
// sha256('evernode.org|registry')
const NAMESPACE = '01EAF09326B4911554384121FF56FA8FECC215FDDE2EC35D9E59F2C53EC665A0'
const server = 'wss://hooks-testnet-v2.xrpl-labs.com';
const CONFIG_PATH = process.env.CONFIG_PATH || 'hook.json';
const WASM_PATH = process.env.WASM_PATH || "build"
const WASM_PATH_ZERO = `${WASM_PATH}/evernodezero.wasm`;
const WASM_PATH_ONE = `${WASM_PATH}/evernodeone.wasm`;
const WASM_PATH_TWO = `${WASM_PATH}/evernodetwo.wasm`;
console.log(WASM_PATH_ZERO, WASM_PATH_ONE, WASM_PATH_TWO);
let cfg;
if (!fs.existsSync(CONFIG_PATH)) {
cfg = {
"registry": {
"address": "",
"secret": ""
}
}
fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2));
}
else {
cfg = JSON.parse(fs.readFileSync(CONFIG_PATH));
}
const secret = cfg.registry.secret;
if (!secret) {
console.error("SETHOOK FAILED: Please specify hook secret in hook.json");
process.exit(1);
}
else {
const api = new xrpljs.Client(server);
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 account = xrpljs.Wallet.fromSeed(secret)
const binaryZero = fs.readFileSync(WASM_PATH_ZERO).toString('hex').toUpperCase();
const binaryOne = fs.readFileSync(WASM_PATH_ONE).toString('hex').toUpperCase();
const binaryTwo = fs.readFileSync(WASM_PATH_TWO).toString('hex').toUpperCase();
const hookTx = {
Account: account.classicAddress,
TransactionType: "SetHook",
Hooks:
[
{
Hook: {
CreateCode: binaryZero.slice(0, 194252),
HookOn: '0000000000000000',
HookNamespace: NAMESPACE,
HookApiVersion: 0,
Flags: hsfOVERRIDE
}
},
{
Hook: {
CreateCode: binaryOne.slice(0, 194252),
HookOn: '0000000000000000',
HookNamespace: NAMESPACE,
HookApiVersion: 0,
Flags: hsfOVERRIDE
}
},
{
Hook: {
CreateCode: binaryTwo.slice(0, 194252),
HookOn: '0000000000000000',
HookNamespace: NAMESPACE,
HookApiVersion: 0,
Flags: hsfOVERRIDE
}
}
]
};
submitTxn(secret, hookTx).then(res => { console.log(res); }).catch(console.error).finally(() => process.exit(0))
}