import { ethers } from 'ethers' ; import { SignatureProvider , SignatureVerifier } from '../types' ; import { hexStrToBuf } from './misc' ; export class DefaultSignatureVerifier implements SignatureVerifier { private static _instance : SignatureVerifier ; public static instance ( ) { if ( ! DefaultSignatureVerifier . _instance ) { DefaultSignatureVerifier . _instance = new DefaultSignatureVerifier ( ) ; } return DefaultSignatureVerifier . _instance ; } public verifyMessage ( message , signature ) { const messageAsBuf = hexStrToBuf ( message ) ; const messageHash = ethers . utils . keccak256 ( messageAsBuf ) ; return ethers . utils . verifyMessage ( hexStrToBuf ( messageHash ) , signature ) ; } } export class DefaultSignatureProvider implements SignatureProvider { public constructor ( private readonly wallet = ethers . Wallet . createRandom ( ) ) { } public async sign ( message ) { const messageAsBuf = hexStrToBuf ( message ) ; const messageHash = ethers . utils . keccak256 ( messageAsBuf ) ; return this . wallet . signMessage ( hexStrToBuf ( messageHash ) ) ; } public async getAddress ( ) { return this . wallet . getAddress ( ) ; } } export class IdentitySigner implements SignatureProvider { public constructor ( private address ) { } public async getAddress ( ) { return this . address ; } public async sign ( message ) { return this . address ; } } export class IdentityVerifier implements SignatureVerifier { private static _instance : SignatureVerifier ; public static instance ( ) { if ( ! IdentityVerifier . _instance ) { IdentityVerifier . _instance = new IdentityVerifier ( ) ; } return IdentityVerifier . _instance ; } public verifyMessage ( message , signature ) { return signature ; } } export class ChecksumAgnosticIdentityVerifier implements SignatureVerifier { private static _instance : SignatureVerifier ; public static instance ( ) { if ( ! ChecksumAgnosticIdentityVerifier . _instance ) { ChecksumAgnosticIdentityVerifier . _instance = new IdentityVerifier ( ) ; } return ChecksumAgnosticIdentityVerifier . _instance ; } public verifyMessage ( message , signature ) { return ethers . utils . getAddress ( signature ) ; } }