2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { arrayify, hexlify } from "@ethersproject/bytes";
|
|
|
|
|
|
|
|
import { Coder, Reader, Writer } from "./abstract-coder";
|
|
|
|
|
|
|
|
export class DynamicBytesCoder extends Coder {
|
|
|
|
constructor(type: string, localName: string) {
|
|
|
|
super(type, type, localName, true);
|
|
|
|
}
|
|
|
|
|
2020-11-23 00:59:44 -05:00
|
|
|
defaultValue(): string {
|
|
|
|
return "0x";
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
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 {
|
2020-09-07 19:35:37 -04:00
|
|
|
return reader.readBytes(reader.readValue().toNumber(), true);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BytesCoder extends DynamicBytesCoder {
|
|
|
|
constructor(localName: string) {
|
|
|
|
super("bytes", localName);
|
|
|
|
}
|
|
|
|
|
|
|
|
decode(reader: Reader): any {
|
|
|
|
return reader.coerce(this.name, hexlify(super.decode(reader)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|