All files / ethers.js/src.ts/abi/coders address.ts

88.88% Statements 32/36
75% Branches 3/4
75% Functions 3/4
88.88% Lines 32/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1630x 1630x 1x 1x     1x 1x 1850x 1850x 1850x 1850x     1850x 1850x 1x 1x 1878x 1878x 1x  
import { getAddress } from "../../address/index.js";
import { toBeHex } from "../../utils/maths.js";
 
import { Typed } from "../typed.js";
import { Coder } from "./abstract-coder.js";
 
import type { Reader, Writer } from "./abstract-coder.js";
 
 
/**
 *  @_ignore
 */
export class AddressCoder extends Coder {
 
    constructor(localName: string) {
        super("address", "address", localName, false);
    }
 
    defaultValue(): string {
        return "0x0000000000000000000000000000000000000000";
    }
 
    encode(writer: Writer, _value: string | Typed): number {
        let value = Typed.dereference(_value, "string");
        try {
            value = getAddress(value);
        } catch (error: any) {
            return this._throwError(error.message, _value);
        }
        return writer.writeValue(value);
    }
 
    decode(reader: Reader): any {
        return getAddress(toBeHex(reader.readValue(), 20));
    }
}