-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
158 lines (121 loc) · 5.49 KB
/
index.ts
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
import { BigNumber, Signer, providers, utils } from "ethers"
import { ChainId, Transaction } from "@biconomy-devx/core-types"
import { CreateSessionDataParams, DEFAULT_BATCHED_SESSION_ROUTER_MODULE, MultiChainUserOpDto, SessionKeyManagerModule } from "@biconomy-devx/modules"
import { MultiChainValidationModule } from "@biconomy-devx/modules/dist/src/MultichainValidationModule"
import { BiconomySmartAccountV2, BiconomySmartAccountV2Config, DEFAULT_ENTRYPOINT_ADDRESS, VoidSigner } from "@biconomy-devx/account"
import fs from 'fs'
import { LocalStorage } from "node-localstorage";
import { hexConcat, hexZeroPad, keccak256, parseUnits } from "ethers/lib/utils"
import { getUserOpHash } from "@biconomy-devx/common"
import MerkleTree from "merkletreejs"
global.localStorage = new LocalStorage('./scratch');
const BATCHED_ROUTER = DEFAULT_BATCHED_SESSION_ROUTER_MODULE
export const PVG: Record<number, number> = {
[ChainId.ARBITRUM_ONE_MAINNET]: 5_000_000,
[ChainId.OPTIMISM_MAINNET]: 40_000_000
}
async function deployData(
leaves: CreateSessionDataParams[],
sessionkeyManager: SessionKeyManagerModule,
multichainModule: MultiChainValidationModule,
signer: Signer,
paymasterAndData: Record<number, string>,
accounts: Record<number, { account: BiconomySmartAccountV2; chainId: number }>,
gasPrices: Record<number, { max: BigNumber, priority: BigNumber }>,
gasLimits: Record<number, { callGasLimit: number, verificationGasLimit: number }>,
) {
const userOps: MultiChainUserOpDto[] = []
const params = {
sessionValidationModule: multichainModule.getAddress(),
sessionSigner: signer
}
for (const acc of Object.values(accounts)) {
const { account } = acc
account.setActiveValidationModule(multichainModule)
const txs: Transaction[] = []
txs.push(await account.getEnableModuleData(BATCHED_ROUTER))
txs.push(await account.getEnableModuleData(sessionkeyManager.getAddress()))
const setMerkleRootData = await sessionkeyManager.createSessionData(leaves)
txs.push({ data: setMerkleRootData.data, to: sessionkeyManager.getAddress() })
let partialUserOp = await account.buildUserOp(txs, {
params,
overrides: {
preVerificationGas: PVG[acc.chainId],
maxFeePerGas: gasPrices[acc.chainId].max,
maxPriorityFeePerGas: gasPrices[acc.chainId].priority,
callGasLimit: gasLimits[acc.chainId].callGasLimit,
verificationGasLimit: gasLimits[acc.chainId].verificationGasLimit,
paymasterData: paymasterAndData[acc.chainId]
},
skipBundlerGasEstimation: true
})
userOps.push({ userOp: partialUserOp, chainId: acc.chainId })
const leaves_ = [];
for (const multiChainOp of userOps) {
const leaf = hexConcat([
hexZeroPad(utils.hexlify((leaves[0]).validUntil), 6),
hexZeroPad(utils.hexlify((leaves[0]).validAfter), 6),
hexZeroPad(getUserOpHash(multiChainOp.userOp, DEFAULT_ENTRYPOINT_ADDRESS, multiChainOp.chainId), 32),
]);
leaves_.push(keccak256(leaf))
}
const merkleTree = new MerkleTree(leaves_, keccak256, { sortPairs: true });
return merkleTree.getHexRoot()
}
}
async function main() {
const contents = JSON.parse(fs.readFileSync('data.txt').toString().slice(1, -2))
const signer = new VoidSigner(utils.getAddress(process.argv[2]))
const multiChainModule = await MultiChainValidationModule.create({
version: 'V1_0_0',
signer: signer
})
const arbConfig: BiconomySmartAccountV2Config = {
chainId: ChainId.ARBITRUM_ONE_MAINNET,
index: 0,
activeValidationModule: multiChainModule,
defaultValidationModule: multiChainModule,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS
}
const optConfig: BiconomySmartAccountV2Config = {
chainId: ChainId.OPTIMISM_MAINNET,
index: 0,
activeValidationModule: multiChainModule,
defaultValidationModule: multiChainModule,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS
}
const swArb = await BiconomySmartAccountV2.create(arbConfig)
const swOpt = await BiconomySmartAccountV2.create(optConfig)
const sessionKeyManager = await SessionKeyManagerModule.create({
smartAccountAddress: await swArb.getAccountAddress()
})
const gasPrices: Record<number, { max: BigNumber, priority: BigNumber }> = {}
const accounts: Record<number, { account: BiconomySmartAccountV2; chainId: number }> = {}
const gasLimits: Record<number, { callGasLimit: number, verificationGasLimit: number }> = {}
const paymasterAndData: Record<number, string> = {}
accounts[42161] = { account: swArb, chainId: 42161 }
accounts[10] = { account: swOpt, chainId: 10 }
//// REPLACE BELOW VALUES WITH ORIGINAL VALUES ///
gasPrices[42161] = {
max: parseUnits('0.1', 9), // maxFeePerGas
priority: BigNumber.from(0) // maxPriorityFeePerGas
}
gasPrices[10] = {
max: parseUnits('0.1', 9), // maxFeePerGas
priority: parseUnits('0.1', 9) // maxPriorityFeePerGas
}
gasLimits[42161] = {
callGasLimit: 1_000_000, // callGasLimit
verificationGasLimit: 200_000 // verificationGasLimit
}
gasLimits[10] = {
callGasLimit: 1_000_000, // callGasLimit
verificationGasLimit: 200_000 // verificationGasLimit
}
paymasterAndData[42161] = '0x' // paymasterAndData
paymasterAndData[19] = '0x' // paymasterAndData
//// REPLACE ABOVE VALUES WITH ORIGINAL VALUES ///
const signData = await deployData(contents.leafNodes, sessionKeyManager, multiChainModule, signer, paymasterAndData, accounts, gasPrices, gasLimits)
console.log('sign payload', signData)
}
main().catch(e => console.error(e))