Skip to content

Commit

Permalink
fix: adding account creation method to ui interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Nov 2, 2023
1 parent 6a8188d commit b9216b5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 77 deletions.
11 changes: 8 additions & 3 deletions src/account-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface AccountCreationPluginConfig {
/**
* If set, indicates which blockchains are compatible with this [[AccountCreationPlugin]].
*/
supportedChains?: Checksum256Type[]
supportedChains?: ChainDefinition[]
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -95,6 +99,7 @@ export class CreateAccountContext {
if (options.accountCreationPlugins) {
this.accountCreationPlugins = options.accountCreationPlugins
}
this.accountCreationPlugin = options.accountCreationPlugin
}

getClient(chain: ChainDefinition): APIClient {
Expand Down
102 changes: 29 additions & 73 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion src/ui.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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]].
*/
Expand All @@ -61,6 +71,8 @@ export interface UserInterface {
login(context: LoginContext): Promise<UserInterfaceLoginResponse>
/** Inform the UI that an error has occurred */
onError: (error: Error) => Promise<void>
/** Inform the UI that an account creation process has started */
onAccountCreate: (context: CreateAccountContext) => Promise<UserInterfaceAccountCreationResponse>
/** Inform the UI that a login call has started **/
onLogin: () => Promise<void>
/** Inform the UI that a login call has completed **/
Expand Down Expand Up @@ -95,6 +107,7 @@ export interface UserInterface {
export abstract class AbstractUserInterface implements UserInterface {
abstract login(context: LoginContext): Promise<UserInterfaceLoginResponse>
abstract onError(error: Error): Promise<void>
abstract onAccountCreate(context: CreateAccountContext): Promise<UserInterfaceAccountCreationResponse>
abstract onLogin(options?: LoginOptions): Promise<void>
abstract onLoginComplete(): Promise<void>
abstract onTransact(): Promise<void>
Expand Down

0 comments on commit b9216b5

Please sign in to comment.