WalletClients
The type of client used by the wallet instance.
Properties
enum {
http
websocket
ipc
}WalletHttpClient
Wallet instance with rpc http/s client.
Wallet(.http)WalletWsClient
Wallet instance with rpc ws/s client.
Wallet(.websocket)WalletIpcClient
Wallet instance with rpc ipc client.
Wallet(.ipc)TransactionEnvelopePool
Pool of prepared transaciton envelopes.
Properties
struct {
mutex: Mutex = .{}
/// DoublyLinkedList queue. Iterate from last to first (LIFO)
pooled_envelopes: TransactionEnvelopeQueue
}Node
LinkedList node.
TransactionEnvelopeQueue.NodeFindTransactionEnvelope
Finds a transaction envelope from the pool based on the transaction type and it's nonce in case there are transactions with the same type. This is thread safe.
Returns null if no transaction was found
Signature
pub fn findTransactionEnvelope(
pool: *TransactionEnvelopePool,
allocator: Allocator,
search: SearchCriteria,
) ?TransactionEnvelopeAddEnvelopeToPool
Adds a new node into the pool. This is thread safe.
Signature
pub fn addEnvelopeToPool(
pool: *TransactionEnvelopePool,
node: *Node,
) voidUnsafeReleaseEnvelopeFromPool
Removes a node from the pool. This is not thread safe.
Signature
pub fn unsafeReleaseEnvelopeFromPool(
pool: *TransactionEnvelopePool,
node: *Node,
) voidReleaseEnvelopeFromPool
Removes a node from the pool. This is thread safe.
Signature
pub fn releaseEnvelopeFromPool(
pool: *TransactionEnvelopePool,
node: *Node,
) voidGetFirstElementFromPool
Gets the last node from the pool and removes it. This is thread safe.
Signature
pub fn getFirstElementFromPool(
pool: *TransactionEnvelopePool,
allocator: Allocator,
) ?TransactionEnvelopeGetLastElementFromPool
Gets the last node from the pool and removes it. This is thread safe.
Signature
pub fn getLastElementFromPool(
pool: *TransactionEnvelopePool,
allocator: Allocator,
) ?TransactionEnvelopeDeinit
Destroys all created pointer. All future operations will deadlock. This is thread safe.
Signature
pub fn deinit(
pool: *TransactionEnvelopePool,
allocator: Allocator,
) voidNode
LinkedList node.
TransactionEnvelopeQueue.NodeWallet
Creates a wallet instance based on which type of client defined in WalletClients.
Depending on the type of client the underlaying methods of rpc_client can be changed.
The http and websocket client do not mirror 100% in terms of their methods.
The client's methods can all be accessed under rpc_client. The same goes for the signer.
Signature
pub fn Wallet(comptime client_type: WalletClients) typeInitErrors
Set of possible errors when starting the wallet.
ClientType.InitErrors || error{IdentityElement}Error
Set of common errors produced by wallet actions.
ClientType.BasicRequestErrorsPrepareError
Set of errors when preparing a transaction
Error || error{
InvalidBlockNumber,
UnableToFetchFeeInfoFromBlock,
MaxFeePerGasUnderflow,
UnsupportedTransactionType,
}AssertionErrors
Set of errors that can be returned on the assertTransaction method.
error{
InvalidChainId,
TransactionTipToHigh,
EmptyBlobs,
TooManyBlobs,
BlobVersionNotSupported,
CreateBlobTransaction,
}Eip3074Envelope
Eip3074 auth message envelope.
Properties
struct {
magic: u8
chain_id: u256
nonce: u256
address: u256
commitment: Hash
}SendSignedTransactionErrors
Set of possible errors when sending signed transactions
Error || Signer.SigningErrors || SerializeErrorsNonceManager
Nonce manager that use's the rpc client as the source of truth for checking internally that the cached and managed values can be used.
Properties
struct {
/// The address that will get it's nonce managed.
address: Address
/// The current nonce in use.
managed: u64
/// The cached nonce.
cache: u64
}InitManager
Sets the initial state of the NonceManager.
Signature
pub fn initManager(address: Address) NonceManagerGetNonce
Gets the nonce from either the cache or from the network.
Resets the manager nonce value and the cache if the nonce value from the network
is higher than one from the cache.
Signature
pub fn getNonce(
self: *Self,
rpc_client: *ClientType,
) ClientType.BasicRequestErrors!u64IncrementNonce
Increments the manager by one.
Signature
pub fn incrementNonce(self: *Self) voidUpdateNonce
Gets the nonce from either the cache or from the network and updates internally.
Resets the manager nonce value and the cache if the nonce value from the network
is higher than one from the cache.
Signature
pub fn updateNonce(
self: *Self,
rpc_client: *ClientType,
) ClientType.BasicRequestErrors!u64ResetNonce
Resets the manager to 0.
Signature
pub fn resetNonce(self: *Self) voidInit
Sets the wallet initial state.
The init opts will depend on the client_type.
Also adds the hability to use a nonce manager or to use the network directly.
Exampleconst uri = try std.Uri.parse("http://localhost:6969/");
var buffer: Hash = undefined;
_ = try std.fmt.hexToBytes(&buffer, "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80");
var wallet = try Wallet(.http).init(buffer, .{
.allocator = testing.allocator,
.network_config = .{
.endpoint = .{ .uri = uri },
},
}, true // Setting to true initializes the NonceManager);
defer wallet.deinit();Signature
pub fn init(
private_key: ?Hash,
opts: ClientInitOptions,
nonce_manager: bool,
) (error{IdentityElement} || ClientType.InitErrors)!*WalletSelfInitUnownedRpcClient
Creates a wallet instance where this wallet client doesn't own the pointer to the rpc client.
Use this if you don't want the rpc client lifetime to be the same
as this wallet instance. Once you are done make sure to use deinitUnowned
instead of the normal deinit method.
Signature
pub fn initUnownedRpcClient(
allocator: Allocator,
private_key: ?Hash,
client: *ClientType,
nonce_manager: bool,
) error{IdentityElement}!WalletSelfDeinitUnowned
Clears memory and destroys any created pointers
Doesn't deinit the rpc client.
Signature
pub fn deinitUnowned(self: *WalletSelf) voidAssertTransaction
Asserts that the transactions is ready to be sent. Will return errors where the values are not expected
Signature
pub fn assertTransaction(
self: *WalletSelf,
tx: TransactionEnvelope,
) AssertionErrors!voidAuthMessageEip3074
Converts to a message that the contracts executing AUTH opcodes can understand.
For more details on the implementation see here.
You can pass null to nonce if you want to target a specific nonce.
Otherwise if with either use the nonce_manager if it can or fetch from the network.
Memory must be freed after calling this method.
This is still experimental since the EIP has not being deployed into any mainnet.
Signature
pub fn authMessageEip3074(
self: *WalletSelf,
invoker_address: Address,
nonce: ?u64,
commitment: Hash,
) (AbiEncoder.Errors || ClientType.BasicRequestErrors)![]u8FindTransactionEnvelopeFromPool
Find a specific prepared envelope from the pool based on the given search criteria.
Signature
pub fn findTransactionEnvelopeFromPool(
self: *WalletSelf,
search: TransactionEnvelopePool.SearchCriteria,
) ?TransactionEnvelopeHashAuthorityEip7702
Generates the authorization hash based on the eip7702 specification. For more information please go here
This is still experimental since the EIP has not being deployed into any mainnet.
Signature
pub fn hashAuthorityEip7702(
self: *WalletSelf,
authority: Address,
nonce: u64,
) RlpEncodeErrors!HashGetWalletAddress
Get the wallet address.
Uses the wallet public key to generate the address.
Signature
pub fn getWalletAddress(self: *WalletSelf) AddressPoolTransactionEnvelope
Converts unprepared transaction envelopes and stores them in a pool.
This appends to the last node of the list.
Signature
pub fn poolTransactionEnvelope(
self: *WalletSelf,
unprepared_envelope: UnpreparedTransactionEnvelope,
) PrepareError!voidPrepareTransaction
Prepares a transaction based on it's type so that it can be sent through the network.\
Only the null struct properties will get changed.
Everything that gets set before will not be touched.
Signature
pub fn prepareTransaction(
self: *WalletSelf,
unprepared_envelope: UnpreparedTransactionEnvelope,
) PrepareError!TransactionEnvelopeRecoverAuthMessageAddress
Recovers the address associated with the signature based on the message.
To reconstruct the message use authMessageEip3074
Reconstructs the message from them and returns the address bytes.
Signature
pub fn recoverAuthMessageAddress(
auth_message: []u8,
sig: Signature,
) Signer.RecoverPubKeyErrors!AddressRecoverAuthorizationAddress
Recovers the address associated with the signature based on the authorization payload.
Signature
pub fn recoverAuthorizationAddress(
self: *WalletSelf,
authorization_payload: AuthorizationPayload,
) (RlpEncodeErrors || Signer.RecoverPubKeyErrors)!AddressSearchPoolAndSendTransaction
Search the internal TransactionEnvelopePool to find the specified transaction based on the type and nonce.
If there are duplicate transaction that meet the search criteria it will send the first it can find.
The search is linear and starts from the first node of the pool.
Signature
pub fn searchPoolAndSendTransaction(
self: *WalletSelf,
search_opts: TransactionEnvelopePool.SearchCriteria,
) (SendSignedTransactionErrors || AssertionErrors || error{TransactionNotFoundInPool})!RPCResponse(Hash)SendBlobTransaction
Sends blob transaction to the network. Trusted setup must be loaded otherwise this will fail.
Signature
pub fn sendBlobTransaction(
self: *WalletSelf,
blobs: []const Blob,
unprepared_envelope: UnpreparedTransactionEnvelope,
trusted_setup: *KZG4844,
) (SendSignedTransactionErrors || CancunSerializeErrors || error{ InvalidTransactionType, TrustedSetupNotLoaded })!RPCResponse(Hash)SendSidecarTransaction
Sends blob transaction to the network. This uses and already prepared sidecar.
Signature
pub fn sendSidecarTransaction(
self: *WalletSelf,
sidecars: []const Sidecar,
unprepared_envelope: UnpreparedTransactionEnvelope,
) SendSignedTransactionErrors!RPCResponse(Hash)SendSignedTransaction
Signs, serializes and send the transaction via eth_sendRawTransaction.
Returns the transaction hash.
Signature
pub fn sendSignedTransaction(
self: *WalletSelf,
tx: TransactionEnvelope,
) SendSignedTransactionErrors!RPCResponse(Hash)SendTransaction
Prepares, asserts, signs and sends the transaction via eth_sendRawTransaction.
If any envelope is in the envelope pool it will use that instead in a LIFO order.
Will return an error if the envelope is incorrect
Signature
pub fn sendTransaction(
self: *WalletSelf,
unprepared_envelope: UnpreparedTransactionEnvelope,
) (SendSignedTransactionErrors || AssertionErrors || PrepareError)!RPCResponse(Hash)SignAuthMessageEip3074
Signs and prepares an eip3074 authorization message. For more details on the implementation see here.
You can pass null to nonce if you want to target a specific nonce.
Otherwise if with either use the nonce_manager if it can or fetch from the network.
This is still experimental since the EIP has not being deployed into any mainnet.
Signature
pub fn signAuthMessageEip3074(
self: *WalletSelf,
invoker_address: Address,
nonce: ?u64,
commitment: Hash,
) (AbiEncoder.Errors || ClientType.BasicRequestErrors || Signer.SigningErrors)!SignatureSignAuthorizationEip7702
Signs and prepares an eip7702 authorization message. For more details on the implementation see here.
You can pass null to nonce if you want to target a specific nonce.
Otherwise if with either use the nonce_manager if it can or fetch from the network.
This is still experimental since the EIP has not being deployed into any mainnet.
Signature
pub fn signAuthorizationEip7702(
self: *WalletSelf,
authority: Address,
nonce: ?u64,
) (ClientType.BasicRequestErrors || Signer.SigningErrors || RlpEncodeErrors)!AuthorizationPayloadSignEthereumMessage
Signs an ethereum message with the specified prefix.
The Signatures recoverId doesn't include the chain_id.
Signature
pub fn signEthereumMessage(
self: *WalletSelf,
message: []const u8,
) (Signer.SigningErrors || Allocator.Error)!SignatureSignTypedData
Signs a EIP712 message according to the expecification https://eips.ethereum.org/EIPS/eip-712
types parameter is expected to be a struct where the struct
keys are used to grab the solidity type information so that the
encoding and hashing can happen based on it. See the specification
for more details.
primary_type is the expected main type that you want to hash this message.
Compilation will fail if the provided string doesn't exist on the types parameter
domain is the values of the defined EIP712Domain. Currently it doesnt not support custom
domain types.
message is expected to be a struct where the solidity types are transalated to the native
zig types. I.E string -> []const u8 or int256 -> i256 and so on.
In the future work will be done where the compiler will offer more clearer types
base on a meta programming type function.
Returns the signature type.
Signature
pub fn signTypedData(
self: *WalletSelf,
comptime eip_types: anytype,
comptime primary_type: []const u8,
domain: ?TypedDataDomain,
message: anytype,
) (Signer.SigningErrors || EIP712Errors)!SignatureVerifyAuthMessage
Verifies if the auth message was signed by the provided address.
To reconstruct the message use authMessageEip3074.
You can pass null to expected_address if you want to use this wallet instance
associated address.
Signature
pub fn verifyAuthMessage(
self: *WalletSelf,
expected_address: ?Address,
auth_message: []u8,
sig: Signature,
) (ClientType.BasicRequestErrors || Signer.RecoverPubKeyErrors)!boolVerifyAuthorization
Verifies if the authorization message was signed by the provided address.\
You can pass null to expected_address if you want to use this wallet instance
associated address.
Signature
pub fn verifyAuthorization(
self: *WalletSelf,
expected_address: ?Address,
authorization_payload: AuthorizationPayload,
) (ClientType.BasicRequestErrors || Signer.RecoverPubKeyErrors || RlpEncodeErrors)!boolVerifyMessage
Verifies if a given signature was signed by the current wallet.
Signature
pub fn verifyMessage(self: *WalletSelf, sig: Signature, message: []const u8) boolVerifyTypedData
Verifies a EIP712 message according to the expecification https://eips.ethereum.org/EIPS/eip-712
types parameter is expected to be a struct where the struct
keys are used to grab the solidity type information so that the
encoding and hashing can happen based on it. See the specification
for more details.
primary_type is the expected main type that you want to hash this message.
Compilation will fail if the provided string doesn't exist on the types parameter
domain is the values of the defined EIP712Domain. Currently it doesnt not support custom
domain types.
message is expected to be a struct where the solidity types are transalated to the native
zig types. I.E string -> []const u8 or int256 -> i256 and so on.
In the future work will be done where the compiler will offer more clearer types
base on a meta programming type function.
Returns the signature type.
Signature
pub fn verifyTypedData(
self: *WalletSelf,
sig: Signature,
comptime eip712_types: anytype,
comptime primary_type: []const u8,
domain: ?TypedDataDomain,
message: anytype,
) (EIP712Errors || Signer.RecoverPubKeyErrors)!boolWaitForTransactionReceipt
Waits until the transaction gets mined and we can grab the receipt. It fails if the retry counter is excedded.
The behaviour of this method varies based on the client type.
If it's called with the websocket client or the ipc client it will create a subscription for new block and wait
until the transaction gets mined. Otherwise it will use the rpc_client pooling_interval property.
Signature
pub fn waitForTransactionReceipt(self: *WalletSelf, tx_hash: Hash, confirmations: u8) (Error || error{
FailedToGetReceipt,
TransactionReceiptNotFound,
TransactionNotFound,
InvalidBlockNumber,
FailedToUnsubscribe,
})!RPCResponse(TransactionReceipt)