From d6a66d7249636134400fd7440dddd13f0831a781 Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Mon, 10 Aug 2026 21:41:38 -0400 Subject: [PATCH] feat: Hook up signers_id host function --- crates/xrpl-host-functions/src/lib.rs | 6 +++++ .../tests/generated_abi.rs | 15 ++++++++++++ crates/xrpl-wasm-vm-ffi/src/lib.rs | 8 +++++++ crates/xrpl-wasm-vm/src/abi.rs | 3 +++ crates/xrpl-wasm-vm/src/register.rs | 18 +++++++++++++++ crates/xrpl-wasm-vm/tests/budgets.rs | 5 ++++ crates/xrpl-wasm-vm/tests/host_calls.rs | 16 +++++++++++++ crates/xrpl-wasm-vm/tests/preflight.rs | 3 ++- crates/xrpl-wasm-vm/tests/support/mod.rs | 23 +++++++++++++++++++ include/xrpl/tx/wasm/HostContext.h | 5 ++++ src/libxrpl/tx/wasm/HostContext.cpp | 17 ++++++++++++++ 11 files changed, 118 insertions(+), 1 deletion(-) diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index 151a4ffbfe..0c85ed7944 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -386,6 +386,12 @@ host_functions! { out: &mut [u8], ) -> HostResult; + /// The 32-byte keylet of a `SignerList`, computed from its 20-byte owner account. + /// Reads the account region and writes the keylet. + #[gas = 350] + #[wasm_name = "signers_id"] + fn signer_list_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 3ba4d96e2d..bce0dbe243 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -360,6 +360,14 @@ impl HostFunctions for FakeHost { put(out, &[account[0]; HASH_LEN]) } + /// The account-only shape, for a `SignerList`. + fn signer_list_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; @@ -569,6 +577,12 @@ fn the_trait_is_implementable() { host.permissioned_domain_keylet(&[], 5, &mut out), Err(HostError::InvalidAccount) ); + assert_eq!(host.signer_list_keylet(&[7; 20], &mut out), Ok(HASH_LEN)); + assert_eq!(out[0], 7); + assert_eq!( + host.signer_list_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(())); @@ -671,6 +685,7 @@ fn the_spec_table_matches_the_declarations() { ("oracle_id", 350), ("paychan_id", 350), ("permissioned_domain_id", 350), + ("signers_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 02776c4b1e..1326364176 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -367,6 +367,10 @@ mod ffi { out: &mut [u8], ) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "signerListKeylet"] + fn signer_list_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; @@ -622,6 +626,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.permissioned_domain_keylet(account, seq, out)) } + fn signer_list_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.signer_list_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 a5bd7ce8c5..1a25377a03 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -366,6 +366,9 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn signer_list_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 22c4407d54..265563c46d 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -670,6 +670,24 @@ pub(crate) fn register_host_functions( ) }, ), + HostFunctionSpec::SignerListKeylet => 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::SignerListKeylet, |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.signer_list_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 e062f1c06c..07db191a31 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -224,6 +224,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $permissioned_domain_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))", 5, ), + HostFunctionSpec::SignerListKeylet => ( + import::SIGNERS_ID, + "(call $signers_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 09dfdd807e..026c830bdc 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -702,6 +702,22 @@ fn permissioned_domain_id_reads_the_account_and_seq() { assert_eq!(*host.domain_keylets_asked.borrow(), vec![(account, 5)]); } +/// An account-only keylet: the account reaches the host and the answered bytes land +/// where the guest asked, with no scalar in the shape. +#[test] +fn signers_id_reads_the_account() { + let account = vec![0u8; 20]; + let host = + FakeHost::new().answering_signer_list_keylet(account.clone(), support::Answer::filler(32)); + + let wat = module( + &[import::SIGNERS_ID, ONE_PAGE], + "(call $signers_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.signer_list_keylets_asked.borrow(), vec![account]); +} + /// 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 1542caf3c1..a05ce0f41f 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; 38] = [ +const ALL_IMPORTS: [&str; 39] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -134,6 +134,7 @@ const ALL_IMPORTS: [&str; 38] = [ import::ORACLE_ID, import::PAYCHAN_ID, import::PERMISSIONED_DOMAIN_ID, + import::SIGNERS_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 9f7c632b7e..0d48cb25aa 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -259,6 +259,11 @@ pub struct FakeHost { pub domain_keylets: HashMap<(Vec, i32), Answer>, /// Every (account, seq) `permissioned_domain_keylet` was asked for. pub domain_keylets_asked: RefCell, i32)>>, + /// What `signer_list_keylet` answers, by account bytes. An unlisted account + /// answers `InvalidAccount`. + pub signer_list_keylets: HashMap, Answer>, + /// Every account `signer_list_keylet` was asked for. + pub signer_list_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. @@ -345,6 +350,8 @@ impl Default for FakeHost { paychannel_keylets_asked: RefCell::new(Vec::new()), domain_keylets: HashMap::new(), domain_keylets_asked: RefCell::new(Vec::new()), + signer_list_keylets: HashMap::new(), + signer_list_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -620,6 +627,11 @@ impl FakeHost { self } + pub fn answering_signer_list_keylet(mut self, account: Vec, answer: Answer) -> FakeHost { + self.signer_list_keylets.insert(account, answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -974,6 +986,16 @@ impl HostFunctions for FakeHost { } } + fn signer_list_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + self.signer_list_keylets_asked + .borrow_mut() + .push(account.to_vec()); + match self.signer_list_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) @@ -1050,6 +1072,7 @@ pub mod import { pub const ORACLE_ID: &str = r#"(import "host_lib" "oracle_id" (func $oracle_id (param i32 i32 i32 i32 i32) (result i32)))"#; pub const PAYCHAN_ID: &str = r#"(import "host_lib" "paychan_id" (func $paychan_id (param i32 i32 i32 i32 i32 i32 i32) (result i32)))"#; pub const PERMISSIONED_DOMAIN_ID: &str = r#"(import "host_lib" "permissioned_domain_id" (func $permissioned_domain_id (param i32 i32 i32 i32 i32) (result i32)))"#; + pub const SIGNERS_ID: &str = r#"(import "host_lib" "signers_id" (func $signers_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 ef8028ea17..8adf9d4622 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -243,6 +243,11 @@ public: std::int32_t seq, rust::Slice out) const noexcept; + // The account id must be 20 bytes, else `InvalidParams`. Writes the 32-byte keylet. + [[nodiscard]] std::int32_t + signerListKeylet(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 b4f90c839b..e78b02d053 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -766,6 +766,23 @@ HostContext::permissionedDomainKeylet( }); } +std::int32_t +HostContext::signerListKeylet( + 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_.signerListKeylet(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