ethers.js/src.ts/abi/coders/string.ts

30 lines
671 B
TypeScript
Raw Normal View History

2022-09-05 23:14:43 +03:00
import { toUtf8Bytes, toUtf8String } from "../../utils/utf8.js";
import { Typed } from "../typed.js";
import { DynamicBytesCoder } from "./bytes.js";
import type { Reader, Writer } from "./abstract-coder.js";
2022-11-28 05:54:49 +03:00
/**
* @_ignore
*/
2022-09-05 23:14:43 +03:00
export class StringCoder extends DynamicBytesCoder {
constructor(localName: string) {
super("string", localName);
}
defaultValue(): string {
return "";
}
encode(writer: Writer, _value: string | Typed): number {
return super.encode(writer, toUtf8Bytes(Typed.dereference(_value, "string")));
}
decode(reader: Reader): any {
return toUtf8String(super.decode(reader));
}
}