Signer behaviour.
A signer module is (at least) capable of signing transactions and listing accounts in the signer.
Builtin Signers
Ethers ships with some default signers that you can use.
Ethers.Signer.JsonRPC: Can be used with most wallets, geth, web3signer or any other platform which exposes a JsonRPC endpoint and implementseth_signTransactionandeth_accountsfunctions.Ethers.Signer.Local: This signs transactions locally but is highly discouraged to use in a production environment as it does not have any security measures built in.
Custom Signers
Custom signers can also be implemented which must adhere to this behvaviour.
For signing transactions in custom signers the functions in Ethers.Transaction module might
become handy. Check out the source code of built in signers for in depth info.
A signer may also implement the optional sign_typed_data/2 callback to support signing
EIP-712 typed structured data (see Ethers.TypedData),
the optional personal_sign/2 callback to support signing
EIP-191 personal messages (see
Ethers.PersonalMessage) and the optional sign_authorization/2 callback to support signing
EIP-7702 authorizations (see
Ethers.Authorization). Signers that do not implement them will simply not support those
signing schemes.
Globally Default Signer
If desired, a signer can be configured to be used for all operations in Ethers using elixir config.
config :ethers,
default_signer: Ethers.Signer.JsonRPC,
default_signer_opts: [url: "..."]
Summary
Callbacks
@callback accounts(opts :: Keyword.t()) :: {:ok, [Ethers.Types.t_address()]} | {:error, reason :: :not_supported | term()}
Returns the available signer account addresses.
This method might not be supported by all signers. If a signer does not support this function
it should return {:error, :not_supported}.
Parameters
- opts: Other options passed to the signer as
signer_opts
@callback personal_sign(message :: binary(), opts :: Keyword.t()) :: {:ok, binary()} | {:error, reason :: term()}
Signs an EIP-191 personal message and returns the signature.
This is an optional callback. Signers that do not implement it do not support personal message signing.
Parameters
- message: The message to sign, as raw bytes. (See
Ethers.PersonalMessage) - opts: Other options passed to the signer as
signer_opts.
Returns {:ok, signature} where signature is a 0x-prefixed 65-byte signature hex string.
@callback sign_authorization( authorization :: Ethers.Authorization.t(), opts :: Keyword.t() ) :: {:ok, Ethers.Authorization.Signed.t()} | {:error, reason :: term()}
Signs an EIP-7702 authorization and returns the signed authorization.
This is an optional callback. Signers that do not implement it do not support authorization
signing. Note that unlike the other signing callbacks this one does not return a hex
signature — it returns the Ethers.Authorization.Signed struct, ready for use in the
authorization_list of an Ethers.Transaction.Eip7702 transaction.
Parameters
- authorization: The authorization to sign. (An
Ethers.Authorizationstruct) - opts: Other options passed to the signer as
signer_opts.
@callback sign_transaction( tx :: Ethers.Transaction.t_payload(), opts :: Keyword.t() ) :: {:ok, encoded_signed_transaction :: binary()} | {:error, reason :: term()}
Signs a binary and returns the signature
Parameters
- tx: The transaction object. (An
Ethers.Transactionstruct) - opts: Other options passed to the signer as
signer_opts.
@callback sign_typed_data(typed_data :: Ethers.TypedData.t(), opts :: Keyword.t()) :: {:ok, binary()} | {:error, reason :: term()}
Signs an EIP-712 typed-data payload and returns the signature.
This is an optional callback. Signers that do not implement it do not support typed-data signing.
Parameters
- typed_data: The typed-data payload to sign. (An
Ethers.TypedDatastruct) - opts: Other options passed to the signer as
signer_opts.
Returns {:ok, signature} where signature is a 0x-prefixed 65-byte signature hex string.