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

32 lines
771 B
TypeScript
Raw Normal View History

2019-05-15 01:25:46 +03:00
"use strict";
import { getAddress } from "@ethersproject/address";
import { hexZeroPad } from "@ethersproject/bytes";
import { Coder, Reader, Writer } from "./abstract-coder";
export class AddressCoder extends Coder {
constructor(localName: string) {
super("address", "address", localName, false);
}
defaultValue(): string {
return "0x0000000000000000000000000000000000000000";
}
2019-05-15 01:25:46 +03:00
encode(writer: Writer, value: string): number {
try {
getAddress(value);
} catch (error) {
this._throwError(error.message, value);
}
return writer.writeValue(value);
}
decode(reader: Reader): any {
return getAddress(hexZeroPad(reader.readValue().toHexString(), 20));
}
}