Skip to content

Commit e888387

Browse files
committed
fix typed data types
1 parent 486553b commit e888387

File tree

7 files changed

+72
-56
lines changed

7 files changed

+72
-56
lines changed

packages/thirdweb/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@
181181
"fuse.js": "7.0.0",
182182
"input-otp": "^1.4.1",
183183
"mipd": "0.0.7",
184-
"ox": "0.4.0",
185184
"uqr": "0.1.2",
186185
"viem": "2.21.54"
187186
},
@@ -200,7 +199,8 @@
200199
"react-native-passkey": "^3",
201200
"react-native-quick-crypto": ">=0.7.0-rc.6 || >=0.7",
202201
"react-native-svg": "^15",
203-
"typescript": ">=5.0.4"
202+
"typescript": ">=5.0.4",
203+
"ox": "0.4.0"
204204
},
205205
"peerDependenciesMeta": {
206206
"react": {

packages/thirdweb/src/utils/hashing/hashMessage.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { type ByteArray, type SignableMessage, concat } from "viem";
1+
import { Bytes as ox__Bytes } from "ox";
22
import type { Hex } from "../encoding/hex.js";
33
import { stringToBytes, toBytes } from "../encoding/to-bytes.js";
4+
import type { SignableMessage } from "../types.js";
45
import { keccak256 } from "./keccak256.js";
56

67
const presignMessagePrefix = "\x19Ethereum Signed Message:\n";
78
type To = "hex" | "bytes";
89

910
type HashMessage<TTo extends To> =
10-
| (TTo extends "bytes" ? ByteArray : never)
11+
| (TTo extends "bytes" ? ox__Bytes.Bytes : never)
1112
| (TTo extends "hex" ? Hex : never);
1213

1314
/**
@@ -38,5 +39,5 @@ export function hashMessage<TTo extends To = "hex">(
3839
const prefixBytes = stringToBytes(
3940
`${presignMessagePrefix}${messageBytes.length}`,
4041
);
41-
return keccak256(concat([prefixBytes, messageBytes]), to_);
42+
return keccak256(ox__Bytes.concat(prefixBytes, messageBytes), to_);
4243
}

packages/thirdweb/src/utils/hashing/hashTypedData.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import {
2-
type AbiParameter,
3-
type TypedData,
4-
type TypedDataDefinition,
5-
concat,
6-
getTypesForEIP712Domain,
7-
hashDomain,
8-
validateTypedData,
9-
} from "viem";
1+
import type * as ox__AbiParameters from "ox/AbiParameters";
2+
import * as ox__Bytes from "ox/Bytes";
3+
import * as ox__TypedData from "ox/TypedData";
104
import { encodeAbiParameters } from "../abi/encodeAbiParameters.js";
115
import { type Hex, toHex } from "../encoding/hex.js";
126
import { keccak256 } from "./keccak256.js";
@@ -17,15 +11,17 @@ type MessageTypeProperty = {
1711
};
1812

1913
export type HashTypedDataParams<
20-
typedData extends TypedData | Record<string, unknown> = TypedData,
14+
typedData extends
15+
| ox__TypedData.TypedData
16+
| Record<string, unknown> = ox__TypedData.TypedData,
2117
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
22-
> = TypedDataDefinition<typedData, primaryType>;
18+
> = ox__TypedData.Definition<typedData, primaryType>;
2319

2420
/**
2521
* @internal
2622
*/
2723
export function hashTypedData<
28-
const typedData extends TypedData | Record<string, unknown>,
24+
const typedData extends ox__TypedData.TypedData | Record<string, unknown>,
2925
primaryType extends keyof typedData | "EIP712Domain",
3026
>(parameters: HashTypedDataParams<typedData, primaryType>): Hex {
3127
const {
@@ -34,13 +30,13 @@ export function hashTypedData<
3430
primaryType,
3531
} = parameters as HashTypedDataParams;
3632
const types = {
37-
EIP712Domain: getTypesForEIP712Domain({ domain }),
33+
EIP712Domain: ox__TypedData.extractEip712DomainTypes(domain),
3834
...parameters.types,
3935
};
4036

4137
// Need to do a runtime validation check on addresses, byte ranges, integer ranges, etc
4238
// as we can't statically check this with TypeScript.
43-
validateTypedData({
39+
ox__TypedData.validate({
4440
domain,
4541
message,
4642
primaryType,
@@ -50,7 +46,7 @@ export function hashTypedData<
5046
const parts: Hex[] = ["0x1901"];
5147
if (domain)
5248
parts.push(
53-
hashDomain({
49+
ox__TypedData.hashDomain({
5450
domain,
5551
types: types as Record<string, MessageTypeProperty[]>,
5652
}),
@@ -69,7 +65,7 @@ export function hashTypedData<
6965
parts.push(hashedStruct);
7066
}
7167

72-
return keccak256(concat(parts));
68+
return keccak256(ox__Bytes.concat(...parts.map((p) => ox__Bytes.fromHex(p))));
7369
}
7470

7571
function encodeData({
@@ -81,7 +77,7 @@ function encodeData({
8177
primaryType: string;
8278
types: Record<string, MessageTypeProperty[]>;
8379
}) {
84-
const encodedTypes: AbiParameter[] = [{ type: "bytes32" }];
80+
const encodedTypes: ox__AbiParameters.Parameter[] = [{ type: "bytes32" }];
8581
const encodedValues: unknown[] = [hashType({ primaryType, types })];
8682

8783
if (!types[primaryType]) throw new Error("Invalid types");
@@ -168,7 +164,7 @@ function encodeField({
168164
// biome-ignore lint/suspicious/noExplicitAny: Can't anticipate types of nested values
169165
value: any;
170166
// biome-ignore lint/suspicious/noExplicitAny: Can't anticipate types of nested values
171-
}): [type: AbiParameter, value: any] {
167+
}): [type: ox__AbiParameters.Parameter, value: any] {
172168
if (types[type] !== undefined) {
173169
return [
174170
{ type: "bytes32" },
@@ -186,15 +182,16 @@ function encodeField({
186182

187183
if (type.lastIndexOf("]") === type.length - 1) {
188184
const parsedType = type.slice(0, type.lastIndexOf("["));
189-
// biome-ignore lint/suspicious/noExplicitAny: Can't anticipate types of nested values
190-
const typeValuePairs = (value as [AbiParameter, any][]).map((item) =>
191-
encodeField({
192-
name,
193-
type: parsedType,
194-
types,
195-
value: item,
196-
}),
197-
);
185+
const typeValuePairs =
186+
// biome-ignore lint/suspicious/noExplicitAny: Can't anticipate types of nested values
187+
(value as [ox__AbiParameters.Parameter, any][]).map((item) =>
188+
encodeField({
189+
name,
190+
type: parsedType,
191+
types,
192+
value: item,
193+
}),
194+
);
198195
return [
199196
{ type: "bytes32" },
200197
keccak256(

packages/thirdweb/src/utils/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type * as ox__Bytes from "ox/Bytes";
2+
import type * as ox__Hex from "ox/Hex";
13
import type { Chain } from "../chains/types.js";
24
import type { ThirdwebClient } from "../client/client.js";
35
import type { Account } from "../wallets/interfaces/wallet.js";
@@ -17,3 +19,18 @@ export type ClientAndChain = {
1719
export type ClientAndChainAndAccount = Prettify<
1820
ClientAndChain & { account: Account }
1921
>;
22+
23+
/**
24+
* A message that can be signed, either as in plaintext or as a raw hex string.
25+
*/
26+
export type SignableMessage =
27+
| string
28+
| {
29+
/** Raw data representation of the message. */
30+
raw: ox__Hex.Hex | ox__Bytes.Bytes;
31+
};
32+
33+
/**
34+
* @internal
35+
*/
36+
export const maxUint96 = 2n ** 96n - 1n;

packages/thirdweb/src/wallets/smart/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as ox__TypedData from "ox/TypedData";
2-
import { type SignableMessage, maxUint96 } from "viem";
1+
import type * as ox__TypedData from "ox/TypedData";
32
import type { Chain } from "../../chains/types.js";
43
import { getCachedChain } from "../../chains/utils.js";
54
import type { ThirdwebClient } from "../../client/client.js";
@@ -18,6 +17,7 @@ import { getAddress } from "../../utils/address.js";
1817
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
1918
import type { Hex } from "../../utils/encoding/hex.js";
2019
import { parseTypedData } from "../../utils/signatures/helpers/parse-typed-data.js";
20+
import { type SignableMessage, maxUint96 } from "../../utils/types.js";
2121
import type {
2222
Account,
2323
SendTransactionOption,
@@ -288,7 +288,7 @@ async function createSmartAccount(
288288
async signTypedData<
289289
const typedData extends ox__TypedData.TypedData | Record<string, unknown>,
290290
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
291-
>(typedData: TypedDataDefinition<typedData, primaryType>) {
291+
>(typedData: ox__TypedData.Definition<typedData, primaryType>) {
292292
if (options.overrides?.signTypedData) {
293293
return options.overrides.signTypedData({
294294
adminAccount: options.personalAccount,

packages/thirdweb/src/wallets/smart/lib/signing.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type { Hex } from "ox";
2-
import type {
3-
SignableMessage,
4-
TypedData,
5-
TypedDataDefinition,
6-
TypedDataDomain,
7-
} from "viem";
1+
import type * as ox__TypedData from "ox/TypedData";
82
import { serializeErc6492Signature } from "../../../auth/serialize-erc6492-signature.js";
93
import { verifyHash } from "../../../auth/verify-hash.js";
104
import {
@@ -14,8 +8,10 @@ import {
148
import { encode } from "../../../transaction/actions/encode.js";
159
import { readContract } from "../../../transaction/read-contract.js";
1610
import { encodeAbiParameters } from "../../../utils/abi/encodeAbiParameters.js";
11+
import type { Hex } from "../../../utils/encoding/hex.js";
1712
import { hashMessage } from "../../../utils/hashing/hashMessage.js";
1813
import { hashTypedData } from "../../../utils/hashing/hashTypedData.js";
14+
import type { SignableMessage } from "../../../utils/types.js";
1915
import type { SmartAccountOptions } from "../types.js";
2016
import { prepareCreateAccount } from "./calls.js";
2117

@@ -93,7 +89,7 @@ export async function deployAndSignMessage({
9389
}
9490

9591
export async function deployAndSignTypedData<
96-
const typedData extends TypedData | Record<string, unknown>,
92+
const typedData extends ox__TypedData.TypedData | Record<string, unknown>,
9793
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
9894
>({
9995
accountContract,
@@ -104,10 +100,12 @@ export async function deployAndSignTypedData<
104100
accountContract: ThirdwebContract;
105101
factoryContract: ThirdwebContract;
106102
options: SmartAccountOptions;
107-
typedData: TypedDataDefinition<typedData, primaryType>;
103+
typedData: ox__TypedData.Definition<typedData, primaryType>;
108104
}) {
109105
const isSelfVerifyingContract =
110-
(typedData.domain as TypedDataDomain)?.verifyingContract?.toLowerCase() ===
106+
(
107+
typedData.domain as ox__TypedData.Domain
108+
)?.verifyingContract?.toLowerCase() ===
111109
accountContract.address?.toLowerCase();
112110

113111
if (isSelfVerifyingContract) {
@@ -205,7 +203,7 @@ async function checkFor712Factory({
205203
}: {
206204
factoryContract: ThirdwebContract;
207205
accountContract: ThirdwebContract;
208-
originalMsgHash: Hex.Hex;
206+
originalMsgHash: Hex;
209207
}) {
210208
try {
211209
const implementationAccount = await readContract({

packages/thirdweb/src/wallets/smart/types.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import type { Address, TypedData } from "abitype";
2-
import type { SignableMessage, TypedDataDefinition } from "viem";
1+
import type * as ox__Address from "ox/Address";
2+
import type * as ox__TypedData from "ox/TypedData";
33
import type { Chain } from "../../chains/types.js";
44
import type { ThirdwebClient } from "../../client/client.js";
55
import type { ThirdwebContract } from "../../contract/contract.js";
66
import type { PreparedTransaction } from "../../transaction/prepare-transaction.js";
77
import type { TransactionReceipt } from "../../transaction/types.js";
88
import type { Hex } from "../../utils/encoding/hex.js";
99
import type { Prettify } from "../../utils/type-utils.js";
10+
import type { SignableMessage } from "../../utils/types.js";
1011
import type { Account, SendTransactionOption } from "../interfaces/wallet.js";
1112

1213
export type TokenPaymasterConfig = {
@@ -53,13 +54,15 @@ export type SmartWalletOptions = Prettify<
5354
message: SignableMessage;
5455
}) => Promise<Hex>;
5556
signTypedData?: <
56-
const typedData extends TypedData | Record<string, unknown>,
57+
const typedData extends
58+
| ox__TypedData.TypedData
59+
| Record<string, unknown>,
5760
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
5861
>(options: {
5962
adminAccount: Account;
6063
accountContract: ThirdwebContract;
6164
factoryContract: ThirdwebContract;
62-
typedData: TypedDataDefinition<typedData, primaryType>;
65+
typedData: ox__TypedData.Definition<typedData, primaryType>;
6366
}) => Promise<Hex>;
6467
};
6568
} & (
@@ -101,7 +104,7 @@ export type SmartWalletConnectionOptions = {
101104
};
102105

103106
export type UserOperationV06 = {
104-
sender: Address;
107+
sender: ox__Address.Address;
105108
nonce: bigint;
106109
initCode: Hex;
107110
callData: Hex;
@@ -145,7 +148,7 @@ export type PackedUserOperation = {
145148
};
146149

147150
export type UserOperationV06Hexed = {
148-
sender: Address;
151+
sender: ox__Address.Address;
149152
nonce: Hex;
150153
initCode: Hex;
151154
callData: Hex;
@@ -210,18 +213,18 @@ export type GasPriceResult = {
210213
};
211214

212215
export type PmTransactionData = {
213-
paymaster: Address;
216+
paymaster: ox__Address.Address;
214217
paymasterInput: Hex;
215218
};
216219

217220
export type UserOperationReceipt = {
218221
receipt: TransactionReceipt;
219222
logs: TransactionReceipt["logs"];
220223
userOpHash: Hex;
221-
entryPoint: Address;
222-
sender: Address;
224+
entryPoint: ox__Address.Address;
225+
sender: ox__Address.Address;
223226
nonce: bigint;
224-
paymaster: Address;
227+
paymaster: ox__Address.Address;
225228
actualGasUsed: bigint;
226229
actualGasCost: bigint;
227230
success: boolean;

0 commit comments

Comments
 (0)