Skip to content

Commit

Permalink
Handle arguments convertion when submitting a package publish transac…
Browse files Browse the repository at this point in the history
…tion (#432)
  • Loading branch information
0xmaayan authored Oct 4, 2024
1 parent 610b17b commit bde8112
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/new-readers-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aptos-labs/wallet-adapter-core": minor
---

Handle arguments conversion when submitting a package publish transaction
11 changes: 11 additions & 0 deletions packages/wallet-adapter-core/src/WalletCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
generateRawTransaction,
SimpleTransaction,
NetworkToChainId,
Hex,
} from "@aptos-labs/ts-sdk";
import EventEmitter from "eventemitter3";
import {
Expand Down Expand Up @@ -67,6 +68,7 @@ import {
fetchDevnetChainId,
generalizedErrorMessage,
getAptosConfig,
handlePublishPackageTransaction,
isAptosNetwork,
isRedirectable,
removeLocalStorage,
Expand Down Expand Up @@ -839,6 +841,15 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
throw new WalletSignAndSubmitMessageError("SCAM SITE DETECTED")
.message;
}

if (
transactionInput.data.function === "0x1::code::publish_package_txn"
) {
({
metadataBytes: transactionInput.data.functionArguments[0],
byteCode: transactionInput.data.functionArguments[1],
} = handlePublishPackageTransaction(transactionInput));
}
}

this.ensureWalletExists(this._wallet);
Expand Down
39 changes: 38 additions & 1 deletion packages/wallet-adapter-core/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import {
Aptos,
AptosConfig,
EntryFunctionArgumentTypes,
Hex,
Network,
NetworkToNodeAPI,
Serializable,
SimpleEntryFunctionArgumentTypes,
} from "@aptos-labs/ts-sdk";
import { NetworkInfo as StandardNetworkInfo } from "@aptos-labs/wallet-standard";
import { convertNetwork } from "../LegacyWalletPlugins/conversion";
import { NetworkInfo } from "../LegacyWalletPlugins/types";
import {
InputTransactionData,
NetworkInfo,
} from "../LegacyWalletPlugins/types";
import { DappConfig } from "../WalletCore";
import { WalletSignAndSubmitMessageError } from "../error";

export function isMobile(): boolean {
return /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune/i.test(
Expand Down Expand Up @@ -110,3 +115,35 @@ export const fetchDevnetChainId = async (): Promise<number> => {
const aptos = new Aptos(); // default to devnet
return await aptos.getChainId();
};

/**
* A helper function to handle the publish package transaction.
* The Aptos SDK expects the metadataBytes and byteCode to be Uint8Array, but in case the arguments are passed in
* as a string, this function converts the string to Uint8Array.
*/
export const handlePublishPackageTransaction = (
transactionInput: InputTransactionData
) => {
// convert the first argument, metadataBytes, to uint8array if is a string
let metadataBytes = transactionInput.data.functionArguments[0];
if (typeof metadataBytes === "string") {
metadataBytes = Hex.fromHexInput(metadataBytes).toUint8Array();
}

// convert the second argument, byteCode, to uint8array if is a string
let byteCode = transactionInput.data.functionArguments[1];
if (Array.isArray(byteCode)) {
byteCode = byteCode.map((byte) => {
if (typeof byte === "string") {
return Hex.fromHexInput(byte).toUint8Array();
}
return byte;
});
} else {
throw new WalletSignAndSubmitMessageError(
"The bytecode argument must be an array."
).message;
}

return { metadataBytes, byteCode };
};

0 comments on commit bde8112

Please sign in to comment.