This post describes some simple differences between Hooks API for JSHooks and CHooks that are not yet documented.
For more information on the Hook API, please see this document.
JSHooks is currently under development, so this information is subject to change.
Hook API Arguments
CHooks takes a pointer and length as arguments.
JSHooks takes a buffer (number[] or hex string) as an argument.
// c
uint8_t value[32];
uint8_t key[2] = {'O', 'P'};
state(value, 32, key, 2);
// js
const key = "4F50"; // hex("OP")
state(key);
const key = [0x4F,0x50];
state(key);
Return value of Hook API
CHooks returns the number of bytes written (positive value) or the error code (negative value) as the return value.
JSHooks returns mostly buffer(number[]) or error code (negative value) as the return value.
// c
const result = state(value, 32, key, 2);
// result >= 0: success and bytecode size written
// result < 0: errorCode
// js
const result = state(key);
// typeof result !== 'number': success and value buffer
// typeof result === 'number': errorCode
Available Hook APIs
trace_num
Not available in JSHooks, use trace
instead
trace_float
Not available in JSHooks, use trace
instead.
otxn_json
Only available in JSHooks, get the transaction that invoked the Hook in JSON format.
// js
const tx = otxn_json();
/**
{
"TransactionType": "Payment",
"Account": "rN7n7otQDd6FazitYQeuPecJ9Du1rEnDr5",
"Destination": "rN7n7otQDd6FazitYQeuPecJ9Du1rEnDr5",
"Amount": "1000000",
"Fee": "10",
"Sequence": 1,
"LastLedgerSequence": 2,
"SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32E5A33F85BE12B9C2A",
"TxnSignature": "30450221008A2E4720667A4E64B87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D87564E1D8"
}
*/
sto_to_json
Only available in JSHooks, convert STObject in Buffer format to json format
// js
const sto = sto_to_json('2A664A653F6140000000000027108114934150EB9C9848F1526CA1BAABBF3BB8CF66743D');
/**
{
"Account": "rNRc2S2GSefSkTkAiyjE6LDzMonpeHp6jS",
"Amount": "10000",
"Expiration": 1716151615
}
*/
sto_from_json
Only available in JSHooks, convert STObject in JSON format to Buffer format
// js
const stobj = {
Account: "rNRc2S2GSefSkTkAiyjE6LDzMonpeHp6jS",
Amount: "10000",
Expiration: 1716151615
}
const sto = sto_from_json(stobj);
// [0x2A, 0x66, 0x4A, 0x65, 0x3F, 0x61, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x10, 0x81, 0x14, 0x93, 0x41, 0x50, 0xEB, 0x9C, 0x98, 0x48, 0xF1, 0x52, 0x6C, 0xA1, 0xBA, 0xAB, 0xBF, 0x3B, 0xB8, 0xCF, 0x66, 0x74, 0x3D]
Top comments (1)
Nice π
Loving how true to JS the implementation isβ¦