Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/rs-platform-wallet-ffi/src/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! FFI bindings for document create operations on `IdentityWallet`.

use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;

use dpp::document::DocumentV0Getters;
use dpp::prelude::Identifier;
use rs_sdk_ffi::{SignerHandle, VTableSigner};

use crate::check_ptr;
use crate::error::*;
use crate::handle::*;
use crate::runtime::block_on_worker;
use crate::types::read_identifier;
use crate::{unwrap_option_or_return, unwrap_result_or_return};

/// Create + broadcast a new document on `contract_id`'s
/// `document_type_name`, owned by `owner_identity_id`, signed via the
/// external `signer_handle`.
///
/// Goes through `IdentityWallet::create_document_with_signer`, which
/// fetches the on-chain contract, builds a revision-1 document from the
/// supplied `properties_json`, selects an AUTHENTICATION + ECDSA key
/// from the in-process `IdentityManager` whose security level satisfies
/// the document type's requirement, broadcasts on the platform-wallet
/// 8 MB worker stack (required to avoid the GroveDB proof-verification
/// stack overflow), and waits for the confirmed document.
///
/// On success the confirmed document's 32-byte id is written to
/// `out_document_id`. The signature never crosses into Swift logic —
/// it routes back through the supplied `signer_handle` (typically
/// `KeychainSigner.handle`); the caller retains ownership of the
/// signer.
///
/// `properties_json` is a NUL-terminated UTF-8 JSON object keyed by
/// property name. Byte-array fields are passed as hex (or base64)
/// strings and identifier fields as base58 (or hex) strings; the
/// schema-driven sanitize step on the Rust side converts them to the
/// protocol's native types. Pass `"{}"` for a document type with no
/// required properties.
#[no_mangle]
pub unsafe extern "C" fn platform_wallet_create_document_with_signer(
wallet_handle: Handle,
owner_identity_id: *const u8,
contract_id: *const u8,
document_type_name: *const c_char,
properties_json: *const c_char,
signer_handle: *mut SignerHandle,
out_document_id: *mut u8,
) -> PlatformWalletFFIResult {
check_ptr!(signer_handle);
check_ptr!(document_type_name);
check_ptr!(properties_json);
check_ptr!(out_document_id);

let owner_id = unwrap_result_or_return!(read_identifier(owner_identity_id));
let contract_id_value = unwrap_result_or_return!(read_identifier(contract_id));

let document_type_str =
unwrap_result_or_return!(CStr::from_ptr(document_type_name).to_str()).to_string();
let properties_str = unwrap_result_or_return!(CStr::from_ptr(properties_json).to_str());

let signer_addr = signer_handle as usize;
let owner_id_for_async = owner_id;
let contract_id_for_async = contract_id_value;

let option = PLATFORM_WALLET_STORAGE.with_item(wallet_handle, |wallet| {
let identity_wallet = wallet.identity().clone();
let result: Result<Identifier, _> = block_on_worker(async move {
let signer: &VTableSigner = &*(signer_addr as *const VTableSigner);
identity_wallet
.create_document_with_signer(
&owner_id_for_async,
&contract_id_for_async,
&document_type_str,
properties_str,
signer,
)
.await
.map(|document| document.id())
});
result
});
let result = unwrap_option_or_return!(option);
let document_id = unwrap_result_or_return!(result);
let bytes = document_id.to_buffer();
let dst = slice::from_raw_parts_mut(out_document_id, 32);
dst.copy_from_slice(&bytes);
PlatformWalletFFIResult::ok()
Comment thread
QuantumExplorer marked this conversation as resolved.
Outdated
Comment on lines +130 to +145

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Nitpick: Successful broadcast is reported as an error if CString::new sees an interior NUL; document id is never written

By the time CString::new(document_json) (line 136) runs, the state transition has already been broadcast and the confirmed document returned from Platform. If CString::new were to fail (only possible if the serialized JSON contains an interior NUL byte — a serde_json::to_string output bug, effectively never), unwrap_result_or_return! exits the FFI with an error and the 32-byte id copy on lines 138-140 never runs, so Swift sees a failed result for an on-chain success and has no id to recover from. Reorder so the id is written before the JSON allocation (or write the id even on JSON marshalling failure with a distinct error code), keeping the success/error signal honest in that degenerate case. Not blocking.

source: ['claude']

}
2 changes: 2 additions & 0 deletions packages/rs-platform-wallet-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod data_contract;
pub mod derivation;
pub mod derive_and_persist_callbacks;
pub mod derive_identity_key_at_slot;
pub mod document;
pub mod dpns;
pub mod error;
pub mod established_contact;
Expand Down Expand Up @@ -88,6 +89,7 @@ pub use data_contract::*;
pub use derivation::*;
pub use derive_and_persist_callbacks::*;
pub use derive_identity_key_at_slot::*;
pub use document::*;
pub use dpns::*;
pub use error::*;
pub use established_contact::*;
Expand Down
Loading
Loading