-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInputVerifier.ts
More file actions
168 lines (142 loc) · 5.22 KB
/
InputVerifier.ts
File metadata and controls
168 lines (142 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type { ChecksummedAddress } from '@base/types/primitives';
import type { Provider as EthersProviderType } from 'ethers';
import type { CoprocessorEIP712DomainType } from './coprocessor/public-api';
import type { IInputVerifier } from './types/private';
import { Contract } from 'ethers';
import { isUint8, isUintBigInt } from '@base/uint';
import { assertIsChecksummedAddressArray } from '@base/address';
import { assertCoprocessorEIP712DomainType } from './coprocessor/guards';
import { executeWithBatching } from '@base/promise';
export class InputVerifier {
static readonly #abi = [
'function getCoprocessorSigners() view returns (address[])',
'function getThreshold() view returns (uint256)',
'function eip712Domain() view returns (bytes1 fields, string name, string version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] extensions)',
] as const;
static readonly #constructorGuard: unique symbol = Symbol(
'InputVerifier.constructorGuard',
);
static {
Object.freeze(InputVerifier.#abi);
}
readonly #address: ChecksummedAddress;
readonly #eip712Domain: CoprocessorEIP712DomainType;
readonly #coprocessorSigners: ChecksummedAddress[];
readonly #coprocessorSignerThreshold: number;
private constructor(
guard: symbol,
params: {
address: ChecksummedAddress;
eip712Domain: CoprocessorEIP712DomainType;
coprocessorSigners: ChecksummedAddress[];
coprocessorSignerThreshold: number;
},
) {
if (guard !== InputVerifier.#constructorGuard) {
throw new Error(
'InputVerifier cannot be constructed directly. Use InputVerifier.loadFromChain() or createInstance() instead.',
);
}
this.#address = params.address;
this.#eip712Domain = { ...params.eip712Domain };
this.#coprocessorSigners = [...params.coprocessorSigners];
this.#coprocessorSignerThreshold = params.coprocessorSignerThreshold;
Object.freeze(this.#eip712Domain);
Object.freeze(this.#coprocessorSigners);
}
public get address(): ChecksummedAddress {
return this.#address;
}
public get eip712Domain(): CoprocessorEIP712DomainType {
return this.#eip712Domain;
}
public get gatewayChainId(): bigint {
return this.#eip712Domain.chainId;
}
public get coprocessorSigners(): ChecksummedAddress[] {
return this.#coprocessorSigners;
}
public get coprocessorSignerThreshold(): number {
return this.#coprocessorSignerThreshold;
}
public get verifyingContractAddressInputVerification(): ChecksummedAddress {
return this.#eip712Domain.verifyingContract;
}
public static async loadFromChain(params: {
inputVerifierContractAddress: ChecksummedAddress;
provider: EthersProviderType;
batchRpcCalls?: boolean;
}): Promise<InputVerifier> {
const contract = new Contract(
params.inputVerifierContractAddress,
InputVerifier.#abi,
params.provider,
) as unknown as IInputVerifier;
// To be removed
if (params.batchRpcCalls === true) {
throw new Error(`Batch RPC Calls not supported!`);
}
////////////////////////////////////////////////////////////////////////////
//
// Important remark:
// =================
// Do NOTE USE `Promise.all` here!
// You may get a server response 500 Internal Server Error
// "Batch of more than 3 requests are not allowed on free tier, to use this
// feature register paid account at drpc.org"
//
////////////////////////////////////////////////////////////////////////////
const rpcCalls = [
() => contract.eip712Domain(),
() => contract.getThreshold(),
() => contract.getCoprocessorSigners(),
];
const res = await executeWithBatching(rpcCalls, params.batchRpcCalls);
const eip712DomainArray = res[0] as unknown[];
const threshold = res[1];
const coprocessorSigners = res[2] as unknown[];
if (!isUint8(threshold)) {
throw new Error(`Invalid InputVerifier Coprocessor signers threshold.`);
}
try {
assertIsChecksummedAddressArray(coprocessorSigners);
} catch (e) {
throw new Error(`Invalid InputVerifier Coprocessor signers addresses.`, {
cause: e,
});
}
const unknownChainId = eip712DomainArray[3];
if (!isUintBigInt(unknownChainId)) {
throw new Error('Invalid InputVerifier EIP-712 domain chainId.');
}
const eip712Domain = {
name: eip712DomainArray[1],
version: eip712DomainArray[2],
chainId: unknownChainId,
verifyingContract: eip712DomainArray[4],
};
try {
assertCoprocessorEIP712DomainType(
eip712Domain,
'InputVerifier.eip712Domain()',
);
} catch (e) {
throw new Error(`Invalid InputVerifier EIP-712 domain.`, { cause: e });
}
if (
eip712Domain.verifyingContract.toLowerCase() ===
params.inputVerifierContractAddress.toLowerCase()
) {
throw new Error(
`Invalid InputVerifier EIP-712 domain. Unexpected verifyingContract.`,
);
}
const inputVerifier = new InputVerifier(InputVerifier.#constructorGuard, {
address: params.inputVerifierContractAddress,
eip712Domain: eip712Domain,
coprocessorSignerThreshold: Number(threshold),
coprocessorSigners,
});
return inputVerifier;
}
}