Skip to content

Instantly share code, notes, and snippets.

@poolsar42
Created June 14, 2023 16:44
Show Gist options
  • Save poolsar42/ea3f671fccda9f51c6edbb16146d23b1 to your computer and use it in GitHub Desktop.
Save poolsar42/ea3f671fccda9f51c6edbb16146d23b1 to your computer and use it in GitHub Desktop.
import { Context } from '../../context'
import {
  createSessionKey,
  createSessionKeySigner,
  getPrivateKeyOwner,
  getZeroDevSigner,
} from '@zerodevapp/sdk'

import { Wallet } from '@ethersproject/wallet'
import { Contract } from '@ethersproject/contracts'
import { arrayify } from '@ethersproject/bytes'
import { JsonRpcProvider } from '@ethersproject/providers'
import { appRouter } from '../router'
import { initAddressNodeByName } from '../../nodes'
import { AddressURNInput } from '@proofzero/platform-middleware/inputValidators'
import { z } from 'zod'
import { AddressURN } from '@proofzero/urns/address'

export const TestWalletSessionKeyInput = z.object({
  sessionKey: z.string(),
  publicSessionKey: z.string(),
})

type TestWalletSessionKeyParams = z.infer<typeof TestWalletSessionKeyInput>

const requestInit = {
  method: 'post',
  headers: {
    'content-type': 'application/json;charset=UTF-8',
  },
}

export const testSessionKeyMethod = async ({
  input,
  ctx,
}: {
  input: TestWalletSessionKeyParams
  ctx: Context
}): Promise<void> => {
  const projectId = %PROJECT ID%

  const privateSigner = new Wallet(
    %DEV'S PRIVATE KEY%
  )

  console.log({ devAddress: await privateSigner.getAddress() })

  const smartContractWalletNode = initAddressNodeByName(
    ctx.addressURN as AddressURN,
    ctx.Address
  )
  const owner = (await smartContractWalletNode.storage.get(
    'privateKey'
  )) as string
  const signer = new Wallet(owner)

  const address = await signer.getAddress()

  const sessionKeySigner = await createSessionKeySigner({
    projectId,
    sessionKeyData: input.sessionKey,
    skipFetchSetup: true,
    rpcProvider: new JsonRpcProvider({
      url: ctx.MUMBAI_PROVIDER_URL,
      skipFetchSetup: true,
    }),
    privateSigner,
  })

  const contractAddress = '0x34bE7f35132E97915633BC1fc020364EA5134863'
  const contractABI = [
    'function mint(address _to) public',
    'function balanceOf(address owner) external view returns (uint256 balance)',
  ]
  const nftContract = new Contract(
    contractAddress,
    contractABI,
    sessionKeySigner
  )

  try {
    const receipt = await sessionKeySigner.sendTransaction({
      to: contractAddress,
      data: nftContract.interface.encodeFunctionData('mint', [address]),
    })
    await receipt.wait()
    console.log({ done: 'SUCCESSFULLY MINTED USING SESSION KEY' })
    console.log({ hash: receipt.hash })
  } catch (e) {
    console.log({ SUCCESSFULLY: 'REVOKED SESSION KEY' })
    console.log('MINT', e)
  }
}

I used it in Address worker - so if you'd like to do the same - don't forget to add to to the router

curl http://localhost:10102/trpc/testSessionKey -H 'rollup-address-urn: %SC WALLET ADDRESS URN%' \ -H 'content-type: application/json' \ -X POST \ -d '{"sessionKey": "%SESSION KEY FROM GALAXY%", "publicSessionKey": "%DEV'S PUBLIC KEY%"}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment