start updating example hooks

This commit is contained in:
Richard Holland
2022-05-23 10:19:57 +00:00
parent f74cc56acd
commit 7216df5829
6 changed files with 219 additions and 118 deletions

View File

@@ -17,7 +17,7 @@ require('../utils-tests.js').TestRig('ws://localhost:6005').then(t=>
} }
const secret = process.argv[2]; const secret = process.argv[2];
const account = t.xrpljs.Wallet.fromSeed(secret) const account = t.xrpljs.Wallet.fromSeed(secret)
t.api.submit( t.feeSubmit(process.argv[2],
{ {
Account: account.classicAddress, Account: account.classicAddress,
TransactionType: "SetHook", TransactionType: "SetHook",
@@ -31,9 +31,8 @@ require('../utils-tests.js').TestRig('ws://localhost:6005').then(t=>
Flags: t.hsfOVERRIDE Flags: t.hsfOVERRIDE
} }
} }
], ]
Fee: "100000" }).then(x=>
}, {wallet: account}).then(x=>
{ {
t.assertTxnSuccess(x) t.assertTxnSuccess(x)
console.log(x); console.log(x);

View File

@@ -1,3 +1,5 @@
all: all:
wasmcc carbon.c -o carbon.wasm -O0 -Wl,--allow-undefined -I../ wasmcc carbon.c -o carbon.wasm -O0 -Wl,--allow-undefined -I../
hook-cleaner carbon.wasm

View File

@@ -1,3 +1,4 @@
all: all:
wasmcc doubler.c -o doubler.wasm -O0 -Wl,--allow-undefined -I../ wasmcc doubler.c -o doubler.wasm -O0 -Wl,--allow-undefined -I../
hook-cleaner doubler.wasm

View File

@@ -209,7 +209,7 @@ extern int64_t ledger_last_hash (uint32_t write_ptr, uint32_t write_len);
* @param write_len The length of that buffer * @param write_len The length of that buffer
* @return The number of bytes written into the buffer of a negative integer if an error occured. * @return The number of bytes written into the buffer of a negative integer if an error occured.
*/ */
extern int64_t nonce (uint32_t write_ptr, uint32_t write_len); extern int64_t etxn_nonce (uint32_t write_ptr, uint32_t write_len);
/** /**

View File

@@ -1,8 +1,14 @@
const fs = require('fs') const fs = require('fs')
const xrpljs = require('xrpl-hooks'); const xrpljs = require('xrpl-hooks');
const kp = require('ripple-keypairs'); const kp = require('ripple-keypairs');
const { exec } = require('child_process'); const crypto = require('crypto')
const rbc = require('xrpl-binary-codec')
const err = (x) =>
{
console.log(x); process.exit(1);
}
// Fails via process.exit // Fails via process.exit
module.exports = { module.exports = {
TestRig: (endpoint)=> TestRig: (endpoint)=>
@@ -11,27 +17,25 @@ module.exports = {
{ {
const api = new xrpljs.Client(endpoint); const api = new xrpljs.Client(endpoint);
const execShell = cmd => const fee = (tx_blob) =>
{ {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
{ {
exec(cmd, (error, stdout, stderr) => let req = {command: 'fee'};
if (tx_blob)
req['tx_blob'] = tx_blob;
api.request(req).then(resp =>
{ {
if (error) resolve(resp.result.drops);
}).catch(e =>
{ {
console.log(error); reject(e);
process.exit(2);
}
console.log("Ran cmd: `" + cmd + "`");
if (stdout)
console.log("stdout:", stdout);
if (stderr)
console.log("stderr:", stderr);
resolve([stdout, stderr]);
}); });
}); });
}; };
const assertTxnSuccess = x => const assertTxnSuccess = x =>
{ {
if (!x || !x.result || x.result.engine_result_code != 0) if (!x || !x.result || x.result.engine_result_code != 0)
@@ -50,26 +54,124 @@ module.exports = {
} }
}; };
const err = (x) =>
{
console.log(x); process.exit(1);
}
const wasm = (x) => const wasm = (x) =>
{ {
return fs.readFileSync(x).toString('hex').toUpperCase(); if (x.slice(0,1) != '/')
x = 'wasm/' + x;
return fs.readFileSync( x).toString('hex').toUpperCase();
};
const feeCompute = (account_seed, txn_org) =>
{
return new Promise((resolve, reject) =>
{
txn_to_send = { ... txn_org };
txn_to_send['SigningPubKey'] = '';
let wal = xrpljs.Wallet.fromSeed(account_seed);
api.prepareTransaction(txn_to_send, {wallet: wal}).then(txn =>
{
let ser = rbc.encode(txn);
fee(ser).then(fees =>
{
let base_drops = fees.base_fee
delete txn_to_send['SigningPubKey']
txn_to_send['Fee'] = base_drops + '';
api.prepareTransaction(txn_to_send, {wallet: wal}).then(txn =>
{
resolve(txn);
}).catch(e=>{reject(e);});
}).catch(e=>{reject(e);});
}).catch(e=>{reject(e);});
});
} }
const genesis = xrpljs.Wallet.fromSeed('snoPBrXtMeMyMHUVTgbuqAfg1SUTb'); 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 genesisseed = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';
const genesisaddr = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const genesis = xrpljs.Wallet.fromSeed(genesisseed);
const randomAccount = ()=> const randomAccount = ()=>
{ {
return xrpljs.Wallet.fromSeed(kp.generateSeed()); const acc = xrpljs.Wallet.fromSeed(kp.generateSeed());
console.log(acc)
return acc
}; };
const findWasm = ()=> const pay_mock = (seed, amt, dest) =>
{ {
return fs.readdirSync('.').filter(fn => fn.endsWith('.wasm')); if (dest.classicAddress != undefined)
dest = dest.classicAddress;
return new Promise((resolve, reject) =>
{
let wal = xrpljs.Wallet.fromSeed(seed);
api.prepareTransaction({
Account: wal.classicAddress,
TransactionType: "Payment",
Amount: ''+amt,
Destination: dest,
SigningPubKey: ''
}, {wallet: wal}).then(txn =>
{
resolve(rbc.encode(txn));
}).catch(e=>
{
reject(e);
});
});
}
const pay = (seed, amt, dest) =>
{
if (dest.classicAddress != undefined)
dest = dest.classicAddress;
return new Promise((resolve, reject) =>
{
let wal = xrpljs.Wallet.fromSeed(seed);
api.submit({
Account: wal.classicAddress,
TransactionType: "Payment",
Amount: ''+amt,
Destination: dest,
Fee: "10000"
}, {wallet: wal}).then(x=>
{
assertTxnSuccess(x);
resolve(x);
}).catch(err);
});
};
const hookHash = fn =>
{
let b = fs.readFileSync('wasm/' + fn);
return crypto.createHash('SHA512').update(b).digest().slice(0,32).toString('hex').toUpperCase()
} }
const fundFromGenesis = (acc) => const fundFromGenesis = (acc) =>
@@ -83,38 +185,25 @@ module.exports = {
Account: genesis.classicAddress, // fund account from genesis Account: genesis.classicAddress, // fund account from genesis
TransactionType: "Payment", TransactionType: "Payment",
Amount: "1000000000", Amount: "1000000000",
Destination: acc Destination: acc,
Fee: "10000"
}, {wallet: genesis}).then(x=> }, {wallet: genesis}).then(x=>
{ {
assertTxnSuccess(x); assertTxnSuccess(x);
resolve(x); resolve();
}).catch(err); }).catch(err);
}); });
}; };
const pay = (seed, amt, dest) => const findWasm = ()=>
{ {
return new Promise((resolve, reject) => return fs.readdirSync('.').filter(fn => fn.endsWith('.wasm'));
{
let wal = xrpljs.Wallet.fromSeed(seed);
api.submit({
Account: wal.classicAddress, // fund account from genesis
TransactionType: "Payment",
Amount: ''+amt,
Destination: dest,
Fee: "10000"
}, {wallet: wal}).then(x=>
{
assertTxnSuccess(x);
resolve(x);
}).catch(err);
});
}; };
api.connect().then(()=> api.connect().then(()=>
{ {
resolve({ resolve({
rbc: rbc,
api: api, api: api,
xrpljs: xrpljs, xrpljs: xrpljs,
assertTxnSuccess: assertTxnSuccess, assertTxnSuccess: assertTxnSuccess,
@@ -127,9 +216,17 @@ module.exports = {
err: err, err: err,
hsfOVERRIDE: 1, hsfOVERRIDE: 1,
hsfNSDELETE: 2, hsfNSDELETE: 2,
hfsOVERRIDE: 1,
hfsNSDELETE: 2,
hookHash: hookHash,
pay: pay, pay: pay,
findWasm: findWasm, pay_mock: pay_mock,
execShell: execShell fee: fee,
genesisseed: genesisseed,
genesisaddr: genesisaddr,
feeCompute: feeCompute,
feeSubmit: feeSubmit,
findWasm: findWasm
}); });
}).catch(err); }).catch(err);
}); });

View File

@@ -1,2 +1,4 @@
guard_checker: guard_checker.cpp Guard.h Enum.h guard_checker: guard_checker.cpp Guard.h Enum.h
g++ -o guard_checker guard_checker.cpp --std=c++17 -g g++ -o guard_checker guard_checker.cpp --std=c++17 -g
install: guard_checker
cp guard_checker /usr/bin/