diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index be7e0addba..cd343fbf68 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -234,6 +234,12 @@ host_functions! { pubkey: &[u8], ) -> HostResult; + /// The 32-byte ledger key (keylet) of an account's `AccountRoot`, computed from a + /// 20-byte account id. Reads the account region and writes the keylet. + #[gas = 350] + #[wasm_name = "accountroot_id"] + fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult; + /// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512. #[gas = 2000] #[wasm_name = "sha512_half"] diff --git a/crates/xrpl-host-functions/tests/generated_abi.rs b/crates/xrpl-host-functions/tests/generated_abi.rs index 26074bc2ce..bde6b3cf94 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -173,6 +173,15 @@ impl HostFunctions for FakeHost { Ok(i32::from(!signature.is_empty())) } + /// A keylet getter: reads an account, writes a 32-byte keylet; `InvalidAccount` + /// on an empty account. + fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + if account.is_empty() { + return Err(HostError::InvalidAccount); + } + put(out, &[account[0]; HASH_LEN]) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -259,6 +268,12 @@ fn the_trait_is_implementable() { ); assert_eq!(host.check_signature(b"msg", b"sig", b"pk"), Ok(1)); assert_eq!(host.check_signature(b"msg", b"", b"pk"), Ok(0)); + assert_eq!(host.account_keylet(&[7; 20], &mut out), Ok(HASH_LEN)); + assert_eq!(out[0], 7); + assert_eq!( + host.account_keylet(&[], &mut out), + Err(HostError::InvalidAccount) + ); assert_eq!(host.sha512_half(b"abc", &mut out), Ok(HASH_LEN)); assert_eq!(out[0], 3); assert_eq!(host.trace("hello", b"xy", true), Ok(())); @@ -345,6 +360,7 @@ fn the_spec_table_matches_the_declarations() { ("home_le_inner_arr_len", 70), ("le_inner_arr_len", 70), ("check_sig", 300), + ("accountroot_id", 350), ("sha512_half", 2000), ("trace", 500), ("trace_num", 500), diff --git a/crates/xrpl-wasm-vm-ffi/src/lib.rs b/crates/xrpl-wasm-vm-ffi/src/lib.rs index cdc220bffe..2f030715f4 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -260,6 +260,10 @@ mod ffi { pubkey: &[u8], ) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "accountKeylet"] + fn account_keylet(self: &HostContext, account: &[u8], out: &mut [u8]) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -407,6 +411,10 @@ impl HostFunctions for CxxHost<'_> { scalar(self.ctx.check_signature(message, signature, pubkey)) } + fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.account_keylet(account, out)) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { bytes_written(self.ctx.sha512_half(data, out)) } diff --git a/crates/xrpl-wasm-vm/src/abi.rs b/crates/xrpl-wasm-vm/src/abi.rs index 7b95bd69d6..daf03874c9 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -265,6 +265,9 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn account_keylet(&self, _account: &[u8], _out: &mut [u8]) -> HostResult { + unreachable!("no unit test in this module calls the host") + } fn sha512_half(&self, _data: &[u8], _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } diff --git a/crates/xrpl-wasm-vm/src/register.rs b/crates/xrpl-wasm-vm/src/register.rs index 1c5d2e9f57..2287d06396 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -324,6 +324,24 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::AccountKeylet => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + acc_ptr: i32, + acc_len: i32, + out_ptr: i32, + out_len: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::AccountKeylet, |c| { + let out = Region::new(out_ptr, out_len); + let account = Region::new(acc_ptr, acc_len); + write_buffered(c, out, |host, data, buf| { + host.account_keylet(account.read(data)?, buf) + }) + }) + }, + ), HostFunctionSpec::Sha512Half => linker.func_wrap( HOST_MODULE, op.wasm_name(), diff --git a/crates/xrpl-wasm-vm/tests/budgets.rs b/crates/xrpl-wasm-vm/tests/budgets.rs index 1d515a727c..cd4c52ba1f 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -144,6 +144,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $check_sig (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0))", 6, ), + HostFunctionSpec::AccountKeylet => ( + import::ACCOUNTROOT_ID, + "(call $accountroot_id (i32.const 0) (i32.const 20) (i32.const 32) (i32.const 32))", + 4, + ), HostFunctionSpec::Sha512Half => ( import::SHA512_HALF, "(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))", diff --git a/crates/xrpl-wasm-vm/tests/host_calls.rs b/crates/xrpl-wasm-vm/tests/host_calls.rs index 51aa5807e8..30e28cd705 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -359,6 +359,37 @@ fn check_sig_reads_all_three_regions_and_returns_the_verdict() { assert_eq!(status(&wat, &host), 0, "the invalid verdict"); } +/// A keylet getter: reads an account region and writes a 32-byte keylet back — the +/// read-input-write-output path. The account reaches the host and the keylet lands +/// where the guest asked. +#[test] +fn accountroot_id_reads_the_account_and_writes_the_keylet() { + // Guest memory is zeroed, so a 20-byte account read is all zeros. + let account = vec![0u8; 20]; + let host = + FakeHost::new().answering_account_keylet(account.clone(), support::Answer::filler(32)); + + let wat = module( + &[import::ACCOUNTROOT_ID, ONE_PAGE], + "(call $accountroot_id (i32.const 0) (i32.const 20) (i32.const 64) (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length"); + assert_eq!(*host.account_keylets_asked.borrow(), vec![account]); + + // The keylet bytes land at the output pointer: filler is 0, 1, 2, ..., so the + // first four load as 0x03020100. + let wat = module( + &[import::ACCOUNTROOT_ID, ONE_PAGE], + "(drop (call $accountroot_id (i32.const 0) (i32.const 20) (i32.const 64) (i32.const 64))) + (i32.load (i32.const 64))", + ); + assert_eq!( + status(&wat, &host), + 0x03020100, + "the first four keylet bytes" + ); +} + /// A leading scalar parameter reaches the host as declared. #[test] fn home_le_field_passes_the_field_selector_through() { diff --git a/crates/xrpl-wasm-vm/tests/preflight.rs b/crates/xrpl-wasm-vm/tests/preflight.rs index 97f589d763..2fac1dc7ab 100644 --- a/crates/xrpl-wasm-vm/tests/preflight.rs +++ b/crates/xrpl-wasm-vm/tests/preflight.rs @@ -98,7 +98,7 @@ fn a_disabled_feature_does_not_pass() { /// Every host function the ABI declares, spelled as a guest imports it. The count /// is asserted against the ABI so a function added to it cannot be left out here. -const ALL_IMPORTS: [&str; 22] = [ +const ALL_IMPORTS: [&str; 23] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -118,6 +118,7 @@ const ALL_IMPORTS: [&str; 22] = [ import::HOME_LE_INNER_ARR_LEN, import::LE_INNER_ARR_LEN, import::CHECK_SIG, + import::ACCOUNTROOT_ID, import::SHA512_HALF, import::TRACE, import::TRACE_NUM, diff --git a/crates/xrpl-wasm-vm/tests/support/mod.rs b/crates/xrpl-wasm-vm/tests/support/mod.rs index 0b8c3f1939..778205be91 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -179,6 +179,11 @@ pub struct FakeHost { pub sig_valid: HostResult, /// Every (message, signature, pubkey) `check_signature` was asked to verify. pub sigs_checked: RefCell, Vec, Vec)>>, + /// What `account_keylet` answers, by account bytes. An unlisted account answers + /// `InvalidAccount`. + pub account_keylets: HashMap, Answer>, + /// Every account `account_keylet` was asked for. + pub account_keylets_asked: RefCell>>, /// What `sha512_half` answers, whatever it is given. pub digest: Answer, /// Every field selector `get_current_ledger_obj_field` was asked for. @@ -233,6 +238,8 @@ impl Default for FakeHost { // Valid by default; the verification itself is the host's job, not the ABI's. sig_valid: Ok(1), sigs_checked: RefCell::new(Vec::new()), + account_keylets: HashMap::new(), + account_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -351,6 +358,11 @@ impl FakeHost { self } + pub fn answering_account_keylet(mut self, account: Vec, answer: Answer) -> FakeHost { + self.account_keylets.insert(account, answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -517,6 +529,16 @@ impl HostFunctions for FakeHost { self.sig_valid } + fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + self.account_keylets_asked + .borrow_mut() + .push(account.to_vec()); + match self.account_keylets.get(account) { + Some(answer) => answer.fill(out), + None => Err(HostError::InvalidAccount), + } + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { self.digested.borrow_mut().push(data.to_vec()); self.digest.fill(out) @@ -576,6 +598,7 @@ pub mod import { pub const HOME_LE_INNER_ARR_LEN: &str = r#"(import "host_lib" "home_le_inner_arr_len" (func $home_le_inner_arr_len (param i32 i32) (result i32)))"#; pub const LE_INNER_ARR_LEN: &str = r#"(import "host_lib" "le_inner_arr_len" (func $le_inner_arr_len (param i32 i32 i32) (result i32)))"#; pub const CHECK_SIG: &str = r#"(import "host_lib" "check_sig" (func $check_sig (param i32 i32 i32 i32 i32 i32) (result i32)))"#; + pub const ACCOUNTROOT_ID: &str = r#"(import "host_lib" "accountroot_id" (func $accountroot_id (param i32 i32 i32 i32) (result i32)))"#; pub const SHA512_HALF: &str = r#"(import "host_lib" "sha512_half" (func $sha512_half (param i32 i32 i32 i32) (result i32)))"#; pub const TRACE: &str = r#"(import "host_lib" "trace" (func $trace (param i32 i32 i32 i32 i32) (result i32)))"#; diff --git a/include/xrpl/tx/wasm/HostContext.h b/include/xrpl/tx/wasm/HostContext.h index 75ad57d064..62bbf4085e 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -120,6 +120,11 @@ public: rust::Slice signature, rust::Slice pubkey) const noexcept; + // The account id must be 20 bytes, else `InvalidParams`. Writes the 32-byte keylet. + [[nodiscard]] std::int32_t + accountKeylet(rust::Slice account, rust::Slice out) + const noexcept; + [[nodiscard]] std::int32_t sha512Half(rust::Slice data, rust::Slice out) const noexcept; diff --git a/src/libxrpl/tx/wasm/HostContext.cpp b/src/libxrpl/tx/wasm/HostContext.cpp index 61df2c6bf3..9cd9ab83bf 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -411,6 +412,22 @@ HostContext::checkSignature( }); } +std::int32_t +HostContext::accountKeylet(rust::Slice account, rust::Slice out) + const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + if (account.size() != AccountID::size()) + return hfErrorToInt(HostFunctionError::InvalidParams); + + auto const value = hostFunctions_.accountKeylet(AccountID::fromVoid(account.data())); + if (!value) + return hfErrorToInt(value.error()); + + return answer(out, value->data(), value->size()); + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept