-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.ts
77 lines (67 loc) · 2.41 KB
/
session.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
import { createKernelAccount, addressToEmptyAccount } from "@zerodev/sdk"
import { KERNEL_V3_1 } from "@zerodev/sdk/constants"
import { toMultiChainECDSAValidator } from "@zerodev/multi-chain-validator"
import { http, createPublicClient } from "viem"
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { mode } from "viem/chains"
import { ENTRYPOINT_ADDRESS_V07 } from "permissionless"
import { toECDSASigner } from '@zerodev/permissions/signers'
import { toPermissionValidator, serializePermissionAccount, deserializePermissionAccount } from '@zerodev/permissions'
import dotenv from 'dotenv'
dotenv.config()
const PROJECT_ID = process.env.ZERODEV_MODE_ID;
const BUNDLER_RPC = `https://rpc.zerodev.app/api/v2/bundler/${PROJECT_ID}?provider=ZERODEV`
const chain = mode
const entryPoint = ENTRYPOINT_ADDRESS_V07
const kernelVersion = KERNEL_V3_1
const useMetaFactory = false
const main = async () => {
// Construct a signer
const privateKey = generatePrivateKey()
const signer = privateKeyToAccount(privateKey)
// Construct a public client
const publicClient = createPublicClient({
chain,
transport: http(BUNDLER_RPC),
})
// Construct a validator
const ecdsaValidator = await toMultiChainECDSAValidator(publicClient, {
signer,
entryPoint,
kernelVersion
})
// Create session key
const sessionPrivateKey = generatePrivateKey()
const sessionKeySigner = await toECDSASigner({
signer: privateKeyToAccount(sessionPrivateKey),
})
const sessionKeyAddress = sessionKeySigner.account.address
const emptyAccount = addressToEmptyAccount(sessionKeyAddress)
const emptySessionKeySigner = await toECDSASigner({ signer: emptyAccount })
const permissionPlugin = await toPermissionValidator(publicClient, {
entryPoint,
kernelVersion,
signer: emptySessionKeySigner,
policies: [],
})
// Create session account
const sessionKeyAccount = await createKernelAccount(publicClient, {
entryPoint,
kernelVersion,
plugins: {
sudo: ecdsaValidator,
regular: permissionPlugin,
},
useMetaFactory: useMetaFactory
})
const approval = await serializePermissionAccount(sessionKeyAccount)
const deserializedSessionKeyAccount = await deserializePermissionAccount(
publicClient,
entryPoint,
KERNEL_V3_1,
approval,
sessionKeySigner
);
console.log('deserializedSessionKeyAccount', deserializedSessionKeyAccount)
}
main()