Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
feat: ✨ add typing to ethereum signers
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseTheRobot committed Sep 22, 2023
1 parent 4c90af5 commit d3755bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
20 changes: 18 additions & 2 deletions src/signing/chains/InjectedTypedEthereumSigner.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
_signTypedData(domain: TypedDataDomain, types: Record<string, TypedDataField[]>, value: Record<string, any>): Promise<string>;
}

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<void> {
this.address = (await this.signer.getAddress()).toString().toLowerCase();
this.publicKey = Buffer.from(this.address); // pk *is* address
Expand All @@ -29,3 +44,4 @@ export default class InjectedTypedEthereumSigner extends InjectedEthereumSigner
return address.toLowerCase() === addr.toLowerCase();
}
}
export default InjectedTypedEthereumSigner;
4 changes: 2 additions & 2 deletions src/signing/chains/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
16 changes: 12 additions & 4 deletions src/signing/chains/injectedEthereumSigner.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
}
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();
}

Expand All @@ -39,3 +46,4 @@ export default class InjectedEthereumSigner implements Signer {
return verifyMessage(message, signature) === address;
}
}
export default InjectedEthereumSigner;

0 comments on commit d3755bd

Please sign in to comment.