Skip to content

Commit

Permalink
Allow some wallet creations to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Dec 5, 2023
1 parent cfa18e5 commit f9ac31c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
51 changes: 38 additions & 13 deletions src/core/account/account-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import {
EdgePendingVoucher,
EdgePluginMap,
EdgeRateCache,
EdgeResult,
EdgeSwapConfig,
EdgeSwapQuote,
EdgeSwapRequest,
EdgeSwapRequestOptions,
EdgeWalletInfo,
EdgeWalletInfoFull,
EdgeWalletStates
} from '../../types/types'
Expand Down Expand Up @@ -506,24 +508,38 @@ export function makeAccountApi(ai: ApiInput, accountId: string): EdgeAccount {

async createCurrencyWallets(
createWallets: EdgeCreateCurrencyWallet[]
): Promise<EdgeCurrencyWallet[]> {
): Promise<Array<EdgeResult<EdgeCurrencyWallet>>> {
const { login, loginTree } = accountState()

const walletInfos = await Promise.all(
createWallets.map(async opts => {
return await makeCurrencyWalletKeys(ai, opts.walletType, opts)
})
// Create the keys:
const keys = await Promise.all(
createWallets.map(
async opts =>
await catchResult(makeCurrencyWalletKeys(ai, opts.walletType, opts))
)
)

// Store the keys on the server:
const walletInfos: EdgeWalletInfo[] = []
for (const result of keys) {
if (result.ok) walletInfos.push(result.result)
}
await applyKit(ai, loginTree, makeKeysKit(ai, login, walletInfos))

// Set up options:
return await Promise.all(
walletInfos.map(async (info, i) => {
return await finishWalletCreation(
ai,
accountId,
info.id,
createWallets[i]
)
})
keys.map(async (info, i) =>
info.ok
? await catchResult(
finishWalletCreation(
ai,
accountId,
info.result.id,
createWallets[i]
)
)
: info
)
)
},

Expand Down Expand Up @@ -681,3 +697,12 @@ function getRawPrivateKey(
}
return info
}

async function catchResult<T>(promise: Promise<T>): Promise<EdgeResult<T>> {
try {
const result = await promise
return { ok: true, result }
} catch (error) {
return { ok: false, error }
}
}
7 changes: 6 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface JsonObject {
[name: string]: any // TODO: this needs to become `unknown`
}

/** When we return errors explicitly instead of throwing them */
export type EdgeResult<T> =
| { ok: true; result: T }
| { ok: false; error: unknown }

/** A collection of unknown extra methods exposed by a plugin. */
export interface EdgeOtherMethods {
readonly [name: string]: any
Expand Down Expand Up @@ -1580,7 +1585,7 @@ export interface EdgeAccount {
) => Promise<EdgeCurrencyWallet>
readonly createCurrencyWallets: (
createWallets: EdgeCreateCurrencyWallet[]
) => Promise<EdgeCurrencyWallet[]>
) => Promise<Array<EdgeResult<EdgeCurrencyWallet>>>
readonly waitForCurrencyWallet: (
walletId: string
) => Promise<EdgeCurrencyWallet>
Expand Down

0 comments on commit f9ac31c

Please sign in to comment.