2022-09-05 23:14:43 +03:00
|
|
|
|
2022-09-09 06:21:08 +03:00
|
|
|
import { defineProperties, getBytesCopy, hexlify } from "../../utils/index.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
2022-11-28 05:54:49 +03:00
|
|
|
/**
|
|
|
|
* @_ignore
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
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 {
|
2022-09-09 06:21:08 +03:00
|
|
|
let data = getBytesCopy(Typed.dereference(_value, this.type));
|
2022-09-05 23:14:43 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|