Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-rockets-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Expose getUserOpHash utility function
1 change: 1 addition & 0 deletions packages/thirdweb/src/exports/wallets/smart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
createUnsignedUserOp,
signUserOp,
createAndSignUserOp,
getUserOpHash,
} from "../../wallets/smart/lib/userop.js";

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

export async function signMessage({
client,
payload: { message, isRaw },
payload: { message, isRaw, originalMessage, chainId },

Check warning on line 9 in packages/thirdweb/src/wallets/in-app/core/actions/sign-message.enclave.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/actions/sign-message.enclave.ts#L9

Added line #L9 was not covered by tests
storage,
}: {
client: ThirdwebClient;
payload: {
message: string;
isRaw: boolean;
originalMessage?: string;
chainId?: number;
};
storage: ClientScopedStorage;
}) {
Expand All @@ -37,6 +39,8 @@
messagePayload: {
message,
isRaw,
originalMessage,
chainId,

Check warning on line 43 in packages/thirdweb/src/wallets/in-app/core/actions/sign-message.enclave.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/actions/sign-message.enclave.ts#L42-L43

Added lines #L42 - L43 were not covered by tests
},
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,19 @@

return { transactionHash };
},
async signMessage({ message }) {
async signMessage({ message, originalMessage, chainId }) {

Check warning on line 220 in packages/thirdweb/src/wallets/in-app/core/wallet/enclave-wallet.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/enclave-wallet.ts#L220

Added line #L220 was not covered by tests
const messagePayload = (() => {
if (typeof message === "string") {
return { message, isRaw: false };
return { message, isRaw: false, originalMessage, chainId };

Check warning on line 223 in packages/thirdweb/src/wallets/in-app/core/wallet/enclave-wallet.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/enclave-wallet.ts#L223

Added line #L223 was not covered by tests
}
return {
message:
typeof message.raw === "string"
? message.raw
: bytesToHex(message.raw),
isRaw: true,
originalMessage,
chainId,

Check warning on line 232 in packages/thirdweb/src/wallets/in-app/core/wallet/enclave-wallet.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/enclave-wallet.ts#L231-L232

Added lines #L231 - L232 were not covered by tests
};
})();

Expand Down
10 changes: 9 additions & 1 deletion packages/thirdweb/src/wallets/interfaces/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,15 @@ export type Account = {
* const signature = await account.signMessage({ message: 'hello!' });
* ```
*/
signMessage: ({ message }: { message: SignableMessage }) => Promise<Hex>;
signMessage: ({
message,
originalMessage,
chainId,
}: {
message: SignableMessage;
originalMessage?: string;
chainId?: number;
}) => Promise<Hex>;
/**
* Sign the given typed data and return the signature
* @example
Expand Down
68 changes: 53 additions & 15 deletions packages/thirdweb/src/wallets/smart/lib/userop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { maxUint96 } from "ox/Solidity";
import { concat, keccak256, toHex } from "viem";
import { concat } from "viem";
import type { Chain } from "../../../chains/types.js";
import type { ThirdwebClient } from "../../../client/client.js";
import {
Expand All @@ -16,9 +16,11 @@
import type { TransactionReceipt } from "../../../transaction/types.js";
import { encodeAbiParameters } from "../../../utils/abi/encodeAbiParameters.js";
import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js";
import type { Hex } from "../../../utils/encoding/hex.js";
import { type Hex, toHex } from "../../../utils/encoding/hex.js";
import { hexToBytes } from "../../../utils/encoding/to-bytes.js";
import { isThirdwebUrl } from "../../../utils/fetch.js";
import { keccak256 } from "../../../utils/hashing/keccak256.js";
import { stringify } from "../../../utils/json.js";
import { resolvePromisedValue } from "../../../utils/promise/resolve-promised-value.js";
import type { Account } from "../../interfaces/wallet.js";
import type {
Expand Down Expand Up @@ -597,6 +599,54 @@
}): Promise<UserOperationV06 | UserOperationV07> {
const { userOp, chain, entrypointAddress, adminAccount } = args;

const userOpHash = await getUserOpHash({
client: args.client,
userOp,
chain,
entrypointAddress,
});

if (adminAccount.signMessage) {
const signature = await adminAccount.signMessage({
message: {
raw: hexToBytes(userOpHash),
},
originalMessage: stringify(userOp),
chainId: chain.id,
});
return {
...userOp,
signature,
};
}
throw new Error("signMessage not implemented in signingAccount");
}

Check warning on line 623 in packages/thirdweb/src/wallets/smart/lib/userop.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/userop.ts#L622-L623

Added lines #L622 - L623 were not covered by tests

/**
* Get the hash of a user operation.
* @param args - The options for getting the user operation hash
* @returns - The user operation hash
* @example
* ```ts
* import { getUserOpHash } from "thirdweb/wallets/smart";
*
* const userOp = await createUnsignedUserOp(...);
* const userOpHash = await getUserOpHash({
* client,
* userOp,
* chain,
* });
* ```
* @walletUtils
*/
export async function getUserOpHash(args: {
client: ThirdwebClient;
userOp: UserOperationV06 | UserOperationV07;
chain: Chain;
entrypointAddress?: string;
}): Promise<Hex> {
const { userOp, chain, entrypointAddress } = args;

const entrypointVersion = getEntryPointVersion(
entrypointAddress || ENTRYPOINT_ADDRESS_v0_6,
);
Expand All @@ -623,19 +673,7 @@
userOp: userOp as UserOperationV06,
});
}

if (adminAccount.signMessage) {
const signature = await adminAccount.signMessage({
message: {
raw: hexToBytes(userOpHash),
},
});
return {
...userOp,
signature,
};
}
throw new Error("signMessage not implemented in signingAccount");
return userOpHash;
}

async function getAccountInitCode(options: {
Expand Down