ethers.js/packages/transaction/src.ts/address.ts

16 lines
646 B
TypeScript
Raw Permalink Normal View History

2022-04-11 17:09:17 -04:00
import { getAddress } from "@ethersproject/address";
import { keccak256 } from "@ethersproject/crypto";
import { SigningKey } from "@ethersproject/signing-key";
2022-04-17 01:07:15 -04:00
import type { BytesLike } from "@ethersproject/logger";
import type { SignatureLike } from "@ethersproject/signing-key";
2022-04-11 17:09:17 -04:00
export function computeAddress(key: string): string {
const publicKey = SigningKey.computePublicKey(key, false);
return getAddress(keccak256("0x" + publicKey.substring(4)).substring(26));
}
2022-04-17 01:07:15 -04:00
export function recoverAddress(digest: BytesLike, signature: SignatureLike): string {
return computeAddress(SigningKey.recoverPublicKey(digest, signature));
}