All files / ethers.js/src.ts/_tests test-address.ts

100% Statements 132/132
100% Branches 30/30
100% Functions 0/0
100% Lines 132/132

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 259x 259x 259x 259x 259x 259x 259x 259x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 259x 259x 259x 259x 259x 259x 1x 1x 1x 1x 1x 16x 160x 160x 160x 160x 16x 1x 1x 1x 1x 1x 16x 128x 128x 128x 128x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import assert from "assert";
 
import { loadTests } from "./utils.js";
 
import type {
    TestCaseAccount,
    TestCaseCreate,
    TestCaseCreate2
} from "./types.js";
 
import {
    getAddress, getIcapAddress,
    getCreateAddress, getCreate2Address
} from "../index.js";
 
 
describe("computes checksum address", function() {
    const tests = loadTests<TestCaseAccount>("accounts");
    for (const test of tests) {
        it(`computes the checksum address: ${ test.name }`, function() {
            assert.equal(getAddress(test.address), test.address);
            assert.equal(getAddress(test.icap), test.address);
            assert.equal(getAddress(test.address.substring(2)), test.address);
            assert.equal(getAddress(test.address.toLowerCase()), test.address);
            assert.equal(getAddress("0x" + test.address.substring(2).toUpperCase()), test.address);
        });
    }
 
    const invalidAddresses: Array<{ name: string, value: any }> = [
        { name: "null", value: null },
        { name: "number", value: 1234 },
        { name: "emtpy bytes", value: "0x" },
        { name: "too short", value: "0x8ba1f109551bd432803012645ac136ddd64dba" },
        { name: "too long", value: "0x8ba1f109551bd432803012645ac136ddd64dba7200" },
    ];
 
    invalidAddresses.forEach(({ name, value }) => {
        it(`correctly fails on invalid address: ${ name }`, function() {
            assert.throws(function() {
                getAddress(value);
            }, function(error: any) {
                return (error.code === "INVALID_ARGUMENT" &&
                    error.message.match(/^invalid address/) &&
                    error.argument === "address" &&
                    error.value === value);
            });
        });
    });
 
    it("correctly fails on invalid checksum", function() {
        const value = "0x8ba1f109551bD432803012645Ac136ddd64DBa72"
        assert.throws(function() {
            getAddress(value);
        }, function(error: any) {
            return (error.code === "INVALID_ARGUMENT" &&
                error.message.match(/^bad address checksum/) &&
                error.argument === "address" &&
                error.value === value);
        });
    });
 
    it("correctly fails on invalid IBAN checksum", function() {
        const value = "XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37";
        assert.throws(function() {
            getAddress(value);
        }, function(error: any) {
            return (error.code === "INVALID_ARGUMENT" &&
                error.message.match(/^bad icap checksum/) &&
                error.argument === "address" &&
                error.value === value);
        });
    });
});
 
describe("computes ICAP address", function() {
    const tests = loadTests<TestCaseAccount>("accounts");
    for (const test of tests) {
        it(`computes the ICAP address: ${ test.name }`, function() {
            assert.equal(getIcapAddress(test.address), test.icap);
            assert.equal(getAddress(test.address.toLowerCase()), test.address);
            assert.equal(getAddress("0x" + test.address.substring(2).toUpperCase()), test.address);
        });
    }
});
 
describe("computes create address", function() {
    const tests = loadTests<TestCaseCreate>("create");
    for (const { sender, creates } of tests) {
        for (const { name, nonce, address } of creates) {
            it(`computes the create address: ${ name }`, function() {
                assert.equal(getCreateAddress({ from: sender, nonce }), address);
            });
        }
    }
});
 
describe("computes create2 address", function() {
    const tests = loadTests<TestCaseCreate2>("create2");
    for (const { sender, creates } of tests) {
        for (const { name, salt, initCodeHash, address } of creates) {
            it(`computes the create2 address: ${ name }`, function() {
                assert.equal(getCreate2Address(sender, salt, initCodeHash), address);
            });
        }
    }
 
    const sender = "0x8ba1f109551bD432803012645Ac136ddd64DBA72";
    const salt = "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8";
    const initCodeHash = "0x8452c9b9140222b08593a26daa782707297be9f7b3e8281d7b4974769f19afd0";
 
    it("correctly fails on invalid salt", function() {
        const badSalt = "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36dea";
        assert.throws(function() {
            getCreate2Address(sender, badSalt, initCodeHash);
        }, function(error: any) {
            return (error.code === "INVALID_ARGUMENT" &&
                error.argument === "salt" &&
                error.value === badSalt);
        });
    });
 
    it("correctly fails on invalid initCodeHash", function() {
        const badInitCodeHash = "0x8452c9b9140222b08593a26daa782707297be9f7b3e8281d7b4974769f19af";
        assert.throws(function() {
            getCreate2Address(sender, salt, badInitCodeHash);
        }, function(error: any) {
            return (error.code === "INVALID_ARGUMENT" &&
                error.argument === "initCodeHash" &&
                error.value === badInitCodeHash);
        });
    });
});