All files / src/chain checksum.ts

91.42% Statements 32/35
66.66% Branches 4/6
100% Functions 15/15
91.42% Lines 32/35

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                        1x           171x 30x   141x     141x           15x           5x           258x 258x         258x       8x 8x 8x             114x       17x       44x       30x           1x 1x     167x       35x 35x           1x 1x     1x       3x 3x           1x 1x     3x       59x 59x      
import {ripemd160, sha256, sha512} from 'hash.js'
 
import {ABIDecoder} from '../serializer/decoder'
import {ABIEncoder} from '../serializer/encoder'
import {ABISerializableObject} from '../serializer/serializable'
import {arrayEquals, arrayToHex, isInstanceOf} from '../utils'
 
import {Bytes, BytesType} from '../'
 
type ChecksumType = Checksum | BytesType
 
class Checksum implements ABISerializableObject {
    static abiName = '__checksum'
    static byteSize: number
 
    static from<T extends typeof Checksum>(this: T, value: ChecksumType): InstanceType<T>
    static from(value: ChecksumType): unknown
    static from(value: ChecksumType) {
        if (isInstanceOf(value, this)) {
            return value
        }
        Iif (isInstanceOf(value, Checksum)) {
            return new this(value.array)
        }
        return new this(Bytes.from(value).array)
    }
 
    static fromABI<T extends typeof Checksum>(this: T, decoder: ABIDecoder): InstanceType<T>
    static fromABI(decoder: ABIDecoder): unknown
    static fromABI(decoder: ABIDecoder) {
        return new this(decoder.readArray(this.byteSize))
    }
 
    static abiDefault<T extends typeof Checksum>(this: T): InstanceType<T>
    static abiDefault(): unknown
    static abiDefault() {
        return new this(new Uint8Array(this.byteSize))
    }
 
    readonly array: Uint8Array
 
    constructor(array: Uint8Array) {
        const byteSize = (this.constructor as typeof Checksum).byteSize
        Iif (array.byteLength !== byteSize) {
            throw new Error(
                `Checksum size mismatch, expected ${byteSize} bytes got ${array.byteLength}`
            )
        }
        this.array = array
    }
 
    equals(other: Checksum160Type | Checksum256Type | Checksum512Type): boolean {
        const self = this.constructor as typeof Checksum
        try {
            return arrayEquals(this.array, self.from(other).array)
        } catch {
            return false
        }
    }
 
    get hexString(): string {
        return arrayToHex(this.array)
    }
 
    toABI(encoder: ABIEncoder) {
        encoder.writeArray(this.array)
    }
 
    toString() {
        return this.hexString
    }
 
    toJSON() {
        return this.toString()
    }
}
 
export type Checksum256Type = Checksum256 | BytesType
export class Checksum256 extends Checksum {
    static abiName = 'checksum256'
    static byteSize = 32
 
    static from(value: Checksum256Type) {
        return super.from(value) as Checksum256
    }
 
    static hash(data: BytesType): Checksum256 {
        const digest = new Uint8Array(sha256().update(Bytes.from(data).array).digest())
        return new Checksum256(digest)
    }
}
 
export type Checksum512Type = Checksum512 | BytesType
export class Checksum512 extends Checksum {
    static abiName = 'checksum512'
    static byteSize = 64
 
    static from(value: Checksum512Type) {
        return super.from(value) as Checksum512
    }
 
    static hash(data: BytesType): Checksum512 {
        const digest = new Uint8Array(sha512().update(Bytes.from(data).array).digest())
        return new Checksum512(digest)
    }
}
 
export type Checksum160Type = Checksum160 | BytesType
export class Checksum160 extends Checksum {
    static abiName = 'checksum160'
    static byteSize = 20
 
    static from(value: Checksum160Type) {
        return super.from(value) as Checksum160
    }
 
    static hash(data: BytesType): Checksum160 {
        const digest = new Uint8Array(ripemd160().update(Bytes.from(data).array).digest())
        return new Checksum160(digest)
    }
}