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

35 lines
845 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);
}
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());
}
}
export class BytesCoder extends DynamicBytesCoder {
constructor(localName: string) {
super("bytes", localName);
}
decode(reader: Reader): any {
return reader.coerce(this.name, hexlify(super.decode(reader)));
}
}