All files / src permission.ts

100% Statements 37/37
85.71% Branches 12/14
100% Functions 13/13
100% Lines 33/33

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 147113x                               1x                                                                     1x 1x 1x           2x   1x       7x 6x   7x 1x           6x             6x       3x 3x   3x 1x           2x       5x 1x   5x 1x           4x             4x       2x 1x   2x 1x           1x       2x   2x       1x 1x        
import {
    API,
    Authority,
    AuthorityType,
    KeyWeight,
    Name,
    NameType,
    PermissionLevel,
    PermissionLevelType,
    PermissionLevelWeight,
    PublicKey,
    PublicKeyType,
    Struct,
    UInt16Type,
    UInt32Type,
    WaitWeight,
} from '@wharfkit/antelope'
 
export interface PermissionData {
    account: NameType
    parent: NameType
    permission: NameType
    auth: AuthorityType | Authority
}
 
export interface AddKeyActionParam {
    permission: Permission
    key: string
}
 
export interface ActionData {
    account: NameType
    parent: NameType
    permission: NameType
    auth: Authority
    authorized_by: NameType
}
 
export type PermissionType =
    | Permission
    | API.v1.AccountPermission
    | {
          perm_name: NameType
          parent: NameType
          required_auth: AuthorityType
          linked_actions?: LinkedActionType[]
      }
 
export type LinkedActionType = LinkedAction | {account: NameType; action: NameType}
 
@Struct.type('linked_actions')
export class LinkedAction extends Struct {
    @Struct.field('name') declare account: Name
    @Struct.field('name', {optional: true}) declare action?: Name
}
 
export type WaitWeightType = WaitWeight | {wait_sec: UInt32Type; weight: UInt16Type}
 
@Struct.type('permission')
export class Permission extends API.v1.AccountPermission {
    get name(): Name {
        return this.perm_name
    }
 
    addKey(key: PublicKeyType, weight = 1): void {
        const exists = this.required_auth.keys.find((k: KeyWeight) =>
            PublicKey.from(key).equals(k.key)
        )
        if (exists) {
            throw new Error(
                `The provided key (${String(key)}) already exists on the "${
                    this.perm_name
                }" permission.`
            )
        }
        this.required_auth.keys.push(
            KeyWeight.from({
                key: key,
                weight: weight,
            })
        )
        // Always sort authorities, required by antelopeio/leap
        this.required_auth.sort()
    }
 
    removeKey(key: PublicKeyType): void {
        const index = this.required_auth.keys.findIndex((k: KeyWeight) =>
            PublicKey.from(key).equals(k.key)
        )
        if (index === -1) {
            throw new Error(
                `The provided key (${String(key)}) does not exist on the "${
                    this.perm_name
                }" permission.`
            )
        }
        this.required_auth.keys.splice(index, 1)
    }
 
    addAccount(permissionLevel: PermissionLevelType | string, weight = 1): void {
        const exists = this.required_auth.accounts.find((k: PermissionLevelWeight) =>
            PermissionLevel.from(permissionLevel).equals(k.permission)
        )
        if (exists) {
            throw new Error(
                `The provided account (${String(
                    PermissionLevel.from(permissionLevel)
                )}) already exists on the "${this.perm_name}" permission.`
            )
        }
        this.required_auth.accounts.push(
            PermissionLevelWeight.from({
                permission: PermissionLevel.from(permissionLevel),
                weight: weight,
            })
        )
        // Always sort authorities, required by antelopeio/leap
        this.required_auth.sort()
    }
 
    removeAccount(permissionLevel: PermissionLevelType): void {
        const index = this.required_auth.accounts.findIndex((a: PermissionLevelWeight) =>
            PermissionLevel.from(permissionLevel).equals(a.permission)
        )
        if (index === -1) {
            throw new Error(
                `The provided permission (${String(permissionLevel)}) does not exist on the "${
                    this.perm_name
                }" permission.`
            )
        }
        this.required_auth.accounts.splice(index, 1)
    }
 
    addWait(wait: WaitWeightType): void {
        this.required_auth.waits.push(WaitWeight.from(wait))
        // Always sort authorities, required by antelopeio/leap
        this.required_auth.sort()
    }
 
    removeWait(wait: WaitWeightType): void {
        this.required_auth.waits = this.required_auth.waits.filter(
            (w: WaitWeight) => !WaitWeight.from(wait).equals(w)
        )
    }
}