-
Notifications
You must be signed in to change notification settings - Fork 58
refactor(sdk): shared wire-request decode and pure DPNS/DashPay document builders #4389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PastaPastaPasta
wants to merge
6
commits into
refactor/dpns-dashpay-document-assembly
from
refactor/document-query-decode-builders
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aee9dd4
feat(sdk): add client-side v1 document-query wire decoders
PastaPastaPasta d30a8fb
refactor(sdk): split consensus label validation from client username …
PastaPastaPasta cb2b811
feat(sdk): validate the DPNS label against the consensus pattern befo…
PastaPastaPasta 2237e0c
refactor(sdk): move pure DPNS and DashPay document builders into dash…
PastaPastaPasta d002c54
refactor(sdk): decode document queries from the wire request in share…
PastaPastaPasta 2e0126d
feat(sdk): request-bound document proof verification shared by SDK an…
PastaPastaPasta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| //! Transport-free DashPay contact request document assembly. | ||
| //! | ||
| //! The Sdk-bound DashPay surface (recipient fetching, ECDH, encryption, | ||
| //! broadcasting) lives in `dash-sdk`; this module is the pure DIP-15 | ||
| //! `contactRequest` document assembly it shares with embedders. All crypto | ||
| //! material arrives here as bytes — key derivation and encryption stay with | ||
| //! the caller. | ||
|
|
||
| use crate::Error; | ||
| use dpp::data_contract::accessors::v0::DataContractV0Getters; | ||
| use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; | ||
| use dpp::data_contract::DataContract; | ||
| use dpp::document::{Document, DocumentV0}; | ||
| use dpp::platform_value::Value; | ||
| use dpp::prelude::Identifier; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| /// Already-derived crypto material and metadata for a DIP-15 | ||
| /// `contactRequest` document. | ||
| /// | ||
| /// Everything here is plain data: the ECDH/encryption that produced | ||
| /// `encrypted_public_key` and `encrypted_account_label`, and the randomness | ||
| /// that produced `entropy`, happen in the caller (`dash-sdk` or an | ||
| /// embedder). | ||
| #[derive(Debug, Clone)] | ||
| pub struct ContactRequestDocumentParams { | ||
| /// The sender's identity id (the document owner) | ||
| pub sender_id: Identifier, | ||
| /// The recipient's identity id (`toUserId`) | ||
| pub recipient_id: Identifier, | ||
| /// The sender's encryption key index used for ECDH | ||
| pub sender_key_index: u32, | ||
| /// The recipient's key index used for ECDH | ||
| pub recipient_key_index: u32, | ||
| /// Reference to the DashPay receiving account | ||
| pub account_reference: u32, | ||
| /// ECDH-encrypted extended public key: exactly 96 bytes | ||
| /// (16-byte IV + 80 bytes of encrypted DIP-15 compact xpub) | ||
| pub encrypted_public_key: Vec<u8>, | ||
| /// Optional encrypted account label: 48-80 bytes | ||
| /// (16-byte IV + 32-64 bytes of encrypted data) | ||
| pub encrypted_account_label: Option<Vec<u8>>, | ||
| /// Optional auto-accept proof (38-102 bytes) - not encrypted | ||
| pub auto_accept_proof: Option<Vec<u8>>, | ||
| /// The entropy that derives the document id; the same entropy must be | ||
| /// attached to the create transition, or platform consensus rejects it | ||
| /// with `InvalidDocumentTransitionIdError`. | ||
| pub entropy: [u8; 32], | ||
| } | ||
|
|
||
| /// Validate the size of a DIP-15 `autoAcceptProof` (38-102 bytes). | ||
| pub fn validate_auto_accept_proof(proof: &[u8]) -> Result<(), Error> { | ||
| if proof.len() < 38 || proof.len() > 102 { | ||
| return Err(Error::InvalidInput(format!( | ||
| "autoAcceptProof must be 38-102 bytes, got {}", | ||
| proof.len() | ||
| ))); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Build a DIP-15 `contactRequest` document from already-derived crypto | ||
| /// material. | ||
| /// | ||
| /// This is the pure document-assembly half of `dash-sdk`'s | ||
| /// `create_contact_request`: the document id derives from `params.entropy`, | ||
| /// the owner is `params.sender_id`, and the properties are exactly the fields | ||
| /// the DashPay contract defines. Broadcast the returned document with the | ||
| /// same `params.entropy` attached to the create transition, or platform | ||
| /// consensus rejects it with `InvalidDocumentTransitionIdError`. | ||
| /// | ||
| /// Returns the assembled `contactRequest` [`Document`]. | ||
| pub fn build_contact_request_document( | ||
| contract: &DataContract, | ||
| params: ContactRequestDocumentParams, | ||
| ) -> Result<Document, Error> { | ||
| if let Some(ref proof) = params.auto_accept_proof { | ||
| validate_auto_accept_proof(proof)?; | ||
| } | ||
|
|
||
| // Validate encrypted public key size (must be exactly 96 bytes: 16-byte IV + 80-byte encrypted data) | ||
| if params.encrypted_public_key.len() != 96 { | ||
| return Err(Error::InvalidInput(format!( | ||
| "Encrypted public key size mismatch: expected 96 bytes, got {}", | ||
| params.encrypted_public_key.len() | ||
| ))); | ||
| } | ||
|
|
||
| // Validate encrypted label size (48-80 bytes: 16-byte IV + 32-64 byte encrypted data) | ||
| if let Some(ref label) = params.encrypted_account_label { | ||
| if label.len() < 48 || label.len() > 80 { | ||
| return Err(Error::InvalidInput(format!( | ||
| "Encrypted account label size out of range: expected 48-80 bytes, got {}", | ||
| label.len() | ||
| ))); | ||
| } | ||
| } | ||
|
|
||
| let contact_request_document_type = | ||
| contract | ||
| .document_type_for_name("contactRequest") | ||
| .map_err(|_| { | ||
| Error::InvalidInput("DashPay contactRequest document type not found".to_string()) | ||
| })?; | ||
|
|
||
| let document_id = Document::generate_document_id_v0( | ||
| &contract.id(), | ||
| ¶ms.sender_id, | ||
| contact_request_document_type.name(), | ||
| params.entropy.as_slice(), | ||
| ); | ||
|
|
||
| let mut properties = BTreeMap::new(); | ||
| properties.insert( | ||
| "toUserId".to_string(), | ||
| Value::Identifier(params.recipient_id.to_buffer()), | ||
| ); | ||
| properties.insert( | ||
| "encryptedPublicKey".to_string(), | ||
| Value::Bytes(params.encrypted_public_key), | ||
| ); | ||
| properties.insert( | ||
| "senderKeyIndex".to_string(), | ||
| Value::U32(params.sender_key_index), | ||
| ); | ||
| properties.insert( | ||
| "recipientKeyIndex".to_string(), | ||
| Value::U32(params.recipient_key_index), | ||
| ); | ||
| properties.insert( | ||
| "accountReference".to_string(), | ||
| Value::U32(params.account_reference), | ||
| ); | ||
|
|
||
| if let Some(label) = params.encrypted_account_label { | ||
| properties.insert("encryptedAccountLabel".to_string(), Value::Bytes(label)); | ||
| } | ||
| if let Some(proof) = params.auto_accept_proof { | ||
| properties.insert("autoAcceptProof".to_string(), Value::Bytes(proof)); | ||
| } | ||
|
|
||
| Ok(Document::V0(DocumentV0 { | ||
| contract_version: None, | ||
| id: document_id, | ||
| owner_id: params.sender_id, | ||
| properties, | ||
| revision: None, | ||
| created_at: None, | ||
| updated_at: None, | ||
| transferred_at: None, | ||
| created_at_block_height: None, | ||
| updated_at_block_height: None, | ||
| transferred_at_block_height: None, | ||
| created_at_core_block_height: None, | ||
| updated_at_core_block_height: None, | ||
| transferred_at_core_block_height: None, | ||
| creator_id: None, | ||
| })) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion: Document fresh IV requirements for public DashPay ciphertexts
The public transport-free builder accepts pre-encrypted DashPay fields and validates only their lengths, but its contract does not tell embedders that the first 16 bytes must contain a fresh, unpredictable AES-CBC IV generated independently for every encrypted field and contact request. The managed SDK follows this rule by filling separate IVs from
StdRng::from_entropy(). Reusing an IV with the same ECDH-derived key exposes equality of plaintext prefixes and allows public on-chain ciphertexts to be correlated. Document the fresh-IV requirement, recommend the canonicalplatform_encryptionformat helpers, and state thatentropymust also be freshly generated for each document to avoid document-ID reuse.source: ['codex']