All files login.ts

84.61% Statements 22/26
66.66% Branches 8/12
57.14% Functions 4/7
84.61% Lines 22/26

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 1221x     1x         1x 1x 1x                                                                                     1x       43x   43x           43x           43x   43x   43x 43x   43x     43x 43x 43x 43x 43x 43x                                                   1x       1x          
import {APIClient, FetchProvider, NameType, PermissionLevel} from '@wharfkit/antelope'
import type {ChainDefinition, Fetch} from '@wharfkit/common'
import {SigningRequestEncodingOptions} from '@wharfkit/signing-request'
import zlib from 'pako'
 
import {UserInterface} from './ui'
import {WalletPluginConfig, WalletPluginMetadata} from './wallet'
 
export enum LoginHookTypes {
    beforeLogin = 'beforeLogin',
    afterLogin = 'afterLogin',
}
 
export type LoginHook = (context: LoginContext) => Promise<void>
 
export interface LoginHooks {
    afterLogin: LoginHook[]
    beforeLogin: LoginHook[]
}
 
/**
 * Options for creating a new context for a [[Kit.login]] call.
 */
export interface LoginContextOptions {
    appName?: NameType
    // client: APIClient
    chain?: ChainDefinition
    chains?: ChainDefinition[]
    fetch: Fetch
    loginPlugins?: AbstractLoginPlugin[]
    permissionLevel?: PermissionLevel
    walletPlugins?: UserInterfaceWalletPlugin[]
    ui: UserInterface
}
 
export interface UserInterfaceRequirements {
    requiresChainSelect: boolean
    requiresPermissionSelect: boolean
    requiresPermissionEntry: boolean
    requiresWalletSelect: boolean
}
 
export interface UserInterfaceWalletPlugin {
    config: WalletPluginConfig
    metadata: WalletPluginMetadata
}
 
/**
 * Temporary context created for the duration of a [[Kit.login]] call.
 *
 * This context is used to store the state of the login request and
 * provide a way for plugins to add hooks into the process.
 */
export class LoginContext {
    appName?: string
    // client: APIClient
    chain?: ChainDefinition
    chains: ChainDefinition[] = []
    fetch: Fetch
    hooks: LoginHooks = {
        afterLogin: [],
        beforeLogin: [],
    }
    permissionLevel?: PermissionLevel
    ui: UserInterface
    uiRequirements: UserInterfaceRequirements = {
        requiresChainSelect: true,
        requiresPermissionSelect: true,
        requiresPermissionEntry: false,
        requiresWalletSelect: true,
    }
    walletPlugins: UserInterfaceWalletPlugin[] = []
    constructor(options: LoginContextOptions) {
        this.appName = String(options.appName)
        // this.client = options.client
        Eif (options.chains) {
            this.chains = options.chains
        }
        Iif (options.chain) {
            this.chain = options.chain
        }
        this.fetch = options.fetch
        this.permissionLevel = options.permissionLevel
        this.walletPlugins = options.walletPlugins || []
        this.ui = options.ui
        options.loginPlugins?.forEach((plugin: AbstractLoginPlugin) => {
            plugin.register(this)
        })
    }
    addHook(t: LoginHookTypes, hook: LoginHook) {
        this.hooks[t].push(hook)
    }
    getClient(chain: ChainDefinition): APIClient {
        return new APIClient({provider: new FetchProvider(chain.url, {fetch: this.fetch})})
    }
    get esrOptions(): SigningRequestEncodingOptions {
        return {
            zlib,
        }
    }
}
 
/**
 * Payload accepted by the [[Kit.login]] method.
 */
export interface LoginPlugin {
    register: (context: LoginContext) => void
}
 
/**
 * Abstract class for [[Kit.login]] plugins to extend.
 */
export abstract class AbstractLoginPlugin implements LoginPlugin {
    abstract register(context: LoginContext): void
}
 
export class BaseLoginPlugin extends AbstractLoginPlugin {
    register() {
        // console.log('Register hooks via context.addHook')
    }
}