Skip to content

Commit 048a139

Browse files
author
Wen Chen
committed
Merge branch 'main' of github.com:wchenNL/elixir_ethers into add_kms_signer
2 parents 44e6405 + 02bbcc4 commit 048a139

12 files changed

+391
-88
lines changed

CHANGELOG.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
# Changelog
22

3-
## v0.2.2 (2023-01-08)
3+
## v0.3.0 (2024-02-05)
4+
5+
### Breaking Changes
6+
7+
- Removed `signature_v`, `signature_recovery_id` and `signature_y_parity` from `Ethers.Transaction`
8+
struct and introduce new `signature_v_or_y_parity` value
9+
- Update `ex_abi` to 0.7.0 with new `method_id` logic for event selectors and use its value
10+
11+
### Enhancements
12+
13+
- Cleanup implementation of Transaction encoders and value decoder
14+
15+
## v0.2.3 (2024-01-29)
16+
17+
### New features
18+
19+
- Add `Ethers.get_transaction_receipt/2` function to query native chain transaction receipt by transaction hash.
20+
21+
### Enhancements
22+
23+
- Add more metadata to `Ethers.Transaction` struct.
24+
- Return `Ethers.Transaction` struct in `Ethers.get_transaction/2` function.
25+
- Support `get_transaction` in batch requests.
26+
27+
## v0.2.2 (2024-01-08)
428

529
### New features
630

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies in your `mix.exs` file:
2323
```elixir
2424
def deps do
2525
[
26-
{:ethers, "~> 0.2.2"},
26+
{:ethers, "~> 0.3.0"},
2727
# Uncomment next line if you want to use local signers
2828
# {:ex_secp256k1, "~> 0.7.2"}
2929
]
@@ -261,7 +261,7 @@ To sign transactions on Ethers, You can specify a `signer` module when sending/s
261261
262262
Ethers has these built-in signers to use:
263263
264-
- `Ethers.Signer.Local`\*: A local signer which loads a private key from `signer_opts` and signs the transactions.
264+
- `Ethers.Signer.Local`: A local signer which loads a private key from `signer_opts` and signs the transactions.
265265
- `Ethers.Signer.JsonRPC`: Uses `eth_signTransaction` Json RPC function to sign transactions. (Using services like [Consensys/web3signer](https://github.com/Consensys/web3signer) or [geth](https://geth.ethereum.org/))
266266
- `Ethers.Signer.KMS`: A KMS-based signer that expects a valid `kms_key_id` from `signer_opts`. Transactions are sent to KMS to sign with the specified [Customer Managed Key](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk). Notice that the keys need to pre-exist as `ECC_SECG_P256K1` asymmetric pairs as described on [AWS](https://docs.aws.amazon.com/kms/latest/developerguide/asymmetric-key-specs.html#key-spec-ecc). Also, please refer to [AWS Key configuration](https://github.com/ex-aws/ex_aws?tab=readme-ov-file#aws-key-configuration) for configuring your app to connect to AWS in order to use the signer.
267267

lib/ethers.ex

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ defmodule Ethers do
8484
gas_price: :eth_gas_price,
8585
get_logs: :eth_get_logs,
8686
get_transaction_count: :eth_get_transaction_count,
87+
get_transaction: :eth_get_transaction_by_hash,
8788
send: :eth_send_transaction
8889
}
8990

@@ -142,17 +143,36 @@ defmodule Ethers do
142143
## Parameters
143144
- tx_hash: Transaction hash which the transaction is queried for.
144145
- overrides:
146+
- rpc_client: The RPC module to use for this request (overrides default).
145147
- rpc_opts: Specific RPC options to specify for this request.
146148
"""
147149
@spec get_transaction(Types.t_hash(), Keyword.t()) ::
148-
{:ok, map()} | {:error, term()}
150+
{:ok, Transaction.t()} | {:error, term()}
149151
def get_transaction(tx_hash, opts \\ []) when is_binary(tx_hash) do
150152
{rpc_client, rpc_opts} = get_rpc_client(opts)
151153

152154
rpc_client.eth_get_transaction_by_hash(tx_hash, rpc_opts)
153155
|> post_process(nil, :get_transaction)
154156
end
155157

158+
@doc """
159+
Returns the receipt of a transaction by it's hash.
160+
161+
## Parameters
162+
- tx_hash: Transaction hash which the transaction receipt is queried for.
163+
- overrides:
164+
- rpc_client: The RPC module to use for this request (overrides default).
165+
- rpc_opts: Specific RPC options to specify for this request.
166+
"""
167+
@spec get_transaction_receipt(Types.t_hash(), Keyword.t()) ::
168+
{:ok, map()} | {:error, term()}
169+
def get_transaction_receipt(tx_hash, opts \\ []) when is_binary(tx_hash) do
170+
{rpc_client, rpc_opts} = get_rpc_client(opts)
171+
172+
rpc_client.eth_get_transaction_receipt(tx_hash, rpc_opts)
173+
|> post_process(nil, :get_transaction_receipt)
174+
end
175+
156176
@doc """
157177
Deploys a contract to the blockchain.
158178
@@ -645,6 +665,13 @@ defmodule Ethers do
645665
defp post_process({:ok, nil}, _tx_hash, :get_transaction),
646666
do: {:error, :transaction_not_found}
647667

668+
defp post_process({:ok, tx_data}, _tx_hash, :get_transaction) do
669+
Transaction.from_map(tx_data)
670+
end
671+
672+
defp post_process({:ok, nil}, _tx_hash, :get_transaction_receipt),
673+
do: {:error, :transaction_receipt_not_found}
674+
648675
defp post_process({:ok, result}, _tx_data, _action),
649676
do: {:ok, result}
650677

lib/ethers/contract_helpers.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ defmodule Ethers.ContractHelpers do
315315
[event_topic_0(selector) | encode_event_sub_topics(selector, args)]
316316
end
317317

318+
defp event_topic_0(%{method_id: method_id}) when byte_size(method_id) == 32 do
319+
Ethers.Utils.hex_encode(method_id)
320+
end
321+
318322
defp event_topic_0(selector) do
319323
selector
320324
|> ABI.FunctionSelector.encode()

lib/ethers/signer/local.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ defmodule Ethers.Signer.Local do
3636
Transaction.encode(tx)
3737
|> keccak_module().hash_256()
3838
|> secp256k1_module().sign(private_key) do
39+
y_parity_or_v = Transaction.calculate_y_parity_or_v(tx, recovery_id)
40+
3941
signed =
40-
%{tx | signature_r: r, signature_s: s, signature_recovery_id: recovery_id}
42+
%Ethers.Transaction{
43+
tx
44+
| signature_r: r,
45+
signature_s: s,
46+
signature_y_parity_or_v: y_parity_or_v
47+
}
4148
|> Transaction.encode()
4249
|> Utils.hex_encode()
4350

0 commit comments

Comments
 (0)