diff --git a/src/internal/provider.ts b/src/internal/provider.ts index d3f177e..31df5f1 100644 --- a/src/internal/provider.ts +++ b/src/internal/provider.ts @@ -178,17 +178,17 @@ export function from< }) })() + const sessions = getActiveSessionKeys(account.keys) + store.setState((x) => ({ ...x, accounts: [account] })) emitter.emit('connect', { chainId: Hex.fromNumber(state.chainId) }) + return [ { address: account.address, capabilities: { - sessions: account.keys.map((key) => ({ - expiry: Number(key.expiry), - id: PublicKey.toHex(key.publicKey), - })), + sessions, }, }, ] satisfies RpcSchema.ExtractReturnType< @@ -275,16 +275,7 @@ export function from< }) emitter.emit('message', { - data: [...account.keys, key] - .map((key) => - AccountDelegation.isActiveSessionKey(key) - ? { - expiry: Number(key.expiry), - id: PublicKey.toHex(key.publicKey), - } - : undefined, - ) - .filter(Boolean), + data: getActiveSessionKeys([...account.keys, key]), type: 'sessionsChanged', }) @@ -312,15 +303,7 @@ export function from< ) : state.accounts[0] - return account?.keys - .map((key) => { - if (!AccountDelegation.isActiveSessionKey(key)) return undefined - return { - expiry: Number(key.expiry), - id: PublicKey.toHex(key.publicKey), - } - }) - .filter(Boolean) + return getActiveSessionKeys(account?.keys ?? []) } case 'porto_ping': { @@ -507,16 +490,6 @@ export function announce(provider: Provider) { }) } -function requireParameter( - param: unknown, - details: string, -): asserts param is NonNullable { - if (typeof param === 'undefined') - throw new RpcResponse.InvalidParamsError({ - message: `Missing required parameter: ${details}`, - }) -} - function getActiveSessionKeyIndex(parameters: { account: AccountDelegation.Account id?: Hex.Hex | undefined @@ -530,3 +503,27 @@ function getActiveSessionKeyIndex(parameters: { if (index === -1) return 0 return index } + +function getActiveSessionKeys( + keys: readonly AccountDelegation.Key[], +): Schema.GrantSessionReturnType[] { + return keys + .map((key) => { + if (!AccountDelegation.isActiveSessionKey(key)) return undefined + return { + expiry: Number(key.expiry), + id: PublicKey.toHex(key.publicKey), + } + }) + .filter(Boolean) as never +} + +function requireParameter( + param: unknown, + details: string, +): asserts param is NonNullable { + if (typeof param === 'undefined') + throw new RpcResponse.InvalidParamsError({ + message: `Missing required parameter: ${details}`, + }) +}