diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index 8458fa4a5d..3d2eb392a6 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -328,6 +328,12 @@ host_functions! { out: &mut [u8], ) -> HostResult; + /// The 32-byte keylet of an `MPToken`, computed from a 24-byte MPT issuance id and + /// the 20-byte holder account. Reads both regions and writes the keylet. + #[gas = 500] + #[wasm_name = "mptoken_id"] + fn mptoken_keylet(&self, mptid: &[u8], holder: &[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 0b04c597cd..6d6ea9ba45 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -296,6 +296,18 @@ impl HostFunctions for FakeHost { put(out, &[issuer[0]; HASH_LEN]) } + /// A keylet from an MPT id and a holder; `InvalidParams` if the id is empty, + /// `InvalidAccount` if the holder is empty. + fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult { + if mptid.is_empty() { + return Err(HostError::InvalidParams); + } + if holder.is_empty() { + return Err(HostError::InvalidAccount); + } + put(out, &[mptid[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; @@ -457,6 +469,15 @@ fn the_trait_is_implementable() { host.mptoken_issuance_keylet(&[], 5, &mut out), Err(HostError::InvalidAccount) ); + assert_eq!( + host.mptoken_keylet(&[9; 24], &[8; 20], &mut out), + Ok(HASH_LEN) + ); + assert_eq!(out[0], 9); + assert_eq!( + host.mptoken_keylet(&[], &[8; 20], &mut out), + Err(HostError::InvalidParams) + ); 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(())); @@ -553,6 +574,7 @@ fn the_spec_table_matches_the_declarations() { ("escrow_id", 350), ("trustline_id", 400), ("mpt_issuance_id", 350), + ("mptoken_id", 500), ("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 8210042e49..9ca13ba351 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -327,6 +327,10 @@ mod ffi { out: &mut [u8], ) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "mptokenKeylet"] + fn mptoken_keylet(self: &HostContext, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -547,6 +551,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.mptoken_issuance_keylet(issuer, seq, out)) } + fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.mptoken_keylet(mptid, holder, 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 614d0216cd..873cd825d2 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -322,6 +322,14 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn mptoken_keylet( + &self, + _mptid: &[u8], + _holder: &[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 fcea025a70..253c1fea4d 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -542,6 +542,27 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::MptokenKeylet => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + mpt_ptr: i32, + mpt_len: i32, + holder_ptr: i32, + holder_len: i32, + out_ptr: i32, + out_len: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::MptokenKeylet, |c| { + let out = Region::new(out_ptr, out_len); + let mptid = Region::new(mpt_ptr, mpt_len); + let holder = Region::new(holder_ptr, holder_len); + write_buffered(c, out, |host, data, buf| { + host.mptoken_keylet(mptid.read(data)?, holder.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 43accf5194..847f0ce468 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -194,6 +194,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $mpt_issuance_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))", 5, ), + HostFunctionSpec::MptokenKeylet => ( + import::MPTOKEN_ID, + "(call $mptoken_id (i32.const 0) (i32.const 24) (i32.const 24) (i32.const 20) (i32.const 44) (i32.const 20))", + 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 456bcbccb5..1ea3d873db 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -589,6 +589,29 @@ fn mpt_issuance_id_reads_the_issuer_and_seq() { assert_eq!(*host.mpt_issuance_keylets_asked.borrow(), vec![(issuer, 5)]); } +/// A keylet from a 24-byte MPT id and a 20-byte holder: both reach the host as a +/// pair, and the keylet lands where the guest asked. +#[test] +fn mptoken_id_reads_the_mptid_and_holder() { + let mptid = vec![0u8; 24]; + let holder = vec![0u8; 20]; + let host = FakeHost::new().answering_mptoken_keylet( + mptid.clone(), + holder.clone(), + support::Answer::filler(32), + ); + + let wat = module( + &[import::MPTOKEN_ID, ONE_PAGE], + "(call $mptoken_id + (i32.const 0) (i32.const 24) + (i32.const 24) (i32.const 20) + (i32.const 64) (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length"); + assert_eq!(*host.mptoken_keylets_asked.borrow(), vec![(mptid, holder)]); +} + /// 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 01d167b607..e468a5deee 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; 32] = [ +const ALL_IMPORTS: [&str; 33] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -128,6 +128,7 @@ const ALL_IMPORTS: [&str; 32] = [ import::ESCROW_ID, import::TRUSTLINE_ID, import::MPT_ISSUANCE_ID, + import::MPTOKEN_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 52000126bb..1ec0684b2f 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -229,6 +229,11 @@ pub struct FakeHost { pub mpt_issuance_keylets: HashMap<(Vec, i32), Answer>, /// Every (issuer, seq) `mptoken_issuance_keylet` was asked for. pub mpt_issuance_keylets_asked: RefCell, i32)>>, + /// What `mptoken_keylet` answers, by (mptid, holder) bytes. An unlisted key answers + /// `InvalidParams`. + pub mptoken_keylets: HashMap<(Vec, Vec), Answer>, + /// Every (mptid, holder) `mptoken_keylet` was asked for. + pub mptoken_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. @@ -303,6 +308,8 @@ impl Default for FakeHost { trust_line_keylets_asked: RefCell::new(Vec::new()), mpt_issuance_keylets: HashMap::new(), mpt_issuance_keylets_asked: RefCell::new(Vec::new()), + mptoken_keylets: HashMap::new(), + mptoken_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -516,6 +523,16 @@ impl FakeHost { self } + pub fn answering_mptoken_keylet( + mut self, + mptid: Vec, + holder: Vec, + answer: Answer, + ) -> FakeHost { + self.mptoken_keylets.insert((mptid, holder), answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -805,6 +822,15 @@ impl HostFunctions for FakeHost { } } + fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult { + let key = (mptid.to_vec(), holder.to_vec()); + self.mptoken_keylets_asked.borrow_mut().push(key.clone()); + match self.mptoken_keylets.get(&key) { + Some(answer) => answer.fill(out), + None => Err(HostError::InvalidParams), + } + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { self.digested.borrow_mut().push(data.to_vec()); self.digest.fill(out) @@ -875,6 +901,7 @@ pub mod import { pub const ESCROW_ID: &str = r#"(import "host_lib" "escrow_id" (func $escrow_id (param i32 i32 i32 i32 i32) (result i32)))"#; pub const TRUSTLINE_ID: &str = r#"(import "host_lib" "trustline_id" (func $trustline_id (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))"#; pub const MPT_ISSUANCE_ID: &str = r#"(import "host_lib" "mpt_issuance_id" (func $mpt_issuance_id (param i32 i32 i32 i32 i32) (result i32)))"#; + pub const MPTOKEN_ID: &str = r#"(import "host_lib" "mptoken_id" (func $mptoken_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 0d430a425a..6597a9f366 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -194,6 +194,14 @@ public: std::int32_t seq, rust::Slice out) const noexcept; + // The MPT id must be 24 bytes and the holder 20, else `InvalidParams`. Writes the + // 32-byte keylet. + [[nodiscard]] std::int32_t + mptokenKeylet( + rust::Slice mptid, + rust::Slice holder, + 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 ba9bf3846f..a488b5fec5 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -644,6 +644,25 @@ HostContext::mptokenIssuanceKeylet( }); } +std::int32_t +HostContext::mptokenKeylet( + rust::Slice mptid, + rust::Slice holder, + rust::Slice out) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + if (mptid.size() != MPTID::size() || holder.size() != AccountID::size()) + return hfErrorToInt(HostFunctionError::InvalidParams); + + auto const value = hostFunctions_.mptokenKeylet( + MPTID::fromVoid(mptid.data()), AccountID::fromVoid(holder.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