From d3755bd1a9153a2f33e4552ff3335fd131705afd Mon Sep 17 00:00:00 2001 From: JesseTheRobot Date: Fri, 22 Sep 2023 21:04:55 +0000 Subject: [PATCH] feat: :sparkles: add typing to ethereum signers --- .../chains/InjectedTypedEthereumSigner.ts | 20 +++++++++++++++++-- src/signing/chains/index.ts | 4 ++-- src/signing/chains/injectedEthereumSigner.ts | 16 +++++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/signing/chains/InjectedTypedEthereumSigner.ts b/src/signing/chains/InjectedTypedEthereumSigner.ts index ef47328..7453cb4 100644 --- a/src/signing/chains/InjectedTypedEthereumSigner.ts +++ b/src/signing/chains/InjectedTypedEthereumSigner.ts @@ -1,14 +1,29 @@ import { SignatureConfig, SIG_CONFIG } from "../../constants"; import { verifyTypedData } from "@ethersproject/wallet"; -import InjectedEthereumSigner from "./injectedEthereumSigner"; import { domain, types } from "./TypedEthereumSigner"; +import type { TypedDataDomain, TypedDataField } from "@ethersproject/abstract-signer"; +import type { Signer } from "../index"; -export default class InjectedTypedEthereumSigner extends InjectedEthereumSigner { +export interface InjectedTypedEthereumSignerMinimalSigner { + getAddress: () => Promise; + _signTypedData(domain: TypedDataDomain, types: Record, value: Record): Promise; +} + +export interface InjectedTypedEthereumSignerMinimalProvider { + getSigner(): InjectedTypedEthereumSignerMinimalSigner; +} + +export class InjectedTypedEthereumSigner implements Signer { readonly ownerLength: number = SIG_CONFIG[SignatureConfig.TYPEDETHEREUM].pubLength; readonly signatureLength: number = SIG_CONFIG[SignatureConfig.TYPEDETHEREUM].sigLength; readonly signatureType: SignatureConfig = SignatureConfig.TYPEDETHEREUM; private address: string; + protected signer: InjectedTypedEthereumSignerMinimalSigner; + public publicKey: Buffer; + constructor(provider: InjectedTypedEthereumSignerMinimalProvider) { + this.signer = provider.getSigner(); + } async ready(): Promise { this.address = (await this.signer.getAddress()).toString().toLowerCase(); this.publicKey = Buffer.from(this.address); // pk *is* address @@ -29,3 +44,4 @@ export default class InjectedTypedEthereumSigner extends InjectedEthereumSigner return address.toLowerCase() === addr.toLowerCase(); } } +export default InjectedTypedEthereumSigner; diff --git a/src/signing/chains/index.ts b/src/signing/chains/index.ts index b8dc254..dd396ba 100644 --- a/src/signing/chains/index.ts +++ b/src/signing/chains/index.ts @@ -1,6 +1,6 @@ export { default as ArweaveSigner } from "./ArweaveSigner"; export { default as InjectedSolanaSigner } from "./injectedSolanaSigner"; -export { default as InjectedEthereumSigner } from "./injectedEthereumSigner"; +export * from "./injectedEthereumSigner"; export { default as SolanaSigner } from "./SolanaSigner"; export { default as PolygonSigner } from "./PolygonSigner"; export { default as NearSigner } from "./NearSigner"; @@ -12,5 +12,5 @@ export { default as AptosSigner } from "./AptosSigner"; export { default as InjectedAptosSigner } from "./InjectedAptosSigner"; export { default as MultiSignatureAptosSigner } from "./multiSignatureAptos"; export { default as TypedEthereumSigner } from "./TypedEthereumSigner"; -export { default as InjectedTypedEthereumSigner } from "./InjectedTypedEthereumSigner"; +export * from "./InjectedTypedEthereumSigner"; export { default as ArconnectSigner } from "./arconnectSigner"; diff --git a/src/signing/chains/injectedEthereumSigner.ts b/src/signing/chains/injectedEthereumSigner.ts index 97d9ce5..5218a9d 100644 --- a/src/signing/chains/injectedEthereumSigner.ts +++ b/src/signing/chains/injectedEthereumSigner.ts @@ -1,20 +1,27 @@ -import type { Web3Provider, JsonRpcSigner } from "@ethersproject/providers"; import { hashMessage } from "@ethersproject/hash"; import { recoverPublicKey } from "@ethersproject/signing-key"; import type { Signer } from "../index"; import { SignatureConfig, SIG_CONFIG } from "../../constants"; +import type { Bytes } from "@ethersproject/bytes"; import { arrayify } from "@ethersproject/bytes"; import { computeAddress } from "@ethersproject/transactions"; import { verifyMessage } from "@ethersproject/wallet"; +export interface InjectedEthereumSignerMinimalSigner { + signMessage(message: string | Bytes): Promise; +} +export interface InjectedEthereumSignerMinimalProvider { + getSigner(): InjectedEthereumSignerMinimalSigner; +} -export default class InjectedEthereumSigner implements Signer { - protected signer: JsonRpcSigner; +export class InjectedEthereumSigner implements Signer { + // protected signer: JsonRpcSigner; + protected signer: InjectedEthereumSignerMinimalSigner; public publicKey: Buffer; readonly ownerLength: number = SIG_CONFIG[SignatureConfig.ETHEREUM].pubLength; readonly signatureLength: number = SIG_CONFIG[SignatureConfig.ETHEREUM].sigLength; readonly signatureType: SignatureConfig = SignatureConfig.ETHEREUM; - constructor(provider: Web3Provider) { + constructor(provider: InjectedEthereumSignerMinimalProvider) { this.signer = provider.getSigner(); } @@ -39,3 +46,4 @@ export default class InjectedEthereumSigner implements Signer { return verifyMessage(message, signature) === address; } } +export default InjectedEthereumSigner;