feat: Hook up deposit_preauth_id host function

This commit is contained in:
TimothyBanks
2026-08-10 20:45:52 -04:00
parent 3e113db4f5
commit a321a5dbfb
11 changed files with 182 additions and 1 deletions

View File

@@ -278,6 +278,18 @@ host_functions! {
out: &mut [u8],
) -> HostResult<usize>;
/// The 32-byte keylet of a `DepositPreauth`, computed from the 20-byte account and
/// the account it authorizes to deposit. Reads both account regions and writes the
/// keylet.
#[gas = 350]
#[wasm_name = "deposit_preauth_id"]
fn deposit_preauth_keylet(
&self,
account: &[u8],
authorize: &[u8],
out: &mut [u8],
) -> HostResult<usize>;
/// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512.
#[gas = 2000]
#[wasm_name = "sha512_half"]

View File

@@ -233,6 +233,22 @@ impl HostFunctions for FakeHost {
put(out, &[account[0]; HASH_LEN])
}
/// The same two-account shape, for a `DepositPreauth`.
fn deposit_preauth_keylet(
&self,
account: &[u8],
authorize: &[u8],
out: &mut [u8],
) -> HostResult<usize> {
if account.is_empty() || authorize.is_empty() {
return Err(HostError::InvalidAccount);
}
if account == authorize {
return Err(HostError::InvalidParams);
}
put(out, &[authorize[0]; HASH_LEN])
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
let mut digest = [0; HASH_LEN];
digest[0] = data.len() as u8;
@@ -355,6 +371,15 @@ fn the_trait_is_implementable() {
host.delegate_keylet(&[], &[8; 20], &mut out),
Err(HostError::InvalidAccount)
);
assert_eq!(
host.deposit_preauth_keylet(&[7; 20], &[8; 20], &mut out),
Ok(HASH_LEN)
);
assert_eq!(out[0], 8);
assert_eq!(
host.deposit_preauth_keylet(&[7; 20], &[7; 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(()));
@@ -446,6 +471,7 @@ fn the_spec_table_matches_the_declarations() {
("check_id", 350),
("credential_id", 350),
("delegate_id", 350),
("deposit_preauth_id", 350),
("sha512_half", 2000),
("trace", 500),
("trace_num", 500),

View File

@@ -291,6 +291,15 @@ mod ffi {
out: &mut [u8],
) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "depositPreauthKeylet"]
fn deposit_preauth_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;
@@ -472,6 +481,15 @@ impl HostFunctions for CxxHost<'_> {
bytes_written(self.ctx.delegate_keylet(account, authorize, out))
}
fn deposit_preauth_keylet(
&self,
account: &[u8],
authorize: &[u8],
out: &mut [u8],
) -> HostResult<usize> {
bytes_written(self.ctx.deposit_preauth_keylet(account, authorize, out))
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

@@ -291,6 +291,14 @@ mod tests {
) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn deposit_preauth_keylet(
&self,
_account: &[u8],
_authorize: &[u8],
_out: &mut [u8],
) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn sha512_half(&self, _data: &[u8], _out: &mut [u8]) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}

View File

@@ -432,6 +432,31 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::DepositPreauthKeylet => 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<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::DepositPreauthKeylet, |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.deposit_preauth_keylet(
account.read(data)?,
authorize.read(data)?,
buf,
)
})
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -169,6 +169,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $delegate_id (i32.const 0) (i32.const 20) (i32.const 20) (i32.const 20) (i32.const 40) (i32.const 32))",
6,
),
HostFunctionSpec::DepositPreauthKeylet => (
import::DEPOSIT_PREAUTH_ID,
"(call $deposit_preauth_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))",

View File

@@ -488,6 +488,31 @@ fn delegate_id_reads_both_accounts_and_writes_the_keylet() {
);
}
/// The same two-account keylet shape as delegate, with its own answer set.
#[test]
fn deposit_preauth_id_reads_both_accounts_and_writes_the_keylet() {
let account = vec![0u8; 20];
let authorize = vec![0u8; 20];
let host = FakeHost::new().answering_deposit_preauth_keylet(
account.clone(),
authorize.clone(),
support::Answer::filler(32),
);
let wat = module(
&[import::DEPOSIT_PREAUTH_ID, ONE_PAGE],
"(call $deposit_preauth_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.deposit_preauth_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() {

View File

@@ -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; 27] = [
const ALL_IMPORTS: [&str; 28] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
@@ -123,6 +123,7 @@ const ALL_IMPORTS: [&str; 27] = [
import::CHECK_ID,
import::CREDENTIAL_ID,
import::DELEGATE_ID,
import::DEPOSIT_PREAUTH_ID,
import::SHA512_HALF,
import::TRACE,
import::TRACE_NUM,

View File

@@ -204,6 +204,11 @@ pub struct FakeHost {
pub delegate_keylets: HashMap<(Vec<u8>, Vec<u8>), Answer>,
/// Every (account, authorize) `delegate_keylet` was asked for.
pub delegate_keylets_asked: RefCell<Vec<(Vec<u8>, Vec<u8>)>>,
/// What `deposit_preauth_keylet` answers, by (account, authorize) bytes. An
/// unlisted key answers `InvalidAccount`.
pub deposit_preauth_keylets: HashMap<(Vec<u8>, Vec<u8>), Answer>,
/// Every (account, authorize) `deposit_preauth_keylet` was asked for.
pub deposit_preauth_keylets_asked: RefCell<Vec<(Vec<u8>, Vec<u8>)>>,
/// What `sha512_half` answers, whatever it is given.
pub digest: Answer,
/// Every field selector `get_current_ledger_obj_field` was asked for.
@@ -268,6 +273,8 @@ impl Default for FakeHost {
credential_keylets_asked: RefCell::new(Vec::new()),
delegate_keylets: HashMap::new(),
delegate_keylets_asked: RefCell::new(Vec::new()),
deposit_preauth_keylets: HashMap::new(),
deposit_preauth_keylets_asked: RefCell::new(Vec::new()),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
digested: RefCell::new(Vec::new()),
@@ -433,6 +440,17 @@ impl FakeHost {
self
}
pub fn answering_deposit_preauth_keylet(
mut self,
account: Vec<u8>,
authorize: Vec<u8>,
answer: Answer,
) -> FakeHost {
self.deposit_preauth_keylets
.insert((account, authorize), answer);
self
}
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
self.digest = answer;
self
@@ -658,6 +676,22 @@ impl HostFunctions for FakeHost {
}
}
fn deposit_preauth_keylet(
&self,
account: &[u8],
authorize: &[u8],
out: &mut [u8],
) -> HostResult<usize> {
let key = (account.to_vec(), authorize.to_vec());
self.deposit_preauth_keylets_asked
.borrow_mut()
.push(key.clone());
match self.deposit_preauth_keylets.get(&key) {
Some(answer) => answer.fill(out),
None => Err(HostError::InvalidAccount),
}
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
self.digested.borrow_mut().push(data.to_vec());
self.digest.fill(out)
@@ -722,6 +756,7 @@ pub mod import {
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 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 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)))"#;

View File

@@ -157,6 +157,13 @@ public:
rust::Slice<std::uint8_t const> authorize,
rust::Slice<std::uint8_t> out) const noexcept;
// Both accounts must be 20 bytes, else `InvalidParams`. Writes the 32-byte keylet.
[[nodiscard]] std::int32_t
depositPreauthKeylet(
rust::Slice<std::uint8_t const> account,
rust::Slice<std::uint8_t const> authorize,
rust::Slice<std::uint8_t> out) const noexcept;
[[nodiscard]] std::int32_t
sha512Half(rust::Slice<std::uint8_t const> data, rust::Slice<std::uint8_t> out) const noexcept;

View File

@@ -546,6 +546,25 @@ HostContext::delegateKeylet(
});
}
std::int32_t
HostContext::depositPreauthKeylet(
rust::Slice<std::uint8_t const> account,
rust::Slice<std::uint8_t const> authorize,
rust::Slice<std::uint8_t> 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_.depositPreauthKeylet(
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<std::uint8_t const> data, rust::Slice<std::uint8_t> out)
const noexcept