All files / src/chain name.ts

94.33% Statements 50/53
95.83% Branches 23/24
83.33% Functions 10/12
93.87% Lines 46/49

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                        1x     1x                             435x 45x 390x 384x 6x 6x             395x               785x         45x         80x       539x         52x           3259x 3114x   145x 74x   71x   384x 384x 384x 3259x 3259x 2x   3259x 16295x 16289x 16289x       384x       80x 80x 80x 1040x 1040x 5200x 5120x 5120x     1040x 608x 432x 14x   418x     80x 398x   80x    
import {ABISerializableObject} from '../serializer/serializable'
import {ABIEncoder} from '../serializer/encoder'
import {ABIDecoder} from '../serializer/decoder'
import {isInstanceOf} from '../utils'
 
import {UInt64} from '../'
 
/** Type representing a name. */
export type NameType = Name | UInt64 | string
 
/** Antelope/EOSIO Name */
export class Name implements ABISerializableObject {
    static abiName = 'name'
 
    /** Regex pattern matching a Antelope/EOSIO name, case-sensitive. */
    static pattern = /^[a-z1-5.]{0,13}$/
 
    /** The numeric representation of the name. */
    value: UInt64
 
    /**
     * The raw representation of the name.
     * @deprecated Use value instead.
     */
    get rawValue(): UInt64 {
        return this.value
    }
 
    /** Create a new Name instance from any of its representing types. */
    static from(value: NameType): Name {
        if (isInstanceOf(value, Name)) {
            return value
        } else if (typeof value === 'string') {
            return new Name(stringToName(value))
        } else Eif (isInstanceOf(value, UInt64)) {
            return new Name(value)
        } else {
            throw new Error('Invalid name')
        }
    }
 
    static fromABI(decoder: ABIDecoder) {
        return new Name(UInt64.fromABI(decoder))
    }
 
    static abiDefault() {
        return new this(UInt64.from(0))
    }
 
    constructor(value: UInt64) {
        this.value = value
    }
 
    /** Return true if this name is equal to passed name. */
    equals(other: NameType) {
        return this.value.equals(Name.from(other).value)
    }
 
    /** Return string representation of this name. */
    toString() {
        return nameToString(this.value)
    }
 
    toABI(encoder: ABIEncoder) {
        this.value.toABI(encoder)
    }
 
    /** @internal */
    toJSON() {
        return this.toString()
    }
}
 
function stringToName(s: string): UInt64 {
    function charToSymbol(c: number) {
        if (c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) {
            return c - 'a'.charCodeAt(0) + 6
        }
        if (c >= '1'.charCodeAt(0) && c <= '5'.charCodeAt(0)) {
            return c - '1'.charCodeAt(0) + 1
        }
        return 0
    }
    const a = new Uint8Array(8)
    let bit = 63
    for (let i = 0; i < s.length; ++i) {
        let c = charToSymbol(s.charCodeAt(i))
        if (bit < 5) {
            c = c << 1
        }
        for (let j = 4; j >= 0; --j) {
            if (bit >= 0) {
                a[Math.floor(bit / 8)] |= ((c >> j) & 1) << bit % 8
                --bit
            }
        }
    }
    return UInt64.from(a)
}
 
function nameToString(n: UInt64): string {
    const a = n.value.toArray('le', 8)
    let result = ''
    for (let bit = 63; bit >= 0; ) {
        let c = 0
        for (let i = 0; i < 5; ++i) {
            if (bit >= 0) {
                c = (c << 1) | ((a[Math.floor(bit / 8)] >> bit % 8) & 1)
                --bit
            }
        }
        if (c >= 6) {
            result += String.fromCharCode(c + 'a'.charCodeAt(0) - 6)
        } else if (c >= 1) {
            result += String.fromCharCode(c + '1'.charCodeAt(0) - 1)
        } else {
            result += '.'
        }
    }
    while (result.endsWith('.')) {
        result = result.substr(0, result.length - 1)
    }
    return result
}