All files / src utils.ts

64.58% Statements 62/96
55.76% Branches 29/52
50% Functions 8/16
64.58% Lines 62/96

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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249326x               1x   1x                     1x 3x     3x     3x 3x                       1x 3x 3x 3x   1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x           3x                   1x 15x 15x 15x   11x 11x 11x     2x 2x 2x 2x     2x 2x 2x 2x           15x     1x     97x 97x                                   1x 1x 1x                                   1x       2x 2x             1x                           1x                               1x                                                                                   2x       17x     17x 2x 2x 15x 2x 2x 1x     17x    
import {
    Action,
    AnyAction,
    FetchProviderOptions,
    Name,
    PermissionLevel,
    PermissionLevelType,
    Transaction,
} from '@wharfkit/antelope'
import type {Fetch, LocaleDefinitions} from '@wharfkit/common'
import {PlaceholderAuth, SigningRequest} from '@wharfkit/signing-request'
import {BroadcastOptions, TransactArgs, TransactPlugin} from './transact'
import {WalletPlugin} from './wallet'
 
/**
 * Return an instance of fetch.
 *
 * @param options FetchProviderOptions
 * @returns Fetch
 */
/* istanbul ignore next */
export function getFetch(options?: FetchProviderOptions): Fetch {
    Iif (options && options.fetch) {
        return options.fetch
    }
    Iif (typeof window !== 'undefined' && window.fetch) {
        return window.fetch.bind(window)
    }
    if (typeof global !== 'undefined' && global.fetch) {
        return global.fetch.bind(global)
    }
    throw new Error('Missing fetch')
}
 
/**
 * Append an action to the end of the array of actions in a SigningRequest.
 *
 * @param request SigningRequest
 * @param action AnyAction
 * @returns SigningRequest
 */
export function appendAction(request: SigningRequest, action: AnyAction): SigningRequest {
    const newAction = Action.from(action)
    const cloned = request.clone()
    switch (cloned.data.req.variantName) {
        case 'action': {
            cloned.data.req.value = [cloned.data.req.value as Action, newAction]
            cloned.data.req.variantIdx = 1
            break
        }
        case 'action[]': {
            const array = cloned.data.req.value as Action[]
            array.push(newAction)
            cloned.data.req.value = array
            break
        }
        case 'transaction': {
            const tx = cloned.data.req.value as Transaction
            tx.actions.push(newAction)
            cloned.data.req.value = tx
            break
        }
        default: {
            throw new Error('unknown data req type')
        }
    }
    return cloned
}
 
/**
 * Prepend an action to the end of the array of actions in a SigningRequest.
 *
 * @param request SigningRequest
 * @param action AnyAction
 * @returns SigningRequest
 */
export function prependAction(request: SigningRequest, action: AnyAction): SigningRequest {
    const newAction = Action.from(action)
    const cloned = request.clone()
    switch (cloned.data.req.variantName) {
        case 'action': {
            cloned.data.req.value = [newAction, cloned.data.req.value as Action]
            cloned.data.req.variantIdx = 1
            break
        }
        case 'action[]': {
            const array = cloned.data.req.value as Action[]
            array.unshift(newAction)
            cloned.data.req.value = array
            break
        }
        case 'transaction': {
            const tx = cloned.data.req.value as Transaction
            tx.actions.unshift(newAction)
            cloned.data.req.value = tx
            break
        }
        default: {
            throw new Error('unknown data req type')
        }
    }
    return cloned
}
 
export function getPluginTranslations(
    transactPlugin: TransactPlugin | WalletPlugin
): LocaleDefinitions {
    if (!transactPlugin.translations) {
        return {}
    }
    const prefixed = {}
    const languages = Object.keys(transactPlugin.translations)
    languages.forEach((lang) => {
        Iif (transactPlugin.translations) {
            prefixed[lang] = {[transactPlugin.id]: transactPlugin.translations[lang]}
        }
    })
    return prefixed
}
 
/**
 * Extract actions from TransactArgs.
 *
 * @param args TransactArgs
 * @returns Array of actions, or empty array if not determinable
 */
export function extractActions(args: TransactArgs): AnyAction[] {
    if (args.action) {
        return [args.action]
    }
    Iif (args.actions) {
        return args.actions
    }
    Iif (args.transaction && args.transaction.actions) {
        return args.transaction.actions
    }
    return []
}
 
/**
 * Check if an action has an authorization matching a given permission level.
 *
 * @param action AnyAction
 * @param permissionLevel PermissionLevel
 * @returns boolean
 */
export function actionMatchesPermission(
    action: AnyAction,
    permissionLevel: PermissionLevel
): boolean {
    if (!action.authorization) {
        return false
    }
    return action.authorization.some(
        (auth: PermissionLevelType) => permissionLevel.equals(auth) || PlaceholderAuth.equals(auth)
    )
}
 
function rewriteAuthIfMatches(
    auth: PermissionLevelType,
    permissionLevel: PermissionLevel,
    newPermission: Name
): PermissionLevelType {
    Iif (permissionLevel.equals(auth) || PlaceholderAuth.equals(auth)) {
        return PermissionLevel.from({
            actor: permissionLevel.actor,
            permission: newPermission,
        })
    }
    return auth
}
 
function rewriteActionAuthorizations(
    action: AnyAction,
    permissionLevel: PermissionLevel,
    newPermission: Name
): AnyAction {
    Iif (!action.authorization) {
        return action
    }
    return {
        ...action,
        authorization: action.authorization.map((auth) =>
            rewriteAuthIfMatches(auth, permissionLevel, newPermission)
        ),
    }
}
 
export function rewriteAuthorizations(
    args: TransactArgs,
    permissionLevel: PermissionLevel,
    newPermission: Name
): TransactArgs {
    Iif (args.action) {
        return {
            ...args,
            action: rewriteActionAuthorizations(args.action, permissionLevel, newPermission),
        }
    }
 
    Iif (args.actions) {
        return {
            ...args,
            actions: args.actions.map((action) =>
                rewriteActionAuthorizations(action, permissionLevel, newPermission)
            ),
        }
    }
 
    Iif (args.transaction && args.transaction.actions) {
        return {
            ...args,
            transaction: {
                ...args.transaction,
                actions: args.transaction.actions.map((action) =>
                    rewriteActionAuthorizations(action, permissionLevel, newPermission)
                ),
            },
        }
    }
 
    return args
}
 
export interface SendTransaction2Options {
    return_failure_trace?: boolean
    retry_trx?: boolean
    retry_trx_num_blocks?: number
}
 
export function buildSendTransaction2Options(
    awaitIrreversible: boolean,
    broadcastOptions?: BroadcastOptions
): SendTransaction2Options {
    const options: SendTransaction2Options = {
        return_failure_trace: broadcastOptions?.returnFailureTrace ?? true,
    }
    if (awaitIrreversible) {
        options.retry_trx = true
        options.retry_trx_num_blocks = undefined
    } else if (broadcastOptions) {
        options.retry_trx = broadcastOptions.retryTrx ?? true
        if (broadcastOptions.retryTrxNumBlocks !== undefined) {
            options.retry_trx_num_blocks = broadcastOptions.retryTrxNumBlocks
        }
    }
    return options
}