From 330a22a4b27f0377aaf90dce273a35f328ebb7c7 Mon Sep 17 00:00:00 2001 From: Jaume Alavedra Date: Tue, 26 Nov 2024 10:58:54 +0100 Subject: [PATCH] fix: session key --- .../AccountRecovery.tsx | 10 ++-- .../auth-sample/src/components/Logout.tsx | 7 ++- .../SessionKey/BackendMintButton.tsx | 2 + .../SessionKey/EIP1193CreateSessionButton.tsx | 7 ++- .../auth-sample/src/pages/forgot-password.tsx | 4 +- examples/apps/auth-sample/src/pages/index.tsx | 1 - examples/apps/auth-sample/src/pages/login.tsx | 55 +++++++++++-------- .../apps/auth-sample/src/pages/register.tsx | 37 +++++++------ sdk/src/evm/evmProvider.ts | 2 +- sdk/src/evm/registerSession.ts | 47 ++++++++++++++-- 10 files changed, 115 insertions(+), 57 deletions(-) diff --git a/examples/apps/auth-sample/src/components/EmbeddedSignerRecovery/AccountRecovery.tsx b/examples/apps/auth-sample/src/components/EmbeddedSignerRecovery/AccountRecovery.tsx index 790f9cf7..d6e99297 100644 --- a/examples/apps/auth-sample/src/components/EmbeddedSignerRecovery/AccountRecovery.tsx +++ b/examples/apps/auth-sample/src/components/EmbeddedSignerRecovery/AccountRecovery.tsx @@ -1,6 +1,7 @@ import {useState} from 'react'; import {useOpenfort} from '../../hooks/useOpenfort'; import Loading from '../Loading'; +import { Button } from '../ui/button'; const AccountRecovery: React.FC = () => { const {handleRecovery} = useOpenfort(); @@ -21,7 +22,7 @@ const AccountRecovery: React.FC = () => {
- +
@@ -48,7 +49,8 @@ const AccountRecovery: React.FC = () => {
- +
diff --git a/examples/apps/auth-sample/src/components/Logout.tsx b/examples/apps/auth-sample/src/components/Logout.tsx index 1325dac2..04d5e913 100644 --- a/examples/apps/auth-sample/src/components/Logout.tsx +++ b/examples/apps/auth-sample/src/components/Logout.tsx @@ -1,6 +1,7 @@ import React from 'react'; import {useOpenfort} from '../hooks/useOpenfort'; import {useRouter} from 'next/router'; +import { Button } from './ui/button'; const LogoutButton: React.FC = () => { const {logout} = useOpenfort(); @@ -16,12 +17,12 @@ const LogoutButton: React.FC = () => { }; return ( - + ); }; diff --git a/examples/apps/auth-sample/src/components/SessionKey/BackendMintButton.tsx b/examples/apps/auth-sample/src/components/SessionKey/BackendMintButton.tsx index 67f60e47..1097dcb2 100644 --- a/examples/apps/auth-sample/src/components/SessionKey/BackendMintButton.tsx +++ b/examples/apps/auth-sample/src/components/SessionKey/BackendMintButton.tsx @@ -4,6 +4,7 @@ import openfort from '../../utils/openfortConfig'; import { Button } from '../ui/button'; import { privateKeyToAccount } from 'viem/accounts'; import { createWalletClient, http } from 'viem'; +import { polygonAmoy } from 'viem/chains'; const BackendMintButton: React.FC<{ handleSetMessage: (message: string) => void; @@ -30,6 +31,7 @@ const BackendMintButton: React.FC<{ const collectResponseJSON = await collectResponse.json(); const walletClient = createWalletClient({ account: privateKeyToAccount(sessionKey), + chain: polygonAmoy, transport: http() }) const signature = await walletClient.signMessage({message: {raw: collectResponseJSON.userOperationHash}}); diff --git a/examples/apps/auth-sample/src/components/SessionKey/EIP1193CreateSessionButton.tsx b/examples/apps/auth-sample/src/components/SessionKey/EIP1193CreateSessionButton.tsx index f186816e..5d66f854 100644 --- a/examples/apps/auth-sample/src/components/SessionKey/EIP1193CreateSessionButton.tsx +++ b/examples/apps/auth-sample/src/components/SessionKey/EIP1193CreateSessionButton.tsx @@ -34,7 +34,12 @@ const EIP1193CreateSessionButton: React.FC<{ }).extend(erc7715Actions()) const [account] = await walletClient.getAddresses() await walletClient.grantPermissions({ - account: accountSession, + signer:{ + type: "account", + data:{ + id: accountSession + } + }, expiry: 60 * 60 * 24, permissions: [ { diff --git a/examples/apps/auth-sample/src/pages/forgot-password.tsx b/examples/apps/auth-sample/src/pages/forgot-password.tsx index 10c43504..bda26266 100644 --- a/examples/apps/auth-sample/src/pages/forgot-password.tsx +++ b/examples/apps/auth-sample/src/pages/forgot-password.tsx @@ -5,7 +5,6 @@ import Link from 'next/link'; import {useRouter} from 'next/router'; import {AuthLayout} from '../components/Layouts/AuthLayout'; -import {Button} from '../components/Button'; import {TextField} from '../components/Fields'; import {StatusType, Toast} from '../components/Toasts'; @@ -13,6 +12,7 @@ import {StatusType, Toast} from '../components/Toasts'; import openfort from '../utils/openfortConfig'; import {getURL} from '../utils/getUrl'; import {AuthPlayerResponse} from '@openfort/openfort-js'; +import { Button } from '@/components/ui/button'; function ForgotPasswordPage() { const router = useRouter(); @@ -99,7 +99,7 @@ function ForgotPasswordPage() { required /> - diff --git a/examples/apps/auth-sample/src/pages/index.tsx b/examples/apps/auth-sample/src/pages/index.tsx index f0c08d25..735918b4 100644 --- a/examples/apps/auth-sample/src/pages/index.tsx +++ b/examples/apps/auth-sample/src/pages/index.tsx @@ -75,7 +75,6 @@ const HomePage: NextPage = () => { Welcome, {user?.player?.name ?? user?.id}!

- {' '}
diff --git a/examples/apps/auth-sample/src/pages/login.tsx b/examples/apps/auth-sample/src/pages/login.tsx index 11567c7a..75d4bdec 100644 --- a/examples/apps/auth-sample/src/pages/login.tsx +++ b/examples/apps/auth-sample/src/pages/login.tsx @@ -3,7 +3,6 @@ import Head from 'next/head'; import Link from 'next/link'; import {useRouter} from 'next/router'; import {AuthLayout} from '../components/Layouts/AuthLayout'; -import {Button, LinkButton} from '../components/Button'; import {TextField} from '../components/Fields'; import {StatusType, Toast} from '../components/Toasts'; import openfort from '../utils/openfortConfig'; @@ -11,8 +10,8 @@ import {getURL} from '../utils/getUrl'; import { AuthPlayerResponse, OAuthProvider, - ThirdPartyOAuthProvider, } from '@openfort/openfort-js'; +import { Button } from '@/components/ui/button'; function LoginPage() { const router = useRouter(); @@ -115,7 +114,7 @@ function LoginPage() { } >
-
+
- { + e.preventDefault(); + router.push('/forgot-password') + }} + className="text-blue-600" > Forgot password? - +
- @@ -162,7 +165,7 @@ function LoginPage() {
- +
- +
- +
diff --git a/examples/apps/auth-sample/src/pages/register.tsx b/examples/apps/auth-sample/src/pages/register.tsx index bf2a2144..ad43ce91 100644 --- a/examples/apps/auth-sample/src/pages/register.tsx +++ b/examples/apps/auth-sample/src/pages/register.tsx @@ -1,9 +1,7 @@ import Head from 'next/head'; import Link from 'next/link'; import {useEffect, useMemo, useState} from 'react'; - import {AuthLayout} from '../components/Layouts/AuthLayout'; -import {Button} from '../components/Button'; import {TextField} from '../components/Fields'; import openfort from '../utils/openfortConfig'; import {CheckCircleIcon} from '@heroicons/react/24/outline'; @@ -11,6 +9,7 @@ import {StatusType, Toast} from '../components/Toasts'; import {AuthPlayerResponse, OAuthProvider} from '@openfort/openfort-js'; import {useRouter} from 'next/router'; import {getURL} from '../utils/getUrl'; +import { Button } from '@/components/ui/button'; type ErrorType = string | null; @@ -160,7 +159,7 @@ function RegisterPage() {
) : (
-
+
- @@ -228,7 +227,7 @@ function RegisterPage() {
- +
- +
- +
- router.push('/connect-wallet')} + className='w-full' + variant='outline' >

Continue with wallet

-
+
diff --git a/sdk/src/evm/evmProvider.ts b/sdk/src/evm/evmProvider.ts index 9ce94449..f3f30830 100644 --- a/sdk/src/evm/evmProvider.ts +++ b/sdk/src/evm/evmProvider.ts @@ -249,7 +249,7 @@ export class EvmProvider implements Provider { [hexlify(chainId)]: { permissions: { supported: true, - signerTypes: ['account'], + signerTypes: ['account', 'key'], keyTypes: ['secp256k1'], permissionTypes: ['contract-calls'], }, diff --git a/sdk/src/evm/registerSession.ts b/sdk/src/evm/registerSession.ts index eb31eba8..d6f80a8a 100644 --- a/sdk/src/evm/registerSession.ts +++ b/sdk/src/evm/registerSession.ts @@ -4,18 +4,51 @@ import { Authentication } from 'configuration/authentication'; import { OneOf } from 'utils/helpers'; import { CreateSessionRequest } from '@openfort/openapi-clients/dist/backend'; import { JsonRpcError, RpcErrorCode } from './JsonRpcError'; -import { Signer } from '../signer/isigner'; +import { Signer as OpenfortSigner } from '../signer/isigner'; import { GrantPermissionsReturnType, SessionResponse } from '../types'; export type WalletRequestPermissionsParams = { params: GrantPermissionsParameters[]; - signer: Signer; + signer: OpenfortSigner; backendClient: BackendApiClients; account: Account; authentication: Authentication; policyId?: string; }; +/** @internal */ +export type AccountSigner = { + type: 'account' + data: { + id: `0x${string}` + } +}; + +/** @internal */ +export type KeySigner = { + type: 'key' + data: { + id: string + } +}; + +/** @internal */ +export type MultiKeySigner = { + type: 'keys' + data: { + ids: string[] + } +}; + +/** @internal */ +export type WalletSigner = { + type: 'wallet' +}; + +export type Signer = OneOf< +AccountSigner | KeySigner | MultiKeySigner | WalletSigner +>; + export type GrantPermissionsParameters = { /** Timestamp (in seconds) that specifies the time by which this session MUST expire. */ expiry: number; @@ -206,10 +239,16 @@ const buildOpenfortTransactions = async ( (p) => p.type === 'contract-call' || p.type === 'erc20-token-transfer', ).map((p) => (p.data as { address: `0x${string}` }).address); - const sessionAddress = typeof param.account === 'string' ? param.account : param.account?.address ?? undefined; + if (param.signer && param.signer.type === 'keys') { + throw new JsonRpcError( + RpcErrorCode.INVALID_PARAMS, + 'Failed to request permissions - missing session address', + ); + } + const sessionAddress = param.signer?.data?.id; if (!sessionAddress) { throw new JsonRpcError( - RpcErrorCode.RPC_SERVER_ERROR, + RpcErrorCode.INVALID_PARAMS, 'Failed to request permissions - missing session address', ); }