@verified-network/verified-sdk 2.4.8 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contract/amm/margin/index.d.ts +1 -1
- package/dist/contract/amm/margin/index.js +2 -2
- package/dist/contract/amm/primary/index.d.ts +1 -1
- package/dist/contract/amm/primary/index.js +2 -2
- package/dist/contract/amm/secondary/index.d.ts +1 -1
- package/dist/contract/amm/secondary/index.js +2 -2
- package/dist/contract/bond/index.d.ts +1 -1
- package/dist/contract/bond/index.js +2 -2
- package/dist/contract/cash/index.d.ts +1 -1
- package/dist/contract/cash/index.js +2 -2
- package/dist/contract/client/index.d.ts +1 -1
- package/dist/contract/client/index.js +2 -2
- package/dist/contract/custody/index.d.ts +1 -1
- package/dist/contract/custody/index.js +2 -2
- package/dist/contract/distribution/index.d.ts +1 -1
- package/dist/contract/distribution/index.js +2 -2
- package/dist/contract/erc20/index.d.ts +1 -1
- package/dist/contract/erc20/index.js +2 -2
- package/dist/contract/factory/index.d.ts +1 -1
- package/dist/contract/factory/index.js +2 -2
- package/dist/contract/index.d.ts +2 -1
- package/dist/contract/index.js +63 -26
- package/dist/contract/liquidity/index.d.ts +1 -1
- package/dist/contract/liquidity/index.js +2 -2
- package/dist/contract/loans/compound/index.d.ts +1 -1
- package/dist/contract/loans/compound/index.js +2 -2
- package/dist/contract/pool/index.d.ts +1 -1
- package/dist/contract/pool/index.js +2 -2
- package/dist/contract/rates/index.d.ts +1 -1
- package/dist/contract/rates/index.js +2 -2
- package/dist/contract/securitiesfactory/index.d.ts +1 -1
- package/dist/contract/securitiesfactory/index.js +2 -2
- package/dist/contract/security/index.d.ts +1 -1
- package/dist/contract/security/index.js +2 -2
- package/dist/contract/token/index.d.ts +1 -1
- package/dist/contract/token/index.js +2 -2
- package/dist/contractAddress/index.js +3 -3
- package/dist/lib/abi/boostrap.d.ts +878 -0
- package/dist/lib/abi/boostrap.js +1151 -0
- package/dist/lib/abi/boostrap.json +1150 -0
- package/dist/lib/abi/composability.d.ts +66 -0
- package/dist/lib/abi/composability.js +273 -0
- package/dist/lib/abi/entrypoint.d.ts +775 -0
- package/dist/lib/abi/entrypoint.js +660 -0
- package/dist/lib/abi/factory.json +325 -0
- package/dist/lib/biconomyRNFix.d.ts +20737 -0
- package/dist/lib/biconomyRNFix.js +974 -0
- package/package.json +2 -2
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.createMultiChainNexusAccount = exports.waitForTransactionReceiptsDecorator = exports.unDelegateDecorator = exports.buildDefaultInstructions = exports.createNexusAccount = exports.computeNexusAccount = exports.PARENT_TYPEHASH = exports.EXECUTE_BATCH = exports.MODE_PAYLOAD = exports.UNUSED = exports.MODE_DEFAULT = exports.EXECTYPE_DELEGATE = exports.EXECTYPE_TRY = exports.EXECTYPE_DEFAULT = exports.CALLTYPE_BATCH = exports.CALLTYPE_SINGLE = void 0;
|
|
18
|
+
const viem_1 = require("viem");
|
|
19
|
+
const boostrap_json_1 = __importDefault(require("./abi/boostrap.json"));
|
|
20
|
+
const factory_json_1 = __importDefault(require("./abi/factory.json"));
|
|
21
|
+
const entrypoint_1 = require("./abi/entrypoint");
|
|
22
|
+
const account_abstraction_1 = require("viem/account-abstraction");
|
|
23
|
+
const accounts_1 = require("viem/accounts");
|
|
24
|
+
const utils_1 = require("viem/utils");
|
|
25
|
+
const composability_1 = require("./abi/composability");
|
|
26
|
+
const NEXUS_ACCOUNT_FACTORY_ADDRESS = "0x000000001D1D5004a02bAfAb9de2D6CE5b7B13de";
|
|
27
|
+
const NEXUS_BOOTSTRAP_ADDRESS = "0x00000000D3254452a909E4eeD47455Af7E27C289";
|
|
28
|
+
const DUMMY_SIGNATURE = "0x81d4b4981670cb18f99f0b4a66446df1bf5b204d24cfcb659bf38ba27a4359b5711649ec2423c5e1247245eba2964679b6a1dbb85c992ae40b9b00c6935b02ff1b";
|
|
29
|
+
const ENTRY_POINT_ADDRESS = "0x0000000071727De22E5E9d8BAf0edAc6f37da032";
|
|
30
|
+
const ENTRYPOINT_SIMULATIONS_any = "0x74Cb5e4eE81b86e70f9045036a1C5477de69eE87";
|
|
31
|
+
exports.CALLTYPE_SINGLE = "0x00"; // 1 byte
|
|
32
|
+
exports.CALLTYPE_BATCH = "0x01"; // 1 byte
|
|
33
|
+
exports.EXECTYPE_DEFAULT = "0x00"; // 1 byte
|
|
34
|
+
exports.EXECTYPE_TRY = "0x01"; // 1 byte
|
|
35
|
+
exports.EXECTYPE_DELEGATE = "0xFF"; // 1 byte
|
|
36
|
+
exports.MODE_DEFAULT = "0x00000000"; // 4 bytes
|
|
37
|
+
exports.UNUSED = "0x00000000"; // 4 bytes
|
|
38
|
+
exports.MODE_PAYLOAD = "0x00000000000000000000000000000000000000000000"; // 22 bytes
|
|
39
|
+
const EXECUTE_SINGLE = (0, viem_1.concat)([
|
|
40
|
+
exports.CALLTYPE_SINGLE,
|
|
41
|
+
exports.EXECTYPE_DEFAULT,
|
|
42
|
+
exports.MODE_DEFAULT,
|
|
43
|
+
exports.UNUSED,
|
|
44
|
+
exports.MODE_PAYLOAD,
|
|
45
|
+
]);
|
|
46
|
+
exports.EXECUTE_BATCH = (0, viem_1.concat)([
|
|
47
|
+
exports.CALLTYPE_BATCH,
|
|
48
|
+
exports.EXECTYPE_DEFAULT,
|
|
49
|
+
exports.MODE_DEFAULT,
|
|
50
|
+
exports.UNUSED,
|
|
51
|
+
exports.MODE_PAYLOAD,
|
|
52
|
+
]);
|
|
53
|
+
exports.PARENT_TYPEHASH = "TypedDataSign(Contents contents,string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)Contents(bytes32 stuff)";
|
|
54
|
+
const index = BigInt(0);
|
|
55
|
+
const toInitData = (mod) => {
|
|
56
|
+
const module = mod.module || mod.address;
|
|
57
|
+
const data = mod.initData || mod.data;
|
|
58
|
+
if (!module || !data) {
|
|
59
|
+
throw new Error("Module or data is missing");
|
|
60
|
+
}
|
|
61
|
+
return { module, data };
|
|
62
|
+
};
|
|
63
|
+
const getInitData = (params) => (0, viem_1.encodeAbiParameters)([
|
|
64
|
+
{ name: "bootstrap", type: "address" },
|
|
65
|
+
{ name: "initData", type: "bytes" },
|
|
66
|
+
], [
|
|
67
|
+
params.bootStrapAddress,
|
|
68
|
+
(0, viem_1.encodeFunctionData)({
|
|
69
|
+
abi: boostrap_json_1.default.abi,
|
|
70
|
+
functionName: "initNexusWithDefaultValidatorAndOtherModulesNoRegistry",
|
|
71
|
+
args: [
|
|
72
|
+
params.defaultValidator.data,
|
|
73
|
+
params.validators,
|
|
74
|
+
params.executors,
|
|
75
|
+
params.hook,
|
|
76
|
+
params.fallbacks,
|
|
77
|
+
params.prevalidationHooks,
|
|
78
|
+
],
|
|
79
|
+
}),
|
|
80
|
+
]);
|
|
81
|
+
const toValidator = (parameters) => {
|
|
82
|
+
const { deInitData = "0x", type = "validator", signer, data = "0x", module } = parameters, rest = __rest(parameters, ["deInitData", "type", "signer", "data", "module"]);
|
|
83
|
+
return Object.assign({ deInitData,
|
|
84
|
+
data,
|
|
85
|
+
module, address: module, signer,
|
|
86
|
+
type, getStubSignature: async () => DUMMY_SIGNATURE, signUserOpHash: async (userOpHash) => await signer.signMessage({ message: { raw: userOpHash } }), signMessage: async (message) => await signer.signMessage({ message }) }, rest);
|
|
87
|
+
};
|
|
88
|
+
const toDefaultModule = (parameters) => toValidator(Object.assign(Object.assign({ initData: parameters.signer.address, data: parameters.signer.address, deInitData: "0x" }, parameters), { address: viem_1.zeroAddress, module: viem_1.zeroAddress, type: "validator", getStubSignature: async () => DUMMY_SIGNATURE }));
|
|
89
|
+
const toEmptyHook = () => ({
|
|
90
|
+
module: viem_1.zeroAddress,
|
|
91
|
+
data: viem_1.zeroHash,
|
|
92
|
+
});
|
|
93
|
+
const computeNexusAccount = async (chain, transport, signer) => {
|
|
94
|
+
const publicClient = (0, viem_1.createPublicClient)({ chain, transport });
|
|
95
|
+
const defaultValidator = toDefaultModule({ signer });
|
|
96
|
+
const validators = [];
|
|
97
|
+
const executors = [];
|
|
98
|
+
const hook = toEmptyHook();
|
|
99
|
+
const fallbacks = [];
|
|
100
|
+
const registryAddress = viem_1.zeroAddress;
|
|
101
|
+
const factoryAddress = NEXUS_ACCOUNT_FACTORY_ADDRESS;
|
|
102
|
+
const bootStrapAddress = NEXUS_BOOTSTRAP_ADDRESS;
|
|
103
|
+
const prevalidationHooks = [];
|
|
104
|
+
const initData = getInitData({
|
|
105
|
+
defaultValidator: toInitData(defaultValidator),
|
|
106
|
+
validators: validators.map(toInitData),
|
|
107
|
+
executors: executors.map(toInitData),
|
|
108
|
+
hook: toInitData(hook),
|
|
109
|
+
fallbacks: fallbacks.map(toInitData),
|
|
110
|
+
registryAddress,
|
|
111
|
+
bootStrapAddress,
|
|
112
|
+
prevalidationHooks,
|
|
113
|
+
});
|
|
114
|
+
const salt = (0, viem_1.pad)((0, viem_1.toHex)(index), { size: 32 });
|
|
115
|
+
const nexusAccount = await publicClient.readContract({
|
|
116
|
+
address: factoryAddress,
|
|
117
|
+
abi: factory_json_1.default.abi,
|
|
118
|
+
functionName: "computeAccountAddress",
|
|
119
|
+
args: [initData, salt],
|
|
120
|
+
});
|
|
121
|
+
if ((nexusAccount === null || nexusAccount === void 0 ? void 0 : nexusAccount.toLowerCase()) !== (viem_1.zeroAddress === null || viem_1.zeroAddress === void 0 ? void 0 : viem_1.zeroAddress.toLowerCase())) {
|
|
122
|
+
return nexusAccount;
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
};
|
|
126
|
+
exports.computeNexusAccount = computeNexusAccount;
|
|
127
|
+
const computeNexusAddress = async ({ chain, transport, index, initData, factoryAddress = NEXUS_ACCOUNT_FACTORY_ADDRESS, }) => {
|
|
128
|
+
const publicClient = (0, viem_1.createPublicClient)({ chain, transport });
|
|
129
|
+
const salt = (0, viem_1.pad)((0, viem_1.toHex)(index), { size: 32 });
|
|
130
|
+
const address = await publicClient.readContract({
|
|
131
|
+
address: factoryAddress,
|
|
132
|
+
abi: factory_json_1.default.abi,
|
|
133
|
+
functionName: "computeAccountAddress",
|
|
134
|
+
args: [initData, salt],
|
|
135
|
+
});
|
|
136
|
+
if (!address || address === viem_1.zeroAddress) {
|
|
137
|
+
throw new Error("Failed to compute Nexus account address");
|
|
138
|
+
}
|
|
139
|
+
return address;
|
|
140
|
+
};
|
|
141
|
+
const buildInitCode = ({ factoryAddress = NEXUS_ACCOUNT_FACTORY_ADDRESS, factoryData, }) => {
|
|
142
|
+
return (0, viem_1.concatHex)([factoryAddress, factoryData]);
|
|
143
|
+
};
|
|
144
|
+
const sanitizeUrl = (value) => {
|
|
145
|
+
return value.replace(/https?:\/\/[^\s]+/g, "");
|
|
146
|
+
};
|
|
147
|
+
class NonceManager {
|
|
148
|
+
constructor() {
|
|
149
|
+
this.isNonceKeyBeingCalculated = new Map();
|
|
150
|
+
}
|
|
151
|
+
static getInstance() {
|
|
152
|
+
if (!NonceManager.instance) {
|
|
153
|
+
NonceManager.instance = new NonceManager();
|
|
154
|
+
}
|
|
155
|
+
return NonceManager.instance;
|
|
156
|
+
}
|
|
157
|
+
buildNonceStoreKey(accountAddress, chainId) {
|
|
158
|
+
return `${accountAddress.toLowerCase()}::${chainId}`;
|
|
159
|
+
}
|
|
160
|
+
async getDefaultNonceKey(accountAddress, chainId) {
|
|
161
|
+
const storeKey = this.buildNonceStoreKey(accountAddress, chainId);
|
|
162
|
+
while (this.isNonceKeyBeingCalculated.get(storeKey)) {
|
|
163
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
164
|
+
}
|
|
165
|
+
this.isNonceKeyBeingCalculated.set(storeKey, true);
|
|
166
|
+
const key = BigInt(Date.now());
|
|
167
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
168
|
+
this.isNonceKeyBeingCalculated.set(storeKey, false);
|
|
169
|
+
return key;
|
|
170
|
+
}
|
|
171
|
+
async getNonceWithKey(client, accountAddress, parameters) {
|
|
172
|
+
var _a;
|
|
173
|
+
const TIMESTAMP_ADJUSTMENT = BigInt(16777215);
|
|
174
|
+
const { key: key_, validationMode, moduleAddress } = parameters;
|
|
175
|
+
try {
|
|
176
|
+
const adjustedKey = BigInt(key_) % TIMESTAMP_ADJUSTMENT;
|
|
177
|
+
const key = (0, viem_1.concat)([
|
|
178
|
+
(0, viem_1.toHex)(adjustedKey, { size: 3 }),
|
|
179
|
+
validationMode,
|
|
180
|
+
moduleAddress,
|
|
181
|
+
]);
|
|
182
|
+
const entryPointContract = (0, viem_1.getContract)({
|
|
183
|
+
address: ENTRY_POINT_ADDRESS,
|
|
184
|
+
abi: entrypoint_1.EntrypointAbi,
|
|
185
|
+
client,
|
|
186
|
+
});
|
|
187
|
+
const nonce = await entryPointContract.read.getNonce([
|
|
188
|
+
accountAddress,
|
|
189
|
+
BigInt(key),
|
|
190
|
+
]);
|
|
191
|
+
return { nonceKey: BigInt(key), nonce };
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
const errorMessage = (_a = error.message) !== null && _a !== void 0 ? _a : "RPC issue";
|
|
195
|
+
throw new Error(`Failed to fetch nonce due to the error: ${sanitizeUrl(errorMessage)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const getDefaultNonceKey = async (accountAddress, chainId) => {
|
|
200
|
+
const manager = NonceManager.getInstance();
|
|
201
|
+
return manager.getDefaultNonceKey(accountAddress, chainId);
|
|
202
|
+
};
|
|
203
|
+
const getNonceWithKeyUtil = async (client, accountAddress, parameters) => {
|
|
204
|
+
const manager = NonceManager.getInstance();
|
|
205
|
+
return manager.getNonceWithKey(client, accountAddress, parameters);
|
|
206
|
+
};
|
|
207
|
+
const buildNexusRuntime = ({ chain, transport, signer, index, factoryAddress = NEXUS_ACCOUNT_FACTORY_ADDRESS, factoryData, initData, defaultValidator, meeConfig, walletClient, }) => {
|
|
208
|
+
const publicClient = (0, viem_1.createPublicClient)({ chain, transport });
|
|
209
|
+
let cachedAddress;
|
|
210
|
+
let module = defaultValidator;
|
|
211
|
+
// Lazy counterfactual address
|
|
212
|
+
const getAddress = async () => {
|
|
213
|
+
if (cachedAddress)
|
|
214
|
+
return cachedAddress;
|
|
215
|
+
cachedAddress = await computeNexusAddress({
|
|
216
|
+
chain,
|
|
217
|
+
transport,
|
|
218
|
+
index,
|
|
219
|
+
initData,
|
|
220
|
+
factoryAddress,
|
|
221
|
+
});
|
|
222
|
+
return cachedAddress;
|
|
223
|
+
};
|
|
224
|
+
// Init code for deployment / bundlers
|
|
225
|
+
const getInitCode = () => (0, viem_1.concatHex)([factoryAddress, factoryData]);
|
|
226
|
+
// Module management
|
|
227
|
+
const setModule = (newModule) => {
|
|
228
|
+
module = newModule;
|
|
229
|
+
};
|
|
230
|
+
const getModule = () => module;
|
|
231
|
+
// Nonce with key
|
|
232
|
+
const getNonceWithKey = async (accountAddress, params) => {
|
|
233
|
+
var _a, _b, _c;
|
|
234
|
+
const key = (_a = params === null || params === void 0 ? void 0 : params.key) !== null && _a !== void 0 ? _a : (await getDefaultNonceKey(accountAddress, chain.id));
|
|
235
|
+
const moduleAddress = (_b = params === null || params === void 0 ? void 0 : params.moduleAddress) !== null && _b !== void 0 ? _b : module.module;
|
|
236
|
+
const validationMode = (_c = params === null || params === void 0 ? void 0 : params.validationMode) !== null && _c !== void 0 ? _c : "0x00";
|
|
237
|
+
return getNonceWithKeyUtil(publicClient, accountAddress, {
|
|
238
|
+
key,
|
|
239
|
+
validationMode,
|
|
240
|
+
moduleAddress,
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
// User operation hash
|
|
244
|
+
const getUserOpHash = (userOp) => {
|
|
245
|
+
return (0, account_abstraction_1.getUserOperationHash)({
|
|
246
|
+
chainId: chain.id,
|
|
247
|
+
entryPointAddress: account_abstraction_1.entryPoint07Address,
|
|
248
|
+
entryPointVersion: "0.7",
|
|
249
|
+
userOperation: userOp,
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
const toDelegation = async (params) => {
|
|
253
|
+
const { authorization: authorization_, multiChain, delegatedContract, chainId, } = params || {};
|
|
254
|
+
const contractAddress = delegatedContract || meeConfig.implementationAddress;
|
|
255
|
+
const authorization = authorization_ ||
|
|
256
|
+
(await walletClient.signAuthorization({
|
|
257
|
+
contractAddress,
|
|
258
|
+
chainId: multiChain ? 0 : chainId,
|
|
259
|
+
}));
|
|
260
|
+
const eip7702Auth = {
|
|
261
|
+
chainId: `0x${authorization.chainId.toString(16)}`,
|
|
262
|
+
address: authorization.address,
|
|
263
|
+
nonce: `0x${authorization.nonce.toString(16)}`,
|
|
264
|
+
r: authorization.r,
|
|
265
|
+
s: authorization.s,
|
|
266
|
+
yParity: `0x${authorization.yParity.toString(16)}`,
|
|
267
|
+
};
|
|
268
|
+
return eip7702Auth;
|
|
269
|
+
};
|
|
270
|
+
const isDelegated = async () => {
|
|
271
|
+
const code = await publicClient.getCode({ address: signer.address });
|
|
272
|
+
return (!!code &&
|
|
273
|
+
(code === null || code === void 0 ? void 0 : code.toLowerCase().includes(meeConfig.implementationAddress.substring(2).toLowerCase())));
|
|
274
|
+
};
|
|
275
|
+
const unDelegate = async (params) => {
|
|
276
|
+
const { authorization } = params || {};
|
|
277
|
+
const deAuthorization = authorization ||
|
|
278
|
+
(await walletClient.signAuthorization({
|
|
279
|
+
address: viem_1.zeroAddress,
|
|
280
|
+
executor: "self",
|
|
281
|
+
}));
|
|
282
|
+
return await walletClient.sendTransaction({
|
|
283
|
+
to: signer.address,
|
|
284
|
+
data: "0xdeadbeef",
|
|
285
|
+
type: "eip7702",
|
|
286
|
+
authorizationList: [deAuthorization],
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
return {
|
|
290
|
+
getAddress,
|
|
291
|
+
getInitCode,
|
|
292
|
+
publicClient,
|
|
293
|
+
signer,
|
|
294
|
+
module,
|
|
295
|
+
setModule,
|
|
296
|
+
getModule,
|
|
297
|
+
getNonceWithKey,
|
|
298
|
+
getUserOpHash,
|
|
299
|
+
isDelegated,
|
|
300
|
+
toDelegation,
|
|
301
|
+
unDelegate,
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
const encodeExecuteBatch = async (calls, mode = exports.EXECUTE_BATCH) => {
|
|
305
|
+
const executionAbiParams = {
|
|
306
|
+
type: "tuple[]",
|
|
307
|
+
components: [
|
|
308
|
+
{ name: "target", type: "address" },
|
|
309
|
+
{ name: "value", type: "uint256" },
|
|
310
|
+
{ name: "callData", type: "bytes" },
|
|
311
|
+
],
|
|
312
|
+
};
|
|
313
|
+
const executions = calls.map((tx) => {
|
|
314
|
+
var _a, _b;
|
|
315
|
+
return ({
|
|
316
|
+
target: tx.to,
|
|
317
|
+
callData: (_a = tx.data) !== null && _a !== void 0 ? _a : "0x",
|
|
318
|
+
value: BigInt((_b = tx.value) !== null && _b !== void 0 ? _b : BigInt(0)),
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
const executionCalldataPrep = (0, viem_1.encodeAbiParameters)([executionAbiParams], [executions]);
|
|
322
|
+
return (0, viem_1.encodeFunctionData)({
|
|
323
|
+
abi: (0, viem_1.parseAbi)([
|
|
324
|
+
"function execute(bytes32 mode, bytes calldata executionCalldata) external",
|
|
325
|
+
]),
|
|
326
|
+
functionName: "execute",
|
|
327
|
+
args: [mode, executionCalldataPrep],
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
const encodeExecute = async (call, mode = EXECUTE_SINGLE) => {
|
|
331
|
+
var _a, _b;
|
|
332
|
+
const executionCalldata = (0, viem_1.encodePacked)(["address", "uint256", "bytes"], [
|
|
333
|
+
call.to,
|
|
334
|
+
BigInt((_a = call.value) !== null && _a !== void 0 ? _a : BigInt(0)),
|
|
335
|
+
((_b = call.data) !== null && _b !== void 0 ? _b : "0x"),
|
|
336
|
+
]);
|
|
337
|
+
return (0, viem_1.encodeFunctionData)({
|
|
338
|
+
abi: (0, viem_1.parseAbi)([
|
|
339
|
+
"function execute(bytes32 mode, bytes calldata executionCalldata) external",
|
|
340
|
+
]),
|
|
341
|
+
functionName: "execute",
|
|
342
|
+
args: [mode, executionCalldata],
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
const encodeExecuteComposable = async (calls) => {
|
|
346
|
+
const composableCalls = calls.map((call) => {
|
|
347
|
+
var _a;
|
|
348
|
+
return {
|
|
349
|
+
to: call.to,
|
|
350
|
+
value: (_a = call.value) !== null && _a !== void 0 ? _a : BigInt(0),
|
|
351
|
+
functionSig: call.functionSig,
|
|
352
|
+
inputParams: call.inputParams,
|
|
353
|
+
outputParams: call.outputParams,
|
|
354
|
+
};
|
|
355
|
+
});
|
|
356
|
+
return (0, viem_1.encodeFunctionData)({
|
|
357
|
+
abi: composability_1.COMPOSABILITY_MODULE_ABI,
|
|
358
|
+
functionName: "executeComposable",
|
|
359
|
+
args: [composableCalls],
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
const signMessage = async (message, module) => {
|
|
363
|
+
const tempSignature = await module.signMessage(message);
|
|
364
|
+
return (0, viem_1.encodePacked)(["address", "bytes"], [module.module, tempSignature]);
|
|
365
|
+
};
|
|
366
|
+
const signUserOperation = async (userOpHash, module) => {
|
|
367
|
+
return module.signUserOpHash(userOpHash);
|
|
368
|
+
};
|
|
369
|
+
const toSigner = async ({ signer, address, }) => {
|
|
370
|
+
var _a, _b, _c;
|
|
371
|
+
if (!signer) {
|
|
372
|
+
throw new Error("Signer is required");
|
|
373
|
+
}
|
|
374
|
+
if ("provider" in signer) {
|
|
375
|
+
const wallet = signer;
|
|
376
|
+
const address = await wallet.getAddress();
|
|
377
|
+
return (0, accounts_1.toAccount)({
|
|
378
|
+
address: (0, viem_1.getAddress)(address),
|
|
379
|
+
async signMessage({ message }) {
|
|
380
|
+
if (typeof message === "string") {
|
|
381
|
+
return await wallet.signMessage(message);
|
|
382
|
+
}
|
|
383
|
+
if (typeof (message === null || message === void 0 ? void 0 : message.raw) === "string") {
|
|
384
|
+
return await wallet.signMessage((0, viem_1.hexToBytes)(message.raw));
|
|
385
|
+
}
|
|
386
|
+
return await wallet.signMessage(message.raw);
|
|
387
|
+
},
|
|
388
|
+
async signTransaction(_) {
|
|
389
|
+
throw new Error("Not supported");
|
|
390
|
+
},
|
|
391
|
+
async signTypedData(typedData) {
|
|
392
|
+
return wallet.signTypedData(typedData.domain, typedData.types, typedData.message);
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if ("type" in signer && signer.type === "local") {
|
|
397
|
+
return signer;
|
|
398
|
+
}
|
|
399
|
+
let walletClient = undefined;
|
|
400
|
+
if ("request" in signer) {
|
|
401
|
+
if (!address) {
|
|
402
|
+
try {
|
|
403
|
+
[address] = await signer.request({
|
|
404
|
+
method: "eth_requestAccounts",
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
catch (_d) {
|
|
408
|
+
[address] = await signer.request({
|
|
409
|
+
method: "eth_accounts",
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (!address) {
|
|
414
|
+
// For TS to be happy
|
|
415
|
+
throw new Error("address is required");
|
|
416
|
+
}
|
|
417
|
+
walletClient = (0, viem_1.createWalletClient)({
|
|
418
|
+
account: address,
|
|
419
|
+
transport: (0, viem_1.custom)(signer),
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (!walletClient) {
|
|
423
|
+
walletClient = signer;
|
|
424
|
+
}
|
|
425
|
+
const addressFromWalletClient = (_b = (_a = walletClient === null || walletClient === void 0 ? void 0 : walletClient.account) === null || _a === void 0 ? void 0 : _a.address) !== null && _b !== void 0 ? _b : (_c = (await (walletClient === null || walletClient === void 0 ? void 0 : walletClient.getAddresses()))) === null || _c === void 0 ? void 0 : _c[0];
|
|
426
|
+
if (!addressFromWalletClient) {
|
|
427
|
+
throw new Error("address not found in wallet client");
|
|
428
|
+
}
|
|
429
|
+
return (0, accounts_1.toAccount)({
|
|
430
|
+
address: addressFromWalletClient,
|
|
431
|
+
async signMessage({ message }) {
|
|
432
|
+
return walletClient.signMessage({ message });
|
|
433
|
+
},
|
|
434
|
+
async signTypedData(typedData) {
|
|
435
|
+
return (0, utils_1.getAction)(walletClient, accounts_1.signTypedData, "signTypedData")(typedData);
|
|
436
|
+
},
|
|
437
|
+
async signTransaction(_) {
|
|
438
|
+
throw new Error("Not supported");
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
};
|
|
442
|
+
const toWalletClient = ({ unresolvedSigner, resolvedSigner, chain, transport, }) => {
|
|
443
|
+
var _a;
|
|
444
|
+
const browserSigner = ((_a = unresolvedSigner === null || unresolvedSigner === void 0 ? void 0 : unresolvedSigner.transport) === null || _a === void 0 ? void 0 : _a.key) === "custom";
|
|
445
|
+
return (0, viem_1.createWalletClient)(browserSigner
|
|
446
|
+
? {
|
|
447
|
+
account: resolvedSigner.address,
|
|
448
|
+
chain,
|
|
449
|
+
// @ts-ignore
|
|
450
|
+
transport: (0, viem_1.custom)(window === null || window === void 0 ? void 0 : window.ethereum),
|
|
451
|
+
}
|
|
452
|
+
: {
|
|
453
|
+
account: resolvedSigner,
|
|
454
|
+
chain,
|
|
455
|
+
transport,
|
|
456
|
+
}).extend(viem_1.publicActions);
|
|
457
|
+
};
|
|
458
|
+
const prepareValidators = async (signer, meeConfig, customValidators) => {
|
|
459
|
+
return [];
|
|
460
|
+
};
|
|
461
|
+
const prepareExecutors = (meeConfig, customExecutors) => {
|
|
462
|
+
let executors = [];
|
|
463
|
+
executors = customExecutors || [];
|
|
464
|
+
return executors;
|
|
465
|
+
};
|
|
466
|
+
const prepareFallbacks = (meeConfig, customFallbacks) => {
|
|
467
|
+
let fallbacks = [];
|
|
468
|
+
fallbacks = customFallbacks || [];
|
|
469
|
+
return fallbacks;
|
|
470
|
+
};
|
|
471
|
+
const getInitDataNoRegistry = (params) => {
|
|
472
|
+
try {
|
|
473
|
+
return (0, viem_1.encodeAbiParameters)([
|
|
474
|
+
{ name: "bootstrap", type: "address" },
|
|
475
|
+
{ name: "bootstrapData", type: "bytes" },
|
|
476
|
+
], [
|
|
477
|
+
params.bootStrapAddress,
|
|
478
|
+
(0, viem_1.encodeFunctionData)({
|
|
479
|
+
abi: boostrap_json_1.default === null || boostrap_json_1.default === void 0 ? void 0 : boostrap_json_1.default.abi,
|
|
480
|
+
functionName: "initNexusWithDefaultValidatorAndOtherModulesNoRegistry",
|
|
481
|
+
args: [
|
|
482
|
+
params.defaultValidator.data,
|
|
483
|
+
params.validators,
|
|
484
|
+
params.executors,
|
|
485
|
+
params.hook,
|
|
486
|
+
params.fallbacks,
|
|
487
|
+
params.prevalidationHooks,
|
|
488
|
+
],
|
|
489
|
+
}),
|
|
490
|
+
]);
|
|
491
|
+
}
|
|
492
|
+
catch (err) {
|
|
493
|
+
console.error("getInitDataNoRegistry failed with error: ", err);
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
const getFactoryData = (parameters) => {
|
|
497
|
+
const { index, initData } = parameters;
|
|
498
|
+
const salt = (0, viem_1.pad)((0, viem_1.toHex)(index), { size: 32 });
|
|
499
|
+
return (0, viem_1.encodeFunctionData)({
|
|
500
|
+
abi: (0, viem_1.parseAbi)([
|
|
501
|
+
"function createAccount(bytes initData, bytes32 salt) external returns (address)",
|
|
502
|
+
]),
|
|
503
|
+
functionName: "createAccount",
|
|
504
|
+
args: [initData, salt],
|
|
505
|
+
});
|
|
506
|
+
};
|
|
507
|
+
const prepareFactoryData = (meeConfig, initDataParams) => {
|
|
508
|
+
let factoryData = "0x";
|
|
509
|
+
let initData = "0x";
|
|
510
|
+
initData =
|
|
511
|
+
initDataParams.customInitData ||
|
|
512
|
+
getInitDataNoRegistry({
|
|
513
|
+
defaultValidator: initDataParams.defaultValidator,
|
|
514
|
+
prevalidationHooks: initDataParams.prevalidationHooks,
|
|
515
|
+
validators: initDataParams.validators,
|
|
516
|
+
executors: initDataParams.executors,
|
|
517
|
+
hook: initDataParams.hook,
|
|
518
|
+
fallbacks: initDataParams.fallbacks,
|
|
519
|
+
bootStrapAddress: meeConfig.bootStrapAddress,
|
|
520
|
+
});
|
|
521
|
+
factoryData = getFactoryData({
|
|
522
|
+
initData,
|
|
523
|
+
index: initDataParams.accountIndex,
|
|
524
|
+
});
|
|
525
|
+
return { initData, factoryData };
|
|
526
|
+
};
|
|
527
|
+
const getAccountDomainStructFields = async (publicClient, accountAddress) => {
|
|
528
|
+
const accountDomainStructFields = (await publicClient.readContract({
|
|
529
|
+
address: accountAddress,
|
|
530
|
+
abi: (0, viem_1.parseAbi)([
|
|
531
|
+
"function eip712Domain() public view returns (bytes1 fields, string memory name, string memory version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] memory extensions)",
|
|
532
|
+
]),
|
|
533
|
+
functionName: "eip712Domain",
|
|
534
|
+
}));
|
|
535
|
+
const [, name, version, chainId, verifyingContract, salt] = accountDomainStructFields;
|
|
536
|
+
const params = (0, viem_1.parseAbiParameters)([
|
|
537
|
+
"bytes32",
|
|
538
|
+
"bytes32",
|
|
539
|
+
"uint256",
|
|
540
|
+
"address",
|
|
541
|
+
"bytes32",
|
|
542
|
+
]);
|
|
543
|
+
return (0, viem_1.encodeAbiParameters)(params, [
|
|
544
|
+
(0, viem_1.keccak256)((0, viem_1.toBytes)(name)),
|
|
545
|
+
(0, viem_1.keccak256)((0, viem_1.toBytes)(version)),
|
|
546
|
+
chainId,
|
|
547
|
+
verifyingContract,
|
|
548
|
+
salt,
|
|
549
|
+
]);
|
|
550
|
+
};
|
|
551
|
+
const eip712WrapHash = (typedHash, appDomainSeparator) => (0, viem_1.keccak256)((0, viem_1.concat)(["0x1901", appDomainSeparator, typedHash]));
|
|
552
|
+
const typeToString = (typeDef) => {
|
|
553
|
+
return Object.entries(typeDef).map(([key, fields]) => {
|
|
554
|
+
const fieldArray = Array.isArray(fields) ? fields : [];
|
|
555
|
+
const fieldStrings = fieldArray
|
|
556
|
+
.map((field) => `${field.type} ${field.name}`)
|
|
557
|
+
.join(",");
|
|
558
|
+
return `${key}(${fieldStrings})`;
|
|
559
|
+
});
|
|
560
|
+
};
|
|
561
|
+
const createNexusAccount = async ({ chain, transport, _signer, index = BigInt(0), customValidators, customExecutors, customHook, customFallbacks, customPrevalidationHooks, customInitData, meeConfig, }) => {
|
|
562
|
+
var _a;
|
|
563
|
+
const publicClient = (0, viem_1.createPublicClient)({ chain, transport });
|
|
564
|
+
const addressesToDeploymentSet = new Set([
|
|
565
|
+
meeConfig.bootStrapAddress,
|
|
566
|
+
meeConfig.defaultValidatorAddress,
|
|
567
|
+
meeConfig.validatorAddress,
|
|
568
|
+
meeConfig.factoryAddress,
|
|
569
|
+
meeConfig.implementationAddress,
|
|
570
|
+
]);
|
|
571
|
+
if (meeConfig.moduleRegistry) {
|
|
572
|
+
addressesToDeploymentSet.add(meeConfig.moduleRegistry.registryAddress);
|
|
573
|
+
}
|
|
574
|
+
if (meeConfig.composableModuleAddress) {
|
|
575
|
+
addressesToDeploymentSet.add(meeConfig.composableModuleAddress);
|
|
576
|
+
}
|
|
577
|
+
// Filtering zero address because sometimes the default validator is zeroAddress which needs to be excluded
|
|
578
|
+
const addressesToDeploymentCheck = [...addressesToDeploymentSet].filter((address) => address !== viem_1.zeroAddress);
|
|
579
|
+
await Promise.all(addressesToDeploymentCheck.map(async (address) => {
|
|
580
|
+
// Checks if the MEE contracts are deployed or not
|
|
581
|
+
// This ensures the MEE version suite is supported or not for the chain
|
|
582
|
+
const bytecode = await publicClient.getCode({
|
|
583
|
+
address,
|
|
584
|
+
});
|
|
585
|
+
if (!bytecode || bytecode === "0x") {
|
|
586
|
+
console.debug(`MEE version (${meeConfig.version}) is not supported for the ${chain.name} chain. Contract address (${address}) is not deployed`);
|
|
587
|
+
throw new Error(`MEE version (${meeConfig.version}) is not supported for the ${chain.name} chain.`);
|
|
588
|
+
}
|
|
589
|
+
}));
|
|
590
|
+
const signer = await toSigner({ signer: _signer });
|
|
591
|
+
const walletClient = toWalletClient({
|
|
592
|
+
unresolvedSigner: _signer,
|
|
593
|
+
resolvedSigner: signer,
|
|
594
|
+
chain,
|
|
595
|
+
transport,
|
|
596
|
+
});
|
|
597
|
+
// Prepare validator modules
|
|
598
|
+
const validators = await prepareValidators(signer, meeConfig, customValidators);
|
|
599
|
+
const defaultValidator = toDefaultModule({ signer });
|
|
600
|
+
// For 1.2.x accounts, no explicit validators will be added. So default validator will be used
|
|
601
|
+
let module = validators[0] || defaultValidator;
|
|
602
|
+
// Prepare executor modules
|
|
603
|
+
const executors = prepareExecutors(meeConfig, customExecutors);
|
|
604
|
+
// Prepare hook module
|
|
605
|
+
const hook = customHook || toEmptyHook();
|
|
606
|
+
// Prepare fallback modules
|
|
607
|
+
const fallbacks = prepareFallbacks(meeConfig, customFallbacks);
|
|
608
|
+
// Generate the initialization data for the account using the initNexus function
|
|
609
|
+
const prevalidationHooks = customPrevalidationHooks || [];
|
|
610
|
+
// prepare factory data
|
|
611
|
+
const { initData, factoryData } = prepareFactoryData(meeConfig, {
|
|
612
|
+
accountIndex: index,
|
|
613
|
+
defaultValidator: toInitData(defaultValidator),
|
|
614
|
+
prevalidationHooks,
|
|
615
|
+
validators: validators.map(toInitData),
|
|
616
|
+
executors: executors.map(toInitData),
|
|
617
|
+
hook: toInitData(hook),
|
|
618
|
+
fallbacks: fallbacks.map(toInitData),
|
|
619
|
+
customInitData,
|
|
620
|
+
});
|
|
621
|
+
let accountId;
|
|
622
|
+
if (meeConfig === null || meeConfig === void 0 ? void 0 : meeConfig.implementationAddress) {
|
|
623
|
+
accountId = (await publicClient.readContract({
|
|
624
|
+
address: meeConfig.implementationAddress,
|
|
625
|
+
abi: (0, viem_1.parseAbi)(["function accountId() public view returns (string)"]),
|
|
626
|
+
functionName: "accountId",
|
|
627
|
+
args: [],
|
|
628
|
+
}));
|
|
629
|
+
}
|
|
630
|
+
const runtime = buildNexusRuntime({
|
|
631
|
+
chain,
|
|
632
|
+
transport,
|
|
633
|
+
signer,
|
|
634
|
+
index,
|
|
635
|
+
factoryAddress: NEXUS_ACCOUNT_FACTORY_ADDRESS,
|
|
636
|
+
factoryData,
|
|
637
|
+
initData,
|
|
638
|
+
defaultValidator,
|
|
639
|
+
meeConfig,
|
|
640
|
+
walletClient,
|
|
641
|
+
});
|
|
642
|
+
const getNonce = async (parameters) => {
|
|
643
|
+
const accountAddress = await runtime.getAddress();
|
|
644
|
+
const { nonce } = await runtime.getNonceWithKey(accountAddress, parameters);
|
|
645
|
+
return nonce;
|
|
646
|
+
};
|
|
647
|
+
const signTypedData = async (parameters) => {
|
|
648
|
+
const { message, primaryType, types: _types, domain } = parameters;
|
|
649
|
+
if (!domain)
|
|
650
|
+
throw new Error("Missing domain");
|
|
651
|
+
if (!message)
|
|
652
|
+
throw new Error("Missing message");
|
|
653
|
+
const types = Object.assign({ EIP712Domain: (0, viem_1.getTypesForEIP712Domain)({ domain }) }, _types);
|
|
654
|
+
// @ts-ignore: Comes from nexus parent typehash
|
|
655
|
+
const messageStuff = message.stuff;
|
|
656
|
+
// @ts-ignore
|
|
657
|
+
validateTypedData({
|
|
658
|
+
domain,
|
|
659
|
+
message,
|
|
660
|
+
primaryType,
|
|
661
|
+
types,
|
|
662
|
+
});
|
|
663
|
+
const appDomainSeparator = (0, viem_1.domainSeparator)({ domain });
|
|
664
|
+
const accountDomainStructFields = await getAccountDomainStructFields(publicClient, await runtime.getAddress());
|
|
665
|
+
const parentStructHash = (0, viem_1.keccak256)((0, viem_1.encodePacked)(["bytes", "bytes"], [
|
|
666
|
+
(0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)(["bytes32, bytes32"]), [
|
|
667
|
+
(0, viem_1.keccak256)((0, viem_1.toBytes)(exports.PARENT_TYPEHASH)),
|
|
668
|
+
messageStuff,
|
|
669
|
+
]),
|
|
670
|
+
accountDomainStructFields,
|
|
671
|
+
]));
|
|
672
|
+
const wrappedTypedHash = eip712WrapHash(parentStructHash, appDomainSeparator);
|
|
673
|
+
let signature = await module.signMessage({
|
|
674
|
+
raw: (0, viem_1.toBytes)(wrappedTypedHash),
|
|
675
|
+
});
|
|
676
|
+
const contentsType = (0, viem_1.toBytes)(typeToString(types)[1]);
|
|
677
|
+
const signatureData = (0, viem_1.concatHex)([
|
|
678
|
+
signature,
|
|
679
|
+
appDomainSeparator,
|
|
680
|
+
messageStuff,
|
|
681
|
+
(0, viem_1.toHex)(contentsType),
|
|
682
|
+
(0, viem_1.toHex)(contentsType.length, { size: 2 }),
|
|
683
|
+
]);
|
|
684
|
+
signature = (0, viem_1.encodePacked)(["address", "bytes"], [module.module, signatureData]);
|
|
685
|
+
return signature;
|
|
686
|
+
};
|
|
687
|
+
return (0, account_abstraction_1.toSmartAccount)({
|
|
688
|
+
client: publicClient,
|
|
689
|
+
entryPoint: {
|
|
690
|
+
abi: entrypoint_1.EntrypointAbi,
|
|
691
|
+
address: ENTRY_POINT_ADDRESS,
|
|
692
|
+
version: "0.7",
|
|
693
|
+
},
|
|
694
|
+
getAddress: runtime.getAddress,
|
|
695
|
+
encodeCalls: (calls) => {
|
|
696
|
+
return calls.length === 1
|
|
697
|
+
? encodeExecute(calls[0], runtime.module)
|
|
698
|
+
: encodeExecuteBatch(calls, runtime.module);
|
|
699
|
+
},
|
|
700
|
+
getFactoryArgs: async () => ({
|
|
701
|
+
factory: meeConfig.factoryAddress,
|
|
702
|
+
factoryData,
|
|
703
|
+
}),
|
|
704
|
+
getStubSignature: async () => module.getStubSignature(),
|
|
705
|
+
/**
|
|
706
|
+
* @description Signs a message
|
|
707
|
+
* @param params - The parameters for signing
|
|
708
|
+
* @param params.message - The message to sign
|
|
709
|
+
* @returns The signature
|
|
710
|
+
*/
|
|
711
|
+
async signMessage({ message }) {
|
|
712
|
+
const tempSignature = await module.signMessage(message);
|
|
713
|
+
return (0, viem_1.encodePacked)(["address", "bytes"], [module.module, tempSignature]);
|
|
714
|
+
},
|
|
715
|
+
signTypedData,
|
|
716
|
+
signUserOperation: async (parameters) => {
|
|
717
|
+
const { chainId = publicClient.chain.id } = parameters, userOpWithoutSender = __rest(parameters, ["chainId"]);
|
|
718
|
+
const address = await runtime.getAddress();
|
|
719
|
+
const userOperation = Object.assign(Object.assign({}, userOpWithoutSender), { sender: address });
|
|
720
|
+
const hash = (0, account_abstraction_1.getUserOperationHash)({
|
|
721
|
+
chainId,
|
|
722
|
+
entryPointAddress: account_abstraction_1.entryPoint07Address,
|
|
723
|
+
entryPointVersion: "0.7",
|
|
724
|
+
userOperation,
|
|
725
|
+
});
|
|
726
|
+
return await module.signUserOpHash(hash);
|
|
727
|
+
},
|
|
728
|
+
getNonce,
|
|
729
|
+
extend: {
|
|
730
|
+
entryPointAddress: account_abstraction_1.entryPoint07Address,
|
|
731
|
+
accountId,
|
|
732
|
+
encodeExecute,
|
|
733
|
+
encodeExecuteBatch,
|
|
734
|
+
encodeExecuteComposable,
|
|
735
|
+
factoryData,
|
|
736
|
+
registryAddress: ((_a = meeConfig.moduleRegistry) === null || _a === void 0 ? void 0 : _a.registryAddress) || viem_1.zeroAddress,
|
|
737
|
+
walletClient,
|
|
738
|
+
version: meeConfig,
|
|
739
|
+
isDelegated: runtime.isDelegated,
|
|
740
|
+
toDelegation: runtime.toDelegation,
|
|
741
|
+
unDelegate: runtime.unDelegate,
|
|
742
|
+
getAddress: runtime.getAddress,
|
|
743
|
+
getInitCode: runtime.getInitCode,
|
|
744
|
+
getNonceWithKey: runtime.getNonceWithKey,
|
|
745
|
+
getUserOpHash: runtime.getUserOpHash,
|
|
746
|
+
setModule: runtime.setModule,
|
|
747
|
+
getModule: runtime.getModule,
|
|
748
|
+
factoryAddress: NEXUS_ACCOUNT_FACTORY_ADDRESS,
|
|
749
|
+
signer: runtime.signer,
|
|
750
|
+
publicClient: runtime.publicClient,
|
|
751
|
+
chain,
|
|
752
|
+
module: runtime.module,
|
|
753
|
+
},
|
|
754
|
+
});
|
|
755
|
+
};
|
|
756
|
+
exports.createNexusAccount = createNexusAccount;
|
|
757
|
+
const buildChainConfigurations = (chains, transports) => {
|
|
758
|
+
if (chains.length !== transports.length) {
|
|
759
|
+
throw new Error("chains and transports length mismatch");
|
|
760
|
+
}
|
|
761
|
+
return chains.map((chain, i) => ({
|
|
762
|
+
chain,
|
|
763
|
+
transport: transports[i],
|
|
764
|
+
}));
|
|
765
|
+
};
|
|
766
|
+
const buildDefaultInstructions = async (baseParams, instructions) => {
|
|
767
|
+
const { currentInstructions = [] } = baseParams;
|
|
768
|
+
return [
|
|
769
|
+
...currentInstructions,
|
|
770
|
+
...(Array.isArray(instructions) ? instructions : [instructions]),
|
|
771
|
+
];
|
|
772
|
+
};
|
|
773
|
+
exports.buildDefaultInstructions = buildDefaultInstructions;
|
|
774
|
+
const buildDecorator = async (baseParams, parameters) => {
|
|
775
|
+
const { type, data } = parameters;
|
|
776
|
+
switch (type) {
|
|
777
|
+
// case "intent": {
|
|
778
|
+
// return buildIntent(baseParams, data)
|
|
779
|
+
// }
|
|
780
|
+
case "default": {
|
|
781
|
+
return (0, exports.buildDefaultInstructions)(baseParams, data);
|
|
782
|
+
}
|
|
783
|
+
// case "transferFrom": {
|
|
784
|
+
// return buildTransferFrom(baseParams, data)
|
|
785
|
+
// }
|
|
786
|
+
// case "transfer": {
|
|
787
|
+
// return buildTransfer(baseParams, data)
|
|
788
|
+
// }
|
|
789
|
+
// case "approve": {
|
|
790
|
+
// return buildApprove(baseParams, data)
|
|
791
|
+
// }
|
|
792
|
+
// case "withdrawal": {
|
|
793
|
+
// return buildWithdrawal(baseParams, data)
|
|
794
|
+
// }
|
|
795
|
+
// case "batch": {
|
|
796
|
+
// return buildBatch(baseParams, data)
|
|
797
|
+
// }
|
|
798
|
+
// case "multichain": {
|
|
799
|
+
// return buildMultichainInstructions(baseParams, data)
|
|
800
|
+
// }
|
|
801
|
+
default: {
|
|
802
|
+
throw new Error(`Unknown build action type: ${type}`);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
const getUnifiedERC20BalanceDecorator = async (parameters) => {
|
|
807
|
+
const { mcToken, account: account_ } = parameters;
|
|
808
|
+
const relevantTokensByChain = Array.from(mcToken.deployments).filter(([chainId]) => account_.deployments.some((account) => { var _a; return ((_a = account.client.chain) === null || _a === void 0 ? void 0 : _a.id) === chainId; }));
|
|
809
|
+
const balances = await Promise.all(relevantTokensByChain.map(async ([chainId, address]) => {
|
|
810
|
+
const { publicClient, address: accountAddress } = account_.deploymentOn(chainId, true);
|
|
811
|
+
const tokenContract = (0, viem_1.getContract)({
|
|
812
|
+
abi: viem_1.erc20Abi,
|
|
813
|
+
address,
|
|
814
|
+
client: publicClient,
|
|
815
|
+
});
|
|
816
|
+
const [balance, decimals] = await Promise.all([
|
|
817
|
+
tokenContract.read.balanceOf([accountAddress]),
|
|
818
|
+
tokenContract.read.decimals(),
|
|
819
|
+
]);
|
|
820
|
+
return {
|
|
821
|
+
balance,
|
|
822
|
+
decimals,
|
|
823
|
+
chainId,
|
|
824
|
+
};
|
|
825
|
+
}));
|
|
826
|
+
return Object.assign(Object.assign({}, balances
|
|
827
|
+
.map((balance) => {
|
|
828
|
+
return {
|
|
829
|
+
balance: balance.balance,
|
|
830
|
+
decimals: balance.decimals,
|
|
831
|
+
};
|
|
832
|
+
})
|
|
833
|
+
.reduce((curr, acc) => {
|
|
834
|
+
if (curr.decimals !== acc.decimals) {
|
|
835
|
+
throw Error(`
|
|
836
|
+
Error while trying to fetch a unified ERC20 balance. The addresses provided
|
|
837
|
+
in the mapping don't have the same number of decimals across all chains.
|
|
838
|
+
The function can't fetch a unified balance for token mappings with differing
|
|
839
|
+
decimals.
|
|
840
|
+
`);
|
|
841
|
+
}
|
|
842
|
+
return {
|
|
843
|
+
balance: curr.balance + acc.balance,
|
|
844
|
+
decimals: curr.decimals,
|
|
845
|
+
};
|
|
846
|
+
})), { breakdown: balances, mcToken });
|
|
847
|
+
};
|
|
848
|
+
const waitForTransactionReceipts = async (parameters) => {
|
|
849
|
+
const receipts = await Promise.all(parameters.account.deployments.map(({ publicClient }, i) => publicClient.waitForTransactionReceipt({
|
|
850
|
+
hash: parameters.hashes[i],
|
|
851
|
+
confirmations: 5,
|
|
852
|
+
})));
|
|
853
|
+
const failure = receipts.find((receipt) => receipt.status !== "success");
|
|
854
|
+
return { receipts, status: failure ? failure.status : "success" };
|
|
855
|
+
};
|
|
856
|
+
const isDelegatedDecorator = async (parameters) => (await Promise.all(parameters.account.deployments.map(({ isDelegated }) => isDelegated()))).every(Boolean);
|
|
857
|
+
const unDelegateDecorator = async (parameters) => {
|
|
858
|
+
const hashes = await Promise.all(parameters.account.deployments.map(({ unDelegate }) => unDelegate()));
|
|
859
|
+
return await waitForTransactionReceipts({
|
|
860
|
+
account: parameters.account,
|
|
861
|
+
hashes,
|
|
862
|
+
});
|
|
863
|
+
};
|
|
864
|
+
exports.unDelegateDecorator = unDelegateDecorator;
|
|
865
|
+
const waitForTransactionReceiptsDecorator = async (parameters) => {
|
|
866
|
+
const receipts = await Promise.all(parameters.account.deployments.map(({ publicClient }, i) => publicClient.waitForTransactionReceipt({
|
|
867
|
+
hash: parameters.hashes[i],
|
|
868
|
+
confirmations: 5,
|
|
869
|
+
})));
|
|
870
|
+
const failure = receipts.find((receipt) => receipt.status !== "success");
|
|
871
|
+
return { receipts, status: failure ? failure.status : "success" };
|
|
872
|
+
};
|
|
873
|
+
exports.waitForTransactionReceiptsDecorator = waitForTransactionReceiptsDecorator;
|
|
874
|
+
const createMultiChainNexusAccount = async (params) => {
|
|
875
|
+
const { signer: unresolvedSigner, chains, transports } = params, accountParameters = __rest(params, ["signer", "chains", "transports"]);
|
|
876
|
+
if (!(chains === null || chains === void 0 ? void 0 : chains.length)) {
|
|
877
|
+
throw new Error("No chains provided");
|
|
878
|
+
}
|
|
879
|
+
const chainConfigurations = buildChainConfigurations(chains, transports);
|
|
880
|
+
if (chainConfigurations.length === 0) {
|
|
881
|
+
throw new Error("No chain configuration provided");
|
|
882
|
+
}
|
|
883
|
+
const meeConfig = {
|
|
884
|
+
version: "2.0.0",
|
|
885
|
+
accountId: "biconomy.nexus.1.2.0",
|
|
886
|
+
factoryAddress: "0x000000001D1D5004a02bAfAb9de2D6CE5b7B13de", // Nexus Account Factory Address
|
|
887
|
+
bootStrapAddress: "0x00000000D3254452a909E4eeD47455Af7E27C289",
|
|
888
|
+
implementationAddress: "0x000000004F43C49e93C970E84001853a70923B03",
|
|
889
|
+
validatorAddress: "0x00000000d12897DDAdC2044614A9677B191A2d95", // K1 MEE Validator Address
|
|
890
|
+
defaultValidatorAddress: viem_1.zeroAddress,
|
|
891
|
+
ethForwarderAddress: "0x000000Afe527A978Ecb761008Af475cfF04132a1",
|
|
892
|
+
};
|
|
893
|
+
// 🔁 Create one Nexus account per chain
|
|
894
|
+
const deployments = await Promise.all(chainConfigurations.map(async ({ chain, transport }) => (0, exports.createNexusAccount)(Object.assign({ chain,
|
|
895
|
+
transport, _signer: unresolvedSigner, meeConfig }, accountParameters))));
|
|
896
|
+
/* -------------------------------------------------- */
|
|
897
|
+
/* Deployment helpers */
|
|
898
|
+
/* -------------------------------------------------- */
|
|
899
|
+
function deploymentOn(chainId, strictMode) {
|
|
900
|
+
const deployment = deployments.find((dep) => { var _a; return ((_a = dep.publicClient.chain) === null || _a === void 0 ? void 0 : _a.id) === chainId; });
|
|
901
|
+
if (!deployment && strictMode) {
|
|
902
|
+
throw new Error(`Deployment not found for chainId: ${chainId}`);
|
|
903
|
+
}
|
|
904
|
+
return deployment;
|
|
905
|
+
}
|
|
906
|
+
function addressOn(chainId, strictMode) {
|
|
907
|
+
var _a;
|
|
908
|
+
return (_a = deploymentOn(chainId, strictMode)) === null || _a === void 0 ? void 0 : _a.address;
|
|
909
|
+
}
|
|
910
|
+
/* -------------------------------------------------- */
|
|
911
|
+
/* Base multichain account */
|
|
912
|
+
/* -------------------------------------------------- */
|
|
913
|
+
const baseAccount = {
|
|
914
|
+
signer: deployments[0].signer, // resolved signer
|
|
915
|
+
deployments,
|
|
916
|
+
deploymentOn,
|
|
917
|
+
addressOn,
|
|
918
|
+
};
|
|
919
|
+
// /* -------------------------------------------------- */
|
|
920
|
+
// /* Decorators (SDK parity) */
|
|
921
|
+
// /* -------------------------------------------------- */
|
|
922
|
+
const getUnifiedERC20Balance = (mcToken) => getUnifiedERC20BalanceDecorator({
|
|
923
|
+
mcToken,
|
|
924
|
+
account: baseAccount,
|
|
925
|
+
});
|
|
926
|
+
const build = (params, currentInstructions) => buildDecorator({
|
|
927
|
+
currentInstructions,
|
|
928
|
+
accountAddress: baseAccount.signer.address,
|
|
929
|
+
}, params);
|
|
930
|
+
// const buildComposable = (
|
|
931
|
+
// params: any,
|
|
932
|
+
// currentInstructions?: any[]
|
|
933
|
+
// ) =>
|
|
934
|
+
// buildComposableDecorator(
|
|
935
|
+
// {
|
|
936
|
+
// currentInstructions,
|
|
937
|
+
// accountAddress: baseAccount.signer.address
|
|
938
|
+
// },
|
|
939
|
+
// params
|
|
940
|
+
// )
|
|
941
|
+
// const buildBridgeInstructions = (
|
|
942
|
+
// params: any
|
|
943
|
+
// ) =>
|
|
944
|
+
// buildBridgeInstructionsDecorator({
|
|
945
|
+
// ...params
|
|
946
|
+
// })
|
|
947
|
+
// const queryBridge = (params: any) =>
|
|
948
|
+
// queryBridgeDecorator({ ...params })
|
|
949
|
+
const isDelegated = (parameters) => isDelegatedDecorator(Object.assign(Object.assign({}, parameters), { account: baseAccount }));
|
|
950
|
+
const unDelegate = (parameters) => (0, exports.unDelegateDecorator)(Object.assign(Object.assign({}, parameters), { account: baseAccount }));
|
|
951
|
+
const waitForTransactionReceipts = (parameters) => (0, exports.waitForTransactionReceiptsDecorator)(Object.assign(Object.assign({}, parameters), { account: baseAccount }));
|
|
952
|
+
// const read = <T>(params: any) =>
|
|
953
|
+
// multichainRead(baseAccount, params) as Promise<
|
|
954
|
+
// any
|
|
955
|
+
// >
|
|
956
|
+
/* -------------------------------------------------- */
|
|
957
|
+
/* Delegation (chainId = 0 semantics) */
|
|
958
|
+
/* -------------------------------------------------- */
|
|
959
|
+
const toDelegation = async () => await deployments[0].extend.toDelegation({ multiChain: true });
|
|
960
|
+
/* -------------------------------------------------- */
|
|
961
|
+
/* Final multichain account */
|
|
962
|
+
/* -------------------------------------------------- */
|
|
963
|
+
return Object.assign(Object.assign({}, baseAccount), { getUnifiedERC20Balance,
|
|
964
|
+
build,
|
|
965
|
+
// buildComposable,
|
|
966
|
+
// buildBridgeInstructions,
|
|
967
|
+
// queryBridge,
|
|
968
|
+
isDelegated,
|
|
969
|
+
unDelegate,
|
|
970
|
+
waitForTransactionReceipts,
|
|
971
|
+
// read,
|
|
972
|
+
toDelegation });
|
|
973
|
+
};
|
|
974
|
+
exports.createMultiChainNexusAccount = createMultiChainNexusAccount;
|