bytes.ts 952 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { getBytesCopy, hexlify } from "../../utils/index.js";
  2. import { Coder } from "./abstract-coder.js";
  3. import type { Reader, Writer } from "./abstract-coder.js";
  4. /**
  5. * @_ignore
  6. */
  7. export class DynamicBytesCoder extends Coder {
  8. constructor(type: string, localName: string) {
  9. super(type, type, localName, true);
  10. }
  11. defaultValue(): string {
  12. return "0x";
  13. }
  14. encode(writer: Writer, value: any): number {
  15. value = getBytesCopy(value);
  16. let length = writer.writeValue(value.length);
  17. length += writer.writeBytes(value);
  18. return length;
  19. }
  20. decode(reader: Reader): any {
  21. return reader.readBytes(reader.readIndex(), true);
  22. }
  23. }
  24. /**
  25. * @_ignore
  26. */
  27. export class BytesCoder extends DynamicBytesCoder {
  28. constructor(localName: string) {
  29. super("bytes", localName);
  30. }
  31. decode(reader: Reader): any {
  32. return hexlify(super.decode(reader));
  33. }
  34. }