ethers.js/packages/abi/src.ts/coders/bytes.ts

39 lines
908 B
TypeScript
Raw Normal View History

2019-05-15 01:25:46 +03: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);
}
defaultValue(): string {
return "0x";
}
2019-05-15 01:25:46 +03: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 {
return reader.readBytes(reader.readValue().toNumber(), true);
2019-05-15 01:25:46 +03: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)));
}
}