Skip to content

Commit

Permalink
fix: getting createAccount method working
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Oct 25, 2023
1 parent 817b42a commit 04f8ed1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 49 deletions.
72 changes: 54 additions & 18 deletions src/account-creation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Checksum256, Checksum256Type, NameType, PermissionLevel, PublicKey} from '@wharfkit/antelope'
import { LocaleDefinitions } from '.'
import {Checksum256Type, NameType, PublicKey, FetchProvider, APIClient} from '@wharfkit/antelope'
import type {ChainDefinition, Fetch} from '@wharfkit/common'
import {LocaleDefinitions, UserInterface} from '.'

/**
* The static configuration of an [[AccountCreationPlugin]].
Expand All @@ -9,10 +10,6 @@ export interface AccountCreationPluginConfig {
* Indicates if the pp requires the user to manually select the blockchain to authorize against.
*/
requiresChainSelect: boolean
/**
* Indicates if the [[AccountCreationPlugin]] requires the user to select an account name to use from a list.
*/
requiresAccountNameSelect?: boolean
/**
* If set, indicates which blockchains are compatible with this [[AccountCreationPlugin]].
*/
Expand Down Expand Up @@ -51,29 +48,68 @@ export interface AccountCreationPluginMetadata {


/**
* The response for a login call of a [[WalletPlugin]].
*/
export interface AccountCreationPluginCreateResponse {
chain: Checksum256
permissionLevel: PermissionLevel
* Options for createAccount call.
**/
export interface CreateAccountOptions {
chain?: ChainDefinition
chains?: ChainDefinition[]
accountName?: NameType
}

interface AccountCreationContext {
chain: Checksum256
/**
* The response for a createAccount call.
*/
export interface CreateAccountResponse {
chain: ChainDefinition
accountName: NameType
}

export interface CreateAccountContextOptions {
appName?: NameType
// client: APIClient
chain?: ChainDefinition
chains?: ChainDefinition[]
fetch: Fetch
accountCreationPlugins?: AccountCreationPlugin[]
ui: UserInterface
}

export class CreateAccountContext {
appName?: string
chain?: ChainDefinition
chains: ChainDefinition[] = []
fetch: Fetch
ui: UserInterface
constructor(options: CreateAccountContextOptions) {
this.appName = String(options.appName)
if (options.chains) {
this.chains = options.chains
}
if (options.chain) {
this.chain = options.chain
}
this.fetch = options.fetch
this.ui = options.ui
}

getClient(chain: ChainDefinition): APIClient {
return new APIClient({provider: new FetchProvider(chain.url, {fetch: this.fetch})})
}
}

/**
* Interface which all 3rd party account creation plugins must implement.
*/

export interface AccountCreationPlugin {
/** A URL friendly (lower case, no spaces, etc) ID for this plugin - Used in serialization */
get id(): string

/** A display name for the account creation service that is presented to users. */
get name(): string

/** The [[SessionKit]] configuration parameters for this [[WalletPlugin]]. */
config: AccountCreationPluginConfig
/** The metadata for the [[WalletPlugin]] itself. */
metadata: AccountCreationPluginMetadata
/** Any translations this plugin requires */
translations?: LocaleDefinitions

Expand All @@ -82,7 +118,7 @@ export interface AccountCreationPlugin {
*
* @param context The [[AccountCreationContext]] for the [[WalletPlugin]] to use.
*/
create(context: AccountCreationContext): Promise<AccountCreationPluginCreateResponse>
create(options: CreateAccountContext): Promise<CreateAccountResponse>
}

/**
Expand All @@ -91,10 +127,10 @@ export interface AccountCreationPlugin {
export abstract class AbstractAccounCreationPlugin implements AccountCreationPlugin {
config: AccountCreationPluginConfig = {
requiresChainSelect: true,
requiresAccountNameSelect: true,
}
metadata: AccountCreationPluginMetadata = {}
translations?: LocaleDefinitions
abstract get id(): string
abstract create(context: AccountCreationContext): Promise<AccountCreationPluginCreateResponse>
abstract get name(): string
abstract create(options: CreateAccountOptions): Promise<CreateAccountResponse>
}
71 changes: 40 additions & 31 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ChainDefinitionType, Fetch} from '@wharfkit/common'
import {ChainDefinition, type ChainDefinitionType, type Fetch} from '@wharfkit/common'
import type {Contract} from '@wharfkit/contract'
import {
Checksum256,
Expand All @@ -8,7 +8,6 @@ import {
PermissionLevel,
PermissionLevelType,
} from '@wharfkit/antelope'
import {ChainDefinition} from '@wharfkit/common'

import {
AbstractLoginPlugin,
Expand All @@ -29,20 +28,8 @@ import {
import {WalletPlugin, WalletPluginLoginResponse, WalletPluginMetadata} from './wallet'
import {UserInterface} from './ui'
import {getFetch} from './utils'
import { AccountCreationPlugin } from './account-creation'
import {AccountCreationPlugin, CreateAccountOptions, CreateAccountResponse, CreateAccountContext} from './account-creation'

export interface CreateAccountOptions {
chain?: ChainDefinition | Checksum256Type
chains?: Checksum256Type[]
accountCreationPlugins?: AccountCreationPlugin[]
transactPlugins?: TransactPlugin[]
transactPluginsOptions?: TransactPluginsOptions
}

export interface CreateAccountResult {
account: NameType
chain: Checksum256Type
}

export interface LoginOptions {
chain?: ChainDefinition | Checksum256Type
Expand Down Expand Up @@ -171,14 +158,8 @@ export class SessionKit {

/**
* Request account creation.
*
* @mermaid - Account creation sequence diagram
* flowchart LR
* A((Create Account)) --> B{{"Hook(s): beforeCreateAccount"}}
* B --> C[Account Creation Plugin]
* C --> D[Session]
*/
async createAccount(options?: CreateAccountOptions): Promise<CreateAccountResult> {
async createAccount(options?: CreateAccountOptions): Promise<CreateAccountResponse> {
try {
if (this.accountCreationPlugins.length === 0) {
throw new Error('No account creation plugins available.')
Expand All @@ -191,7 +172,7 @@ export class SessionKit {
title: 'Select an account creation service',
elements: this.accountCreationPlugins.map((p) => ({
type: 'button',
label: p.metadata.name,
label: p.name,
data: p.id,
})),
})
Expand All @@ -204,21 +185,49 @@ export class SessionKit {
if (!accountCreationPlugin) {
throw new Error('No account creation plugin selected.')
}

// Initialize the account creator object
const accountCreator = new AccountCreator({
supportedChains: accountCreationPlugin.config.supportedChains,
scope: 'wallet',
returnUrl: accountCreationPlugin.config.returnUrl,

let chain: ChainDefinition | undefined

const chains: ChainDefinition[] =
(options?.chains || this.chains).filter(c => {
return accountCreationPlugin?.config.supportedChains?.find(supportedChainId => c.id.equals(supportedChainId))
})

if (options?.chain) {
chain = options?.chain
} else if (chains.length === 1) {
chain = chains[0]
} else if (accountCreationPlugin.config.requiresChainSelect && chains.length > 1) {
const chainIndex = await this.ui.prompt({
title: 'Select a chain to create an account on',
elements: chains.map((c, index) => ({
type: 'button',
label: c.name,
data: index,
})),
})

console.log({ chainIndex })

chain = chains[chainIndex as number]
}

const createAccountContext = new CreateAccountContext({
appName: this.appName,
chain,
chains,
fetch: this.fetch,
accountCreationPlugins: this.accountCreationPlugins,
ui: this.ui,
})

// Open a popup window prompting the user to create an account.
return accountCreator.createAccount()
return accountCreationPlugin.create(createAccountContext)
} catch (error: any) {
await this.ui.onError(error)
throw new Error(error)
}
}

/**
* Request a session from an account.
*
Expand Down

0 comments on commit 04f8ed1

Please sign in to comment.