inkpad_seal/
storage.rs

1//! Storage functions
2use crate::derive::Host;
3use inkpad_derive::host;
4use inkpad_executor::{derive::Value, Error, Result, ReturnCode};
5use inkpad_sandbox::{Sandbox, StorageKey};
6use inkpad_std::vec;
7
8/// Retrieve the value under the given key from storage.
9#[host(seal0)]
10pub fn seal_get_storage(key_ptr: u32, out_ptr: u32, out_len_ptr: u32) -> Result<Option<Value>> {
11    let mut key: StorageKey = [0; 32];
12    sandbox.read_sandbox_memory_into_buf(key_ptr, &mut key)?;
13    if let Some(value) = sandbox.get_storage(&key)? {
14        sandbox.write_sandbox_output(out_ptr, out_len_ptr, &value)?;
15        Ok(Some(Value::I32(ReturnCode::Success as i32)))
16    } else {
17        Err(Error::ExecuteFailed(ReturnCode::KeyNotFound))
18    }
19}
20
21/// Clear the value at the given key in the contract storage.
22#[host(seal0)]
23pub fn seal_clear_storage(key_ptr: u32) -> Result<Option<Value>> {
24    let mut key: StorageKey = [0; 32];
25    sandbox.read_sandbox_memory_into_buf(key_ptr, &mut key)?;
26    if sandbox.set_storage(key, vec![]).is_ok() {
27        Ok(None)
28    } else {
29        Err(Error::SetStorageFailed)
30    }
31}
32
33/// Set the value at the given key in the contract storage.
34///
35/// The value length must not exceed the maximum defined by the contracts module parameters.
36/// Storing an empty value is disallowed.
37#[host(seal0)]
38pub fn seal_set_storage(key_ptr: u32, value_ptr: u32, value_len: u32) -> Result<Option<Value>> {
39    let mut key: StorageKey = [0; 32];
40    sandbox.read_sandbox_memory_into_buf(key_ptr, &mut key)?;
41    let value = sandbox.read_sandbox_memory(value_ptr, value_len)?;
42
43    sandbox.set_storage(key, value)?;
44
45    Ok(None)
46}