Skip to content

Commit

Permalink
chore: sl wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Jul 4, 2024
1 parent e1150d6 commit 977e35e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 191 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VITE_PAYMASTER_KEY=
VITE_BUNDLER_URL=https://bundler.biconomy.io/api/v2/80002/cJPK7B3ru.dd7f7861-190d-45ic-af80-6877f74b8f44
VITE_EPHEMERAL_KEY=
VITE_SILENCE_LABS_NPM_TOKEN=npm_XXX
1 change: 1 addition & 0 deletions .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
CI: false
NODE_OPTIONS: --max_old_space_size=4096
REACT_APP_PKEY: ${{ secrets.REACT_APP_PKEY }}
VITE_SILENCE_LABS_NPM_TOKEN: ${{ secrets.VITE_SILENCE_LABS_NPM_TOKEN }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
CI: false
NODE_OPTIONS: --max_old_space_size=4096
REACT_APP_PKEY: ${{ secrets.REACT_APP_PKEY }}
VITE_SILENCE_LABS_NPM_TOKEN: ${{ secrets.VITE_SILENCE_LABS_NPM_TOKEN }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@mui/styles": "^5.11.12",
"@noble/ed25519": "^2.1.0",
"@rainbow-me/rainbowkit": "^2.0.0",
"@silencelaboratories/walletprovider-sdk": "^0.0.8",
"@tanstack/react-query": "^5.22.2",
"ethers": "^6.12.1",
"react": "^18.2.0",
Expand Down
173 changes: 45 additions & 128 deletions src/components/Modules/CreateDanSession.tsx
Original file line number Diff line number Diff line change
@@ -1,159 +1,76 @@
import {
DEFAULT_SESSION_KEY_MANAGER_MODULE,
type DanModuleInfo,
PaymasterMode,
type PolicyWithoutSessionKey,
type Session,
SessionLocalStorage,
type Transaction,
createDANSessionKeyManagerModule,
createERC20SessionDatum,
getDANSessionKey
createDistributedSession
} from "@biconomy/account"
import { bigIntReplacer, useSmartAccount } from "@biconomy/use-aa"
import { makeStyles } from "@mui/styles"
import type React from "react"
import { useState } from "react"
import { ToastContainer } from "react-toastify"
import "react-toastify/dist/ReactToastify.css"
import { type Hex, encodeAbiParameters, parseUnits } from "viem"
import { useAccount } from "wagmi"
import type { Hex } from "viem"
import Button from "../Button"
import UseDanSession from "./UseDanSession"

import { configInfo } from "../../utils"

const CreateDanSession: React.FC = () => {
const classes = useStyles()
const token = configInfo.usdc.address as Hex
const amount = parseUnits("50".toString(), 6)

const { address: eoa } = useAccount()
const nftAddress: Hex = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e"
const { smartAccountAddress, smartAccountClient } = useSmartAccount()
const [session, setSession] = useState<Session | null>(null)
const [danModuleInfo, setDanModuleInfo] = useState<
undefined | DanModuleInfo
>()

const policy: any[] = [
{ interval: { validAfter: 0, validUntil: 0 } },
"{SESSION_KEY}",
token,
eoa!,
parseUnits("50".toString(), 6)
const policy: PolicyWithoutSessionKey[] = [
{
contractAddress: nftAddress,
functionSelector: "safeMint(address)",
rules: [
{
offset: 0,
condition: 0,
referenceValue: smartAccountAddress
}
],
interval: {
validUntil: 0,
validAfter: 0
},
valueLimit: 0n
}
]

const createDanSessionHandler = async () => {
try {
if (!smartAccountClient || !smartAccountAddress) {
throw new Error("Smart Account not found")
}
console.log(
"use",
smartAccountClient,
"address: ",
smartAccountAddress,
"to create the session"
)

const {
sessionKeyEOA,
mpcKeyId,
ephSK,
partiesNumber,
threshold,
eoaAddress
} = await getDANSessionKey(smartAccountClient)

setDanModuleInfo({
mpcKeyId,
ephSK,
partiesNumber,
threshold,
eoaAddress,
chainId: 80002
})

const sessionStorageClient = new SessionLocalStorage(smartAccountAddress)

const sessionsModule = await createDANSessionKeyManagerModule({
smartAccountAddress,
sessionStorageClient
})

console.log("session module")
console.log(sessionsModule)

const createSessionDataParams = createERC20SessionDatum({
interval: { validAfter: 0, validUntil: 0 },
sessionKeyAddress: sessionKeyEOA,
sessionKeyData: encodeAbiParameters(
[
{ type: "address" },
{ type: "address" },
{ type: "address" },
{ type: "uint256" }
const policy: PolicyWithoutSessionKey[] = [
{
contractAddress: nftAddress,
functionSelector: "safeMint(address)",
rules: [
{
offset: 0,
condition: 0,
referenceValue: smartAccountAddress
}
],
[sessionKeyEOA, token, eoa!, amount]
)
})

const { data: policyData, sessionIDInfo } =
await sessionsModule.createSessionData([createSessionDataParams])

console.log("sessionKeyEOA a", sessionKeyEOA, {
matchedLeaf: await sessionStorageClient.getSessionData({
sessionID: sessionIDInfo[0]
})
})

const permitTx = {
to: DEFAULT_SESSION_KEY_MANAGER_MODULE,
data: policyData
}

const txs: Transaction[] = []

const isDeployed = await smartAccountClient.isAccountDeployed()
const enableSessionTx = await smartAccountClient.getEnableModuleData(
DEFAULT_SESSION_KEY_MANAGER_MODULE
)

if (isDeployed) {
const enabled = await smartAccountClient.isModuleEnabled(
DEFAULT_SESSION_KEY_MANAGER_MODULE
)
if (!enabled) {
txs.push(enableSessionTx)
}
} else {
txs.push(enableSessionTx)
}

txs.push(permitTx)

const userOpResponse = await smartAccountClient.sendTransaction(txs, {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
nonceOptions: {
nonceKey: Date.now()
interval: {
validUntil: 0,
validAfter: 0
},
valueLimit: 0n
}
})

console.log("userOpResponse", userOpResponse)

const {
receipt: { transactionHash },
success
} = await userOpResponse.wait()
]

console.log("transactionHash", transactionHash)
const { wait, session } = await createDistributedSession(
smartAccountClient,
policy
)

const resultingSession = {
sessionStorageClient,
sessionIDInfo
}
const { success } = await wait()

// Handle Success. Keep the "Session" (StorageClient and sessionIDs) and set it to the session
success && setSession(resultingSession)
success && setSession(session)
} catch (error) {
console.error("Error creating session:", error)
}
Expand Down Expand Up @@ -185,8 +102,8 @@ const CreateDanSession: React.FC = () => {

<pre>policy: {JSON.stringify(policy, bigIntReplacer, 2)}</pre>

{!!session && danModuleInfo ? (
<UseDanSession session={session} danModuleInfo={danModuleInfo!} />
{!!session ? (
<UseDanSession session={session} />
) : (
<Button title="Create Session" onClickFunc={createDanSessionHandler} />
)}
Expand Down
95 changes: 34 additions & 61 deletions src/components/Modules/UseDanSession.tsx
Original file line number Diff line number Diff line change
@@ -1,93 +1,66 @@
import {
type DanModuleInfo,
PaymasterMode,
type Session,
createDANSessionKeyManagerModule
type Transaction,
createDANSessionKeyManagerModule,
createSessionSmartAccountClient,
getChain
} from "@biconomy/account"
import { useSmartAccount } from "@biconomy/use-aa"
import type React from "react"
import "react-toastify/dist/ReactToastify.css"
import { type Hex, encodeFunctionData, parseAbi, parseUnits } from "viem"
import { configInfo } from "../../utils"
import { type Hex, encodeFunctionData, parseAbi } from "viem"
import { biconomyPaymasterApiKey, bundlerUrl } from "../../utils/chainConfig"
import Button from "../Button"

const withSponsorship = {
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
}
interface props {
session: Session
danModuleInfo: DanModuleInfo
}

const UseDanSession: React.FC<props> = ({
session,
danModuleInfo: {
ephSK,
mpcKeyId,
threshold,
partiesNumber,
chainId,
eoaAddress
}
}) => {
const token = configInfo.usdc.address as Hex
const amount = parseUnits("1".toString(), 6)

const sessionID = session.sessionIDInfo[0]
console.log({ session, sessionID, eoaAddress })
const UseDanSession: React.FC<props> = ({ session }) => {
const nftAddress: Hex = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e"
const chain = getChain(80002)

const { smartAccountAddress, smartAccountClient } = useSmartAccount()
const { smartAccountAddress } = useSmartAccount()

const transactions = {
to: token,
const nftMintTx: Transaction = {
to: nftAddress,
data: encodeFunctionData({
abi: parseAbi(["function transfer(address _to, uint256 _value)"]),
functionName: "transfer",
args: [eoaAddress as Hex, amount]
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [smartAccountAddress]
})
}

const useDanSessionHandler = async () => {
if (!smartAccountClient || !smartAccountAddress) {
if (!smartAccountAddress) {
throw new Error("Smart Account not found")
}
if (!session) {
throw new Error("Session not found")
}

const sessionStorageClient = session.sessionStorageClient

const matchedLeaf = await session.sessionStorageClient.getSessionData({
sessionID
})

console.log("sessionKeyEoa b", matchedLeaf.sessionPublicKey, {
matchedLeaf
})

smartAccountClient.setActiveValidationModule(
await createDANSessionKeyManagerModule({
smartAccountAddress,
sessionStorageClient
})
const smartAccountWithSession = await createSessionSmartAccountClient(
{
accountAddress: smartAccountAddress, // Set the account address on behalf of the user
biconomyPaymasterApiKey,
bundlerUrl,
chainId: chain.id
},
session,
"DAN"
)

// Send the transactions using session params
const { wait } = await smartAccountClient.sendTransaction(transactions, {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
nonceOptions: {
nonceKey: Date.now()
},
params: {
sessionID,
danModuleInfo: {
eoaAddress,
ephSK,
threshold,
partiesNumber,
chainId,
mpcKeyId
}
}
})
// Wait for the createSessionTx
const { wait } = await smartAccountWithSession.sendSessionTransaction(
[session, chain, null],
nftMintTx,
withSponsorship
)

const {
receipt: { transactionHash },
success
Expand Down
2 changes: 1 addition & 1 deletion src/components/Modules/UseSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface props {
sessionID: string
}

const UseSession: React.FC<props> = ({ smartAccountAddress, sessionID }) => {
const UseSession: React.FC<props> = ({ smartAccountAddress }) => {
const {
mutate,
data: userOpResponse,
Expand Down

0 comments on commit 977e35e

Please sign in to comment.