-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathKMSVerifier.ts
More file actions
163 lines (138 loc) · 4.92 KB
/
KMSVerifier.ts
File metadata and controls
163 lines (138 loc) · 4.92 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
import type { ChecksummedAddress } from '@base/types/primitives';
import type { Provider as EthersProviderType } from 'ethers';
import type { KmsEIP712DomainType } from './kms/public-api';
import type { IKMSVerifier } from './types/private';
import { Contract } from 'ethers';
import { isUint8 } from '@base/uint';
import { assertIsChecksummedAddressArray } from '@base/address';
import { assertKmsEIP712DomainType } from './kms/guards';
import { executeWithBatching } from '@base/promise';
export class KMSVerifier {
static readonly #abi = [
'function getKmsSigners() 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(
'KMSVerifier.constructorGuard',
);
static {
Object.freeze(KMSVerifier.#abi);
}
readonly #address: ChecksummedAddress;
readonly #verifyingContractAddressDecryption: ChecksummedAddress;
readonly #eip712Domain: KmsEIP712DomainType;
readonly #kmsSigners: ChecksummedAddress[];
readonly #kmsSignerThreshold: number;
private constructor(
guard: symbol,
params: {
address: ChecksummedAddress;
eip712Domain: KmsEIP712DomainType;
kmsSigners: ChecksummedAddress[];
kmsSignerThreshold: number;
},
) {
if (guard !== KMSVerifier.#constructorGuard) {
throw new Error(
'KMSVerifier cannot be constructed directly. Use KMSVerifier.loadFromChain() or createInstance() instead.',
);
}
this.#address = params.address;
this.#verifyingContractAddressDecryption =
params.eip712Domain.verifyingContract;
this.#eip712Domain = { ...params.eip712Domain };
this.#kmsSigners = [...params.kmsSigners];
this.#kmsSignerThreshold = params.kmsSignerThreshold;
Object.freeze(this.#eip712Domain);
Object.freeze(this.#kmsSigners);
}
public get address(): ChecksummedAddress {
return this.#address;
}
public get eip712Domain(): KmsEIP712DomainType {
return this.#eip712Domain;
}
public get gatewayChainId(): bigint {
return this.#eip712Domain.chainId;
}
public get kmsSigners(): ChecksummedAddress[] {
return this.#kmsSigners;
}
public get kmsSignerThreshold(): number {
return this.#kmsSignerThreshold;
}
public get verifyingContractAddressDecryption(): ChecksummedAddress {
return this.#verifyingContractAddressDecryption;
}
public static async loadFromChain(params: {
kmsContractAddress: ChecksummedAddress;
provider: EthersProviderType;
batchRpcCalls?: boolean;
}): Promise<KMSVerifier> {
const contract = new Contract(
params.kmsContractAddress,
KMSVerifier.#abi,
params.provider,
) as unknown as IKMSVerifier;
// 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.getKmsSigners(),
];
const res = await executeWithBatching(rpcCalls, params.batchRpcCalls);
const eip712DomainArray = res[0] as unknown[];
const kmsSignerThreshold = res[1];
const kmsSigners = res[2] as unknown[];
if (!isUint8(kmsSignerThreshold)) {
throw new Error(`Invalid KMSVerifier kms signers threshold.`);
}
try {
assertIsChecksummedAddressArray(kmsSigners);
} catch (e) {
throw new Error(`Invalid KMSVerifier kms signers addresses.`, {
cause: e,
});
}
const eip712Domain = {
name: eip712DomainArray[1],
version: eip712DomainArray[2],
chainId: eip712DomainArray[3],
verifyingContract: eip712DomainArray[4],
};
try {
assertKmsEIP712DomainType(eip712Domain, 'KMSVerifier.eip712Domain()');
} catch (e) {
throw new Error(`Invalid KMSVerifier EIP-712 domain.`, { cause: e });
}
if (
eip712Domain.verifyingContract.toLowerCase() ===
params.kmsContractAddress.toLowerCase()
) {
throw new Error(
`Invalid KMSVerifier EIP-712 domain. Unexpected verifyingContract.`,
);
}
const kmsVerifier = new KMSVerifier(KMSVerifier.#constructorGuard, {
address: params.kmsContractAddress,
eip712Domain: eip712Domain,
kmsSignerThreshold: Number(kmsSignerThreshold),
kmsSigners: kmsSigners,
});
return kmsVerifier;
}
}