| 6 | pub use ton_connect::gram_ton_connect_crypto; |
| 7 |
| 8 | use base64::{Engine as _, engine::general_purpose::STANDARD}; |
| 9 | use ton::{ |
| 10 | block_tlb::Msg, |
| 11 | ton_core::{cell::TonCell, traits::tlb::TLB}, |
| 12 | }; |
| 13 |
| 14 | uniffi::setup_scaffolding!(); |
| 15 | wallet_engine::uniffi_reexport_scaffolding!(); |
| 16 |
| 17 | #[derive(Debug, thiserror::Error, uniffi::Error)] |
| 18 | pub enum GramWalletError { |
| 19 | #[error("Invalid Gram Wallet protocol data")] |
| 20 | InvalidData, |
| 21 | #[error("{message}")] |
| 22 | Protocol { message: String }, |
| 23 | } |
| 24 |
| 25 | type Result<T> = std::result::Result<T, GramWalletError>; |
| 26 |
| 27 | fn checked<T, E>(value: std::result::Result<T, E>) -> Result<T> { |
| 28 | value.map_err(|_| GramWalletError::InvalidData) |
| 29 | } |
| 30 |
| 31 | /// Validates the single-root BoC envelope before passing untrusted cells to the wallet. |
| 32 | #[uniffi::export] |
| 33 | pub fn is_valid_cell_boc(boc: String) -> bool { |
| 34 | ton_connect_core::CellBoc::try_from(boc).is_ok() |
| 35 | } |
| 36 |
| 37 | /// Hash of the message body, as used by the MTProto transaction matcher. |
| 38 | /// This deliberately differs from the hash of the complete external message. |
| 39 | #[uniffi::export] |
| 40 | pub fn message_body_hash(boc: String) -> Result<String> { |
| 41 | let message = checked(Msg::<TonCell>::from_boc(checked(STANDARD.decode(boc))?))?; |
| 42 | Ok(STANDARD.encode(checked(message.body.value.cell_hash())?.as_slice())) |
| 43 | } |
| 44 |
| 45 | #[cfg(test)] |
| 46 | mod tests { |
| 47 | use super::*; |
| 48 | use ton::{ |
| 49 | block_tlb::{CommonMsgInfo, CommonMsgInfoExtIn, CommonMsgInfoInt}, |
| 50 | ton_core::types::{ |
| 51 | TonAddress, |
| 52 | tlb_core::{EitherRefLayout, MsgAddressExt, TLBCoins, TLBEitherRef}, |
| 53 | }, |
| 54 | }; |
| 55 |
| 56 | #[test] |
| 57 | fn hashes_body_for_both_delivery_forms_and_cell_layouts() { |
| 58 | let address: TonAddress = |
| 59 | "0:1111111111111111111111111111111111111111111111111111111111111111" |
| 60 | .parse() |
| 61 | .unwrap(); |
| 62 | let mut body = TonCell::builder(); |
| 63 | body.write_bits([0x12, 0x34], 16).unwrap(); |
| 64 | let body = body.build().unwrap(); |
| 65 | let expected = STANDARD.encode(body.cell_hash().unwrap().as_slice()); |
| 66 | let external = CommonMsgInfo::ExtIn(CommonMsgInfoExtIn { |
| 67 | src: MsgAddressExt::NONE, |
| 68 | dst: address.to_msg_address_int(), |
| 69 | import_fee: TLBCoins::ZERO, |
| 70 | }); |
| 71 | let internal = CommonMsgInfo::Int(CommonMsgInfoInt::new( |
| 72 | address.to_msg_address(), |
| 73 | TLBCoins::ZERO, |
| 74 | )); |
| 75 | for info in [external, internal] { |
| 76 | for layout in [EitherRefLayout::ToRef, EitherRefLayout::ToCell] { |
| 77 | let mut message = Msg::new(info.clone(), body.clone()); |
| 78 | message.body = TLBEitherRef::new_with_layout(body.clone(), layout); |
| 79 | assert_eq!( |
| 80 | message_body_hash(STANDARD.encode(message.to_boc().unwrap())).unwrap(), |
| 81 | expected |
| 82 | ); |
| 83 | assert_ne!( |
| 84 | STANDARD.encode(message.to_cell().unwrap().cell_hash().unwrap().as_slice()), |
| 85 | expected |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | assert!(message_body_hash("invalid".to_owned()).is_err()); |
| 90 | assert!(message_body_hash(STANDARD.encode(body.to_boc().unwrap())).is_err()); |
| 91 | } |
| 92 | } |
| 93 |