All files wallet.ts

100% Statements 19/19
100% Branches 2/2
100% Functions 5/5
100% Lines 17/17

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 155 156 157 158 159 160 161 162 163 164 165 1661x 1x                                                                 1x       1x       1x       1x       1x       1x       1x     84x                                                                                                                                                       1x 35x 35x         35x                 201x     29x     53x            
import {Checksum256, Checksum256Type, PermissionLevel, Signature, Struct} from '@wharfkit/antelope'
import {Logo} from '@wharfkit/common'
import type {LocaleDefinitions} from '@wharfkit/common'
import {ResolvedSigningRequest} from '@wharfkit/signing-request'
 
import {LoginContext} from './login'
import {TransactContext} from './transact'
 
/**
 * The static configuration of a [[WalletPlugin]].
 */
export interface WalletPluginConfig {
    /**
     * Indicates if the pp requires the user to manually select the blockchain to authorize against.
     */
    requiresChainSelect: boolean
    /**
     * Indicates if the [[WalletPlugin]] requires the user to select a permission to use from a list.
     */
    requiresPermissionSelect: boolean
    /**
     * Indicates if the [[WalletPlugin]] requires the user to manually enter a permission to use.
     */
    requiresPermissionEntry?: boolean
    /**
     * If set, indicates which blockchains are compatible with this [[WalletPlugin]].
     */
    supportedChains?: Checksum256Type[]
}
 
/**
 * The metadata of a [[WalletPlugin]] for display purposes.
 */
@Struct.type('wallet_plugin_metadata')
export class WalletPluginMetadata extends Struct {
    /**
     * A display name for the wallet that is presented to users.
     */
    @Struct.field('string', {optional: true}) declare name?: string
    /**
     * A wallet description to further identify the wallet for users.
     */
    @Struct.field('string', {optional: true}) declare description?: string
    /**
     * Wallet branding
     */
    @Struct.field(Logo, {optional: true}) declare logo?: Logo
    /**
     * Link to the homepage for the wallet
     */
    @Struct.field('string', {optional: true}) declare homepage?: string
    /**
     * Link to the download page for the wallet
     */
    @Struct.field('string', {optional: true}) declare download?: string
    /**
     * The public key being used by the wallet plugin
     */
    @Struct.field('string', {optional: true}) declare publicKey?: string
 
    static from(data) {
        return new WalletPluginMetadata({
            ...data,
            logo: data.logo ? Logo.from(data.logo) : undefined,
        })
    }
}
 
/**
 * The response for a login call of a [[WalletPlugin]].
 */
export interface WalletPluginLoginResponse {
    chain: Checksum256
    permissionLevel: PermissionLevel
}
 
/**
 * The response for a sign call of a [[WalletPlugin]].
 */
export interface WalletPluginSignResponse {
    resolved?: ResolvedSigningRequest
    signatures: Signature[]
}
 
/**
 * Persistent storage format for wallet specified data.
 */
export type WalletPluginData = Record<string, any>
 
/**
 * The serialized form of a [[WalletPlugin]] instance.
 */
export interface SerializedWalletPlugin {
    id: string
    data: WalletPluginData
}
 
/**
 * Interface which all 3rd party wallet plugins must implement.
 */
export interface WalletPlugin {
    /** A URL friendly (lower case, no spaces, etc) ID for this plugin - Used in serialization */
    get id(): string
    /** A method to return the data that needs to persist for the plguin - Used in serialization */
    get data(): WalletPluginData
    set data(data: WalletPluginData)
    /** The [[SessionKit]] configuration parameters for this [[WalletPlugin]]. */
    config: WalletPluginConfig
    /** The metadata for the [[WalletPlugin]] itself. */
    metadata: WalletPluginMetadata
    /** Any translations this plugin requires */
    translations?: LocaleDefinitions
    /**
     * Request the [[WalletPlugin]] to log in a user and return a [[WalletPluginLoginResponse]].
     *
     * @param context The [[LoginContext]] for the [[WalletPlugin]] to use.
     */
    login(context: LoginContext): Promise<WalletPluginLoginResponse>
    /**
     * Requests the [[WalletPlugin]] to sign a transaction and return a [[WalletPluginSignResponse]]]
     *
     * @param transaction The transaction to sign.
     * @param context The [[TransactContext]] for the [[WalletPlugin]] to use.
     */
    sign(
        transaction: ResolvedSigningRequest,
        context: TransactContext
    ): Promise<WalletPluginSignResponse>
    /**
     * Serialize the [[WalletPlugin]] ID and data into a plain object.
     */
    serialize(): WalletPluginData
}
 
/**
 * Abstract class which all 3rd party [[WalletPlugin]] implementations may extend.
 */
export abstract class AbstractWalletPlugin implements WalletPlugin {
    _data: WalletPluginData = {}
    config: WalletPluginConfig = {
        requiresChainSelect: true,
        requiresPermissionSelect: false,
        requiresPermissionEntry: false,
    }
    metadata: WalletPluginMetadata = new WalletPluginMetadata({})
    translations?: LocaleDefinitions
    abstract get id(): string
    abstract login(context: LoginContext): Promise<WalletPluginLoginResponse>
    abstract sign(
        transaction: ResolvedSigningRequest,
        context: TransactContext
    ): Promise<WalletPluginSignResponse>
    get data(): WalletPluginData {
        return this._data
    }
    set data(data: WalletPluginData) {
        this._data = data
    }
    serialize(): SerializedWalletPlugin {
        return {
            id: this.id,
            data: this.data,
        }
    }
}