2022-04-11 17:09:17 -04:00
|
|
|
import { arrayify, hexlify } from "@ethersproject/bytes";
|
|
|
|
|
|
|
|
import { Coder } from "./abstract-coder.js";
|
|
|
|
|
|
|
|
import type { Reader, Writer } from "./abstract-coder.js";
|
|
|
|
|
|
|
|
|
|
|
|
export class DynamicBytesCoder extends Coder {
|
|
|
|
constructor(type: string, localName: string) {
|
|
|
|
super(type, type, localName, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultValue(): string {
|
|
|
|
return "0x";
|
|
|
|
}
|
|
|
|
|
|
|
|
encode(writer: Writer, value: any): number {
|
|
|
|
value = arrayify(value);
|
|
|
|
let length = writer.writeValue(value.length);
|
|
|
|
length += writer.writeBytes(value);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode(reader: Reader): any {
|
|
|
|
return reader.readBytes(reader.readIndex(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BytesCoder extends DynamicBytesCoder {
|
|
|
|
constructor(localName: string) {
|
|
|
|
super("bytes", localName);
|
|
|
|
}
|
|
|
|
|
|
|
|
decode(reader: Reader): any {
|
2022-04-19 03:33:55 -04:00
|
|
|
return hexlify(super.decode(reader));
|
2022-04-11 17:09:17 -04:00
|
|
|
}
|
|
|
|
}
|