Skip to content

Commit

Permalink
[wallet connect] add executeSerializedMoveCall (MystenLabs#3072)
Browse files Browse the repository at this point in the history
  • Loading branch information
gegaowp authored Jul 8, 2022
1 parent 3d71cea commit b9e4b12
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
41 changes: 29 additions & 12 deletions wallet/src/background/Transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import Browser from 'webextension-polyfill';

import { Window } from './Window';

import type { MoveCallTransaction } from '@mysten/sui.js';
import type { TransactionRequest } from '_payloads/transactions';
import type { TransactionRequestResponse } from '_payloads/transactions/ui/TransactionRequestResponse';
import type { ContentScriptConnection } from '_src/background/connections/ContentScriptConnection';

type Transaction = TransactionRequest['tx'];

const TX_STORE_KEY = 'transactions';

function openTxWindow(txRequestID: string) {
Expand All @@ -26,11 +25,13 @@ class Transactions {
private _txResponseMessages = new Subject<TransactionRequestResponse>();

public async executeTransaction(
tx: Transaction,
tx: MoveCallTransaction | undefined,
txBytes: Uint8Array | undefined,
connection: ContentScriptConnection
) {
const txRequest = this.createTransactionRequest(
tx,
txBytes,
connection.origin,
connection.originFavIcon
);
Expand Down Expand Up @@ -92,18 +93,34 @@ class Transactions {
}

private createTransactionRequest(
tx: Transaction,
tx: MoveCallTransaction | undefined,
txBytes: Uint8Array | undefined,
origin: string,
originFavIcon?: string
): TransactionRequest {
return {
id: uuidV4(),
tx,
approved: null,
origin,
originFavIcon,
createdDate: new Date().toISOString(),
};
if (tx !== undefined) {
return {
id: uuidV4(),
approved: null,
origin,
originFavIcon,
createdDate: new Date().toISOString(),
type: 'move-call',
tx,
};
} else if (txBytes !== undefined) {
return {
id: uuidV4(),
approved: null,
origin,
originFavIcon,
createdDate: new Date().toISOString(),
type: 'serialized-move-call',
txBytes,
};
} else {
throw new Error("Either tx or txBytes needs to be defined.");
}
}

private async saveTransactionRequests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class ContentScriptConnection extends Connection {
try {
const result = await Transactions.executeTransaction(
payload.transaction,
payload.transactionBytes,
this
);
this.send(
Expand Down
10 changes: 10 additions & 0 deletions wallet/src/dapp-interface/DAppInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ export class DAppInterface {
);
}

public executeSerializedMoveCall(transactionBytes: Uint8Array) {
return mapToPromise(
this.send<ExecuteTransactionRequest, ExecuteTransactionResponse>({
type: 'execute-transaction-request',
transactionBytes,
}),
(response) => response.result
);
}

private send<
RequestPayload extends Payload,
ResponsePayload extends Payload | void = void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type { BasePayload, Payload } from '_payloads';

export interface ExecuteTransactionRequest extends BasePayload {
type: 'execute-transaction-request';
transaction: MoveCallTransaction;
transaction?: MoveCallTransaction;
transactionBytes?: Uint8Array;
}

export function isExecuteTransactionRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import type { MoveCallTransaction, TransactionResponse } from '@mysten/sui.js';
export type TransactionRequest = {
id: string;
approved: boolean | null;
tx: MoveCallTransaction;
origin: string;
originFavIcon?: string;
txResult?: TransactionResponse;
txResultError?: string;
createdDate: string;
};
} & (
{
type: 'move-call';
tx: MoveCallTransaction;
} |
{
type: 'serialized-move-call';
txBytes: Uint8Array;
}
);
7 changes: 5 additions & 2 deletions wallet/src/ui/app/pages/dapp-tx-approval/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function DappTxApprovalPage() {
// TODO: add more tx types/make it generic
const valuesContent = useMemo(
() =>
txRequest?.tx
txRequest?.type === 'move-call'
? [
{ label: 'Transaction type', content: 'MoveCall' },
{
Expand All @@ -89,7 +89,10 @@ export function DappTxApprovalPage() {
},
{ label: 'Gas budget', content: txRequest.tx.gasBudget },
]
: [],
: [
{ label: 'Transaction type', content: 'SerializedMoveCall' },
{ label: 'Contents', content: txRequest?.txBytes },
],
[txRequest]
);
return (
Expand Down
10 changes: 9 additions & 1 deletion wallet/src/ui/app/redux/slices/transaction-requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createEntityAdapter,
createSlice,
} from '@reduxjs/toolkit';
import { Base64DataBuffer } from '@mysten/sui.js';

import type { TransactionResponse } from '@mysten/sui.js';
import type { PayloadAction } from '@reduxjs/toolkit';
Expand Down Expand Up @@ -45,7 +46,14 @@ export const respondToTransactionRequest = createAsyncThunk<
if (approved) {
const signer = api.getSignerInstance(keypairVault.getKeyPair());
try {
txResult = await signer.executeMoveCall(txRequest.tx);
if (txRequest.type === 'move-call') {
txResult = await signer.executeMoveCall(txRequest.tx);
} else if (txRequest.type === 'serialized-move-call') {
const txBytes = new Base64DataBuffer(txRequest.txBytes);
txResult = await signer.signAndExecuteTransaction(txBytes);
} else {
throw new Error(`Either tx or txBytes needs to be defined.`);
}
} catch (e) {
tsResultError = (e as Error).message;
}
Expand Down

0 comments on commit b9e4b12

Please sign in to comment.