Skip to content

Commit

Permalink
feat: improve signing & broadcast API
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Aug 23, 2024
1 parent 1d8ede5 commit 48e796b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 119 deletions.
33 changes: 13 additions & 20 deletions apps/namadillo/src/lib/query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { EncodedTx } from "@heliax/namada-sdk/web";
import { getIntegration } from "@namada/integrations";
import {
Account,
AccountType,
Signer,
TxMsgValue,
TxProps,
WrapperTxMsgValue,
WrapperTxProps,
WrapperTxMsgValue as WrapperTxProps,
} from "@namada/types";
import { getIndexerApi } from "atoms/api";
import { chainParametersAtom } from "atoms/chain";
Expand All @@ -24,7 +23,7 @@ export type TransactionPair<T> = {
export type EncodedTxData<T> = {
type: string;
txs: TxProps[];
wrapperTxMsg: Uint8Array;
wrapperTxProps: WrapperTxProps;
meta?: {
props: T[];
};
Expand All @@ -36,7 +35,7 @@ export type TransactionNotification = {
};

export type PreparedTransaction<T> = {
encodedTx: EncodedTx;
encodedTx: WrapperTxProps;
signedTx: Uint8Array;
meta: T;
};
Expand All @@ -47,7 +46,7 @@ const getTxProps = (
account: Account,
gasConfig: GasConfig,
chain: ChainSettings
): WrapperTxMsgValue => {
): WrapperTxProps => {
invariant(
!!account.publicKey,
"Account doesn't contain a publicKey attached to it"
Expand Down Expand Up @@ -77,18 +76,18 @@ const isPublicKeyRevealed = async (address: Address): Promise<boolean> => {
* Builds an batch transactions based on the provided query properties.
* Each transaction is built through the provided transaction function `txFn`.
* @param {T[]} queryProps - An array of properties used to build transactions.
* @param {(WrapperTxProps, T) => Promise<EncodedTx>} txFn - Function to build each transaction.
* @param {(WrapperTxProps, T) => Promise<TxMsgValue>} txFn - Function to build each transaction.
*/
export const buildTx = async <T>(
account: Account,
gasConfig: GasConfig,
chain: ChainSettings,
queryProps: T[],
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<EncodedTx>
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<TxMsgValue>
): Promise<EncodedTxData<T>> => {
const { tx } = await getSdkInstance();
const wrapperTxProps = getTxProps(account, gasConfig, chain);
const txs: EncodedTx[] = [];
const txs: TxMsgValue[] = [];
const txProps: TxProps[] = [];

// Determine if RevealPK is needed:
Expand All @@ -105,15 +104,14 @@ export const buildTx = async <T>(
txs.push(...encodedTxs);

if (account.type === AccountType.Ledger) {
txProps.push(...txs.map(({ tx }) => tx));
txProps.push(...txs);
} else {
txProps.push(tx.buildBatch(txs.map(({ tx }) => tx)));
txProps.push(tx.buildBatch(txs));
}

return {
txs: txProps,
// TODO: Update broadcastTx to support WrapperTxMsg so encoding isn't necessary!
wrapperTxMsg: tx.encodeTxArgs(wrapperTxProps),
wrapperTxProps,
type: txFn.name,
meta: {
props: queryProps,
Expand Down Expand Up @@ -167,7 +165,7 @@ export const buildTxPair = async <T>(
gasConfig: GasConfig,
chain: ChainSettings,
queryProps: T[],
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<EncodedTx>,
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<TxMsgValue>,
owner: string
): Promise<TransactionPair<T>> => {
const encodedTxData = await buildTx<T>(
Expand Down Expand Up @@ -200,12 +198,7 @@ export const broadcastTx = async <T>(
})
);
try {
// TODO: rpc.broadcastTx returns a TxResponseProps object now, containing hashes and
// applied status of each commitment
await rpc.broadcastTx({
wrapperTxMsg: encodedTx.wrapperTxMsg,
tx: signedTx,
});
await rpc.broadcastTx(signedTx, encodedTx.wrapperTxProps);
eventType &&
window.dispatchEvent(
new CustomEvent(`${eventType}.Success`, {
Expand Down
11 changes: 4 additions & 7 deletions packages/sdk/examples/submitTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const submitTransfer = async (
const { chainId, publicKey } = tx;
const { source, target, amount } = transfer;

const wrapperTxMsgValue = {
const wrapperTxProps = {
token: nativeToken,
feeAmount: BigNumber(5),
gasLimit: BigNumber(20_000),
Expand All @@ -40,18 +40,15 @@ export const submitTransfer = async (
const sdk = getSdk(cryptoMemory, nodeUrl, "storage path", nativeToken);

console.log("Building transfer transaction...");
const encodedTx = await sdk.tx.buildTransparentTransfer(wrapperTxMsgValue, {
const encodedTx = await sdk.tx.buildTransparentTransfer(wrapperTxProps, {
data: [transparentTransferMsgValue],
});

console.log("Signing transaction...");
const signedTx = await sdk.signing.sign(encodedTx.tx, signingKey);
const signedTx = await sdk.signing.sign(encodedTx, signingKey);

console.log("Broadcasting transaction...");
await sdk.rpc.broadcastTx({
wrapperTxMsg: encodedTx.wrapperTxMsg,
tx: signedTx,
});
await sdk.rpc.broadcastTx(signedTx, wrapperTxProps);
process.exit(0);
} catch (error) {
console.error("Error:", error);
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 @@ -22,7 +22,7 @@ export type {
} from "./rpc";

export { TxType, TxTypeLabel } from "./tx";
export type { EncodedTx, SignedTx, SupportedTx } from "./tx";
export type { SupportedTx } from "./tx";

export { Sdk } from "./sdk";

Expand Down
24 changes: 18 additions & 6 deletions packages/sdk/src/rpc/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import {
Sdk as SdkWasm,
TransferToEthereum,
} from "@namada/shared";
import { TxResponseMsgValue, TxResponseProps } from "@namada/types";
import {
Message,
TxResponseMsgValue,
TxResponseProps,
WrapperTxMsgValue,
WrapperTxProps,
} from "@namada/types";

import { SignedTx } from "../tx/types";
import {
Balance,
BondsResponse,
Expand Down Expand Up @@ -211,12 +216,19 @@ export class Rpc {
/**
* Broadcast a Tx to the ledger
* @async
* @param signedTx - Transaction with signature
* @param signedTxBytes - Transaction with signature
* @param args - WrapperTxProps
* @returns TxResponseProps object
*/
async broadcastTx(signedTx: SignedTx): Promise<TxResponseProps> {
const { wrapperTxMsg, tx } = signedTx;
const response = await this.sdk.process_tx(tx, wrapperTxMsg);
async broadcastTx(
signedTxBytes: Uint8Array,
args: WrapperTxProps
): Promise<TxResponseProps> {
const wrapperTxMsgValue = new WrapperTxMsgValue(args);
const msg = new Message<WrapperTxMsgValue>();
const encodedArgs = msg.encode(wrapperTxMsgValue);

const response = await this.sdk.process_tx(signedTxBytes, encodedArgs);
return deserialize(Buffer.from(response), TxResponseMsgValue);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/tests/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ describe("Tx", () => {
amount: BigNumber(123),
};

const encodedTx = await tx.buildTransparentTransfer(txProps, {
const builtTx = await tx.buildTransparentTransfer(txProps, {
data: [transferProps],
});
expect(encodedTx).toBeDefined();
expect(tx).toBeDefined();

const txBytes = encodedTx.tx.bytes;
const txBytes = builtTx.bytes;
// TODO: Better test here, this is just a placeholder
expect(txBytes.length).toEqual(1000);
expect(addressExistsRoute).toHaveBeenCalledTimes(2);
Expand Down
Loading

0 comments on commit 48e796b

Please sign in to comment.