All files / src/chain permission-level.ts

91.66% Statements 11/12
66.66% Branches 6/9
100% Functions 3/3
91.66% Lines 11/12

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            2x 1x 1x       125x 2x 2x         2x   125x         8x 8x       2x      
import {Name, NameType, Struct} from '../'
 
export type PermissionLevelType = PermissionLevel | {actor: NameType; permission: NameType}
 
/** Antelope/EOSIO Permission Level, a.k.a "auth". */
@Struct.type('permission_level')
export class PermissionLevel extends Struct {
    @Struct.field('name') actor!: Name
    @Struct.field('name') permission!: Name
 
    /** Create new permission level from representing types. Can be expressed as a string in the format `<actor>@<permission>`. */
    static from(value: PermissionLevelType | string): PermissionLevel {
        if (typeof value === 'string') {
            const parts = value.split('@')
            Iif (parts.length !== 2 && parts[0].length > 0 && parts[1].length > 0) {
                throw new Error(
                    'Invalid permission level string, should be in the format <actor>@<permission>'
                )
            }
            value = {actor: parts[0], permission: parts[1]}
        }
        return super.from(value) as PermissionLevel
    }
 
    /** Return true if this permission level equals other. */
    equals(other: PermissionLevelType | string) {
        const otherPerm = PermissionLevel.from(other)
        return this.actor.equals(otherPerm.actor) && this.permission.equals(otherPerm.permission)
    }
 
    toString() {
        return `${this.actor}@${this.permission}`
    }
}