All files / src account-creation.ts

43.33% Statements 13/30
42.85% Branches 6/14
20% Functions 1/5
37.03% Lines 10/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 15564x 1x                                           1x       1x       1x       1x       1x                                                                                     1x                                                                                                                           1x                 1x  
import {APIClient, FetchProvider, NameType, Struct} from '@wharfkit/antelope'
import {Logo} from '@wharfkit/common'
import type {ChainDefinition, Fetch, LocaleDefinitions} from '@wharfkit/common'
import {UserInterface} from '.'
 
/**
 * The static configuration of an [[AccountCreationPlugin]].
 */
export interface AccountCreationPluginConfig {
    /**
     * Indicates if the plugin requires the user to manually select the blockchain to create an account on.
     */
    requiresChainSelect: boolean
    /**
     * If set, indicates which blockchains are compatible with this [[AccountCreationPlugin]].
     */
    supportedChains?: ChainDefinition[]
}
 
/**
 * The metadata of an [[AccountCreationPlugin]].
 */
@Struct.type('account_creation_plugin_metadata')
export class AccountCreationPluginMetadata extends Struct {
    /**
     * A display name for the account creation service that is presented to users.
     */
    @Struct.field('string') declare name: string
    /**
     * A description to further identify the account creation service for users.
     */
    @Struct.field('string', {optional: true}) declare description?: string
    /**
     * Account creation service branding.
     */
    @Struct.field(Logo, {optional: true}) declare logo?: Logo
    /**
     * Link to the homepage for the account creation service.
     */
    @Struct.field('string', {optional: true}) declare homepage?: string
 
    static from(data) {
        return new AccountCreationPluginMetadata({
            ...data,
            logo: data.logo ? Logo.from(data.logo) : undefined,
        })
    }
}
 
/**
 * Options for createAccount call.
 **/
export interface CreateAccountOptions {
    accountName?: NameType
    chain?: ChainDefinition
    pluginId?: string
}
 
/**
 * The response for a createAccount call.
 */
export interface CreateAccountResponse {
    chain: ChainDefinition
    accountName: NameType
}
 
export interface CreateAccountContextOptions {
    accountCreationPlugins?: AccountCreationPlugin[]
    appName?: NameType
    // client: APIClient
    chain?: ChainDefinition
    chains?: ChainDefinition[]
    fetch: Fetch
    ui: UserInterface
    uiRequirements?: UserInterfaceAccountCreationRequirements
}
 
export interface UserInterfaceAccountCreationRequirements {
    requiresChainSelect: boolean
    requiresPluginSelect: boolean
}
 
export class CreateAccountContext {
    accountCreationPlugins: AccountCreationPlugin[] = []
    appName?: string
    chain?: ChainDefinition
    chains?: ChainDefinition[]
    fetch: Fetch
    ui: UserInterface
    uiRequirements: UserInterfaceAccountCreationRequirements = {
        requiresChainSelect: true,
        requiresPluginSelect: true,
    }
 
    constructor(options: CreateAccountContextOptions) {
        this.appName = String(options.appName)
        Iif (options.chains) {
            this.chains = options.chains
        }
        Iif (options.chain) {
            this.chain = options.chain
        }
        this.fetch = options.fetch
        this.ui = options.ui
        Iif (options.accountCreationPlugins) {
            this.accountCreationPlugins = options.accountCreationPlugins
        }
        Iif (options.uiRequirements) {
            this.uiRequirements = options.uiRequirements
        }
    }
 
    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
    /** Any translations this plugin requires */
    translations?: LocaleDefinitions
 
    /**
     * Request the [[AccountCreationPlugin]] to create a new account.
     *
     * @param context The [[AccountCreationContext]] for the [[WalletPlugin]] to use.
     */
    create(options: CreateAccountContext): Promise<CreateAccountResponse>
}
 
/**
 * Abstract class which all 3rd party [[AccountCreation]] implementations may extend.
 */
export abstract class AbstractAccountCreationPlugin implements AccountCreationPlugin {
    config: AccountCreationPluginConfig = {
        requiresChainSelect: true,
    }
    metadata: AccountCreationPluginMetadata = new AccountCreationPluginMetadata({})
    translations?: LocaleDefinitions
    abstract get id(): string
    abstract get name(): string
    abstract create(options: CreateAccountOptions): Promise<CreateAccountResponse>
}