All files / ethers.js/src.ts/hash authorization.ts

71.05% Statements 27/38
100% Branches 0/0
0% Functions 0/2
71.05% Lines 27/38

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 391x 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 { getAddress } from "../address/index.js";
import { keccak256 } from "../crypto/index.js";
import { recoverAddress } from "../transaction/index.js";
import {
    assertArgument, concat, encodeRlp, toBeArray
} from "../utils/index.js";
 
import type { Addressable } from "../address/index.js";
import type { SignatureLike } from "../crypto/index.js";
import type { BigNumberish, Numeric } from "../utils/index.js";
 
export interface AuthorizationRequest {
    address: string | Addressable;
    nonce?: Numeric;
    chainId?: BigNumberish;
}
 
/**
 *  Computes the [[link-eip-7702]] authorization digest to sign.
 */
export function hashAuthorization(auth: AuthorizationRequest): string {
    assertArgument(typeof(auth.address) === "string", "invalid address for hashAuthorization", "auth.address", auth);
    return keccak256(concat([
        "0x05", encodeRlp([
            (auth.chainId != null) ? toBeArray(auth.chainId): "0x",
            getAddress(auth.address),
            (auth.nonce != null) ? toBeArray(auth.nonce): "0x",
        ])
    ]));
}
 
/**
 *  Return the address of the private key that produced
 *  the signature %%sig%% during signing for %%message%%.
 */
export function verifyAuthorization(auth: AuthorizationRequest, sig: SignatureLike): string {
    return recoverAddress(hashAuthorization(auth), sig);
}