All files index.ts

25.92% Statements 7/27
40% Branches 6/15
28.57% Functions 2/7
23.07% Lines 6/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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 14022x                           1x   1x       1x                         1x                                                                                                                                                                                                                 1x  
import {
    ABIDef,
    AbstractWalletPlugin,
    ActionType,
    LoginContext,
    ResolvedSigningRequest,
    Serializer,
    TransactContext,
    TransactHookTypes,
    WalletPlugin,
    WalletPluginConfig,
    WalletPluginLoginResponse,
    WalletPluginMetadata,
    WalletPluginSignResponse,
} from '@wharfkit/session'
 
export class WalletPluginCleos extends AbstractWalletPlugin implements WalletPlugin {
    /**
     * The logic configuration for the wallet plugin.
     */
    readonly config: WalletPluginConfig = {
        // Should the user interface display a chain selector?
        requiresChainSelect: true,
 
        // Should the user interface display a permission selector?
        requiresPermissionSelect: false,
 
        // Should the user interface require input for the permission?
        requiresPermissionEntry: true,
    }
    /**
     * The metadata for the wallet plugin to be displayed in the user interface.
     */
    readonly metadata: WalletPluginMetadata = WalletPluginMetadata.from({
        name: 'cleos',
        description: 'Copy and paste the transactions to sign in cleos.',
        logo: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAACXBIWXMAAFiVAABYlQHZbTfTAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAlESURBVHgB7d3vcRRHGgfgV1f33TiCWyIAIvAqgkMRsERgXwRIEZiLgCUCQwQsEYAj0GaAiGDd7W3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HkwOgHAJKQQeBNCYHQCgMlIIbBMjydhTmA0AoBJKcOB0xACN/0eBxAATE6eGEz1MHwncN2bOIAAYLJSCJynRw6CdfRteeityAKASbvWG+j1e4F1DOgJCQCakCcIrwXBKvqwTnV26Ns/EwA0pQRBniT8EgZ5bNziR0SrVKdlUvRgtgPTpPJWXJaKzWaTtwDPUv0ntuHwQxxuXv5bh1rF4cOV3ODfDnnrXycA6EJ5Uw69d+BPKUyWMSwAXpdvGu6dIQB0TABAxwQAdEwAQMcEAHRMAEDHBAB0TABAxwQAdEwAQMcEAHRMAEDHBAB0rKYAGLpnexbAXloKAFc+w55qCoB1DPNgs9nMAthZNQEw0gkniwB2Vtsk4NATW54FsLPaAuCg202umaVhgBCAHdUWAKsY7qW5ANhNbQFw0PVGNzxI9SqA76oqANJEYF4KXMVw89QLEALwHTV+Cfg2xrFIIfDOcAC+rsYAWMZ4N7nMU70zMQi3qy4AyjDg/zGeWaplCoHLHAR6BPCXk6hQaqR5Iu8ythN6x5BD5mN5tnhvHMc1j2F7T57XcjNQlVeD5V5ACoHcC3gRx5GDZR7QuZq3A7+MPu97hztTbQCUuYDnARxN1QeCpBBYpcdFAEdR/YlAKQTOY5yPg4AbpnIk2FmYD4DRTSIAynzAaQgBGNVkDgUtB4YIAVrwKSoxqVOBSwg8iXF2DcJ9+RyVmNyx4Hk4kCrPCVgdYKqGnnw1msneC1BWB3JvYB0wHcsyp1WFKvcC7Guz2ZzH9jzAWUDdHo50AO4omrgZqPQG8gRh3j+wDqjTRU2NP2uiB3BT6hEsYtsjmAfU4X+p8b+MyjQZAF+Uvf/zUo/C7UHcvVVsG381E3/XNR0AtymhMCs/zgKOIzf4dU0TfgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMCu/gBMvdXsNeGJnQAAAABJRU5ErkJggg==',
        homepage: 'https://github.com/antelopeio/leap',
        download: 'https://github.com/antelopeio/leap',
    })
    /**
     * A unique string identifier for this wallet plugin.
     *
     * It's recommended this is all lower case, no spaces, and only URL-friendly special characters (dashes, underscores, etc)
     */
    get id(): string {
        return 'cleos'
    }
    /**
     * Performs the wallet logic required to login and return the chain and permission level to use.
     *
     * @param options WalletPluginLoginOptions
     * @returns Promise<WalletPluginLoginResponse>
     */
    async login(context: LoginContext): Promise<WalletPluginLoginResponse> {
        Iif (!context.chain) {
            throw new Error('The cleos wallet plugin requires a chain to be selected.')
        }
        Iif (!context.permissionLevel) {
            throw new Error('The cleos wallet plugin requires a permissionLevel to be specified.')
        }
        return {
            chain: context.chain.id,
            permissionLevel: context.permissionLevel,
        }
    }
    /**
     * Performs the wallet logic required to sign a transaction and return the signature.
     *
     * @param chain ChainDefinition
     * @param resolved ResolvedSigningRequest
     * @returns Promise<Signature>
     */
    async sign(
        resolved: ResolvedSigningRequest,
        context: TransactContext
    ): Promise<WalletPluginSignResponse> {
        Iif (!context.ui) {
            throw new Error('The cleos wallet plugin requires a UI to be provided.')
        }
        // During the signing process, add a hook to display the cleos command to the user
        context.addHook(TransactHookTypes.afterSign, async () => {
            // Decode the transaction to be human readable
            const actions: ActionType[] = []
            for (const action of resolved.transaction.actions) {
                const abi: ABIDef = await context.abiCache.getAbi(action.account)
                actions.push({
                    account: action.account,
                    name: action.name,
                    authorization: action.authorization,
                    data: Serializer.objectify(action.decodeData(abi)),
                })
            }
            const decoded = {
                ...resolved.transaction,
                actions,
            }
            // Create the cleos command that will be used to sign the transaction
            const command = `cleos -u ${context.chain.url} push transaction '${JSON.stringify(
                decoded,
                null,
                4
            )}'`
            // Prompt the user with the command to sign the transaction
            await context.ui?.prompt({
                title: 'Sign with cleos',
                body: 'Copy the command to sign the transaction using cleos.',
                elements: [
                    {
                        type: 'textarea',
                        data: {
                            content: command,
                        },
                    },
                    {
                        type: 'button',
                        data: {
                            label: 'Copy to clipboard',
                            onClick: () => {
                                // Ensure the copy to clipboard will work
                                if (!navigator.clipboard || !window.isSecureContext) {
                                    alert(
                                        'Copy to clipboard is not supported in this browser. Please copy the command manually.'
                                    )
                                } else {
                                    navigator.clipboard.writeText(command)
                                }
                            },
                        },
                    },
                ],
            })
        })
        // Return no signatures, as the cleos command will be used to sign the transaction
        return {
            signatures: [],
        }
    }
}