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

100% Statements 113/113
100% Branches 27/27
100% Functions 0/0
100% Lines 113/113

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 1141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 259x 259x 259x 259x 259x 1x 1x 1x 1x 1x 1x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 1x 1x 3200x 1664x 1664x 1664x 1664x 1664x 1664x 1664x 1x 1x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 1x 1x 3200x 3200x 3200x 3200x 3200x 3200x 3200x 1x 1x 1x 1x 1x 1x 1x 1x 130x 130x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x  
import assert from "assert";
 
import { loadTests } from "./utils.js";
 
import type {
    TestCaseAccount, TestCaseTypedData, TestCaseTransaction
} from "./types.js";
 
 
import { hexlify, randomBytes, Wallet } from "../index.js";
 
import type { HDNodeWallet } from "../index.js";
 
 
describe("Test Private Key Wallet", function() {
    const tests = loadTests<TestCaseAccount>("accounts");
 
    tests.forEach(({ name, privateKey, address }) => {
        it(`creates wallet: ${ name }`, function() {
            const wallet = new Wallet(privateKey);
            assert.equal(wallet.privateKey, privateKey);
            assert.equal(wallet.address, address);
        });
    });
});
 
describe("Test Transaction Signing", function() {
    const tests = loadTests<TestCaseTransaction>("transactions");
    for (const test of tests) {
        it(`tests signing a legacy transaction: ${ test.name }`, async function() {
            const wallet = new Wallet(test.privateKey);
            const txData = Object.assign({ }, test.transaction, { type: 0, accessList: undefined, maxFeePerGas: undefined, maxPriorityFeePerGas: undefined });
 
            // Use the testcase sans the chainId for a legacy test
            if (txData.chainId != null && parseInt(txData.chainId) != 0) { txData.chainId = "0x00"; }
 
            const signed = await wallet.signTransaction(txData);
            assert.equal(signed, test.signedLegacy, "signedLegacy");
        });
    }
 
    for (const test of tests) {
        if (!test.signedEip155) { continue; }
        it(`tests signing an EIP-155 transaction: ${ test.name }`, async function() {
            const wallet = new Wallet(test.privateKey);
            const txData = Object.assign({ }, test.transaction, { type: 0, accessList: undefined, maxFeePerGas: undefined, maxPriorityFeePerGas: undefined });
            const signed = await wallet.signTransaction(txData);
            assert.equal(signed, test.signedEip155, "signedEip155");
        });
    }
 
    for (const test of tests) {
        it(`tests signing a Berlin transaction: ${ test.name }`, async function() {
            const wallet = new Wallet(test.privateKey);
            const txData = Object.assign({ }, test.transaction, { type: 1, maxFeePerGas: undefined, maxPriorityFeePerGas: undefined });
            const signed = await wallet.signTransaction(txData);
            assert.equal(signed, test.signedBerlin, "signedBerlin");
        });
    }
 
    for (const test of tests) {
        it(`tests signing a London transaction: ${ test.name }`, async function() {
            const wallet = new Wallet(test.privateKey);
            const txData = Object.assign({ }, test.transaction, { type: 2 });
            const signed = await wallet.signTransaction(txData);
            assert.equal(signed, test.signedLondon, "signedLondon");
        });
    }
});
 
describe("Test Message Signing (EIP-191)", function() {
});
 
describe("Test Typed-Data Signing (EIP-712)", function() {
    const tests = loadTests<TestCaseTypedData>("typed-data");
    for (const test of tests) {
        const { privateKey, signature } = test;
        if (privateKey == null || signature == null) { continue; }
        it(`tests signing typed-data: ${ test.name }`, async function() {
            const wallet = new Wallet(privateKey);
            const sig = await wallet.signTypedData(test.domain, test.types, test.data);
            assert.equal(sig, signature, "signature");
        });
    }
});
 
describe("Test Wallet Encryption", function() {
    const password = "foobar";
 
    // Loop:
    //  1 : random wallet (uses HDNodeWallet under the hood)
    //  2 : Wallet using private key (uses Wallet explicitly)
 
    for (let i = 0; i < 2; i++) {
        let wallet: Wallet | HDNodeWallet = Wallet.createRandom();
 
        it("encrypts a random wallet: sync", function() {
            this.timeout(30000);
            const json = wallet.encryptSync(password);
            const decrypted = Wallet.fromEncryptedJsonSync(json, password);
            assert.equal(decrypted.address, wallet.address, "address");
        });
 
        it("encrypts a random wallet: async", async function() {
            this.timeout(30000);
            const json = await wallet.encrypt(password);
            const decrypted = await Wallet.fromEncryptedJson(json, password);
            assert.equal(decrypted.address, wallet.address, "address");
        });
 
        wallet = new Wallet(hexlify(randomBytes(32)));
    }
});