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

View File

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

View File

@@ -1,3 +1,4 @@
all:
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
* @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,137 +1,234 @@
const fs = require('fs')
const xrpljs = require('xrpl-hooks');
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
module.exports = {
TestRig: (endpoint)=>
{
return new Promise((resolve, reject)=>
{
const api = new xrpljs.Client(endpoint);
const api = new xrpljs.Client(endpoint);
const execShell = cmd =>
{
return new Promise((resolve, reject) =>
const fee = (tx_blob) =>
{
exec(cmd, (error, stdout, stderr) =>
return new Promise((resolve, reject) =>
{
if (error)
let req = {command: 'fee'};
if (tx_blob)
req['tx_blob'] = tx_blob;
api.request(req).then(resp =>
{
console.log(error);
process.exit(2);
}
console.log("Ran cmd: `" + cmd + "`");
if (stdout)
console.log("stdout:", stdout);
if (stderr)
console.log("stderr:", stderr);
resolve([stdout, stderr]);
resolve(resp.result.drops);
}).catch(e =>
{
reject(e);
});
});
});
};
};
const assertTxnSuccess = x =>
{
if (!x || !x.result || x.result.engine_result_code != 0)
const assertTxnSuccess = x =>
{
console.log("Transaction failed:", x)
process.exit(1);
}
};
const assertTxnFailure = x =>
{
if (!x || !x.result || x.result.engine_result_code == 0)
{
console.log("Transaction failed:", x)
process.exit(1);
}
};
const err = (x) =>
{
console.log(x); process.exit(1);
}
const wasm = (x) =>
{
return fs.readFileSync(x).toString('hex').toUpperCase();
}
const genesis = xrpljs.Wallet.fromSeed('snoPBrXtMeMyMHUVTgbuqAfg1SUTb');
const randomAccount = ()=>
{
return xrpljs.Wallet.fromSeed(kp.generateSeed());
};
const findWasm = ()=>
{
return fs.readdirSync('.').filter(fn => fn.endsWith('.wasm'));
}
const fundFromGenesis = (acc) =>
{
return new Promise((resolve, reject) =>
{
if (typeof(acc) != 'string')
acc = acc.classicAddress;
api.submit({
Account: genesis.classicAddress, // fund account from genesis
TransactionType: "Payment",
Amount: "1000000000",
Destination: acc
}, {wallet: genesis}).then(x=>
if (!x || !x.result || x.result.engine_result_code != 0)
{
assertTxnSuccess(x);
resolve(x);
}).catch(err);
});
};
const pay = (seed, amt, dest) =>
{
return new Promise((resolve, reject) =>
console.log("Transaction failed:", x)
process.exit(1);
}
};
const assertTxnFailure = x =>
{
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=>
if (!x || !x.result || x.result.engine_result_code == 0)
{
assertTxnSuccess(x);
resolve(x);
}).catch(err);
});
};
console.log("Transaction failed:", x)
process.exit(1);
}
};
api.connect().then(()=>
{
resolve({
api: api,
xrpljs: xrpljs,
assertTxnSuccess: assertTxnSuccess,
assertTxnFailure: assertTxnFailure,
wasm: wasm,
kp: kp,
genesis: genesis,
randomAccount: randomAccount,
fundFromGenesis: fundFromGenesis,
err: err,
hsfOVERRIDE: 1,
hsfNSDELETE: 2,
pay: pay,
findWasm: findWasm,
execShell: execShell
});
}).catch(err);
const wasm = (x) =>
{
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 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 acc = xrpljs.Wallet.fromSeed(kp.generateSeed());
console.log(acc)
return acc
};
const pay_mock = (seed, amt, dest) =>
{
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) =>
{
return new Promise((resolve, reject) =>
{
if (typeof(acc) != 'string')
acc = acc.classicAddress;
api.submit({
Account: genesis.classicAddress, // fund account from genesis
TransactionType: "Payment",
Amount: "1000000000",
Destination: acc,
Fee: "10000"
}, {wallet: genesis}).then(x=>
{
assertTxnSuccess(x);
resolve();
}).catch(err);
});
};
const findWasm = ()=>
{
return fs.readdirSync('.').filter(fn => fn.endsWith('.wasm'));
};
api.connect().then(()=>
{
resolve({
rbc: rbc,
api: api,
xrpljs: xrpljs,
assertTxnSuccess: assertTxnSuccess,
assertTxnFailure: assertTxnFailure,
wasm: wasm,
kp: kp,
genesis: genesis,
randomAccount: randomAccount,
fundFromGenesis: fundFromGenesis,
err: err,
hsfOVERRIDE: 1,
hsfNSDELETE: 2,
hfsOVERRIDE: 1,
hfsNSDELETE: 2,
hookHash: hookHash,
pay: pay,
pay_mock: pay_mock,
fee: fee,
genesisseed: genesisseed,
genesisaddr: genesisaddr,
feeCompute: feeCompute,
feeSubmit: feeSubmit,
findWasm: findWasm
});
}).catch(err);
});
}
};

View File

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