All files / src/chain checksum.ts

91.66% Statements 33/36
33.33% Branches 1/3
100% Functions 15/15
91.66% Lines 33/36

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                        1x 1x           413x 217x   196x     196x           17x           5x           641x 641x         641x       8x 8x 8x             265x       19x       57x       32x         1x 1x 1x     409x       214x         1x 1x 1x     1x       13x         1x 1x 1x     3x       196x      
import {ripemd160} from '@noble/hashes/legacy.js'
import {sha256, sha512} from '@noble/hashes/sha2.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 {
        return new Checksum256(sha256(Bytes.from(data).array))
    }
}
 
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 {
        return new Checksum512(sha512(Bytes.from(data).array))
    }
}
 
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 {
        return new Checksum160(ripemd160(Bytes.from(data).array))
    }
}