Skip to content

Commit

Permalink
fix auth binding
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Feb 20, 2025
1 parent 2b6ddd1 commit 0c657ff
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 29 deletions.
50 changes: 50 additions & 0 deletions packages/fcl-ethereum-provider/src/util/fcl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {CurrentUser, Service} from "@onflow/typedefs"
import * as fcl from "@onflow/fcl"

export function scopedFclUser(
user: typeof fcl.currentUser,
service: Service
): ReturnType<typeof fcl.currentUser> {
function scoped(state: CurrentUser): CurrentUser {
const authnService = state?.services.find(
service => service.type === "authn"
)
const isMatchingService =
authnService?.uid && authnService?.uid === service.uid

return isMatchingService
? state
: {
f_vsn: "1.0.0",
f_type: "CurrentUser",
loggedIn: false,
services: [],
addr: undefined,
cid: undefined,
}
}

return {
async authenticate() {
return await user.authenticate({service})
},
unauthenticate() {
return user.unauthenticate()
},
subscribe(callback: Function) {
return user.subscribe((state: CurrentUser) => {
const scopedState = scoped(state)
if (scopedState) {
callback(scopedState)
}
})
},
// TODO: should these check whether the scope is active?
async snapshot() {
return scoped(await user.snapshot())
},
signUserMessage: user.signUserMessage.bind(user),
resolveArgument: user.resolveArgument.bind(user),
authorization: user.authorization.bind(user),
}
}
80 changes: 51 additions & 29 deletions packages/fcl-ethereum-provider/src/wc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {formatChainId} from "./util/eth"
import {getAccountsFromNamespaces} from "@walletconnect/utils"
import {FLOW_METHODS} from "@onflow/fcl-wc"
import * as fcl from "@onflow/fcl"
import {Service} from "@onflow/typedefs"
import {CurrentUser, Service} from "@onflow/typedefs"

const BASE_WC_SERVICE = (
externalProvider: InstanceType<typeof UniversalProvider>
Expand All @@ -26,7 +26,7 @@ const BASE_WC_SERVICE = (
f_vsn: "1.0.0",
type: "authn",
method: "WC/RPC",
uid: "https://walletconnect.com",
uid: "cross-vm-walletconnect#authn",
endpoint: "flow_authn",
optIn: true,
provider: {
Expand All @@ -48,39 +48,62 @@ export class ExtendedEthereumProvider extends EthereumProvider {
opts: EthereumProviderOptions
): Promise<ExtendedEthereumProvider> {
const provider = new ExtendedEthereumProvider()
await provider.initialize(opts)

// Bind FCL user authentication to the UniversalProvider
const fclUser = fcl.currentUser()
// Refresh the FCL user to align with the WalletConnect session
async function refreshFclUser() {
const fclUser = fcl.currentUser()
const wcService = BASE_WC_SERVICE(provider.signer)
const snapshot = await fclUser.snapshot()

await provider.initialize(opts)
const snapshot = await fclUser.snapshot()
const authnService = snapshot?.services.find(
service => service.type === "authn"
)
const externalProvider = authnService?.params?.externalProvider as any
const externalProviderTopic =
typeof externalProvider === "string"
? externalProvider
: externalProvider?.session?.topic
if (
provider.signer.session &&
(!authnService || externalProviderTopic === provider.signer.session.topic)
) {
await fclUser.authenticate({
service: BASE_WC_SERVICE(provider.signer),
})
} else {
await fclUser.unauthenticate()
// Find the authentication service from the current FCL user snapshot
const authnService = snapshot?.services.find(
service => service.type === "authn"
)

// If there’s no auth service or the auth service
if (authnService && authnService.uid !== wcService.uid) {
// TODO: need to handle... maybe wait for condition to reauthenticate
} else {
// Determine the external provider's topic from the auth service params
const externalProvider = authnService?.params?.externalProvider as
| string
| InstanceType<typeof UniversalProvider>
| undefined
const externalProviderTopic =
typeof externalProvider === "string"
? externalProvider
: externalProvider?.session?.topic

// If the provider is already connected with a matching session, re-authenticate the user
if (
provider.signer.session &&
(!externalProviderTopic ||
externalProviderTopic === provider.signer.session.topic)
) {
await fclUser.authenticate({service: wcService})
} else if (!provider.signer.session) {
// If no session is set but FCL is still authenticated, unauthenticate the user
await fclUser.unauthenticate()
}
}
}

// Set up event listeners regardless of the current authentication state
provider.on("connect", async () => {
await fclUser
.authenticate({service: BASE_WC_SERVICE(provider.signer)})
.catch(console.error)
try {
await refreshFclUser()
} catch (error) {
console.error("Error during authentication on connect:", error)
}
})

provider.on("disconnect", async () => {
await fclUser.unauthenticate()
try {
await refreshFclUser()
} catch (error) {
console.error("Error during unauthentication on disconnect:", error)
}
})

return provider
Expand All @@ -92,7 +115,6 @@ export class ExtendedEthereumProvider extends EthereumProvider {
async connect(
opts?: Parameters<InstanceType<typeof EthereumProvider>["connect"]>[0]
) {
console.log("HEY")
if (!this.signer.client) {
throw new Error("Provider not initialized. Call init() first")
}
Expand Down Expand Up @@ -144,7 +166,7 @@ export class ExtendedEthereumProvider extends EthereumProvider {
}

async disconnect() {
const fclUser = fcl.currentUser()
const fclUser = fcl.currentUser
fclUser.unauthenticate()
return await super.disconnect()
}
Expand Down

0 comments on commit 0c657ff

Please sign in to comment.