Skip to content

Commit

Permalink
chore(account): update ZeroDev
Browse files Browse the repository at this point in the history
  • Loading branch information
szkl committed Jan 30, 2024
1 parent 5d80b01 commit 8a12678
Show file tree
Hide file tree
Showing 90 changed files with 157 additions and 918 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/plur-npm-1.0.0-1fcc5c988f-bd8725178b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/resolve-patch-1169b4314d-064d09c180.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 5 additions & 2 deletions platform/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ethersproject/wallet": "5.7.0",
"@proofzero/platform-clients": "workspace:*",
"@proofzero/platform.core": "workspace:*",
"@proofzero/types": "workspace:*",
"@proofzero/urns": "workspace:*",
"@proofzero/utils": "workspace:*",
"@trpc/server": "10.8.1",
"@types/node": "18.15.3",
"@zerodevapp/sdk": "3.1.57",
"@zerodev/ecdsa-validator": "5.0.2",
"@zerodev/presets": "5.1.1",
"@zerodev/sdk": "5.0.4",
"@zerodev/session-key": "5.0.1",
"do-proxy": "1.3.3",
"jose": "4.11.0",
"permissionless": "0.0.33",
"random-words": "2.0.0",
"remix-auth-google": "1.2.0",
"viem": "1.20.0",
Expand Down
2 changes: 2 additions & 0 deletions platform/account/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export const NO_OP_ACCOUNT_PLACEHOLDER = AccountURNSpace.componentizedUrn(
{ addr_type: EmailAccountType.Email, node_type: NodeType.Email },
{ alias: 'no-op-account-placeholder' }
)

export const ZERODEV_SESSION_KEY_TTL = 90 * 24 * 60 * 60 * 10000 // 90 days
20 changes: 11 additions & 9 deletions platform/account/src/jsonrpc/methods/initSmartContractWallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { z } from 'zod'
import { Wallet } from '@ethersproject/wallet'
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { createEcdsaKernelAccountClient } from '@zerodev/presets/zerodev'

import { router } from '@proofzero/platform.core'
import { AccountURNInput } from '@proofzero/platform-middleware/inputValidators'
Expand All @@ -8,7 +10,6 @@ import { Context } from '../../context'
import { CryptoAccountType, NodeType } from '@proofzero/types/account'
import { initAccountNodeByName } from '../../nodes'
import createImageClient from '@proofzero/platform-clients/image'
import { getZeroDevSigner } from '@zerodevapp/sdk'
import { EDGE_ACCOUNT } from '@proofzero/platform.account/src/constants'

import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
Expand Down Expand Up @@ -37,21 +38,22 @@ export const initSmartContractWalletMethod = async ({
ctx: Context
}): Promise<InitSmartContractWalletResult> => {
const nodeClient = ctx.account
const identity = await nodeClient?.class.getIdentity()

const identity = await nodeClient?.class.getIdentity()
if (!identity) {
throw new Error('missing identity')
}

const owner = Wallet.createRandom()
const privateKey = generatePrivateKey()
const signer = privateKeyToAccount(privateKey)

const smartContractWallet = await getZeroDevSigner({
skipFetchSetup: true,
const kernelClient = await createEcdsaKernelAccountClient({
chain: mainnet,
projectId: ctx.env.SECRET_ZERODEV_PROJECTID,
owner,
signer,
})

const smartContractWalletAddress = await smartContractWallet.getAddress()
const smartContractWalletAddress = kernelClient.account.address

const { accountURN, baseAccountURN } = generateSmartWalletAccountUrn(
smartContractWalletAddress,
Expand All @@ -70,7 +72,7 @@ export const initSmartContractWalletMethod = async ({
gradientSeed: smartContractWalletAddress,
})
await Promise.all([
smartContractWalletNode.storage.put('privateKey', owner.privateKey),
smartContractWalletNode.storage.put('privateKey', privateKey),
smartContractWalletNode.class.setAddress(smartContractWalletAddress),
smartContractWalletNode.class.setNickname(input.nickname),
smartContractWalletNode.class.setNodeType(NodeType.Crypto),
Expand Down
69 changes: 40 additions & 29 deletions platform/account/src/jsonrpc/methods/registerWalletSessionKey.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { z } from 'zod'
import { type Hex, createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { addressToEmptyAccount, createKernelAccount } from '@zerodev/sdk'
import { signerToEcdsaValidator } from '@zerodev/ecdsa-validator'
import {
serializeSessionKeyAccount,
signerToSessionKeyValidator,
} from '@zerodev/session-key'

import { Context } from '../../context'
import { initAccountNodeByName } from '../../nodes'
import {
createSessionKey,
getZeroDevSigner,
getPrivateKeyOwner,
} from '@zerodevapp/sdk'

import { PaymasterSchema } from '@proofzero/platform/starbase/src/jsonrpc/validators/app'
import { BadRequestError, InternalServerError } from '@proofzero/errors'
import { ZERODEV_SESSION_KEY_TTL } from '../../constants'
import { generateSmartWalletAccountUrn } from '../../utils'

export const RegisterSessionKeyInput = z.object({
sessionPublicKey: z.string(),
sessionKeyAddress: z.custom<`0x${string}`>(
(val: unknown) => typeof val === 'string' && val.startsWith('0x')
),
smartContractWalletAddress: z.string(),
paymaster: PaymasterSchema,
})
Expand All @@ -34,9 +41,9 @@ export const registerSessionKeyMethod = async ({
// This method is being called only from galaxy
// All authorization checks are done in galaxy

const { paymaster, smartContractWalletAddress, sessionPublicKey } = input
const { paymaster, smartContractWalletAddress, sessionKeyAddress } = input

const { baseAccountURN } = await generateSmartWalletAccountUrn(
const { baseAccountURN } = generateSmartWalletAccountUrn(
smartContractWalletAddress,
'' // empty string because we only care about base urn
)
Expand All @@ -46,38 +53,42 @@ export const registerSessionKeyMethod = async ({
ctx.env.Account
)

const ownerPrivateKey = (await smartContractWalletNode.storage.get(
const ownerPrivateKey = await smartContractWalletNode.storage.get<Hex>(
'privateKey'
)) as string
)

if (!ownerPrivateKey) {
throw new BadRequestError({ message: 'missing private key for the user' })
}

let sessionKey = ''

if (paymaster && paymaster.provider === 'zerodev') {
const zdSigner = await getZeroDevSigner({
projectId: paymaster.secret,
owner: getPrivateKeyOwner(ownerPrivateKey),
skipFetchSetup: true,
})

// We need a 90 days in Unix time from now
// 90 days * 24 hours * 60 minutes * 60 seconds * 1000 milliseconds
const truncatedValidUntil = Math.floor(Date.now() / 1000) + 7776000000

try {
sessionKey = await createSessionKey(
zdSigner,
[],
truncatedValidUntil,
sessionPublicKey
)
const signer = privateKeyToAccount(ownerPrivateKey)

const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})

const emptySessionKeySigner = addressToEmptyAccount(sessionKeyAddress)

const sessionKeyAccount = await createKernelAccount(publicClient, {
plugins: {
defaultValidator: await signerToEcdsaValidator(publicClient, {
signer,
}),
validator: await signerToSessionKeyValidator(publicClient, {
signer: emptySessionKeySigner,
validatorData: { validUntil: Date.now() + ZERODEV_SESSION_KEY_TTL },
}),
},
})

return serializeSessionKeyAccount(sessionKeyAccount)
} catch (e) {
throw new InternalServerError({ message: 'Failed to create session key' })
}
}

return sessionKey
return ''
}
Loading

0 comments on commit 8a12678

Please sign in to comment.