All files / ethers.js/src.ts/abi/coders fixed-bytes.ts

94.59% Statements 35/37
80% Branches 4/5
80% Functions 4/5
94.59% Lines 35/37

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 381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1928x 1928x 1928x 1928x 1928x 1928x 1928x 1928x 1928x     1928x 1928x 2123x 2123x 2123x 2123x 1928x 1928x 2031x 2031x 1928x  
 
import { defineProperties, getBytesCopy, hexlify } from "../../utils/index.js";
 
import { Typed } from "../typed.js";
import { Coder } from "./abstract-coder.js";
 
import type { BytesLike } from "../../utils/index.js";
 
import type { Reader, Writer } from "./abstract-coder.js";
 
 
/**
 *  @_ignore
 */
export class FixedBytesCoder extends Coder {
    readonly size!: number;
 
    constructor(size: number, localName: string) {
        let name = "bytes" + String(size);
        super(name, name, localName, false);
        defineProperties<FixedBytesCoder>(this, { size }, { size: "number" });
    }
 
    defaultValue(): string {
        return ("0x0000000000000000000000000000000000000000000000000000000000000000").substring(0, 2 + this.size * 2);
    }
 
    encode(writer: Writer, _value: BytesLike | Typed): number {
        let data = getBytesCopy(Typed.dereference(_value, this.type));
        if (data.length !== this.size) { this._throwError("incorrect data length", _value); }
        return writer.writeBytes(data);
    }
 
    decode(reader: Reader): any {
        return hexlify(reader.readBytes(this.size));
    }
}