All files / src login.ts

87.5% Statements 28/32
80% Branches 12/15
62.5% Functions 5/8
87.5% Lines 28/32

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 145530x             1x     1x           2x 1x 1x                                                                                           1x   71x     71x   71x                     71x             71x     71x 71x 71x     71x 71x   71x     71x 71x 71x 71x 71x 71x 71x                                                   1x       1x       1x  
import {
    APIClient,
    Checksum256Type,
    FetchProvider,
    NameType,
    PermissionLevel,
    PublicKey,
} from '@wharfkit/antelope'
import type {ChainDefinition, Fetch} from '@wharfkit/common'
import {SigningRequestEncodingOptions} from '@wharfkit/signing-request'
import * as zlib from 'pako'
 
import type {Session} from './session'
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
    arbitrary?: Record<string, any>
    // client: APIClient
    chain?: ChainDefinition
    chains?: ChainDefinition[]
    fetch: Fetch
    loginPlugins?: AbstractLoginPlugin[]
    permissionLevel?: PermissionLevel
    walletPlugins?: UserInterfaceWalletPlugin[]
    ui: UserInterface
    sessionKeyManager?: any
}
 
export interface UserInterfaceRequirements {
    requiresChainSelect: boolean
    requiresPermissionSelect: boolean
    requiresPermissionEntry: boolean
    requiresWalletSelect: boolean
}
 
export interface UserInterfaceWalletPlugin {
    config: WalletPluginConfig
    metadata: WalletPluginMetadata
    retrievePublicKey: ((chainId: Checksum256Type) => Promise<PublicKey>) | undefined
}
 
/**
 * 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
    arbitrary: Record<string, any> = {}
    // client: APIClient
    chain?: ChainDefinition
    chains: ChainDefinition[] = []
    fetch: Fetch
    hooks: LoginHooks = {
        afterLogin: [],
        beforeLogin: [],
    }
    permissionLevel?: PermissionLevel
    /**
     * The session created during login.
     * Only available in afterLogin hooks.
     */
    session?: Session
    ui: UserInterface
    uiRequirements: UserInterfaceRequirements = {
        requiresChainSelect: true,
        requiresPermissionSelect: true,
        requiresPermissionEntry: false,
        requiresWalletSelect: true,
    }
    walletPluginIndex?: number
    walletPlugins: UserInterfaceWalletPlugin[] = []
    sessionKeyManager?: any
    constructor(options: LoginContextOptions) {
        this.appName = String(options.appName)
        if (options.arbitrary) {
            this.arbitrary = options.arbitrary
        }
        // this.client = options.client
        if (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.sessionKeyManager = options.sessionKeyManager
        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')
    }
}