All files index.ts

92.59% Statements 25/27
60% Branches 6/10
100% Functions 5/5
92.59% Lines 25/27

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 9023x                       1x                   1x 1x 1x     1x 1x       1x 1x     1x 1x 1x 1x 1x     1x           1x   1x   1x 1x       1x       1x                 1x                         1x   1x   1x  
import {
    AbstractTransactPlugin,
    Action,
    Name,
    NameType,
    prependAction,
    PrivateKey,
    PrivateKeyType,
    SigningRequest,
    Struct,
    TransactContext,
    TransactHookTypes,
} from '@wharfkit/session'
 
interface ResourceProviderCosignerOptions {
    readonly actor: NameType
    readonly permission: NameType
    readonly privateKey: PrivateKeyType
    readonly contract?: NameType
    readonly action?: NameType
}
 
class noop extends Struct {
    static abiName = 'noop'
    static abiFields = []
}
 
export class TransactPluginCosigner extends AbstractTransactPlugin {
    public id = 'transact-plugin-cosigner'
    readonly actor: Name
    readonly permission: Name
    readonly privateKey: PrivateKey
    readonly contract: Name = Name.from('greymassnoop')
    readonly action: Name = Name.from('noop')
 
    constructor(options: ResourceProviderCosignerOptions) {
        super()
        this.actor = Name.from(options.actor)
        this.permission = Name.from(options.permission)
        this.privateKey = PrivateKey.from(options.privateKey)
        Iif (options.contract) {
            this.contract = Name.from(options.contract)
        }
        Iif (options.action) {
            this.action = Name.from(options.action)
        }
    }
 
    register(context: TransactContext): void {
        context.addHook(TransactHookTypes.beforeSign, async (request, context) => {
            // Modify request data to prepend noop action
            const modifiedRequest = await this.prependAction(request)
            // Sign Transaction
            const resolved = await context.resolve(modifiedRequest)
            const signature = this.privateKey.signDigest(
                resolved.transaction.signingDigest(request.getChainId())
            )
            // Create a new request that is a 'Transaction' and not an 'Action[]' array.
            const newRequest = await context.createRequest({
                transaction: resolved.transaction,
            })
            // Return modified request and new signature
            return {
                request: newRequest,
                signatures: [signature],
            }
        })
    }
 
    async prependAction(request: SigningRequest): Promise<SigningRequest> {
        // Create noop action to assume resource costs
        const newAction = Action.from({
            account: this.contract,
            name: this.action,
            authorization: [
                {
                    actor: this.actor,
                    permission: this.permission,
                },
            ],
            data: noop.from({}),
        })
 
        // Prepend this action to the request
        const newRequest = prependAction(request, newAction)
        // Return the modified request
        return newRequest
    }
}