From 96c35fd1955f4d1ccb3d550de415f65b294f7948 Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Fri, 23 Aug 2024 09:39:28 -0400 Subject: [PATCH] feat: support all existing Tx --- apps/namadillo/src/lib/query.ts | 1 + packages/sdk/src/index.ts | 2 +- packages/sdk/src/tx/tx.ts | 66 ++++++++++++++---------------- packages/sdk/src/tx/types.ts | 12 +----- packages/shared/lib/src/sdk/mod.rs | 50 +--------------------- packages/shared/lib/src/sdk/tx.rs | 4 -- 6 files changed, 35 insertions(+), 100 deletions(-) diff --git a/apps/namadillo/src/lib/query.ts b/apps/namadillo/src/lib/query.ts index 62b13a5e44..2c5899efa5 100644 --- a/apps/namadillo/src/lib/query.ts +++ b/apps/namadillo/src/lib/query.ts @@ -112,6 +112,7 @@ export const buildTx = async ( return { txs: txProps, + // TODO: Update broadcastTx to support WrapperTxMsg so encoding isn't necessary! wrapperTxMsg: tx.encodeTxArgs(wrapperTxProps), type: txFn.name, meta: { diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 52e6a39523..bc9af84b4d 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -21,7 +21,7 @@ export type { Unbonds, } from "./rpc"; -export { BuiltTx, TxType, TxTypeLabel } from "./tx"; +export { TxType, TxTypeLabel } from "./tx"; export type { EncodedTx, SignedTx, SupportedTx } from "./tx"; export { Sdk } from "./sdk"; diff --git a/packages/sdk/src/tx/tx.ts b/packages/sdk/src/tx/tx.ts index de110bdbf9..7987cca030 100644 --- a/packages/sdk/src/tx/tx.ts +++ b/packages/sdk/src/tx/tx.ts @@ -62,12 +62,12 @@ export class Tx { new TransparentTransferMsgValue(transferProps) ); - const builtTx = await this.sdk.build_transparent_transfer( + const serializedTx = await this.sdk.build_transparent_transfer( encodedTransfer, encodedWrapperArgs ); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -78,9 +78,9 @@ export class Tx { */ async buildRevealPk(wrapperTxProps: WrapperTxProps): Promise { const encodedWrapperArgs = this.encodeTxArgs(wrapperTxProps); - const builtTx = await this.sdk.build_reveal_pk(encodedWrapperArgs); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const serializedTx = await this.sdk.build_reveal_pk(encodedWrapperArgs); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -101,14 +101,8 @@ export class Tx { encodedBond, encodedWrapperArgs ); - - try { - const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); - return new EncodedTx(encodedWrapperArgs, tx); - } catch (e) { - console.warn(e); - throw Error(`${e}`); - } + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -126,12 +120,12 @@ export class Tx { const encodedWrapperArgs = this.encodeTxArgs(wrapperTxProps); const encodedUnbond = unbondMsg.encode(new UnbondMsgValue(unbondProps)); - const builtTx = await this.sdk.build_unbond( + const serializedTx = await this.sdk.build_unbond( encodedUnbond, encodedWrapperArgs ); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -148,12 +142,12 @@ export class Tx { const bondMsg = new Message(); const encodedWrapperArgs = this.encodeTxArgs(wrapperTxProps); const encodedWithdraw = bondMsg.encode(new WithdrawMsgValue(withdrawProps)); - const builtTx = await this.sdk.build_withdraw( + const serializedTx = await this.sdk.build_withdraw( encodedWithdraw, encodedWrapperArgs ); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -172,12 +166,12 @@ export class Tx { const encodedRedelegate = redelegateMsg.encode( new RedelegateMsgValue(redelegateProps) ); - const builtTx = await this.sdk.build_redelegate( + const serializedTx = await this.sdk.build_redelegate( encodedRedelegate, encodedWrapperArgs ); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -196,12 +190,12 @@ export class Tx { const encodedIbcTransfer = ibcTransferMsg.encode( new IbcTransferMsgValue(ibcTransferProps) ); - const builtTx = await this.sdk.build_ibc_transfer( + const serializedTx = await this.sdk.build_ibc_transfer( encodedIbcTransfer, encodedWrapperArgs ); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -220,12 +214,12 @@ export class Tx { const encodedEthBridgeTransfer = ethBridgeTransferMsg.encode( new EthBridgeTransferMsgValue(ethBridgeTransferProps) ); - const builtTx = await this.sdk.build_eth_bridge_transfer( + const serializedTx = await this.sdk.build_eth_bridge_transfer( encodedEthBridgeTransfer, encodedWrapperArgs ); - - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -245,11 +239,12 @@ export class Tx { new VoteProposalMsgValue(voteProposalProps) ); - const builtTx = await this.sdk.build_vote_proposal( + const serializedTx = await this.sdk.build_vote_proposal( encodedVoteProposal, encodedWrapperArgs ); - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -268,12 +263,12 @@ export class Tx { const encodedClaimRewards = claimRewardsMsg.encode( new ClaimRewardsMsgValue(claimRewardsProps) ); - - const builtTx = await this.sdk.build_claim_rewards( + const serializedTx = await this.sdk.build_claim_rewards( encodedClaimRewards, encodedWrapperArgs ); - return new EncodedTx(encodedWrapperArgs, builtTx); + const tx = deserialize(Buffer.from(serializedTx), TxMsgValue); + return new EncodedTx(encodedWrapperArgs, tx); } /** @@ -288,7 +283,8 @@ export class Tx { return msg.encode(txMsgValue); }); - return SdkWasm.build_batch(encodedTxs.map((tx) => [...tx])); + const batch = SdkWasm.build_batch(encodedTxs.map((tx) => [...tx])); + return deserialize(Buffer.from(batch), TxMsgValue); } /** diff --git a/packages/sdk/src/tx/types.ts b/packages/sdk/src/tx/types.ts index ca5e97c875..4b942c4dfb 100644 --- a/packages/sdk/src/tx/types.ts +++ b/packages/sdk/src/tx/types.ts @@ -14,16 +14,6 @@ export class EncodedTx { public readonly tx: TxProps ) {} - /** - * Return serialized tx bytes for external signing. This will clear - * the BuiltTx struct instance from wasm memory, then return the bytes. - * @returns Serialized tx bytes - */ - toBytes(): Uint8Array { - const bytes = new Uint8Array(this.tx.bytes); - return bytes; - } - /** * Return the inner Tx hash of the built Tx * @returns string of tx hash @@ -49,5 +39,5 @@ export class SignedTx { ) {} } -export { BuiltTx, TxType, TxTypeLabel } from "@namada/shared"; +export { TxType, TxTypeLabel } from "@namada/shared"; export type { SupportedTx } from "@namada/shared"; diff --git a/packages/shared/lib/src/sdk/mod.rs b/packages/shared/lib/src/sdk/mod.rs index c4475dcc0e..78f5c0ea87 100644 --- a/packages/shared/lib/src/sdk/mod.rs +++ b/packages/shared/lib/src/sdk/mod.rs @@ -32,54 +32,6 @@ use namada_sdk::{Namada, NamadaImpl}; use std::str::FromStr; use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue}; -#[wasm_bindgen] -#[derive(Clone)] -pub struct BuiltTx { - tx: Vec, - signing_data: Vec, -} - -#[wasm_bindgen] -impl BuiltTx { - #[wasm_bindgen(constructor)] - pub fn new(tx: Vec, signing_data_bytes: JsValue) -> Result { - let signing_data_bytes: Vec> = signing_data_bytes - .into_serde() - .expect("Deserializing should not fail"); - - let mut signing_data: Vec = vec![]; - - for bytes in signing_data_bytes { - let sd: tx::SigningData = borsh::from_slice(&bytes)?; - let signing_tx_data: SigningTxData = sd.to_signing_tx_data()?; - signing_data.push(signing_tx_data); - } - - Ok(BuiltTx { tx, signing_data }) - } - - pub fn tx_bytes(&self) -> Vec { - self.tx.clone() - } - - pub fn tx_hash(&self) -> Result { - let tx: Tx = borsh::from_slice(&self.tx_bytes())?; - to_js_result(tx.header_hash().to_string()) - } - - pub fn signing_data_bytes(&self) -> Result { - let mut signing_data_bytes: Vec> = vec![]; - - for signing_tx_data in self.signing_data.clone() { - let signing_data = tx::SigningData::from_signing_tx_data(signing_tx_data)?; - let bytes = signing_data.to_bytes()?; - signing_data_bytes.push(bytes); - } - - Ok(JsValue::from_serde(&signing_data_bytes)?) - } -} - /// Represents the Sdk public API. #[wasm_bindgen] pub struct Sdk { @@ -299,7 +251,7 @@ impl Sdk { let mut txs: Vec<(Tx, SigningTxData)> = vec![]; - // Iterate through provided BuiltTx and deserialize bytes to Tx + // Iterate through provided tx::Tx and deserialize bytes to Namada Tx for built_tx in built_txs.into_iter() { let tx_bytes = built_tx.tx_bytes(); let signing_tx_data = built_tx.signing_tx_data()?; diff --git a/packages/shared/lib/src/sdk/tx.rs b/packages/shared/lib/src/sdk/tx.rs index 1d4fdd8d08..019ea48a72 100644 --- a/packages/shared/lib/src/sdk/tx.rs +++ b/packages/shared/lib/src/sdk/tx.rs @@ -101,10 +101,6 @@ impl SigningData { account_public_keys_map, }) } - - pub fn to_bytes(&self) -> Result, JsError> { - Ok(borsh::to_vec(&self)?) - } } /// Serializable Tx for exported build functions