diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index f088d018d9..1cb3ca9cb4 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -267,6 +267,17 @@ host_functions! { out: &mut [u8], ) -> HostResult; + /// The 32-byte keylet of a `Delegate` object, computed from the 20-byte account + /// and the account it authorizes. Reads both account regions and writes the keylet. + #[gas = 350] + #[wasm_name = "delegate_id"] + fn delegate_keylet( + &self, + account: &[u8], + authorize: &[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 8ab48d1525..ee929b2cf1 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -216,6 +216,23 @@ impl HostFunctions for FakeHost { put(out, &[subject[0]; HASH_LEN]) } + /// A keylet from two accounts; `InvalidAccount` if either is empty, `InvalidParams` + /// if they are equal. + fn delegate_keylet( + &self, + account: &[u8], + authorize: &[u8], + out: &mut [u8], + ) -> HostResult { + if account.is_empty() || authorize.is_empty() { + return Err(HostError::InvalidAccount); + } + if account == authorize { + return Err(HostError::InvalidParams); + } + 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; @@ -329,6 +346,15 @@ fn the_trait_is_implementable() { host.credential_keylet(&[], &[8; 20], b"cred", &mut out), Err(HostError::InvalidAccount) ); + assert_eq!( + host.delegate_keylet(&[7; 20], &[8; 20], &mut out), + Ok(HASH_LEN) + ); + assert_eq!(out[0], 7); + assert_eq!( + host.delegate_keylet(&[], &[8; 20], &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(())); @@ -419,6 +445,7 @@ fn the_spec_table_matches_the_declarations() { ("amm_id", 450), ("check_id", 350), ("credential_id", 350), + ("delegate_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 210a192ed1..82db4c7c8f 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -282,6 +282,15 @@ mod ffi { out: &mut [u8], ) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "delegateKeylet"] + fn delegate_keylet( + self: &HostContext, + account: &[u8], + authorize: &[u8], + out: &mut [u8], + ) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -454,6 +463,15 @@ impl HostFunctions for CxxHost<'_> { ) } + fn delegate_keylet( + &self, + account: &[u8], + authorize: &[u8], + out: &mut [u8], + ) -> HostResult { + bytes_written(self.ctx.delegate_keylet(account, authorize, 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 f8d7b535e1..3072b32028 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -283,6 +283,14 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn delegate_keylet( + &self, + _account: &[u8], + _authorize: &[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 8034d7bf1e..e1cb00795d 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -411,6 +411,27 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::DelegateKeylet => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + acc_ptr: i32, + acc_len: i32, + auth_ptr: i32, + auth_len: i32, + out_ptr: i32, + out_len: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::DelegateKeylet, |c| { + let out = Region::new(out_ptr, out_len); + let account = Region::new(acc_ptr, acc_len); + let authorize = Region::new(auth_ptr, auth_len); + write_buffered(c, out, |host, data, buf| { + host.delegate_keylet(account.read(data)?, authorize.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 97ab613b50..5cdba9a128 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -164,6 +164,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $credential_id (i32.const 0) (i32.const 20) (i32.const 20) (i32.const 20) (i32.const 40) (i32.const 4) (i32.const 44) (i32.const 20))", 8, ), + HostFunctionSpec::DelegateKeylet => ( + import::DELEGATE_ID, + "(call $delegate_id (i32.const 0) (i32.const 20) (i32.const 20) (i32.const 20) (i32.const 40) (i32.const 32))", + 6, + ), 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 994aa87608..efa5fd8b82 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -462,6 +462,32 @@ fn credential_id_reads_subject_issuer_and_type() { ); } +/// A two-account keylet getter: both accounts reach the host as a pair, and the +/// keylet lands where the guest asked. +#[test] +fn delegate_id_reads_both_accounts_and_writes_the_keylet() { + let account = vec![0u8; 20]; + let authorize = vec![0u8; 20]; + let host = FakeHost::new().answering_delegate_keylet( + account.clone(), + authorize.clone(), + support::Answer::filler(32), + ); + + let wat = module( + &[import::DELEGATE_ID, ONE_PAGE], + "(call $delegate_id + (i32.const 0) (i32.const 20) + (i32.const 20) (i32.const 20) + (i32.const 64) (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length"); + assert_eq!( + *host.delegate_keylets_asked.borrow(), + vec![(account, authorize)] + ); +} + /// 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 d968769999..f5c877f318 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; 26] = [ +const ALL_IMPORTS: [&str; 27] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -122,6 +122,7 @@ const ALL_IMPORTS: [&str; 26] = [ import::AMM_ID, import::CHECK_ID, import::CREDENTIAL_ID, + import::DELEGATE_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 300ddd3047..707aaf0f5f 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -199,6 +199,11 @@ pub struct FakeHost { pub credential_keylets: HashMap<(Vec, Vec, Vec), Answer>, /// Every (subject, issuer, type) `credential_keylet` was asked for. pub credential_keylets_asked: RefCell, Vec, Vec)>>, + /// What `delegate_keylet` answers, by (account, authorize) bytes. An unlisted key + /// answers `InvalidAccount`. + pub delegate_keylets: HashMap<(Vec, Vec), Answer>, + /// Every (account, authorize) `delegate_keylet` was asked for. + pub delegate_keylets_asked: RefCell, Vec)>>, /// What `sha512_half` answers, whatever it is given. pub digest: Answer, /// Every field selector `get_current_ledger_obj_field` was asked for. @@ -261,6 +266,8 @@ impl Default for FakeHost { check_keylets_asked: RefCell::new(Vec::new()), credential_keylets: HashMap::new(), credential_keylets_asked: RefCell::new(Vec::new()), + delegate_keylets: HashMap::new(), + delegate_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -416,6 +423,16 @@ impl FakeHost { self } + pub fn answering_delegate_keylet( + mut self, + account: Vec, + authorize: Vec, + answer: Answer, + ) -> FakeHost { + self.delegate_keylets.insert((account, authorize), answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -627,6 +644,20 @@ impl HostFunctions for FakeHost { } } + fn delegate_keylet( + &self, + account: &[u8], + authorize: &[u8], + out: &mut [u8], + ) -> HostResult { + let key = (account.to_vec(), authorize.to_vec()); + self.delegate_keylets_asked.borrow_mut().push(key.clone()); + match self.delegate_keylets.get(&key) { + 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) @@ -690,6 +721,7 @@ pub mod import { pub const AMM_ID: &str = r#"(import "host_lib" "amm_id" (func $amm_id (param i32 i32 i32 i32 i32 i32) (result i32)))"#; pub const CHECK_ID: &str = r#"(import "host_lib" "check_id" (func $check_id (param i32 i32 i32 i32 i32) (result i32)))"#; pub const CREDENTIAL_ID: &str = r#"(import "host_lib" "credential_id" (func $credential_id (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))"#; + pub const DELEGATE_ID: &str = r#"(import "host_lib" "delegate_id" (func $delegate_id (param i32 i32 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 f39302dd31..684d249fd0 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -150,6 +150,13 @@ public: rust::Slice credentialType, rust::Slice out) const noexcept; + // Both accounts must be 20 bytes, else `InvalidParams`. Writes the 32-byte keylet. + [[nodiscard]] std::int32_t + delegateKeylet( + rust::Slice account, + rust::Slice authorize, + 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 5bd1e3fe3f..277ce42e74 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -527,6 +527,25 @@ HostContext::credentialKeylet( }); } +std::int32_t +HostContext::delegateKeylet( + rust::Slice account, + rust::Slice authorize, + rust::Slice out) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + if (account.size() != AccountID::size() || authorize.size() != AccountID::size()) + return hfErrorToInt(HostFunctionError::InvalidParams); + + auto const value = hostFunctions_.delegateKeylet( + AccountID::fromVoid(account.data()), AccountID::fromVoid(authorize.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