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

28 lines
674 B
TypeScript
Raw Permalink Normal View History

2019-05-14 18:25:46 -04: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);
}
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));
}
}