Signers
Deprecation of Hethers.js by October 20, 2023
As we continue to evolve and improve the Hedera ecosystem, we are committed to ensuring that our developer tools and resources remain easy to use and up-to-date. With this goal in mind, the Hethers.js library will be deprecated by October 20, 2023.
🚨 The documentation site will be taken offline on January 31, 2024.
A Signer in hethers is an abstraction of a Hedera Account, which can be used to sign messages and transactions and send signed transactions to the Hedera Network to execute state-changing operations.
The available operations depend largely on the sub-class used.
Signer
The Signer class is abstract and cannot be directly instantiated. Instead, use the Wallet.
signer.connect( provider )
⇒ Signer
signer.connect( provider )
⇒ SignerSub-classes must implement this. Returns a new instance of the Signer, connected to the provider.
signer.getAddress( )
⇒ Promise< string< Address > >
signer.getAddress( )
⇒ Promise< string< Address > >Returns a Promise that resolves to the account address.
This is a Promise so that a Signer can be designed around an asynchronous source, such as hardware wallets.
Sub-classes must implement this.
Signer.isSigner( object )
⇒ boolean
Signer.isSigner( object )
⇒ booleanReturns true if and only if object is a Signer.
Methods
signer.getBalance( )
⇒ Promise< BigNumber >
signer.getBalance( )
⇒ Promise< BigNumber >Returns the balance of this wallet.
signer.getChainId( )
⇒ Promise< number >
signer.getChainId( )
⇒ Promise< number >Returns the chain ID this wallet is connected to.
signer.call( transactionRequest )
⇒ Promise< string< DataHexString > >
signer.call( transactionRequest )
⇒ Promise< string< DataHexString > > Returns the result of calling using the transactionRequest, with this account address being used as the from
field. Contrary to other EVM networks, static calls in Hedera are paid, thus executing a call
method is performed through the Signer
. The provider interface executes a ContractLocalCall
query under the hood. This is useful for calling getters on Contracts.
Note: to
can be either an address or an alias.
Signing
signer.signMessage( message )
⇒ Promise< string< RawSignature > >
signer.signMessage( message )
⇒ Promise< string< RawSignature > >This returns a Promise which resolves to the Raw Signature of message.
Sub-classes must implement this, however they may throw if signing a message is not supported
signer.signTransaction( transactionRequest )
⇒ Promise< string< DataHexString > >
signer.signTransaction( transactionRequest )
⇒ Promise< string< DataHexString > >Returns a Promise which resolves to the signed transaction of the transactionRequest. This method does not populate any missing fields.
Sub-classes must implement this, however they may throw if signing a transaction is not supported, which is common for security reasons in many clients.
signer.sendTransaction( transactionRequest )
⇒ Promise< TransactionResponse >
signer.sendTransaction( transactionRequest )
⇒ Promise< TransactionResponse >This method populates the transactionRequest with missing fields, using populateTransaction and returns a Promise which resolves to the transaction.
Sub-classes must implement this, however they may throw if sending a transaction is not supported or if the Wallet is offline and not connected to a Provider.
Signs the transaction with the key given upon creation. Transaction can be:
FileCreateTransaction
- when there is onlyfileChunk
field in thetransaction.customData
objectFileAppendTransaction
- when there is bothfileChunk
and afileId
fieldsContractCreateTransaction
- when there is abytecodeFileId
fieldContractExecuteTransaction
- when there is ato
field present. Ignores the other fieldsTransferTransaction
- when there is bothto
and avalue
fieldsAccountCreateTransaction
- when there is apublicKey
field in thetransaction.customData
object, alsoinitialBalance
can be provided as optional field incustomData
Sub-Classes
It is very important that all important properties of a Signer are immutable. Since Hedera Hashgraph is very asynchronous and deals with critical data, keeping properties such as the provider and address static throughout the life-cycle of the Signer helps prevent serious issues and many other classes and libraries make this assumption.
A sub-class must extend Signer and must call super()
.
signer.checkTransaction( transactionRequest )
⇒ TransactionRequest
signer.checkTransaction( transactionRequest )
⇒ TransactionRequestThis is generally not required to be overridden, but may be needed to provide custom behaviour in sub-classes.
This should return a copy of the transactionRequest, with any properties needed by call
and populateTransaction
(which is used by sendTransaction). It should also throw an error if any unknown key is specified.
The default implementation checks only if valid TransactionRequest properties exist and adds from
to the transaction if it does not exist.
If there is a from
field it must be verified to be equal to the Signer's address.
signer.populateTransaction( transactionRequest )
⇒ Promise< TransactionRequest >
signer.populateTransaction( transactionRequest )
⇒ Promise< TransactionRequest >This is generally not required to be overridden, but may be needed to provide custom behaviour in sub-classes.
This should return a copy of transactionRequest, follow the same procedure as checkTransaction
and fill in any properties required for sending a transaction. The result should have all promises resolved; if needed the resolveProperties utility function can be used for this.
The default implementation calls checkTransaction
and resolves to
, adds customData
based on the related operations on Signer.
Wallet
The Wallet class inherits Signer and can sign transactions and messages using a private key and account ID as a standard Externally Owned Account (EOA).
new hethers.Wallet( privateKey [ , provider ] )
⇒ Wallet
new hethers.Wallet( privateKey [ , provider ] )
⇒ WalletCreate a new Wallet instance for privateKey and optionally connected to the provider. Note: Using only a private Key for wallet initialization is not enough for further transaction execution in Hedera Network. Connecting to an account is mandatory for a fully operational wallet.
Note: ECDSA privateKeys which do not start with 0x
are also accepted.
Create a new Wallet instance for ExternallyOwnedAccount and optionally connected to the provider.
Note: That wallet is fully operational, and can sign or send transactions, if accountId or address and privateKey are set up in the ExternallyOwnedAccount
object.
hethers.Wallet.createRandom( [ options = {} ] )
⇒ Wallet
hethers.Wallet.createRandom( [ options = {} ] )
⇒ WalletReturns a new Wallet with a random private key, generated from cryptographically secure entropy sources. If the current environment does not have a secure entropy source, an error is thrown. The library supports HIP-32, thus the created wallet will have an alias populated. In order for the wallet to be fully functional (be able to send transactions), it must be created by sending hbars to the alias for example.
hethers.Wallet.fromEncryptedJson( json , password [ , progress ] )
⇒ Promise< Wallet >
hethers.Wallet.fromEncryptedJson( json , password [ , progress ] )
⇒ Promise< Wallet >Create an instance by decrypting an encrypted JSON wallet.
If progress is provided it will be called during decryption with a value between 0 and 1 indicating the progress towards completion.
hethers.Wallet.fromEncryptedJsonSync( json , password )
⇒ Wallet
hethers.Wallet.fromEncryptedJsonSync( json , password )
⇒ WalletCreate an instance from an encrypted JSON wallet.
This operation will operate synchronously which will lock up the user interface, possibly for a non-trivial duration. Most applications should use the asynchronous fromEncryptedJson
instead.
hethers.Wallet.fromMnemonic( mnemonic [ , path , [ wordlist ] ] )
⇒ Wallet
hethers.Wallet.fromMnemonic( mnemonic [ , path , [ wordlist ] ] )
⇒ WalletCreate an instance from a mnemonic phrase.
Properties
wallet.address
⇒ string< Address >
wallet.address
⇒ string< Address >The address for the account this Wallet represents.
wallet.provider
⇒ Provider
wallet.provider
⇒ ProviderThe provider this wallet is connected to, which will be used for any Methods. This can be null.
A Wallet instance is immutable, so if you wish to change the Provider, you may use the connect
method to create a new instance connected to the desired provider.
wallet.publicKey
⇒ string< DataHexString >
wallet.publicKey
⇒ string< DataHexString >The uncompressed public key for this Wallet represents.
Methods
wallet.connectAccount( < AccountLike > )
⇒ Wallet
wallet.connectAccount( < AccountLike > )
⇒ WalletReturn a new instance of a fully operational wallet, that can be used for transaction signing and sending.
wallet.connect( provider )
⇒ Wallet
wallet.connect( provider )
⇒ WalletReturn a new instance, connected to the provider.
wallet.createAccount( publicKey , [ initialBalance: BigInt ])
⇒ < TransactionResponse >
wallet.createAccount( publicKey , [ initialBalance: BigInt ])
⇒ < TransactionResponse > The transaction receipt containts the ID of the newly created account in the TransactionReceipt.accountAddress
field.
Note: There are two ways you can create an account either by using an existing account or by using an alias. If you have an existing account, you can use that account to create a new account and to pay for the associated transaction fees in hbar. If you do not have access to an existing account, you can create a testnet account via the Hedera Developer Portal or a mainnet account through one of the options listed here.
wallet.getAddress()
⇒ Promise< string >
wallet.getAddress()
⇒ Promise< string >The address for the account this Wallet represents.
wallet.encrypt( password , [ options = {} , [ progress ] ] )
⇒ Promise< string >
wallet.encrypt( password , [ options = {} , [ progress ] ] )
⇒ Promise< string >Encrypt the wallet using password returning a Promise which resolves to a JSON wallet.
If progress is provided it will be called during decryption with a value between 0 and 1 indicating the progress towards completion.
ExternallyOwnedAccount
This is an interface that contains a minimal set of properties required for Externally Owned Accounts(EOA) which can have certain operations performed, such as encoding as a JSON wallet.
eoa.address
⇒ string< Address >
eoa.address
⇒ string< Address >The Address of this EOA.
eoa.privateKey
⇒ string< DataHexString< 32 > >
eoa.privateKey
⇒ string< DataHexString< 32 > >The privateKey of this EOA.
eoa.account
⇒ string< Account >
eoa.account
⇒ string< Account >The account Id of the EOA.
eoa.alias
=> string< DataHexString< 32 > >
eoa.alias
=> string< DataHexString< 32 > >
Last updated