2022-04-11 17:14:19 -04:00
var _ _classPrivateFieldGet = ( this && this . _ _classPrivateFieldGet ) || function ( receiver , state , kind , f ) {
if ( kind === "a" && ! f ) throw new TypeError ( "Private accessor was defined without a getter" ) ;
if ( typeof state === "function" ? receiver !== state || ! f : ! state . has ( receiver ) ) throw new TypeError ( "Cannot read private member from an object whose class did not declare it" ) ;
return kind === "m" ? f : kind === "a" ? f . call ( receiver ) : f ? f . value : state . get ( receiver ) ;
} ;
var _ _classPrivateFieldSet = ( this && this . _ _classPrivateFieldSet ) || function ( receiver , state , value , kind , f ) {
if ( kind === "m" ) throw new TypeError ( "Private method is not writable" ) ;
if ( kind === "a" && ! f ) throw new TypeError ( "Private accessor was defined without a setter" ) ;
if ( typeof state === "function" ? receiver !== state || ! f : ! state . has ( receiver ) ) throw new TypeError ( "Cannot write private member to an object whose class did not declare it" ) ;
return ( kind === "a" ? f . call ( receiver , value ) : f ? f . value = value : state . set ( receiver , value ) ) , value ;
} ;
var _AbstractSigner _instances , _AbstractSigner _checkProvider , _AbstractSigner _fromify , _VoidSigner _instances , _VoidSigner _throwUnsupported , _WrappedSigner _signer ;
import { defineProperties , resolveProperties } from "@ethersproject/properties" ;
2022-04-17 04:31:42 -04:00
import { Transaction } from "@ethersproject/transaction" ;
2022-04-11 17:14:19 -04:00
import { logger } from "./logger.js" ;
export class AbstractSigner {
constructor ( provider ) {
_AbstractSigner _instances . add ( this ) ;
defineProperties ( this , { provider : ( provider || null ) } ) ;
}
async getBalance ( blockTag ) {
return _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "getBalance" ) . getBalanceOf ( await this . getAddress ( ) , blockTag ) ;
}
async getTransactionCount ( blockTag ) {
return _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "getTransactionCount" ) . getTransactionCountOf ( await this . getAddress ( ) , blockTag ) ;
}
async estimateGas ( tx ) {
return _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "estimateGas" ) . estimateGas ( await _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _fromify ) . call ( this , tx ) ) ;
}
async call ( tx ) {
return _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "call" ) . call ( await _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _fromify ) . call ( this , tx ) ) ;
}
async populateTransaction ( tx ) {
const provider = _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "populateTransaction" ) ;
//let pop: Deferrable<TransactionRequest> = Object.assign({ }, tx);
let pop = Object . assign ( { } , tx ) ;
if ( pop . to != null ) {
pop . to = provider . resolveName ( pop . to ) . then ( ( to ) => {
if ( to == null ) {
return logger . throwArgumentError ( "transaction to ENS name not configured" , "tx.to" , pop . to ) ;
}
return to ;
} ) ;
}
if ( pop . from != null ) {
const from = pop . from ;
pop . from = Promise . all ( [
this . getAddress ( ) ,
this . resolveName ( from )
] ) . then ( ( [ address , from ] ) => {
if ( ! from || address . toLowerCase ( ) !== from . toLowerCase ( ) ) {
return logger . throwArgumentError ( "transaction from mismatch" , "tx.from" , from ) ;
}
return address ;
} ) ;
}
if ( pop . nonce == null ) {
pop . nonce = this . getTransactionCount ( ) ;
}
pop = resolveProperties ( pop ) ;
if ( pop . gasLimit == null ) {
pop . gasLimit = provider . estimateGas ( pop ) ;
}
pop = resolveProperties ( pop ) ;
//@TODO: Copy logic from AbstractSigner in v5
return await resolveProperties ( pop ) ;
}
async resolveName ( name ) {
const provider = _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "resolveName" ) ;
return await provider . resolveName ( name ) ;
}
async sendTransaction ( tx ) {
const provider = _ _classPrivateFieldGet ( this , _AbstractSigner _instances , "m" , _AbstractSigner _checkProvider ) . call ( this , "sendTransaction" ) ;
const txObj = Transaction . from ( await this . populateTransaction ( tx ) ) ;
return await provider . broadcastTransaction ( await this . signTransaction ( txObj ) ) ;
}
}
_AbstractSigner _instances = new WeakSet ( ) , _AbstractSigner _checkProvider = function _AbstractSigner _checkProvider ( operation ) {
if ( this . provider ) {
return this . provider ;
}
return logger . throwError ( "missing provider" , "UNSUPPORTED_OPERATION" , { operation } ) ;
} , _AbstractSigner _fromify = async function _AbstractSigner _fromify ( tx ) {
tx = Object . assign ( { } , tx ) ;
const actions = [ ] ;
if ( tx . from != null ) {
const from = tx . from ;
actions . push ( Promise . all ( [
this . getAddress ( ) ,
this . resolveName ( from )
] ) . then ( ( [ address , from ] ) => {
if ( ! from || address . toLowerCase ( ) !== from . toLowerCase ( ) ) {
logger . throwArgumentError ( "transaction from mismatch" , "tx.from" , from ) ;
}
tx . from = address ;
} ) ) ;
}
await Promise . all ( actions ) ;
return tx ;
} ;
export class VoidSigner extends AbstractSigner {
constructor ( address , provider ) {
super ( provider ) ;
_VoidSigner _instances . add ( this ) ;
defineProperties ( this , { address } ) ;
}
async getAddress ( ) { return this . address ; }
connect ( provider ) {
return new VoidSigner ( this . address , provider ) ;
}
async signTransaction ( tx ) {
_ _classPrivateFieldGet ( this , _VoidSigner _instances , "m" , _VoidSigner _throwUnsupported ) . call ( this , "transactions" , "signTransaction" ) ;
}
async signMessage ( message ) {
_ _classPrivateFieldGet ( this , _VoidSigner _instances , "m" , _VoidSigner _throwUnsupported ) . call ( this , "messages" , "signMessage" ) ;
}
async signTypedData ( domain , types , value ) {
_ _classPrivateFieldGet ( this , _VoidSigner _instances , "m" , _VoidSigner _throwUnsupported ) . call ( this , "typed-data" , "signTypedData" ) ;
}
}
_VoidSigner _instances = new WeakSet ( ) , _VoidSigner _throwUnsupported = function _VoidSigner _throwUnsupported ( suffix , operation ) {
return logger . throwError ( ` VoidSigner cannot sign ${ suffix } ` , "UNSUPPORTED_OPERATION" , {
operation
} ) ;
} ;
export class WrappedSigner extends AbstractSigner {
//get provider(): null | Provider { return this.#signer.provider; }
constructor ( signer ) {
super ( signer . provider ) ;
_WrappedSigner _signer . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _WrappedSigner _signer , signer , "f" ) ;
}
async getAddress ( ) {
return await _ _classPrivateFieldGet ( this , _WrappedSigner _signer , "f" ) . getAddress ( ) ;
}
connect ( provider ) {
return new WrappedSigner ( _ _classPrivateFieldGet ( this , _WrappedSigner _signer , "f" ) . connect ( provider ) ) ;
}
async signTransaction ( tx ) {
return await _ _classPrivateFieldGet ( this , _WrappedSigner _signer , "f" ) . signTransaction ( tx ) ;
}
async sendTransaction ( tx ) {
return await _ _classPrivateFieldGet ( this , _WrappedSigner _signer , "f" ) . sendTransaction ( tx ) ;
}
async signMessage ( message ) {
return await _ _classPrivateFieldGet ( this , _WrappedSigner _signer , "f" ) . signMessage ( message ) ;
}
async signTypedData ( domain , types , value ) {
return await _ _classPrivateFieldGet ( this , _WrappedSigner _signer , "f" ) . signTypedData ( domain , types , value ) ;
}
}
_WrappedSigner _signer = new WeakMap ( ) ;
//# sourceMappingURL=abstract-signer.js.map