Skip to content

Commit

Permalink
feat: support all existing Tx
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Aug 23, 2024
1 parent c043d58 commit 1d8ede5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 104 deletions.
1 change: 1 addition & 0 deletions apps/namadillo/src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const buildTx = async <T>(

return {
txs: txProps,
// TODO: Update broadcastTx to support WrapperTxMsg so encoding isn't necessary!
wrapperTxMsg: tx.encodeTxArgs(wrapperTxProps),
type: txFn.name,
meta: {
Expand Down
3 changes: 0 additions & 3 deletions packages/sdk/examples/submitTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ export const submitTransfer = async (
const { cryptoMemory } = initSync();
const sdk = getSdk(cryptoMemory, nodeUrl, "storage path", nativeToken);

console.log("Revealing public key...");
await sdk.tx.revealPk(signingKey, wrapperTxMsgValue);

console.log("Building transfer transaction...");
const encodedTx = await sdk.tx.buildTransparentTransfer(wrapperTxMsgValue, {
data: [transparentTransferMsgValue],
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/tests/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Tx", () => {
});
expect(encodedTx).toBeDefined();

const txBytes = encodedTx.toBytes();
const txBytes = encodedTx.tx.bytes;
// TODO: Better test here, this is just a placeholder
expect(txBytes.length).toEqual(1000);
expect(addressExistsRoute).toHaveBeenCalledTimes(2);
Expand Down
66 changes: 31 additions & 35 deletions packages/sdk/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -78,9 +78,9 @@ export class Tx {
*/
async buildRevealPk(wrapperTxProps: WrapperTxProps): Promise<EncodedTx> {
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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -148,12 +142,12 @@ export class Tx {
const bondMsg = new Message<WithdrawProps>();
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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
12 changes: 1 addition & 11 deletions packages/sdk/src/tx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
50 changes: 1 addition & 49 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
signing_data: Vec<SigningTxData>,
}

#[wasm_bindgen]
impl BuiltTx {
#[wasm_bindgen(constructor)]
pub fn new(tx: Vec<u8>, signing_data_bytes: JsValue) -> Result<BuiltTx, JsError> {
let signing_data_bytes: Vec<Vec<u8>> = signing_data_bytes
.into_serde()
.expect("Deserializing should not fail");

let mut signing_data: Vec<SigningTxData> = 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<u8> {
self.tx.clone()
}

pub fn tx_hash(&self) -> Result<JsValue, JsError> {
let tx: Tx = borsh::from_slice(&self.tx_bytes())?;
to_js_result(tx.header_hash().to_string())
}

pub fn signing_data_bytes(&self) -> Result<JsValue, JsError> {
let mut signing_data_bytes: Vec<Vec<u8>> = 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 {
Expand Down Expand Up @@ -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()?;
Expand Down
4 changes: 0 additions & 4 deletions packages/shared/lib/src/sdk/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ impl SigningData {
account_public_keys_map,
})
}

pub fn to_bytes(&self) -> Result<Vec<u8>, JsError> {
Ok(borsh::to_vec(&self)?)
}
}

/// Serializable Tx for exported build functions
Expand Down

0 comments on commit 1d8ede5

Please sign in to comment.