All files / src wallet.ts

96.15% Statements 25/26
80% Branches 8/10
85.71% Functions 6/7
95.83% Lines 23/24

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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207394x             1x 1x                                                                   1x       1x       1x       1x       1x       1x       1x     207x                                                                                                                                                                                           1x 79x 79x         79x                 455x     74x                   54x 54x 54x 54x               54x   1x  
import {
    Checksum256,
    Checksum256Type,
    PermissionLevel,
    PublicKey,
    Signature,
    Struct,
} from '@wharfkit/antelope'
import {Logo} from '@wharfkit/common'
import type {LocaleDefinitions} from '@wharfkit/common'
import {IdentityProof, ResolvedSigningRequest} from '@wharfkit/signing-request'
 
import {LoginContext} from './login'
import {TransactContext} from './transact'
import {LogoutContext} from './kit'
 
/**
 * 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
    identityProof?: IdentityProof
}
 
/**
 * 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>
    /**
     * Optional method that is used to signal to the wallet plugin to logout the user.
     *
     * @param session The [[Session]] instance that is being logged out.
     * @returns A promise that resolves when the wallet plugin logout process is complete.
     * @throws An error if the logout could not happen.
     */
    logout?(context: LogoutContext): Promise<void>
 
    /**
     * Serialize the [[WalletPlugin]] ID and data into a plain object.
     */
    serialize(): WalletPluginData
 
    /**
     * Requests a public key from the wallet plugin.
     */
    retrievePublicKey?(chainId: Checksum256Type): Promise<PublicKey>
 
    /** Return a copy of this [[WalletPlugin]] with its own session data. */
    clone?(): WalletPlugin
}
 
/**
 * 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,
        }
    }
    /** Shallow copy with fresh data and metadata; every other field must be safe to share. */
    clone(): WalletPlugin {
        const cloned = Object.create(Object.getPrototypeOf(this))
        Object.assign(cloned, this)
        cloned._data = {}
        cloned.metadata = WalletPluginMetadata.from({
            name: this.metadata.name,
            description: this.metadata.description,
            logo: this.metadata.logo,
            homepage: this.metadata.homepage,
            download: this.metadata.download,
            publicKey: this.metadata.publicKey,
        })
        return cloned
    }
}