Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore address/credentialId from storage #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Porto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export function create(config: Config | undefined = {}): Porto {
(_, get) => ({
accounts: [],
chain: chains[0],
address: undefined,
credentialId: undefined,

// computed
get chainId() {
Expand All @@ -87,6 +89,8 @@ export function create(config: Config | undefined = {}): Porto {
merge,
partialize(state) {
return {
address: state.address,
credentialId: state.credentialId,
accounts: state.accounts.map((account) => ({
...account,
keys: account.keys.map((key) => ({
Expand Down Expand Up @@ -182,6 +186,8 @@ export type State<
> = {
accounts: AccountDelegation.Account[]
chain: chains[number]
address: Address.Address | undefined
credentialId: string | undefined
readonly chainId: chains[number]['id']
readonly client: Client<
Transport,
Expand Down
16 changes: 13 additions & 3 deletions src/internal/accountDelegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,21 @@ export async function create<chain extends Chain | undefined>(
privateKey,
})

return initialize(client, {
const { account } = await initialize(client, {
...result,
authorization,
signature,
})

const credentialId = result.account.keys.find(
(key) => key.type === 'webauthn',
)?.id as string

return {
account,
address,
credentialId,
}
}

export declare namespace create {
Expand Down Expand Up @@ -397,7 +407,6 @@ export async function load<chain extends Chain | undefined>(
parameters: load.Parameters = {},
) {
const { authorizeKeys = [], rpId } = parameters

let address: Address.Address
let raw: PublicKeyCredential
let credentialId: string
Expand Down Expand Up @@ -426,7 +435,6 @@ export async function load<chain extends Chain | undefined>(
address,
keys: authorizeKeys ?? [],
})

const { signature, metadata, ...rest } = await WebAuthnP256.sign({
challenge: payload,
credentialId,
Expand Down Expand Up @@ -511,6 +519,8 @@ export async function load<chain extends Chain | undefined>(
label,
keys,
},
address,
credentialId,
}
}

Expand Down
45 changes: 33 additions & 12 deletions src/internal/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ export function from<
case 'eth_requestAccounts': {
if (!headless) throw new ox_Provider.UnsupportedMethodError()

const { account } = await AccountDelegation.load(state.client, {
rpId: keystoreHost,
})
const { account, address, credentialId } =
await AccountDelegation.load(state.client, {
rpId: keystoreHost,
address: state.address,
credentialId: state.credentialId,
})

store.setState((x) => ({ ...x, accounts: [account] }))
store.setState((x) => ({
...x,
address,
credentialId,
accounts: [account],
}))

emitter.emit('connect', { chainId: Hex.fromNumber(state.chainId) })
return [account.address] satisfies RpcSchema.ExtractReturnType<
Expand Down Expand Up @@ -163,7 +171,7 @@ export function from<
})
: undefined

const { account } = await (async () => {
const { account, address, credentialId } = await (async () => {
if (createAccount) {
const { label } =
typeof createAccount === 'object' ? createAccount : {}
Expand All @@ -177,12 +185,19 @@ export function from<
return await AccountDelegation.load(state.client, {
authorizeKeys: key ? [key] : undefined,
rpId: keystoreHost,
address: state.address,
credentialId: state.credentialId,
})
})()

const sessions = getActiveSessionKeys(account.keys)

store.setState((x) => ({ ...x, accounts: [account] }))
store.setState((x) => ({
...x,
address,
credentialId,
accounts: [account],
}))

emitter.emit('connect', { chainId: Hex.fromNumber(state.chainId) })

Expand All @@ -208,13 +223,19 @@ export function from<
>) ?? [{}]

// TODO: wait for tx to be included/make counterfactual?
const { account } = await AccountDelegation.create(state.client, {
delegation: state.delegation,
label,
rpId: keystoreHost,
})
const { account, address, credentialId } =
await AccountDelegation.create(state.client, {
delegation: state.delegation,
label,
rpId: keystoreHost,
})

store.setState((x) => ({ ...x, accounts: [account] }))
store.setState((x) => ({
...x,
address,
credentialId,
accounts: [account],
}))

emitter.emit('connect', { chainId: Hex.fromNumber(state.chainId) })
return account.address satisfies RpcSchema.ExtractReturnType<
Expand Down