Skip to content

Commit 59faac0

Browse files
committed
viem to ox updates
1 parent 90a16da commit 59faac0

File tree

22 files changed

+2424
-2743
lines changed

22 files changed

+2424
-2743
lines changed

packages/thirdweb/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
"fuse.js": "7.0.0",
218218
"input-otp": "^1.4.1",
219219
"mipd": "0.0.7",
220-
"ox": "0.4.1",
220+
"ox": "0.4.2",
221221
"uqr": "0.1.2",
222222
"viem": "2.21.54"
223223
},

packages/thirdweb/src/auth/is-erc6492-signature.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sliceHex } from "viem";
1+
import * as ox__Hex from "ox/Hex";
22
import type { Hex } from "../utils/encoding/hex.js";
33
import { ERC_6492_MAGIC_VALUE } from "./constants.js";
44

@@ -19,5 +19,5 @@ import { ERC_6492_MAGIC_VALUE } from "./constants.js";
1919
* @auth
2020
*/
2121
export function isErc6492Signature(signature: Hex): boolean {
22-
return sliceHex(signature, -32) === ERC_6492_MAGIC_VALUE;
22+
return ox__Hex.slice(signature, -32) === ERC_6492_MAGIC_VALUE;
2323
}

packages/thirdweb/src/auth/parse-erc6492-signature.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { decodeAbiParameters, isErc6492Signature } from "viem";
1+
import * as ox__AbiParameters from "ox/AbiParameters";
2+
import * as ox__Address from "ox/Address";
3+
import { WrappedSignature as ox__WrappedSignature } from "ox/erc6492";
24
import type { Hex } from "../utils/encoding/hex.js";
35
import type { OneOf } from "../utils/type-utils.js";
46
import type { Erc6492Signature } from "./types.js";
@@ -29,13 +31,17 @@ export type ParseErc6492SignatureReturnType = OneOf<
2931
export function parseErc6492Signature(
3032
signature: Hex,
3133
): ParseErc6492SignatureReturnType {
32-
if (!isErc6492Signature(signature)) {
34+
if (!ox__WrappedSignature.validate(signature)) {
3335
return { signature };
3436
}
3537

36-
const [address, data, originalSignature] = decodeAbiParameters(
38+
const [address, data, originalSignature] = ox__AbiParameters.decode(
3739
[{ type: "address" }, { type: "bytes" }, { type: "bytes" }],
3840
signature,
3941
);
40-
return { address: address, data, signature: originalSignature };
42+
return {
43+
address: ox__Address.checksum(address),
44+
data,
45+
signature: originalSignature,
46+
};
4147
}

packages/thirdweb/src/auth/verify-hash.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import {
2-
type Signature,
3-
encodeDeployData,
4-
encodeFunctionData,
5-
isErc6492Signature,
6-
serializeSignature,
7-
universalSignatureValidatorAbi,
8-
universalSignatureValidatorByteCode,
9-
} from "viem";
1+
import * as ox__Abi from "ox/Abi";
2+
import * as ox__AbiConstructor from "ox/AbiConstructor";
3+
import * as ox__AbiFunction from "ox/AbiFunction";
4+
import * as ox__Signature from "ox/Signature";
5+
import { WrappedSignature as ox__WrappedSignature } from "ox/erc6492";
106
import type { Chain } from "../chains/types.js";
117
import type { ThirdwebClient } from "../client/client.js";
128
import { type ThirdwebContract, getContract } from "../contract/contract.js";
@@ -20,7 +16,7 @@ import { serializeErc6492Signature } from "./serialize-erc6492-signature.js";
2016

2117
export type VerifyHashParams = {
2218
hash: Hex;
23-
signature: string | Uint8Array | Signature;
19+
signature: string | Uint8Array | ox__Signature.Signature;
2420
address: string;
2521
client: ThirdwebClient;
2622
chain: Chain;
@@ -71,7 +67,7 @@ export async function verifyHash({
7167
const signatureHex = (() => {
7268
if (isHex(signature)) return signature;
7369
if (typeof signature === "object" && "r" in signature && "s" in signature)
74-
return serializeSignature(signature);
70+
return ox__Signature.toHex(signature);
7571
if (signature instanceof Uint8Array) return fromBytes(signature, "hex");
7672
// We should never hit this but TS doesn't know that
7773
throw new Error(
@@ -85,7 +81,7 @@ export async function verifyHash({
8581
if (!accountFactory) return signatureHex;
8682

8783
// If this sigature was already wrapped for ERC-6492, carry on
88-
if (isErc6492Signature(signatureHex)) return signatureHex;
84+
if (ox__WrappedSignature.validate(signatureHex)) return signatureHex;
8985

9086
// Otherwise, serialize the signature for ERC-6492 validation
9187
return serializeErc6492Signature({
@@ -100,23 +96,23 @@ export async function verifyHash({
10096
data: Hex;
10197
};
10298
const zkSyncChain = await isZkSyncChain(chain);
99+
const abi = ox__Abi.from(ox__WrappedSignature.universalSignatureValidatorAbi);
103100
if (zkSyncChain) {
104101
// zksync chains dont support deploying code with eth_call
105102
// need to call a deployed contract instead
106103
verificationData = {
107104
to: ZKSYNC_VALIDATOR_ADDRESS,
108-
data: encodeFunctionData({
109-
abi: universalSignatureValidatorAbi,
110-
functionName: "isValidSig",
111-
args: [address, hash, wrappedSignature],
112-
}),
105+
data: ox__AbiFunction.encodeData(
106+
ox__AbiFunction.fromAbi(abi, "isValidSig"),
107+
[address, hash, wrappedSignature],
108+
),
113109
};
114110
} else {
111+
const validatorConstructor = ox__AbiConstructor.fromAbi(abi);
115112
verificationData = {
116-
data: encodeDeployData({
117-
abi: universalSignatureValidatorAbi,
113+
data: ox__AbiConstructor.encode(validatorConstructor, {
118114
args: [address, hash, wrappedSignature],
119-
bytecode: universalSignatureValidatorByteCode,
115+
bytecode: ox__WrappedSignature.universalSignatureValidatorBytecode,
120116
}),
121117
};
122118
}

packages/thirdweb/src/auth/verify-signature.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import { type SignableMessage, type Signature, recoverAddress } from "viem";
1+
import * as ox__Bytes from "ox/Bytes";
2+
import * as ox__Hex from "ox/Hex";
3+
import * as ox__Secp256k1 from "ox/Secp256k1";
4+
import * as ox__Signature from "ox/Signature";
25
import type { Chain } from "../chains/types.js";
36
import type { ThirdwebClient } from "../client/client.js";
47
import { type Hex, isHex } from "../utils/encoding/hex.js";
58
import { hashMessage } from "../utils/hashing/hashMessage.js";
69
import type { Prettify } from "../utils/type-utils.js";
710
import { verifyHash } from "./verify-hash.js";
811

12+
type Message = Prettify<
13+
| string
14+
| {
15+
raw: Hex | Uint8Array;
16+
}
17+
>;
18+
919
/**
1020
* @auth
1121
*/
1222
export type VerifyEOASignatureParams = {
13-
message: string | SignableMessage;
14-
signature: string | Uint8Array | Signature;
23+
message: string | Message;
24+
signature: string | Uint8Array | ox__Signature.Signature;
1525
address: string;
1626
};
1727

@@ -39,9 +49,9 @@ export async function verifyEOASignature(options: VerifyEOASignatureParams) {
3949
return false;
4050
}
4151

42-
const recoveredAddress = await recoverAddress({
43-
hash: messageHash,
44-
signature: options.signature,
52+
const recoveredAddress = ox__Secp256k1.recoverAddress({
53+
payload: messageHash,
54+
signature: ox__Signature.fromHex(options.signature),
4555
});
4656

4757
if (recoveredAddress.toLowerCase() === options.address.toLowerCase()) {
@@ -103,9 +113,28 @@ export async function verifyContractWalletSignature({
103113
accountFactory,
104114
}: VerifyContractWalletSignatureParams) {
105115
const messageHash = hashMessage(message);
116+
117+
const parsedSignature = (() => {
118+
if (ox__Bytes.validate(signature)) {
119+
const sig = ox__Signature.fromBytes(signature);
120+
return {
121+
r: ox__Hex.fromNumber(sig.r),
122+
s: ox__Hex.fromNumber(sig.s),
123+
yParity: sig.yParity,
124+
};
125+
} else if (typeof signature === "object") {
126+
return {
127+
r: ox__Hex.fromNumber(signature.r),
128+
s: ox__Hex.fromNumber(signature.s),
129+
yParity: signature.yParity,
130+
};
131+
}
132+
return signature;
133+
})();
134+
106135
return verifyHash({
107136
hash: messageHash,
108-
signature,
137+
signature: parsedSignature,
109138
address,
110139
client,
111140
chain,

packages/thirdweb/src/auth/verify-typed-data.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import type { Signature, TypedData, TypedDataDefinition } from "viem";
2-
import { hashTypedData } from "viem";
1+
import type * as ox__Signature from "ox/Signature";
2+
import * 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 { Hex } from "../utils/encoding/hex.js";
66
import type { HashTypedDataParams } from "../utils/hashing/hashTypedData.js";
77
import { type VerifyHashParams, verifyHash } from "./verify-hash.js";
88

99
export type VerifyTypedDataParams<
10-
typedData extends TypedData | Record<string, unknown> = TypedData,
10+
typedData extends
11+
| ox__TypedData.TypedData
12+
| Record<string, unknown> = ox__TypedData.TypedData,
1113
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
1214
> = Omit<VerifyHashParams, "hash"> &
13-
TypedDataDefinition<typedData, primaryType> & {
15+
ox__TypedData.Definition<typedData, primaryType> & {
1416
address: string;
15-
signature: string | Uint8Array | Signature;
17+
signature: string | Uint8Array | ox__Signature.Signature;
1618
client: ThirdwebClient;
1719
chain: Chain;
1820
accountFactory?: {
@@ -80,7 +82,7 @@ export type VerifyTypedDataParams<
8082
* @auth
8183
*/
8284
export async function verifyTypedData<
83-
typedData extends TypedData | Record<string, unknown>,
85+
typedData extends ox__TypedData.TypedData | Record<string, unknown>,
8486
primaryType extends keyof typedData | "EIP712Domain",
8587
>({
8688
address,
@@ -93,7 +95,7 @@ export async function verifyTypedData<
9395
primaryType,
9496
types,
9597
}: VerifyTypedDataParams<typedData, primaryType>): Promise<boolean> {
96-
const messageHash = hashTypedData({
98+
const messageHash = ox__TypedData.getSignPayload({
9799
message,
98100
domain,
99101
primaryType,

packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getContractAddress } from "viem";
1+
import * as ox__ContractAddress from "ox/ContractAddress";
22
import { getGasPrice } from "../../../gas/get-gas-price.js";
33
import { eth_getBalance } from "../../../rpc/actions/eth_getBalance.js";
44
import { eth_sendRawTransaction } from "../../../rpc/actions/eth_sendRawTransaction.js";
@@ -208,7 +208,7 @@ async function _getCreate2FactoryDeploymentInfo(
208208
},
209209
signature: SIGNATURE,
210210
});
211-
const create2FactoryAddress = getContractAddress({
211+
const create2FactoryAddress = ox__ContractAddress.from({
212212
from: deploymentTransaction.signerAddress,
213213
nonce: 0n,
214214
});

packages/thirdweb/src/contract/verification/constructor-params.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { Abi } from "abitype";
2-
import { decodeAbiParameters } from "viem";
1+
import type * as ox__Abi from "ox/Abi";
2+
import * as ox__AbiConstructor from "ox/AbiConstructor";
3+
import * as ox__AbiParameters from "ox/AbiParameters";
34
import { eth_getTransactionByHash } from "../../rpc/actions/eth_getTransactionByHash.js";
45
import { getRpcClient } from "../../rpc/rpc.js";
56
import type { ThirdwebContract } from "../contract.js";
@@ -10,19 +11,9 @@ type FetchConstructorParamsOptions = {
1011
contract: ThirdwebContract;
1112
explorerApiUrl: string;
1213
explorerApiKey: string;
13-
abi: Abi;
14+
abi: ox__Abi.Abi;
1415
};
1516

16-
// TODO: move to abi helpers (?)
17-
function extractConstructorParamsFromAbi(abi: Abi) {
18-
for (const input of abi) {
19-
if (input.type === "constructor") {
20-
return input.inputs || [];
21-
}
22-
}
23-
return [];
24-
}
25-
2617
const RequestStatus = {
2718
OK: "1",
2819
NOTOK: "0",
@@ -37,7 +28,8 @@ const RequestStatus = {
3728
export async function fetchConstructorParams(
3829
options: FetchConstructorParamsOptions,
3930
): Promise<string> {
40-
const constructorParamTypes = extractConstructorParamsFromAbi(options.abi);
31+
const abiConstructor = ox__AbiConstructor.fromAbi(options.abi);
32+
const constructorParamTypes = ox__AbiParameters.from(abiConstructor.inputs);
4133
if (constructorParamTypes.length === 0) {
4234
return "";
4335
}
@@ -114,7 +106,8 @@ export async function fetchConstructorParams(
114106
try {
115107
// sanity check that the constructor params are valid
116108
// TODO: should we sanity check after each attempt?
117-
decodeAbiParameters(constructorParamTypes, `0x${constructorArgs}`);
109+
110+
ox__AbiParameters.decode(constructorParamTypes, `0x${constructorArgs}`);
118111
} catch {
119112
throw new Error(
120113
"Verifying this contract requires it to be published. Run `npx thirdweb publish` to publish this contract, then try again.",

packages/thirdweb/src/event/types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AbiParameter, AbiParameterToPrimitiveType } from "abitype";
2-
import type { Hex, LogTopic } from "viem";
2+
import type * as ox__Hex from "ox/Hex";
3+
import type { Log as ox__Log } from "ox/Log";
34
import type { Filter, MaybeRequired, Prettify } from "../utils/type-utils.js";
45

56
//////////////////////////////////////////////////////////////////////
@@ -83,11 +84,11 @@ type _HasUnnamedAbiParameter<TAbiParameters extends readonly AbiParameter[]> =
8384
* @internal
8485
*/
8586
type LogTopicType<
86-
TPrimitiveType = Hex,
87-
TTopic extends LogTopic = LogTopic,
88-
> = TTopic extends Hex
87+
TPrimitiveType = ox__Hex.Hex,
88+
TTopic extends ox__Log = ox__Log,
89+
> = TTopic extends ox__Hex.Hex
8990
? TPrimitiveType
90-
: TTopic extends Hex[]
91+
: TTopic extends ox__Hex.Hex[]
9192
? TPrimitiveType[]
9293
: TTopic extends null
9394
? null

packages/thirdweb/src/exports/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export { stringify } from "../utils/json.js";
167167
// ------------------------------------------------
168168
// values
169169
// ------------------------------------------------
170-
export { maxUint256 } from "viem";
170+
export { maxUint256 } from "ox/Solidity";
171171

172172
// ------------------------------------------------
173173
// jwt

0 commit comments

Comments
 (0)