| 6 |
| 7 | Supplying `recipientPublicKey` skips the recipient's `get_public_key` lookup, |
| 8 | including for an uninitialized wallet. Omit it or pass `null` to use the lookup. |
| 9 | -The engine verifies locally that the supplied key derives the recipient address |
| 10 | -using [supported default wallet parameters](README.md#sending-gram). |
| 11 | -Unmatched keys or unsupported parameters are rejected before any HTTP request |
| 12 | -or protected-secret access. The engine asks the platform host for the protected |
| 13 | +The supplied key is used directly, without checking whether it derives the |
| 14 | +recipient address. The caller must provide the recipient's comment-decryption |
| 15 | +key. Invalid key lengths are rejected before HTTP or protected-secret access. |
| 16 | +The engine asks the platform host for the protected |
| 17 | mnemonic with reason `"encryptComment"`. Put `body` into the same immutable |
| 18 | intent passed to `previewSend` and `send`. |
| 19 |
| 20 | diff --git a/src/domain/encrypted_comment.rs b/src/domain/encrypted_comment.rs |
| 21 | index 2b6de66..e84aa0f 100644 |
| 22 | --- a/src/domain/encrypted_comment.rs |
| 23 | +++ b/src/domain/encrypted_comment.rs |
| 24 | @@ -16,9 +16,8 @@ pub struct CreateEncryptedCommentRequest { |
| 25 | pub comment: String, |
| 26 | /// Optional 32-byte Ed25519 public key used instead of an on-chain lookup. |
| 27 | /// |
| 28 | - /// The engine verifies this key against `recipient` by deriving supported |
| 29 | - /// wallet addresses with default parameters. A mismatch or unsupported |
| 30 | - /// wallet configuration is rejected before authorizing the sender's secret. |
| 31 | + /// Used directly without deriving or checking the recipient address. The |
| 32 | + /// caller must supply the key used by the recipient for comment decryption. |
| 33 | #[serde(default)] |
| 34 | #[uniffi(default = None)] |
| 35 | pub recipient_public_key: Option<Vec<u8>>, |
| 36 | @@ -36,8 +35,8 @@ pub struct EncryptedCommentRecipientRequest { |
| 37 | pub recipient: TonAddressString, |
| 38 | /// Optional 32-byte Ed25519 public key used instead of an on-chain lookup. |
| 39 | /// |
| 40 | - /// It is verified against `recipient` exactly as |
| 41 | - /// [`CreateEncryptedCommentRequest::recipient_public_key`] is. |
| 42 | + /// The host authenticates it as described by |
| 43 | + /// [`CreateEncryptedCommentRequest::recipient_public_key`]. |
| 44 | #[serde(default)] |
| 45 | #[uniffi(default = None)] |
| 46 | pub recipient_public_key: Option<Vec<u8>>, |
| 47 | diff --git a/src/domain/nft_send.rs b/src/domain/nft_send.rs |
| 48 | index d1cf4c1..2d474f6 100644 |
| 49 | --- a/src/domain/nft_send.rs |
| 50 | +++ b/src/domain/nft_send.rs |
| 51 | @@ -68,6 +68,16 @@ pub struct NftTransferPreviewRequest { |
| 52 | pub intent: NftTransferIntent, |
| 53 | } |
| 54 |
| 55 | +/// Requests signed NFT transfer delivery forms without submitting either one. |
| 56 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, uniffi::Record)] |
| 57 | +#[serde(rename_all = "camelCase")] |
| 58 | +pub struct PrepareNftTransferRequest { |
| 59 | + /// Correlation identifier, shared with the preview for a stable TEP-62 query ID. |
| 60 | + pub operation_id: NonEmptyString, |
| 61 | + /// Immutable NFT transfer choices. |
| 62 | + pub intent: NftTransferIntent, |
| 63 | +} |
| 64 | + |
| 65 | /// Requests one owner-signed TEP-62 NFT transfer. |
| 66 | #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, uniffi::Record)] |
| 67 | #[serde(rename_all = "camelCase")] |
| 68 | diff --git a/src/engine/encrypted_comment.rs b/src/engine/encrypted_comment.rs |
| 69 | index d0cfafb..321472d 100644 |
| 70 | --- a/src/engine/encrypted_comment.rs |
| 71 | +++ b/src/engine/encrypted_comment.rs |
| 72 | @@ -10,10 +10,9 @@ use crate::wallet::encrypted_comment::{ |
| 73 | encrypt_comment as encrypt_body, validate_encrypted_comment_body, |
| 74 | }; |
| 75 | use crate::wallet::key_history::{KeyChange, recover_signing_keys}; |
| 76 | -use crate::wallet::recipient_public_key::verify_recipient_public_key; |
| 77 | use crate::{ |
| 78 | AccountStatus, Boc, CreateEncryptedCommentRequest, DecryptCommentRequest, |
| 79 | - EncryptedCommentRecipientRequest, HttpRequest, Network, ProtectedSecretRead, TonAddressString, |
| 80 | + EncryptedCommentRecipientRequest, HttpRequest, ProtectedSecretRead, TonAddressString, |
| 81 | WalletClientConfig, WalletClientError, |
| 82 | }; |
| 83 |
| 84 | @@ -42,8 +41,7 @@ impl WalletClient { |
| 85 | /// wallet's `get_public_key` get-method, then asks the platform host to |
| 86 | /// authorize this wallet's protected mnemonic. The sender key is this |
| 87 | /// wallet's current signing key. |
| 88 | - /// A supplied key must locally derive the recipient's address using supported |
| 89 | - /// default wallet parameters. Verification happens before secret authorization. |
| 90 | + /// The host authenticates supplied recipient keys, including rotated keys. |
| 91 | /// No secret is requested when the comment is already too large. |
| 92 | /// The recipient key is resolved exactly as |
| 93 | /// [`Self::resolve_encrypted_comment_recipient`] resolves it, including its |
| 94 | @@ -70,9 +68,6 @@ impl WalletClient { |
| 95 | if state.active_send.is_some() || state.active_resolution.is_some() { |
| 96 | return Err(WalletClientError::SendAlreadyInProgress); |
| 97 | } |
| 98 | - if let Some(key) = &provided_public_key { |
| 99 | - verify_provided_recipient_key(&request.recipient, key, state.config.network)?; |
| 100 | - } |
| 101 | let generation = next_resolution_generation(&mut state)?; |
| 102 | let config = state.config.clone(); |
| 103 | let public_key_source = recipient_public_key_source( |
| 104 | @@ -148,7 +143,6 @@ impl WalletClient { |
| 105 | let mut state = self.lock()?; |
| 106 | ensure_running(&state)?; |
| 107 | if let Some(key) = provided_public_key { |
| 108 | - verify_provided_recipient_key(&request.recipient, &key, state.config.network)?; |
| 109 | return Ok(key.to_vec()); |
| 110 | } |
| 111 | if state.active_send.is_some() || state.active_resolution.is_some() { |
| 112 | @@ -495,15 +489,6 @@ fn provided_recipient_key(key: Option<&[u8]>) -> Result<Option<[u8; 32]>, Wallet |
| 113 | }) |
| 114 | } |
| 115 |
| 116 | -fn verify_provided_recipient_key( |
| 117 | - recipient: &TonAddressString, |
| 118 | - key: &[u8; 32], |
| 119 | - network: Network, |
| 120 | -) -> Result<(), WalletClientError> { |
| 121 | - verify_recipient_public_key(recipient, key, network) |
| 122 | - .map_err(|error| encrypted_comment_error(error.to_string())) |
| 123 | -} |
| 124 | - |
| 125 | fn next_resolution_generation(state: &mut State) -> Result<u64, WalletClientError> { |
| 126 | state.resolution_generation = state |
| 127 | .resolution_generation |
| 128 | @@ -862,13 +847,18 @@ mod tests { |
| 129 | } |
| 130 |
| 131 | #[test] |
| 132 | - fn supplied_key_for_another_wallet_is_rejected_before_io() { |
| 133 | + fn supplied_key_need_not_derive_the_recipient_address() { |
| 134 | + const RECIPIENT_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; |
| 135 | + let recipient_wallet = |
| 136 | + derive_wallet(RECIPIENT_MNEMONIC, Network::Testnet).expect("recipient wallet derives"); |
| 137 | + let key_address = |
| 138 | + TonAddressString::from_address(&recipient_wallet.address, Network::Testnet); |
| 139 | let config = client_config(); |
| 140 | - let recipient = config.address.clone(); |
| 141 | - let correct_key = config.public_key.clone(); |
| 142 | - let wrong_key = SigningKey::from_bytes(&[7; 32]).verifying_key().to_bytes(); |
| 143 | + let source = config.address.clone(); |
| 144 | + let recipient = TonAddressString::try_from(RECIPIENT).expect("recipient"); |
| 145 | + assert_ne!(recipient, key_address); |
| 146 | let http = Arc::new(PublicKeyHost { |
| 147 | - public_key: wrong_key, |
| 148 | + public_key: [0; 32], |
| 149 | requests: Mutex::new(Vec::new()), |
| 150 | }); |
| 151 | let platform = Arc::new(SecretHost { |
| 152 | @@ -877,20 +867,24 @@ mod tests { |
| 153 | let client = |
| 154 | WalletClient::new(config, http.clone(), platform.clone()).expect("client builds"); |
| 155 |
| 156 | - let error = block_on( |
| 157 | + let body = block_on( |
| 158 | client.create_encrypted_comment(CreateEncryptedCommentRequest { |
| 159 | - recipient: recipient.clone(), |
| 160 | - comment: "must remain private".to_owned(), |
| 161 | - recipient_public_key: Some(wrong_key.to_vec()), |
| 162 | + recipient, |
| 163 | + comment: "use the supplied encryption key".to_owned(), |
| 164 | + recipient_public_key: Some(recipient_wallet.key_pair.public_key.to_vec()), |
| 165 | }), |
| 166 | ) |
| 167 | - .expect_err("a key for another wallet must fail"); |
| 168 | + .expect("supplied encryption key need not derive the destination address"); |
| 169 | + let keys = |
| 170 | + comment_keys(RECIPIENT_MNEMONIC.as_bytes(), Network::Testnet, &key_address).unwrap(); |
| 171 | + let plaintext = decrypt_comment_with_keys([&keys.signing], &source, &body) |
| 172 | + .expect("holder of the supplied key decrypts the comment"); |
| 173 | + assert_eq!(plaintext, "use the supplied encryption key"); |
| 174 | + assert!(http.requests.lock().expect("request lock").is_empty()); |
| 175 | assert_eq!( |
| 176 | - error, |
| 177 | - encrypted_comment_error(EncryptedCommentError::RecipientPublicKeyMismatch.to_string()), |
| 178 | + *platform.reasons.lock().expect("reason lock"), |
| 179 | + [SecretAccessReason::EncryptComment], |
| 180 | ); |
| 181 | - assert!(http.requests.lock().expect("request lock").is_empty()); |
| 182 | - assert!(platform.reasons.lock().expect("reason lock").is_empty()); |
| 183 | assert!( |
| 184 | client |
| 185 | .lock() |
| 186 | @@ -898,20 +892,6 @@ mod tests { |
| 187 | .active_resolution |
| 188 | .is_none() |
| 189 | ); |
| 190 | - |
| 191 | - block_on( |
| 192 | - client.create_encrypted_comment(CreateEncryptedCommentRequest { |
| 193 | - recipient, |
| 194 | - comment: "the correct key still works".to_owned(), |
| 195 | - recipient_public_key: Some(correct_key), |
| 196 | - }), |
| 197 | - ) |
| 198 | - .expect("rejected key does not leave a pending operation"); |
| 199 | - assert!(http.requests.lock().expect("request lock").is_empty()); |
| 200 | - assert_eq!( |
| 201 | - *platform.reasons.lock().expect("reason lock"), |
| 202 | - [SecretAccessReason::EncryptComment], |
| 203 | - ); |
| 204 | } |
| 205 |
| 206 | fn resolve( |
| 207 | @@ -984,7 +964,7 @@ mod tests { |
| 208 | } |
| 209 |
| 210 | #[test] |
| 211 | - fn resolution_verifies_a_supplied_key_locally_even_while_the_slot_is_busy() { |
| 212 | + fn resolution_accepts_host_authenticated_keys_even_while_the_slot_is_busy() { |
| 213 | let config = secretless_config(); |
| 214 | let recipient = config.address.clone(); |
| 215 | let correct_key = config.public_key.clone(); |
| 216 | @@ -1000,10 +980,10 @@ mod tests { |
| 217 | .expect("the supplied key derives the recipient"), |
| 218 | correct_key |
| 219 | ); |
| 220 | - assert!(matches!( |
| 221 | - resolve(&client, recipient.clone(), Some(wrong_key.to_vec())), |
| 222 | - Err(WalletClientError::EncryptedCommentUnavailable { .. }) |
| 223 | - )); |
| 224 | + assert_eq!( |
| 225 | + resolve(&client, recipient.clone(), Some(wrong_key.to_vec())).unwrap(), |
| 226 | + wrong_key.to_vec() |
| 227 | + ); |
| 228 | assert!(matches!( |
| 229 | resolve(&client, recipient.clone(), Some(vec![7; 31])), |
| 230 | Err(WalletClientError::EncryptedCommentUnavailable { .. }) |
| 231 | diff --git a/src/engine/key_rotation.rs b/src/engine/key_rotation.rs |
| 232 | index cfded89..b41e18d 100644 |
| 233 | --- a/src/engine/key_rotation.rs |
| 234 | +++ b/src/engine/key_rotation.rs |
| 235 | @@ -16,7 +16,8 @@ use super::state::{OperationFamily, ensure_running}; |
| 236 |
| 237 | #[uniffi::export] |
| 238 | impl WalletClient { |
| 239 | - /// Generates a new signing half and a signed Wallet rev00 key-change message. |
| 240 | + /// Generates one new signing half and both signed Wallet rev00 delivery forms. |
| 241 | + /// `signed_boc` aliases the form selected by `request.message_kind`. |
| 242 | /// |
| 243 | /// The client first fetches fresh account state through its configured |
| 244 | /// provider. Active wallets use the on-chain `seqno` getter, while an |
| 245 | @@ -182,6 +183,8 @@ impl WalletClient { |
| 246 | }, |
| 247 | new_public_key: prepared.new_public_key.to_vec(), |
| 248 | signed_boc: prepared.signed_boc, |
| 249 | + external_boc: prepared.external_boc, |
| 250 | + internal_boc: prepared.internal_boc, |
| 251 | seqno, |
| 252 | valid_until: request.valid_until, |
| 253 | message_kind: request.message_kind, |
| 254 | diff --git a/src/engine/nft_transfer.rs b/src/engine/nft_transfer.rs |
| 255 | index c727fc4..052ced4 100644 |
| 256 | --- a/src/engine/nft_transfer.rs |
| 257 | +++ b/src/engine/nft_transfer.rs |
| 258 | @@ -3,8 +3,9 @@ |
| 259 | use crate::domain::bounded_diagnostic; |
| 260 | use crate::wallet::nft_transfer::canonicalize_nft_transfer; |
| 261 | use crate::{ |
| 262 | - NftItem, NftTransferPreviewRequest, NftTransferRequest, SendPreview, SendPreviewRequest, |
| 263 | - SendRequest, SendResult, TonAddressString, WalletClientError, |
| 264 | + NftItem, NftTransferPreviewRequest, NftTransferRequest, PrepareNftTransferRequest, |
| 265 | + PrepareTransferRequest, PreparedTransfer, SendPreview, SendPreviewRequest, SendRequest, |
| 266 | + SendResult, TonAddressString, WalletClientError, |
| 267 | }; |
| 268 |
| 269 | use super::WalletClient; |
| 270 | @@ -13,6 +14,30 @@ use super::state::ensure_running; |
| 271 |
| 272 | #[uniffi::export] |
| 273 | impl WalletClient { |
| 274 | + /// Validates ownership and emulation, then signs external and internal delivery |
| 275 | + /// alternatives without broadcasting or writing the send journal. |
| 276 | + pub async fn prepare_nft_transfer( |
| 277 | + &self, |
| 278 | + request: PrepareNftTransferRequest, |
| 279 | + ) -> Result<PreparedTransfer, WalletClientError> { |
| 280 | + let _preview = self |
| 281 | + .preview_nft_transfer(NftTransferPreviewRequest { |
| 282 | + operation_id: request.operation_id.clone(), |
| 283 | + intent: request.intent.clone(), |
| 284 | + }) |
| 285 | + .await?; |
| 286 | + let source = self.nft_transfer_source()?; |
| 287 | + let canonical = canonicalize_nft_transfer(&request.operation_id, &source, &request.intent) |
| 288 | + .map_err(|error| { |
| 289 | + nft_unavailable(format!("failed to build TEP-62 transfer: {error}")) |
| 290 | + })?; |
| 291 | + self.prepare_transfer(PrepareTransferRequest { |
| 292 | + operation_id: request.operation_id, |
| 293 | + intent: canonical.intent, |
| 294 | + }) |
| 295 | + .await |
| 296 | + } |
| 297 | + |
| 298 | /// Validates current ownership and emulates one typed TEP-62 NFT transfer. |
| 299 | /// |
| 300 | /// Reuse this request's operation ID for [`Self::send_nft_transfer`] after |
| 301 | diff --git a/src/engine/preview.rs b/src/engine/preview.rs |
| 302 | index 4a91f8a..2f60cfa 100644 |
| 303 | --- a/src/engine/preview.rs |
| 304 | +++ b/src/engine/preview.rs |
| 305 | @@ -29,6 +29,28 @@ impl WalletClient { |
| 306 | &self, |
| 307 | request: SendPreviewRequest, |
| 308 | ) -> Result<SendPreview, WalletClientError> { |
| 309 | + self.preview_send_or_key_rotation(Some(request)).await |
| 310 | + } |
| 311 | + |
| 312 | + /// Emulates external key rotation using public state and zero signatures. |
| 313 | + /// Never unlocks a secret, generates a replacement phrase, or submits a BOC. |
| 314 | + pub async fn preview_key_rotation(&self) -> Result<SendPreview, WalletClientError> { |
| 315 | + self.preview_send_or_key_rotation(None).await |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +impl WalletClient { |
| 320 | + async fn preview_send_or_key_rotation( |
| 321 | + &self, |
| 322 | + request: Option<SendPreviewRequest>, |
| 323 | + ) -> Result<SendPreview, WalletClientError> { |
| 324 | + let key_rotation = request.is_none(); |
| 325 | + let request = request.unwrap_or_else(|| SendPreviewRequest { |
| 326 | + intent: crate::SendIntent { |
| 327 | + expiration: crate::SendExpiration::EngineDefault, |
| 328 | + messages: Vec::new(), |
| 329 | + }, |
| 330 | + }); |
| 331 | let ( |
| 332 | generation, |
| 333 | config, |
| 334 | @@ -89,10 +111,13 @@ impl WalletClient { |
| 335 | })?; |
| 336 |
| 337 | let available = account.balance_nanograms.clone(); |
| 338 | - let requested = request |
| 339 | - .intent |
| 340 | - .exact_value_total() |
| 341 | - .map_err(|_| self.preview_error(generation, WalletClientError::InvalidSendRequest))?; |
| 342 | + let requested = if key_rotation { |
| 343 | + Some(crate::UnsignedDecimalString::from(0_u64)) |
| 344 | + } else { |
| 345 | + request.intent.exact_value_total().map_err(|_| { |
| 346 | + self.preview_error(generation, WalletClientError::InvalidSendRequest) |
| 347 | + })? |
| 348 | + }; |
| 349 |
| 350 | if let Some(nanograms) = &requested |
| 351 | && nanograms > &available |
| 352 | @@ -148,14 +173,27 @@ impl WalletClient { |
| 353 | seqno, |
| 354 | }; |
| 355 |
| 356 | - let boc = prepare_transfer_emulation( |
| 357 | - &expected_source, |
| 358 | - &config.public_key, |
| 359 | - config.network, |
| 360 | - &request, |
| 361 | - &fresh, |
| 362 | - valid_until, |
| 363 | - ) |
| 364 | + let boc = if key_rotation { |
| 365 | + crate::wallet::key_rotation::prepare_key_rotation_emulation( |
| 366 | + &expected_source, |
| 367 | + &config.public_key, |
| 368 | + config.network, |
| 369 | + fresh.seqno, |
| 370 | + fresh.needs_state_init(), |
| 371 | + valid_until, |
| 372 | + ) |
| 373 | + .map_err(|error| error.to_string()) |
| 374 | + } else { |
| 375 | + prepare_transfer_emulation( |
| 376 | + &expected_source, |
| 377 | + &config.public_key, |
| 378 | + config.network, |
| 379 | + &request, |
| 380 | + &fresh, |
| 381 | + valid_until, |
| 382 | + ) |
| 383 | + .map_err(|error| error.to_string()) |
| 384 | + } |
| 385 | .map_err(|error| { |
| 386 | self.preview_error( |
| 387 | generation, |
| 388 | @@ -205,7 +243,10 @@ impl WalletClient { |
| 389 | self.finish_preview(generation)?; |
| 390 | Ok(preview) |
| 391 | } |
| 392 | +} |
| 393 |
| 394 | +#[uniffi::export] |
| 395 | +impl WalletClient { |
| 396 | /// Emulates an already signed external-message BOC without submitting it. |
| 397 | /// |
| 398 | /// The request is validated against the configured source and fresh wallet |
| 399 | diff --git a/src/ton_connect/derived.rs b/src/ton_connect/derived.rs |
| 400 | index c08665e..e0809f3 100644 |
| 401 | --- a/src/ton_connect/derived.rs |
| 402 | +++ b/src/ton_connect/derived.rs |
| 403 | @@ -245,6 +245,18 @@ impl TonConnectDerivedSession { |
| 404 | reason = "UniFFI exports owned records, byte buffers, and strings at the foreign-language boundary" |
| 405 | )] |
| 406 | impl TonConnectDerivedSession { |
| 407 | + /// Opens an opaque MTProto envelope for hosts that own RPC decoding. |
| 408 | + /// In particular, Gram Wallet also supports relayed `signMessage`. |
| 409 | + pub fn decrypt_envelope(&self, body: Vec<u8>) -> Result<Vec<u8>, TonConnectSessionError> { |
| 410 | + self.crypto.decrypt(self.peer, &body).map_err(session_error) |
| 411 | + } |
| 412 | + |
| 413 | + /// Seals a host-validated protocol envelope without exporting session keys. |
| 414 | + pub fn encrypt_envelope(&self, body: Vec<u8>) -> Result<Vec<u8>, TonConnectSessionError> { |
| 415 | + let body = Zeroizing::new(body); |
| 416 | + self.crypto.encrypt(self.peer, &body).map_err(session_error) |
| 417 | + } |
| 418 | + |
| 419 | /// Returns the session public key `W` as 64 lowercase hex characters. |
| 420 | #[must_use] |
| 421 | pub fn public_key_hex(&self) -> String { |
| 422 | diff --git a/src/wallet/encrypted_comment.rs b/src/wallet/encrypted_comment.rs |
| 423 | index 1477d4e..238e4dd 100644 |
| 424 | --- a/src/wallet/encrypted_comment.rs |
| 425 | +++ b/src/wallet/encrypted_comment.rs |
| 426 | @@ -40,10 +40,6 @@ pub(crate) enum EncryptedCommentError { |
| 427 | WalletIdentityMismatch, |
| 428 | #[error("the peer Ed25519 public key is invalid")] |
| 429 | InvalidPeerPublicKey, |
| 430 | - #[error( |
| 431 | - "the recipient public key does not match a supported wallet address with default parameters" |
| 432 | - )] |
| 433 | - RecipientPublicKeyMismatch, |
| 434 | #[error("secure random generation failed")] |
| 435 | RandomGeneration, |
| 436 | #[error("the encrypted comment body is malformed")] |
| 437 | diff --git a/src/wallet/key_rotation.rs b/src/wallet/key_rotation.rs |
| 438 | index cbc3e6a..351bc24 100644 |
| 439 | --- a/src/wallet/key_rotation.rs |
| 440 | +++ b/src/wallet/key_rotation.rs |
| 441 | @@ -48,6 +48,8 @@ pub(crate) struct PreparedKeyRotationMaterial { |
| 442 | pub(crate) replacement_mnemonic: SensitiveMnemonic, |
| 443 | pub(crate) new_public_key: [u8; 32], |
| 444 | pub(crate) signed_boc: Boc, |
| 445 | + pub(crate) external_boc: Boc, |
| 446 | + pub(crate) internal_boc: Boc, |
| 447 | } |
| 448 |
| 449 | pub(crate) fn prepare_key_rotation( |
| 450 | @@ -143,24 +145,34 @@ fn prepare_with_new_half( |
| 451 | .map_err(|_| KeyRotationError::Preparation)? |
| 452 | .as_slice(), |
| 453 | ); |
| 454 | - let request = build_change_public_key_request( |
| 455 | - message_kind, |
| 456 | - wallet_id, |
| 457 | - valid_until, |
| 458 | - seqno, |
| 459 | - new_public_key, |
| 460 | - proof_signature.to_bytes(), |
| 461 | - encrypt_old_private_key(current_key, new_key), |
| 462 | - ) |
| 463 | - .map_err(|_| KeyRotationError::Preparation)?; |
| 464 | - let signed_request = sign_cell(current_key, &request)?; |
| 465 | - let message = wrap_signed_request(wallet_address, message_kind, signed_request, state_init)?; |
| 466 | - let signed_boc = Boc::try_from( |
| 467 | - message |
| 468 | - .to_boc() |
| 469 | - .map_err(|_| KeyRotationError::Preparation)?, |
| 470 | - ) |
| 471 | - .map_err(|_| KeyRotationError::Preparation)?; |
| 472 | + // Generate the replacement only once. The opcodes differ, so each delivery |
| 473 | + // form needs its own current-key signature over the same replacement key. |
| 474 | + let build = |kind, init| { |
| 475 | + let request = build_change_public_key_request( |
| 476 | + kind, |
| 477 | + wallet_id, |
| 478 | + valid_until, |
| 479 | + seqno, |
| 480 | + new_public_key, |
| 481 | + proof_signature.to_bytes(), |
| 482 | + encrypt_old_private_key(current_key, new_key), |
| 483 | + ) |
| 484 | + .map_err(|_| KeyRotationError::Preparation)?; |
| 485 | + let signed_request = sign_cell(current_key, &request)?; |
| 486 | + let message = wrap_signed_request(wallet_address, kind, signed_request, init)?; |
| 487 | + Boc::try_from( |
| 488 | + message |
| 489 | + .to_boc() |
| 490 | + .map_err(|_| KeyRotationError::Preparation)?, |
| 491 | + ) |
| 492 | + .map_err(|_| KeyRotationError::Preparation) |
| 493 | + }; |
| 494 | + let external_boc = build(KeyRotationMessageKind::External, state_init.clone())?; |
| 495 | + let internal_boc = build(KeyRotationMessageKind::Internal, state_init)?; |
| 496 | + let signed_boc = match message_kind { |
| 497 | + KeyRotationMessageKind::External => external_boc.clone(), |
| 498 | + KeyRotationMessageKind::Internal => internal_boc.clone(), |
| 499 | + }; |
| 500 |
| 501 | let mut replacement_phrase = Zeroizing::new(String::new()); |
| 502 | replacement_phrase.push_str(¤t.anchor().to_phrase()); |
| 503 | @@ -174,9 +186,71 @@ fn prepare_with_new_half( |
| 504 | replacement_mnemonic, |
| 505 | new_public_key, |
| 506 | signed_boc, |
| 507 | + external_boc, |
| 508 | + internal_boc, |
| 509 | }) |
| 510 | } |
| 511 |
| 512 | +/// Constructs an external rotation preview using public state only. Both the |
| 513 | +/// owner signature and the new-key proof are zero placeholders; this BOC must |
| 514 | +/// never authorize a real rotation, even if the emulation provider broadcasts it. |
| 515 | +pub(crate) fn prepare_key_rotation_emulation( |
| 516 | + source: &TonAddressString, |
| 517 | + anchor_public_key: &[u8], |
| 518 | + network: Network, |
| 519 | + seqno: u32, |
| 520 | + needs_state_init: bool, |
| 521 | + valid_until: u64, |
| 522 | +) -> Result<Boc, KeyRotationError> { |
| 523 | + let valid_until = |
| 524 | + u32::try_from(valid_until).map_err(|_| KeyRotationError::ExpirationOutOfRange)?; |
| 525 | + let wallet_id = match network { |
| 526 | + Network::Mainnet => ton::ton_wallet::WALLET_SUBWALLET_ID_DEFAULT, |
| 527 | + Network::Testnet => ton::ton_wallet::WALLET_SUBWALLET_ID_DEFAULT_TESTNET, |
| 528 | + }; |
| 529 | + // Encoded Ed25519 base point: a valid public key unrelated to the wallet. |
| 530 | + let mut placeholder_key = [0x66; 32]; |
| 531 | + placeholder_key[0] = 0x58; |
| 532 | + let request = build_change_public_key_request( |
| 533 | + KeyRotationMessageKind::External, |
| 534 | + wallet_id, |
| 535 | + valid_until, |
| 536 | + seqno, |
| 537 | + placeholder_key, |
| 538 | + [0; 64], |
| 539 | + [0; 32], |
| 540 | + ) |
| 541 | + .map_err(|_| KeyRotationError::Preparation)?; |
| 542 | + let mut body = TonCell::builder(); |
| 543 | + body.write_bits([0; 64], SIGNATURE_BITS) |
| 544 | + .map_err(|_| KeyRotationError::Preparation)?; |
| 545 | + body.write_cell(&request) |
| 546 | + .map_err(|_| KeyRotationError::Preparation)?; |
| 547 | + let body = body.build().map_err(|_| KeyRotationError::Preparation)?; |
| 548 | + let init = if needs_state_init { |
| 549 | + let (address, init) = derive_wallet_public_state(anchor_public_key, network) |
| 550 | + .map_err(|_| KeyRotationError::Preparation)?; |
| 551 | + if &address != source.as_address() { |
| 552 | + return Err(KeyRotationError::WalletIdentityMismatch); |
| 553 | + } |
| 554 | + Some(init) |
| 555 | + } else { |
| 556 | + None |
| 557 | + }; |
| 558 | + let message = wrap_signed_request( |
| 559 | + source.as_address(), |
| 560 | + KeyRotationMessageKind::External, |
| 561 | + body, |
| 562 | + init, |
| 563 | + )?; |
| 564 | + Boc::try_from( |
| 565 | + message |
| 566 | + .to_boc() |
| 567 | + .map_err(|_| KeyRotationError::Preparation)?, |
| 568 | + ) |
| 569 | + .map_err(|_| KeyRotationError::Preparation) |
| 570 | +} |
| 571 | + |
| 572 | fn build_rotation_proof(wallet_address: &TonAddress) -> Result<TonCell, TonCoreError> { |
| 573 | let workchain = i8::try_from(wallet_address.workchain).map_err(|_| { |
| 574 | TonCoreError::Custom("Wallet key-rotation proof requires an int8 workchain".to_owned()) |
| 575 | @@ -594,6 +668,149 @@ mod tests { |
| 576 | )); |
| 577 | } |
| 578 |
| 579 | + #[test] |
| 580 | + fn paired_rotation_uses_one_replacement_and_verifiable_channel_signatures() { |
| 581 | + for phrase in [CURRENT_PHRASE, ROTATED_PHRASE] { |
| 582 | + let current = SensitiveMnemonic::from_bytes(phrase.as_bytes().to_vec()).unwrap(); |
| 583 | + let wallet = derive_wallet(phrase, Network::Testnet).unwrap(); |
| 584 | + let source = TonAddressString::from_address(&wallet.address, Network::Testnet); |
| 585 | + let material = prepare_key_rotation( |
| 586 | + ¤t, |
| 587 | + Network::Testnet, |
| 588 | + &source, |
| 589 | + SEQNO, |
| 590 | + false, |
| 591 | + u64::from(VALID_UNTIL), |
| 592 | + KeyRotationMessageKind::External, |
| 593 | + ) |
| 594 | + .unwrap(); |
| 595 | + let replacement = |
| 596 | + RotationMnemonic::parse(material.replacement_mnemonic.as_str().unwrap()).unwrap(); |
| 597 | + let keys = derive_rotation_keys(&replacement); |
| 598 | + assert_eq!( |
| 599 | + keys.signing.verifying_key().to_bytes(), |
| 600 | + material.new_public_key |
| 601 | + ); |
| 602 | + let current_keys = derive_rotation_keys(&RotationMnemonic::parse(phrase).unwrap()); |
| 603 | + assert_eq!( |
| 604 | + keys.anchor.verifying_key(), |
| 605 | + current_keys.anchor.verifying_key() |
| 606 | + ); |
| 607 | + assert_ne!( |
| 608 | + keys.signing.verifying_key(), |
| 609 | + current_keys.signing.verifying_key() |
| 610 | + ); |
| 611 | + assert_eq!(material.signed_boc, material.external_boc); |
| 612 | + let proof = build_rotation_proof(&wallet.address).unwrap(); |
| 613 | + let proof_signature = keys |
| 614 | + .signing |
| 615 | + .sign(proof.cell_hash().unwrap().as_slice()) |
| 616 | + .to_bytes(); |
| 617 | + for (kind, boc, opcode) in [ |
| 618 | + ( |
| 619 | + KeyRotationMessageKind::External, |
| 620 | + &material.external_boc, |
| 621 | + CHANGE_PUBLIC_KEY_EXTERNAL_OPCODE, |
| 622 | + ), |
| 623 | + ( |
| 624 | + KeyRotationMessageKind::Internal, |
| 625 | + &material.internal_boc, |
| 626 | + CHANGE_PUBLIC_KEY_INTERNAL_OPCODE, |
| 627 | + ), |
| 628 | + ] { |
| 629 | + let message = Msg::<TonCell>::from_boc(boc.as_bytes().to_vec()).unwrap(); |
| 630 | + assert!(matches!( |
| 631 | + (&message.info, kind), |
| 632 | + (CommonMsgInfo::ExtIn(_), KeyRotationMessageKind::External) |
| 633 | + | (CommonMsgInfo::Int(_), KeyRotationMessageKind::Internal) |
| 634 | + )); |
| 635 | + let request = build_change_public_key_request( |
| 636 | + kind, |
| 637 | + wallet.wallet_id, |
| 638 | + VALID_UNTIL, |
| 639 | + SEQNO, |
| 640 | + material.new_public_key, |
| 641 | + proof_signature, |
| 642 | + encrypt_old_private_key(¤t_keys.signing, &keys.signing), |
| 643 | + ) |
| 644 | + .unwrap(); |
| 645 | + assert_signed_request( |
| 646 | + &message.body.value, |
| 647 | + &request, |
| 648 | + current_keys.signing.verifying_key().to_bytes(), |
| 649 | + opcode, |
| 650 | + material.new_public_key, |
| 651 | + ); |
| 652 | + } |
| 653 | + } |
| 654 | + } |
| 655 | + |
| 656 | + #[test] |
| 657 | + fn rotation_preview_has_no_valid_owner_or_replacement_signature() { |
| 658 | + let wallet = derive_wallet(CURRENT_PHRASE, Network::Testnet).unwrap(); |
| 659 | + let keys = derive_rotation_keys(&RotationMnemonic::parse(CURRENT_PHRASE).unwrap()); |
| 660 | + let source = TonAddressString::from_address(&wallet.address, Network::Testnet); |
| 661 | + for deploy in [false, true] { |
| 662 | + let boc = prepare_key_rotation_emulation( |
| 663 | + &source, |
| 664 | + &keys.anchor.verifying_key().to_bytes(), |
| 665 | + Network::Testnet, |
| 666 | + SEQNO, |
| 667 | + deploy, |
| 668 | + u64::from(VALID_UNTIL), |
| 669 | + ) |
| 670 | + .unwrap(); |
| 671 | + let message = Msg::<TonCell>::from_boc(boc.as_bytes().to_vec()).unwrap(); |
| 672 | + assert!(matches!(message.info, CommonMsgInfo::ExtIn(_))); |
| 673 | + assert_eq!(message.init.is_some(), deploy); |
| 674 | + let mut parser = message.body.value.parser(); |
| 675 | + let owner_signature = parser.read_bits(SIGNATURE_BITS).unwrap(); |
| 676 | + assert_eq!(owner_signature, [0; 64]); |
| 677 | + assert_eq!( |
| 678 | + parser.read_num::<u32>(32).unwrap(), |
| 679 | + CHANGE_PUBLIC_KEY_EXTERNAL_OPCODE |
| 680 | + ); |
| 681 | + let _ = parser.read_num::<u32>(32).unwrap(); |
| 682 | + assert_eq!(parser.read_num::<u32>(32).unwrap(), VALID_UNTIL); |
| 683 | + assert_eq!(parser.read_num::<u32>(32).unwrap(), SEQNO); |
| 684 | + let key: [u8; 32] = parser.read_bits(256).unwrap().try_into().unwrap(); |
| 685 | + let proof_cell = parser.read_next_ref().unwrap(); |
| 686 | + let proof_signature = proof_cell.parser().read_bits(SIGNATURE_BITS).unwrap(); |
| 687 | + assert_eq!(proof_signature, [0; 64]); |
| 688 | + assert_eq!(read_encrypted_old_private_key(&mut parser), [0; 32]); |
| 689 | + parser.ensure_empty().unwrap(); |
| 690 | + let proof = build_rotation_proof(&wallet.address).unwrap(); |
| 691 | + assert!( |
| 692 | + VerifyingKey::from_bytes(&key) |
| 693 | + .unwrap() |
| 694 | + .verify_strict( |
| 695 | + proof.cell_hash().unwrap().as_slice(), |
| 696 | + &Signature::from_slice(&proof_signature).unwrap() |
| 697 | + ) |
| 698 | + .is_err() |
| 699 | + ); |
| 700 | + let request = build_change_public_key_request( |
| 701 | + KeyRotationMessageKind::External, |
| 702 | + wallet.wallet_id, |
| 703 | + VALID_UNTIL, |
| 704 | + SEQNO, |
| 705 | + key, |
| 706 | + [0; 64], |
| 707 | + [0; 32], |
| 708 | + ) |
| 709 | + .unwrap(); |
| 710 | + assert!( |
| 711 | + keys.signing |
| 712 | + .verifying_key() |
| 713 | + .verify_strict( |
| 714 | + request.cell_hash().unwrap().as_slice(), |
| 715 | + &Signature::from_slice(&owner_signature).unwrap() |
| 716 | + ) |
| 717 | + .is_err() |
| 718 | + ); |
| 719 | + } |
| 720 | + } |
| 721 | + |
| 722 | fn deterministic_material( |
| 723 | message_kind: KeyRotationMessageKind, |
| 724 | ) -> ( |
| 725 | diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs |
| 726 | index 8843ccb..4a58213 100644 |
| 727 | --- a/src/wallet/mod.rs |
| 728 | +++ b/src/wallet/mod.rs |
| 729 | @@ -10,7 +10,6 @@ pub(crate) mod key_rotation; |
| 730 | pub(crate) mod mnemonic; |
| 731 | mod mnemonic_scheme; |
| 732 | pub(crate) mod nft_transfer; |
| 733 | -pub(crate) mod recipient_public_key; |
| 734 | pub(crate) mod send; |
| 735 | pub(crate) mod slip_0010; |
| 736 | pub(crate) mod transfer; |
| 737 | @@ -217,6 +216,10 @@ pub struct PreparedKeyRotation { |
| 738 | /// Its `ChangePublicKey` request holds two references: the new key's |
| 739 | /// wallet-address proof signature and the encrypted old private key. |
| 740 | pub signed_boc: Boc, |
| 741 | + /// External delivery form for the same replacement key, sequence and expiration. |
| 742 | + pub external_boc: Boc, |
| 743 | + /// Internal alternative; submit only one of the two delivery forms. |
| 744 | + pub internal_boc: Boc, |
| 745 | /// Sequence number covered by the signed request. |
| 746 | pub seqno: u32, |
| 747 | /// Unix expiration timestamp covered by the signed request. |
| 748 | diff --git a/src/wallet/recipient_public_key.rs b/src/wallet/recipient_public_key.rs |
| 749 | deleted file mode 100644 |
| 750 | index 5fb18f4..0000000 |
| 751 | --- a/src/wallet/recipient_public_key.rs |
| 752 | +++ /dev/null |
| 753 | @@ -1,265 +0,0 @@ |
| 754 | -//! Local address binding for caller-supplied encrypted-comment public keys. |
| 755 | - |
| 756 | -use ton::block_tlb::StateInit; |
| 757 | -use ton::ton_core::cell::{TonCell, TonHash}; |
| 758 | -use ton::ton_core::traits::tlb::TLB as _; |
| 759 | -use ton::ton_wallet::{ |
| 760 | - WALLET_ID_DEFAULT, WALLET_SUBWALLET_ID_DEFAULT, WALLET_SUBWALLET_ID_DEFAULT_TESTNET, |
| 761 | - WALLET_V5R1_ID_DEFAULT, WALLET_V5R1_ID_DEFAULT_TESTNET, WalletData, WalletV1V2Data, |
| 762 | - WalletV3Data, WalletV4Data, WalletV5Data, WalletVersion, |
| 763 | -}; |
| 764 | - |
| 765 | -use super::encrypted_comment::EncryptedCommentError; |
| 766 | -use crate::{Network, TonAddressString}; |
| 767 | - |
| 768 | -/// Authenticates a supplied key by reconstructing standard initial wallet states. |
| 769 | -/// |
| 770 | -/// This works before deployment and never trusts a provider response. V1-V5 |
| 771 | -/// use their standard wallet IDs, including the workchain-aware SDK defaults; |
| 772 | -/// Wallet rev00 uses the engine's network-specific ID in workchain zero. Custom |
| 773 | -/// wallet IDs and unknown wallet contracts cannot be proven with only a key and |
| 774 | -/// are rejected. This binds only the initial key; a Wallet rev00 recipient |
| 775 | -/// running this engine still decrypts comments encrypted to it after a |
| 776 | -/// signing-key rotation, because decryption also tries the anchor key. |
| 777 | -pub(crate) fn verify_recipient_public_key( |
| 778 | - recipient: &TonAddressString, |
| 779 | - public_key: &[u8; 32], |
| 780 | - network: Network, |
| 781 | -) -> Result<(), EncryptedCommentError> { |
| 782 | - let public_key = |
| 783 | - TonHash::from_slice(public_key).map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 784 | - let workchain = recipient.as_address().workchain; |
| 785 | - |
| 786 | - let data = WalletV1V2Data::new(public_key.clone()) |
| 787 | - .to_cell() |
| 788 | - .map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 789 | - if matches_versions( |
| 790 | - recipient, |
| 791 | - &[ |
| 792 | - WalletVersion::V1R1, |
| 793 | - WalletVersion::V1R2, |
| 794 | - WalletVersion::V1R3, |
| 795 | - WalletVersion::V2R1, |
| 796 | - WalletVersion::V2R2, |
| 797 | - ], |
| 798 | - &data, |
| 799 | - )? { |
| 800 | - return Ok(()); |
| 801 | - } |
| 802 | - |
| 803 | - // ton-org/ton's V3/V4 constructors add the workchain to the legacy ID. |
| 804 | - // Also recognize the fixed default used by the vendored constructors. |
| 805 | - let legacy_ids = [ |
| 806 | - Some(WALLET_ID_DEFAULT), |
| 807 | - WALLET_ID_DEFAULT |
| 808 | - .checked_add(workchain) |
| 809 | - .filter(|id| *id != WALLET_ID_DEFAULT), |
| 810 | - ]; |
| 811 | - for wallet_id in legacy_ids.into_iter().flatten() { |
| 812 | - let data = WalletV3Data::new(wallet_id, public_key.clone()) |
| 813 | - .to_cell() |
| 814 | - .map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 815 | - if matches_versions( |
| 816 | - recipient, |
| 817 | - &[WalletVersion::V3R1, WalletVersion::V3R2], |
| 818 | - &data, |
| 819 | - )? { |
| 820 | - return Ok(()); |
| 821 | - } |
| 822 | - let data = WalletV4Data::new(wallet_id, public_key.clone()) |
| 823 | - .to_cell() |
| 824 | - .map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 825 | - if matches_versions( |
| 826 | - recipient, |
| 827 | - &[WalletVersion::V4R1, WalletVersion::V4R2], |
| 828 | - &data, |
| 829 | - )? { |
| 830 | - return Ok(()); |
| 831 | - } |
| 832 | - } |
| 833 | - |
| 834 | - let (v5_id, wallet_id) = match network { |
| 835 | - Network::Mainnet => (WALLET_V5R1_ID_DEFAULT, WALLET_SUBWALLET_ID_DEFAULT), |
| 836 | - Network::Testnet => ( |
| 837 | - WALLET_V5R1_ID_DEFAULT_TESTNET, |
| 838 | - WALLET_SUBWALLET_ID_DEFAULT_TESTNET, |
| 839 | - ), |
| 840 | - }; |
| 841 | - let v5_ids = [ |
| 842 | - Some(v5_id), |
| 843 | - v5_workchain_id(v5_id, workchain).filter(|id| *id != v5_id), |
| 844 | - ]; |
| 845 | - for wallet_id in v5_ids.into_iter().flatten() { |
| 846 | - let data = WalletV5Data::new(wallet_id, public_key.clone()) |
| 847 | - .to_cell() |
| 848 | - .map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 849 | - if matches_versions(recipient, &[WalletVersion::V5R1], &data)? { |
| 850 | - return Ok(()); |
| 851 | - } |
| 852 | - } |
| 853 | - |
| 854 | - if workchain == 0 { |
| 855 | - let data = WalletData::new(wallet_id, public_key) |
| 856 | - .to_cell() |
| 857 | - .map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 858 | - if matches_versions(recipient, &[WalletVersion::Wallet], &data)? { |
| 859 | - return Ok(()); |
| 860 | - } |
| 861 | - } |
| 862 | - |
| 863 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 864 | -} |
| 865 | - |
| 866 | -fn matches_versions( |
| 867 | - recipient: &TonAddressString, |
| 868 | - versions: &[WalletVersion], |
| 869 | - data: &TonCell, |
| 870 | -) -> Result<bool, EncryptedCommentError> { |
| 871 | - for &version in versions { |
| 872 | - let code = |
| 873 | - WalletVersion::get_code(version).map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 874 | - let address = StateInit::new(code.clone(), data.clone()) |
| 875 | - .derive_address(recipient.as_address().workchain) |
| 876 | - .map_err(|_| EncryptedCommentError::CellEncoding)?; |
| 877 | - if address == *recipient.as_address() { |
| 878 | - return Ok(true); |
| 879 | - } |
| 880 | - } |
| 881 | - Ok(false) |
| 882 | -} |
| 883 | - |
| 884 | -/// V5's client context encodes workchain as int8 immediately after its tag bit. |
| 885 | -/// See ton-org/ton's `WalletV5R1WalletId.ts`; the remaining default context bits |
| 886 | -/// and network ID are already encoded in the workchain-zero constant. |
| 887 | -fn v5_workchain_id(default_id: i32, workchain: i32) -> Option<i32> { |
| 888 | - let workchain = i8::try_from(workchain).ok()?; |
| 889 | - let workchain_bits = u32::from(u8::from_ne_bytes(workchain.to_ne_bytes())) << 23; |
| 890 | - Some(default_id ^ i32::from_ne_bytes(workchain_bits.to_ne_bytes())) |
| 891 | -} |
| 892 | - |
| 893 | -#[cfg(test)] |
| 894 | -mod tests { |
| 895 | - use ton::ton_core::types::TonAddress; |
| 896 | - use ton::ton_wallet::{KeyPair, TonWallet}; |
| 897 | - |
| 898 | - use super::*; |
| 899 | - |
| 900 | - type TestResult = Result<(), Box<dyn std::error::Error>>; |
| 901 | - |
| 902 | - const PUBLIC_KEY: [u8; 32] = [0x5a; 32]; |
| 903 | - |
| 904 | - fn wallet_address( |
| 905 | - version: WalletVersion, |
| 906 | - workchain: i32, |
| 907 | - wallet_id: i32, |
| 908 | - network: Network, |
| 909 | - ) -> Result<TonAddressString, Box<dyn std::error::Error>> { |
| 910 | - let wallet = TonWallet::new_with_params( |
| 911 | - version, |
| 912 | - KeyPair { |
| 913 | - public_key: PUBLIC_KEY, |
| 914 | - secret_key: [0; 64], |
| 915 | - }, |
| 916 | - workchain, |
| 917 | - wallet_id, |
| 918 | - )?; |
| 919 | - Ok(TonAddressString::from_address(&wallet.address, network)) |
| 920 | - } |
| 921 | - |
| 922 | - #[test] |
| 923 | - fn binds_all_supported_initial_wallet_versions_on_both_networks() -> TestResult { |
| 924 | - for network in [Network::Mainnet, Network::Testnet] { |
| 925 | - for version in [ |
| 926 | - WalletVersion::V1R1, |
| 927 | - WalletVersion::V1R2, |
| 928 | - WalletVersion::V1R3, |
| 929 | - WalletVersion::V2R1, |
| 930 | - WalletVersion::V2R2, |
| 931 | - WalletVersion::V3R1, |
| 932 | - WalletVersion::V3R2, |
| 933 | - WalletVersion::V4R1, |
| 934 | - WalletVersion::V4R2, |
| 935 | - ] { |
| 936 | - let recipient = wallet_address(version, 0, WALLET_ID_DEFAULT, network)?; |
| 937 | - verify_recipient_public_key(&recipient, &PUBLIC_KEY, network)?; |
| 938 | - assert!(matches!( |
| 939 | - verify_recipient_public_key(&recipient, &[0x33; 32], network), |
| 940 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 941 | - )); |
| 942 | - } |
| 943 | - let (v5_id, wallet_id) = match network { |
| 944 | - Network::Mainnet => (WALLET_V5R1_ID_DEFAULT, WALLET_SUBWALLET_ID_DEFAULT), |
| 945 | - Network::Testnet => ( |
| 946 | - WALLET_V5R1_ID_DEFAULT_TESTNET, |
| 947 | - WALLET_SUBWALLET_ID_DEFAULT_TESTNET, |
| 948 | - ), |
| 949 | - }; |
| 950 | - for (version, wallet_id) in [ |
| 951 | - (WalletVersion::V5R1, v5_id), |
| 952 | - (WalletVersion::Wallet, wallet_id), |
| 953 | - ] { |
| 954 | - let recipient = wallet_address(version, 0, wallet_id, network)?; |
| 955 | - verify_recipient_public_key(&recipient, &PUBLIC_KEY, network)?; |
| 956 | - assert!(matches!( |
| 957 | - verify_recipient_public_key(&recipient, &[0x33; 32], network), |
| 958 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 959 | - )); |
| 960 | - let other_network = match network { |
| 961 | - Network::Mainnet => Network::Testnet, |
| 962 | - Network::Testnet => Network::Mainnet, |
| 963 | - }; |
| 964 | - assert!(matches!( |
| 965 | - verify_recipient_public_key(&recipient, &PUBLIC_KEY, other_network), |
| 966 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 967 | - )); |
| 968 | - } |
| 969 | - } |
| 970 | - Ok(()) |
| 971 | - } |
| 972 | - |
| 973 | - #[test] |
| 974 | - fn binds_workchain_aware_standard_ids_and_address_spellings() -> TestResult { |
| 975 | - for (version, wallet_id) in [ |
| 976 | - (WalletVersion::V3R2, 698_983_190), |
| 977 | - (WalletVersion::V4R2, 698_983_190), |
| 978 | - // Values independently pinned by the official V5 wallet-ID module. |
| 979 | - (WalletVersion::V5R1, 8_388_369), |
| 980 | - ] { |
| 981 | - let recipient = wallet_address(version, -1, wallet_id, Network::Mainnet)?; |
| 982 | - verify_recipient_public_key(&recipient, &PUBLIC_KEY, Network::Mainnet)?; |
| 983 | - let raw = TonAddressString::try_from(recipient.as_address().to_hex())?; |
| 984 | - verify_recipient_public_key(&raw, &PUBLIC_KEY, Network::Mainnet)?; |
| 985 | - } |
| 986 | - let recipient = wallet_address(WalletVersion::V5R1, -1, 8_388_605, Network::Testnet)?; |
| 987 | - verify_recipient_public_key(&recipient, &PUBLIC_KEY, Network::Testnet)?; |
| 988 | - Ok(()) |
| 989 | - } |
| 990 | - |
| 991 | - #[test] |
| 992 | - fn rejects_nondefault_ids_unknown_contracts_and_unrelated_addresses() -> TestResult { |
| 993 | - let custom = wallet_address(WalletVersion::V4R2, 0, 42, Network::Mainnet)?; |
| 994 | - assert!(matches!( |
| 995 | - verify_recipient_public_key(&custom, &PUBLIC_KEY, Network::Mainnet), |
| 996 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 997 | - )); |
| 998 | - let unsupported = wallet_address( |
| 999 | - WalletVersion::HLV2R2, |
| 1000 | - 0, |
| 1001 | - WALLET_ID_DEFAULT, |
| 1002 | - Network::Mainnet, |
| 1003 | - )?; |
| 1004 | - assert!(matches!( |
| 1005 | - verify_recipient_public_key(&unsupported, &PUBLIC_KEY, Network::Mainnet), |
| 1006 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 1007 | - )); |
| 1008 | - let unrelated = TonAddressString::from_address( |
| 1009 | - &TonAddress::new(0, TonHash::from_slice(&[0x11; 32])?), |
| 1010 | - Network::Mainnet, |
| 1011 | - ); |
| 1012 | - assert!(matches!( |
| 1013 | - verify_recipient_public_key(&unrelated, &PUBLIC_KEY, Network::Mainnet), |
| 1014 | - Err(EncryptedCommentError::RecipientPublicKeyMismatch) |
| 1015 | - )); |
| 1016 | - Ok(()) |
| 1017 | - } |
| 1018 | -} |
| 1019 |