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

PKG -- [sdk] Restore previous account resolution ordering #1910

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/tasty-radios-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/sdk": patch
---

Restore account resolution ordering (fixes issue where FCL currentUser authz would run without knowing custom authz accounts)
70 changes: 40 additions & 30 deletions packages/sdk/src/resolve/resolve-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ function uniqueAccountsFlatMap(accounts: InteractionAccount[]) {
return uniqueAccountsFlatMapped
}

async function recurseResolveAccount(
async function resolveSingleAccount(
ix: Interaction,
currentAccountTempId: string,
depthLimit = MAX_DEPTH_LIMIT,
{debugLogger}: {debugLogger: (msg?: string, indent?: number) => void}
) {
): Promise<[ids: string | string[], hasNewAccounts: boolean]> {
if (depthLimit <= 0) {
throw new Error(
`recurseResolveAccount Error: Depth limit (${MAX_DEPTH_LIMIT}) reached. Ensure your authorization functions resolve to an account after ${MAX_DEPTH_LIMIT} resolves.`
Expand All @@ -162,7 +162,7 @@ async function recurseResolveAccount(

let account = ix.accounts[currentAccountTempId]

if (!account) return null
if (!account) return [[], false]

debugLogger(
`account: ${account.tempId}`,
Expand Down Expand Up @@ -200,32 +200,25 @@ async function recurseResolveAccount(

account = addAccountToIx(ix, account)

const recursedAccounts = await Promise.all(
flatResolvedAccounts.map(
async (resolvedAccount: InteractionAccount) => {
return await recurseResolveAccount(
ix,
resolvedAccount.tempId,
depthLimit - 1,
{debugLogger}
return [
flatResolvedAccounts
? flatResolvedAccounts.map(
(flatResolvedAccount: InteractionAccount) =>
flatResolvedAccount.tempId
)
}
)
)

return recursedAccounts
? recurseFlatMap(recursedAccounts)
: account.tempId
: account.tempId,
true,
]
} else {
debugLogger(
`account: ${account.tempId} -- cache HIT`,
Math.max(MAX_DEPTH_LIMIT - depthLimit, 0)
)

return account.resolve
return [account.resolve, false]
}
}
return account.tempId
return [account.tempId, false]
}

const getAccountTempIDs = (rawTempIds: string | string[] | null) => {
Expand All @@ -238,6 +231,7 @@ const getAccountTempIDs = (rawTempIds: string | string[] | null) => {
async function resolveAccountType(
ix: Interaction,
type: ROLES,
depthLimit = MAX_DEPTH_LIMIT,
{debugLogger}: {debugLogger: (msg?: string, indent?: number) => void}
) {
invariant(
Expand All @@ -254,18 +248,16 @@ async function resolveAccountType(
let accountTempIDs = getAccountTempIDs(ix[type])

let allResolvedAccounts: InteractionAccount[] = []
let hasNewAccounts = false
for (let accountId of accountTempIDs) {
let account = ix.accounts[accountId]
invariant(Boolean(account), `resolveAccountType Error: account not found`)

let resolvedAccountTempIds = await recurseResolveAccount(
ix,
accountId,
MAX_DEPTH_LIMIT,
{
let [resolvedAccountTempIds, acctHasNewAccounts] =
await resolveSingleAccount(ix, accountId, depthLimit, {
debugLogger,
}
)
})
hasNewAccounts = acctHasNewAccounts || hasNewAccounts

resolvedAccountTempIds = Array.isArray(resolvedAccountTempIds)
? resolvedAccountTempIds
Expand Down Expand Up @@ -320,6 +312,8 @@ async function resolveAccountType(
}
}
}

return hasNewAccounts
}

export async function resolveAccounts(
Expand All @@ -337,9 +331,25 @@ export async function resolveAccounts(
}
let [debugLogger, getDebugMessage] = debug()
try {
await resolveAccountType(ix, ROLES.PROPOSER, {debugLogger})
await resolveAccountType(ix, ROLES.AUTHORIZATIONS, {debugLogger})
await resolveAccountType(ix, ROLES.PAYER, {debugLogger})
let depthLimit = MAX_DEPTH_LIMIT
let shouldContinue = true
while (shouldContinue) {
if (depthLimit <= 0) {
throw new Error(
`resolveAccounts Error: Depth limit (${MAX_DEPTH_LIMIT}) reached. Ensure your authorization functions resolve to an account after ${MAX_DEPTH_LIMIT} resolves.`
)
}

shouldContinue =
(await resolveAccountType(ix, ROLES.PROPOSER, depthLimit, {
debugLogger,
})) ||
(await resolveAccountType(ix, ROLES.AUTHORIZATIONS, depthLimit, {
debugLogger,
})) ||
(await resolveAccountType(ix, ROLES.PAYER, depthLimit, {debugLogger}))
depthLimit--
}

await removeUnusedIxAccounts(ix, {debugLogger})

Expand Down
Loading