diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index c263b243dc..b5fc0e7a00 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -290,6 +290,12 @@ host_functions! { out: &mut [u8], ) -> HostResult; + /// The 32-byte keylet of an account's `DID`, computed from its 20-byte account id. + /// Reads the account region and writes the keylet. + #[gas = 350] + #[wasm_name = "did_id"] + fn did_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 796856a729..06bd8bccdc 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -249,6 +249,14 @@ impl HostFunctions for FakeHost { put(out, &[authorize[0]; HASH_LEN]) } + /// A single-account keylet, for a `DID`. + fn did_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; @@ -380,6 +388,12 @@ fn the_trait_is_implementable() { host.deposit_preauth_keylet(&[7; 20], &[7; 20], &mut out), Err(HostError::InvalidParams) ); + assert_eq!(host.did_keylet(&[7; 20], &mut out), Ok(HASH_LEN)); + assert_eq!(out[0], 7); + assert_eq!( + host.did_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(())); @@ -472,6 +486,7 @@ fn the_spec_table_matches_the_declarations() { ("credential_id", 350), ("delegate_id", 350), ("deposit_preauth_id", 350), + ("did_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 f1fe6a41cc..34b4ad199e 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -300,6 +300,10 @@ mod ffi { out: &mut [u8], ) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "didKeylet"] + fn did_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; @@ -490,6 +494,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.deposit_preauth_keylet(account, authorize, out)) } + fn did_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.did_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 44f1269f55..b0336de410 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -299,6 +299,9 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn did_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 37721058b7..480913a75e 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -457,6 +457,24 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::DidKeylet => 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::DidKeylet, |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.did_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 c9ef1c97e9..f3d62bbb4a 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -174,6 +174,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $deposit_preauth_id (i32.const 0) (i32.const 20) (i32.const 20) (i32.const 20) (i32.const 40) (i32.const 32))", 6, ), + HostFunctionSpec::DidKeylet => ( + import::DID_ID, + "(call $did_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 f180aefedf..a462f8feaf 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -513,6 +513,20 @@ fn deposit_preauth_id_reads_both_accounts_and_writes_the_keylet() { ); } +/// A single-account keylet getter (like accountroot), with its own answer set. +#[test] +fn did_id_reads_the_account_and_writes_the_keylet() { + let account = vec![0u8; 20]; + let host = FakeHost::new().answering_did_keylet(account.clone(), support::Answer::filler(32)); + + let wat = module( + &[import::DID_ID, ONE_PAGE], + "(call $did_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.did_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 9edd436d58..dea1571b2c 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; 28] = [ +const ALL_IMPORTS: [&str; 29] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -124,6 +124,7 @@ const ALL_IMPORTS: [&str; 28] = [ import::CREDENTIAL_ID, import::DELEGATE_ID, import::DEPOSIT_PREAUTH_ID, + import::DID_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 38d7931d36..6eb69db451 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -209,6 +209,11 @@ pub struct FakeHost { pub deposit_preauth_keylets: HashMap<(Vec, Vec), Answer>, /// Every (account, authorize) `deposit_preauth_keylet` was asked for. pub deposit_preauth_keylets_asked: RefCell, Vec)>>, + /// What `did_keylet` answers, by account bytes. An unlisted account answers + /// `InvalidAccount`. + pub did_keylets: HashMap, Answer>, + /// Every account `did_keylet` was asked for. + pub did_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. @@ -275,6 +280,8 @@ impl Default for FakeHost { delegate_keylets_asked: RefCell::new(Vec::new()), deposit_preauth_keylets: HashMap::new(), deposit_preauth_keylets_asked: RefCell::new(Vec::new()), + did_keylets: HashMap::new(), + did_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -451,6 +458,11 @@ impl FakeHost { self } + pub fn answering_did_keylet(mut self, account: Vec, answer: Answer) -> FakeHost { + self.did_keylets.insert(account, answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -692,6 +704,14 @@ impl HostFunctions for FakeHost { } } + fn did_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult { + self.did_keylets_asked.borrow_mut().push(account.to_vec()); + match self.did_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) @@ -757,6 +777,8 @@ pub mod import { 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 DEPOSIT_PREAUTH_ID: &str = r#"(import "host_lib" "deposit_preauth_id" (func $deposit_preauth_id (param i32 i32 i32 i32 i32 i32) (result i32)))"#; + pub const DID_ID: &str = + r#"(import "host_lib" "did_id" (func $did_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 d7f0d8685f..41f424dd28 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -164,6 +164,11 @@ public: rust::Slice authorize, rust::Slice out) const noexcept; + // The account id must be 20 bytes, else `InvalidParams`. Writes the 32-byte keylet. + [[nodiscard]] std::int32_t + didKeylet(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 f340260982..1290462771 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -565,6 +565,22 @@ HostContext::depositPreauthKeylet( }); } +std::int32_t +HostContext::didKeylet(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_.didKeylet(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