import { Address, HexString, IChannelSigner, PrivateKey, PublicKey, PublicIdentifier, SignatureString, UrlString, } from "@connext/types"; import { Wallet, Signer, providers } from "ethers"; import { decrypt, encrypt, getAddressFromPublicKey, getRandomPrivateKey, getPublicKeyFromPrivateKey, signChannelMessage, } from "./crypto"; import { getPublicIdentifierFromPublicKey } from "./identifiers"; import { getChainId } from "./chainId"; export const getRandomChannelSigner = (provider?: UrlString | providers.Provider) => new ChannelSigner(getRandomPrivateKey(), provider); export class ChannelSigner extends Signer implements IChannelSigner { public address: Address; public publicIdentifier: PublicIdentifier; public publicKey: PublicKey; public provider?: providers.Provider; // NOTE: without this property, the Signer.isSigner // function will not return true, even though this class // extends / implements the signer interface. See: // https://github.com/ethers-io/ethers.js/issues/779 private readonly _ethersType = "Signer"; constructor(private readonly privateKey: PrivateKey, provider?: UrlString | providers.Provider) { super(); this.privateKey = privateKey; this.publicKey = getPublicKeyFromPrivateKey(privateKey); this.address = getAddressFromPublicKey(this.publicKey); this.publicIdentifier = getPublicIdentifierFromPublicKey(this.publicKey); this.connectProvider(provider); } public async getAddress(): Promise
{ return this.address; } public encrypt = encrypt; public async connectProvider(provider?: UrlString | providers.Provider): Promise