From b9216b50cc3e4a396e79690a8ffbcf85b1981f12 Mon Sep 17 00:00:00 2001 From: dafuga Date: Wed, 1 Nov 2023 17:35:01 -0700 Subject: [PATCH] fix: adding account creation method to ui interface --- src/account-creation.ts | 11 +++-- src/kit.ts | 102 ++++++++++++---------------------------- src/ui.ts | 15 +++++- 3 files changed, 51 insertions(+), 77 deletions(-) diff --git a/src/account-creation.ts b/src/account-creation.ts index 645495c..efb38d9 100644 --- a/src/account-creation.ts +++ b/src/account-creation.ts @@ -14,7 +14,7 @@ export interface AccountCreationPluginConfig { /** * If set, indicates which blockchains are compatible with this [[AccountCreationPlugin]]. */ - supportedChains?: Checksum256Type[] + supportedChains?: ChainDefinition[] } /** @@ -71,21 +71,25 @@ export interface CreateAccountContextOptions { chains?: ChainDefinition[] fetch: Fetch accountCreationPlugins?: AccountCreationPlugin[] + accountCreationPlugin?: AccountCreationPlugin ui: UserInterface } export class CreateAccountContext { appName?: string chain?: ChainDefinition - chains: ChainDefinition[] = [] + chains?: ChainDefinition[] fetch: Fetch ui: UserInterface + accountCreationPlugin?: AccountCreationPlugin accountCreationPlugins: AccountCreationPlugin[] = [] constructor(options: CreateAccountContextOptions) { this.appName = String(options.appName) + console.log('CreateAccountContextOptions', options) if (options.chains) { - this.chains = options.chains + console.log('setting chains', options.chains) + this.chains = [...options.chains] } if (options.chain) { this.chain = options.chain @@ -95,6 +99,7 @@ export class CreateAccountContext { if (options.accountCreationPlugins) { this.accountCreationPlugins = options.accountCreationPlugins } + this.accountCreationPlugin = options.accountCreationPlugin } getClient(chain: ChainDefinition): APIClient { diff --git a/src/kit.ts b/src/kit.ts index 5ef68a3..feb7b20 100644 --- a/src/kit.ts +++ b/src/kit.ts @@ -26,7 +26,7 @@ import { TransactPluginsOptions, } from './transact' import {WalletPlugin, WalletPluginLoginResponse, WalletPluginMetadata} from './wallet' -import {UserInterface} from './ui' +import {UserInterface, UserInterfaceAccountCreationResponse} from './ui' import {getFetch} from './utils' import {AccountCreationPlugin, CreateAccountOptions, CreateAccountResponse, CreateAccountContext} from './account-creation' @@ -171,89 +171,45 @@ export class SessionKit { throw new Error('No account creation plugins available.') } - let accountCreationPlugin: AccountCreationPlugin | undefined - - if (this.accountCreationPlugins.length > 1) { - let pluginId: string | undefined - - await this.ui.prompt({ - title: 'Select an account creation service', - body: '', - elements: this.accountCreationPlugins.map((p) => ({ - type: 'button', - data: { - label: p.name, - onClick: () => pluginId = p.id, - } - })), - }) - - console.log({ pluginId }) - - accountCreationPlugin = this.accountCreationPlugins.find(plugin => plugin.id === pluginId) - } else { - accountCreationPlugin = this.accountCreationPlugins[0] - } - - if (!accountCreationPlugin) { - throw new Error('No account creation plugin selected.') - } - - let chain: ChainDefinition | undefined + console.log({ + optionsChains: options?.chains, + thisChains: this.chains, + }) - const chains: ChainDefinition[] = - (options?.chains || this.chains).filter(c => { - return accountCreationPlugin?.config.supportedChains?.find(supportedChainId => c.id.equals(supportedChainId)) - }) + const context = new CreateAccountContext({ + appName: this.appName, + chain: options?.chain, + chains: options?.chains || this.chains, + fetch: this.fetch, + accountCreationPlugins: this.accountCreationPlugins, + accountCreationPlugin: undefined, + ui: this.ui, + }) - if (options?.chain) { - chain = options?.chain - } else if (chains.length === 1) { - chain = chains[0] - } else if (accountCreationPlugin.config.requiresChainSelect && chains.length > 1) { - let chainIndex: number | undefined - - const prompt = this.ui.prompt({ - title: 'Select a chain to create an account on', - body: '', - elements: chains.map((c, index) => ({ - type: 'button', - data: { - label: c.name, - onClick: () => { - chainIndex = index - prompt.cancel() - }, - }, - })), - }) + console.log({ context }) - await prompt + let accountCreationResponse: UserInterfaceAccountCreationResponse | undefined - console.log({ chainIndex }) + if (this.accountCreationPlugins.length === 1) { + context.accountCreationPlugin = this.accountCreationPlugins[0] - if (!chainIndex) { - const error = new Error('No chain selected.') + console.log({ supportedChains: context.accountCreationPlugin?.config.supportedChains, previousChains: [...(context.chains || [])], config: context.accountCreationPlugin.config }) - this.ui.onError(error) - throw error + if (context.accountCreationPlugin.config.requiresChainSelect && !context.chain) { + context.chains = context.accountCreationPlugin?.config.supportedChains?.filter(chainId => !context.chains || context.chains?.find(c => String(c.id) === String((chainId)))) || context.chains + accountCreationResponse = await context.ui.onAccountCreate(context) } + } else { + accountCreationResponse = await context.ui.onAccountCreate(context) + } - chain = chains[chainIndex] + console.log({ accountCreationResponse }) - console.log({chain}) + if (!context.accountCreationPlugin) { + throw new Error('No account creation plugin selected.') } - const createAccountContext = new CreateAccountContext({ - appName: this.appName, - chain, - chains, - fetch: this.fetch, - accountCreationPlugins: this.accountCreationPlugins, - ui: this.ui, - }) - - return accountCreationPlugin.create(createAccountContext) + return context.accountCreationPlugin.create(context) } catch (error: any) { await this.ui.onError(error) throw new Error(error) diff --git a/src/ui.ts b/src/ui.ts index d93551c..9503cf7 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -1,8 +1,9 @@ import {Checksum256Type, PermissionLevelType} from '@wharfkit/antelope' -import type {Cancelable, LocaleDefinitions} from '@wharfkit/common' +import type {Cancelable, ChainDefinition, LocaleDefinitions} from '@wharfkit/common' import {LoginOptions} from './kit' import {LoginContext} from './login' +import { CreateAccountContext } from './index-module' /** * The arguments for a [[UserInterface.prompt]] call. @@ -36,6 +37,15 @@ export interface UserInterfaceLoginResponse { walletPluginIndex: number } +/** + * The response for an account creation call of a [[UserInterface]]. + */ +export type UserInterfaceAccountCreationResponse = { + chain?: ChainDefinition // If account creation can only be done on one chain. + chains?: ChainDefinition[] // Used if the user should have the option to create his account on multiple chain (wouldn't be returned if requiresChainSelect was set to true). + pluginId?: string // The id of the plugin that was selected (if more than one plugin was available). +} + /** * The options to pass to [[UserInterface.translate]]. */ @@ -61,6 +71,8 @@ export interface UserInterface { login(context: LoginContext): Promise /** Inform the UI that an error has occurred */ onError: (error: Error) => Promise + /** Inform the UI that an account creation process has started */ + onAccountCreate: (context: CreateAccountContext) => Promise /** Inform the UI that a login call has started **/ onLogin: () => Promise /** Inform the UI that a login call has completed **/ @@ -95,6 +107,7 @@ export interface UserInterface { export abstract class AbstractUserInterface implements UserInterface { abstract login(context: LoginContext): Promise abstract onError(error: Error): Promise + abstract onAccountCreate(context: CreateAccountContext): Promise abstract onLogin(options?: LoginOptions): Promise abstract onLoginComplete(): Promise abstract onTransact(): Promise