Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 21, 2024
1 parent 616588c commit 45a9a61
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/internal/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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',
})

Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -507,16 +490,6 @@ export function announce(provider: Provider) {
})
}

function requireParameter(
param: unknown,
details: string,
): asserts param is NonNullable<typeof param> {
if (typeof param === 'undefined')
throw new RpcResponse.InvalidParamsError({
message: `Missing required parameter: ${details}`,
})
}

function getActiveSessionKeyIndex(parameters: {
account: AccountDelegation.Account
id?: Hex.Hex | undefined
Expand All @@ -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<typeof param> {
if (typeof param === 'undefined')
throw new RpcResponse.InvalidParamsError({
message: `Missing required parameter: ${details}`,
})
}

0 comments on commit 45a9a61

Please sign in to comment.