All files / src/chain name.ts

94.54% Statements 52/55
94.73% Branches 18/19
84.61% Functions 11/13
94.11% Lines 48/51

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                      1x 1x     1x                             7926x 84x 7842x 7836x 6x 6x             2829x               10670x         57x         7x         33682x       10030x         2429x           57862x 55212x   2650x 126x   2524x   7836x 7836x 7836x 57862x 57862x 2x   57862x 289310x 289304x 289304x       7836x       33682x 33682x 33682x 437866x 437866x 2189330x 2155648x 2155648x     437866x 201144x 236722x 14x   236708x     33682x 236673x   33682x    
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 if (isInstanceOf(value, UInt64)) {
            return new Name(value)
        } else {
            Ethrow 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)
    }
 
    /** Compare with another name by underlying uint64 value. */
    compare(other: NameType): number {
        return this.value.value.cmp(Name.from(other).value.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
}