All files / src utils.ts

14.28% Statements 6/42
24% Branches 6/25
9.09% Functions 1/11
15% Lines 6/40

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 8332x   1x   1x                                                                                               1x                             1x         2x                    
import {Action, Transaction} from '@wharfkit/session'
 
import {Buyrambytes, Transfer} from './types'
 
export function validateModifications(original: Transaction, modified: Transaction) {
    // Ensure all the original actions exist within the modified transaction
    const originalsExist = original.actions.every((action: Action) =>
        modified.actions.some((modifiedAction: Action) => action.equals(modifiedAction))
    )
    Iif (!originalsExist) {
        throw new Error('The modified transaction does not contain all the original actions.')
    }
 
    // Find all new actions added to this transaction
    const newActions = modified.actions.filter((action: Action) => {
        return !original.actions.some((originalAction: Action) => action.equals(originalAction))
    })
 
    // Iterate and validate each new action
    for (const newAction of newActions) {
        // Determine if a new action has the authorization of the original actor
        const authByUser = newAction.authorization.find((auth: any) => {
            return auth.actor === original.actions[0].authorization[0].actor
        })
        Iif (authByUser) {
            // Ensure if a transaction fee is being paid by the user, it's going to the correct account
            const isTokenTransfer =
                newAction.account.equals('eosio.token') && newAction.name.equals('transfer')
            Iif (isTokenTransfer) {
                const data = Transfer.from(newAction.data)
                Iif (data.to.equals('txfee.wam') && data.memo.startsWith('WAX fee for')) {
                    continue
                }
            }
            // Ensure if a RAM purchase is occurring during a modification, it's going to the user
            const isRAMPurchase =
                newAction.account.equals('eosio') && newAction.name.equals('buyrambytes')
            Iif (isRAMPurchase) {
                const data = Buyrambytes.from(newAction.data)
                Iif (data.receiver.equals(original.actions[0].authorization[0].actor)) {
                    continue
                }
            }
            // If not passing the above rules, throw an error
            throw new Error(
                'The modified transaction contains one or more actions that are not allowed.'
            )
        }
    }
}
 
// Create and return an interval that checks whether or not the window has been closed
export function registerCloseListener(t, popup: Window, reject) {
    const closeListener = setInterval(() => {
        Iif (popup.closed) {
            clearInterval(closeListener)
            reject(
                t('error.closed', {
                    default: 'The Cloud Wallet was closed before the request was completed',
                })
            )
        }
    }, 500)
    return closeListener
}
 
// Retrieve current time
export function getCurrentTime() {
    return Math.floor(new Date().getTime())
}
 
// Ensure the MessageEvent returned from the popup is valid
export function isValidEvent(event: MessageEvent, url: URL, window: Window): boolean {
    const eventOrigin = new URL(event.origin)
    const validOrigin = eventOrigin.origin === url.origin
    const validSource = event.source === window
    const validObject = typeof event.data === 'object'
    Iif (!validObject || !validOrigin || !validSource) {
        return false
    }
    return true
}