All files index.ts

82.75% Statements 24/29
50% Branches 7/14
75% Functions 6/8
92% Lines 23/25

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 8532x                               1x           1x 5x 5x       5x         5x 5x 4x 4x 4x                     2x 2x       2x   2x         2x                     1x   1x 1x 1x 1x 1x           1x  
import {
    AbstractWalletPlugin,
    cancelable,
    Cancelable,
    Checksum256,
    LoginContext,
    PrivateKey,
    PrivateKeyType,
    ResolvedSigningRequest,
    TransactContext,
    Transaction,
    WalletPlugin,
    WalletPluginConfig,
    WalletPluginLoginResponse,
    WalletPluginMetadata,
    WalletPluginSignResponse,
} from '@wharfkit/session'
 
export interface WalletPluginPrivateKeyOptions {
    privateKey: PrivateKeyType
}
 
export class WalletPluginPrivateKey extends AbstractWalletPlugin implements WalletPlugin {
    public id = 'wallet-plugin-privatekey'
    readonly config: WalletPluginConfig = {
        requiresChainSelect: true,
        requiresPermissionSelect: true,
    }
    readonly metadata: WalletPluginMetadata = WalletPluginMetadata.from({
        name: 'Private Key Signer',
        description: '',
    })
    constructor(privateKeyData: PrivateKeyType) {
        super()
        const privateKey = PrivateKey.from(privateKeyData)
        this.data.privateKey = privateKey
        this.metadata.publicKey = String(privateKey.toPublic())
        this.metadata.description = `An unsecured wallet that can sign for authorities using the ${
            String(this.data.publicKey).substring(0, 11) +
            '...' +
            String(this.data.publicKey).substring(
                String(this.data.publicKey).length - 4,
                String(this.data.publicKey).length
            )
        } public key.`
    }
    login(context: LoginContext): Cancelable<WalletPluginLoginResponse> {
        let chain: Checksum256
        if (context.chain) {
            chain = context.chain.id
        } else E{
            chain = context.chains[0].id
        }
        return cancelable(
            new Promise((resolve, reject) => {
                IIif (!context.permissionLevel) {
                    return reject(
                        'Calling login() without a permissionLevel is not supported by the WalletPluginPrivateKey plugin.'
                    )
                }
                resolve({
                    chain,
                    permissionLevel: context.permissionLevel,
                })
            })
        )
    }
    sign(
        resolved: ResolvedSigningRequest,
        context: TransactContext
    ): Cancelable<WalletPluginSignResponse> {
        return cancelable(
            new Promise((resolve) => {
                const transaction = Transaction.from(resolved.transaction)
                const digest = transaction.signingDigest(Checksum256.from(context.chain.id))
                const privateKey = PrivateKey.from(this.data.privateKey)
                const signature = privateKey.signDigest(digest)
                resolve({
                    signatures: [signature],
                })
            })
        )
    }
}