Skip to content

Commit

Permalink
tx impl - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Oct 2, 2024
1 parent 0bcd41c commit b2ec309
Show file tree
Hide file tree
Showing 8 changed files with 4,184 additions and 22 deletions.
7 changes: 7 additions & 0 deletions mm2src/coins/coin_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,10 @@ pub enum MyAddressError {
UnexpectedDerivationMethod(String),
InternalError(String),
}

impl std::error::Error for MyAddressError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
// This error doesn't wrap another error, so we return None
None
}
}
2 changes: 1 addition & 1 deletion mm2src/coins/tendermint/rpc/tendermint_native_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ mod sealed {
.await
.map_err(|e| Error::client_internal(e.to_string()))?;
let response_body = response_to_string(response).await?;
debug!("Incoming response: {}", response_body);
common::log::info!("Incoming response: {}", response_body);
R::Response::from_string(&response_body)
}
}
Expand Down
56 changes: 46 additions & 10 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ impl TendermintCoin {
withdraw_from: Option<WithdrawFrom>,
) -> Result<(AccountId, Option<H256>), WithdrawError> {
if let TendermintActivationPolicy::PublicKey(_) = self.activation_policy {
println!("inside pubkey");
return Ok((self.account_id.clone(), None));
}

Expand Down Expand Up @@ -1222,7 +1223,7 @@ impl TendermintCoin {
}
}

pub(super) fn any_to_transaction_data(
pub(super) async fn any_to_transaction_data(
&self,
maybe_pk: Option<H256>,
message: Any,
Expand All @@ -1231,24 +1232,57 @@ impl TendermintCoin {
timeout_height: u64,
memo: String,
) -> Result<TransactionData, ErrorReport> {
println!("before maybe");
if let Some(priv_key) = maybe_pk {
println!("after maybe");
let tx_raw = self.any_to_signed_raw_tx(&priv_key, account_info, message, fee, timeout_height, memo)?;
let tx_bytes = tx_raw.to_bytes()?;
let hash = sha256(&tx_bytes);

Ok(TransactionData::new_signed(
return Ok(TransactionData::new_signed(
tx_bytes.into(),
hex::encode_upper(hash.as_slice()),
))
));
};

if let TendermintWalletConnectionType::WalletConnect = self.wallet_connection_type {
println!("inside wc");
let ctx = MmArc::from_weak(&self.ctx)
.ok_or(MyAddressError::InternalError(ERRL!("ctx must be initialized already")))?;
let wallet_connect = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)?;

let SerializedUnsignedTx { tx_json, body_bytes } =
self.any_to_serialized_sign_doc(account_info, message, fee, timeout_height, memo)?;

let my_address = self.my_address()?;
let response = wallet_connect
.cosmos_send_sign_tx_request(tx_json, &self.chain_id.to_string(), my_address)
.await?;
let signature = general_purpose::STANDARD.decode(response.signature.signature)?;
let body_bytes = general_purpose::STANDARD.decode(response.signed.body_bytes)?;
let auth_info_bytes = general_purpose::STANDARD.decode(response.signed.auth_info_bytes)?;
let tx_raw = TxRaw {
body_bytes,
auth_info_bytes,
signatures: vec![signature],
};
let tx_raw: Raw = tx_raw.into();
let tx_bytes = tx_raw.to_bytes()?;
let hash = sha256(&tx_bytes);

return Ok(TransactionData::new_signed(
tx_bytes.into(),
hex::encode_upper(hash.as_slice()),
));
};

let SerializedUnsignedTx { tx_json, .. } = if self.is_keplr_from_ledger() {
self.any_to_legacy_amino_json(account_info, message, fee, timeout_height, memo)
} else {
let SerializedUnsignedTx { tx_json, .. } = if self.is_keplr_from_ledger() {
self.any_to_legacy_amino_json(account_info, message, fee, timeout_height, memo)
} else {
self.any_to_serialized_sign_doc(account_info, message, fee, timeout_height, memo)
}?;
self.any_to_serialized_sign_doc(account_info, message, fee, timeout_height, memo)
}?;

Ok(TransactionData::Unsigned(tx_json))
}
Ok(TransactionData::Unsigned(tx_json))
}

fn gen_create_htlc_tx(
Expand Down Expand Up @@ -2348,8 +2382,10 @@ impl MmCoin for TendermintCoin {

let account_info = coin.account_info(&account_id).await?;

println!("Before any_to_transaction_data");
let tx = coin
.any_to_transaction_data(maybe_pk, msg_payload, &account_info, fee, timeout_height, memo.clone())
.await
.map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?;

let internal_id = {
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/tendermint/tendermint_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ impl MmCoin for TendermintToken {

let tx = platform
.any_to_transaction_data(maybe_pk, msg_payload, &account_info, fee, timeout_height, memo.clone())
.await
.map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?;

let internal_id = {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/kdf_walletconnect/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ pub async fn cosmos_sign_tx_direct_impl(
"sign_doc": sign_doc
});

println!("VALUE: {:?}", value);

let request = SessionRequest {
method: WcRequestMethods::CosmosSignDirect.as_ref().to_owned(),
expiry: Some(Utc::now().timestamp() as u64 + 300),
Expand Down
2 changes: 1 addition & 1 deletion mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ pub async fn initialize_walletconnect(ctx: &MmArc) -> MmResult<(), WalletConnect
ctx.spawner().spawn(wallet_connect.clone().connection_live_watcher());

// load session from storage
wallet_connect.load_session_from_storage().await?;
// wallet_connect.load_session_from_storage().await?;

Ok(())
}
20 changes: 10 additions & 10 deletions mm2src/kdf_walletconnect/src/session/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ pub async fn reply_session_proposal_request(

{
// save session to storage
ctx.storage
.db
.save_session(&session)
.await
.mm_err(|err| WalletConnectCtxError::StorageError(err.to_string()))?;
//ctx.storage
// .db
// .save_session(&session)
// .await
// .mm_err(|err| WalletConnectCtxError::StorageError(err.to_string()))?;

let mut old_session = ctx.session.lock().await;
*old_session = Some(session.clone());
Expand Down Expand Up @@ -133,11 +133,11 @@ pub(crate) async fn process_session_propose_response(

{
// save session to storage
ctx.storage
.db
.save_session(&session)
.await
.mm_err(|err| WalletConnectCtxError::StorageError(err.to_string()))?;
//ctx.storage
// .db
// .save_session(&session)
// .await
// .mm_err(|err| WalletConnectCtxError::StorageError(err.to_string()))?;

let mut old_session = ctx.session.lock().await;
*old_session = Some(session);
Expand Down
Loading

0 comments on commit b2ec309

Please sign in to comment.