Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions lib/ethers/signer/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,41 @@ defmodule Ethers.Signer.Local do
alias Ethers.Transaction.Signed
alias Ethers.Utils

if not Code.ensure_loaded?(secp256k1_module()) do
if Code.ensure_loaded?(secp256k1_module()) do
@impl true
def sign_transaction(_tx, _opts), do: {:error, :secp256k1_module_not_loaded}
def sign_transaction(transaction, opts) do
with {:ok, private_key} <- private_key(opts),
:ok <- validate_private_key(private_key, Keyword.get(opts, :from)),
encoded = Transaction.encode(transaction),
sign_hash = keccak_module().hash_256(encoded),
{:ok, {r, s, recovery_id}} <- secp256k1_module().sign(sign_hash, private_key) do
signed_transaction =
%Signed{
payload: transaction,
signature_r: r,
signature_s: s,
signature_y_parity_or_v: Signed.calculate_y_parity_or_v(transaction, recovery_id)
}

encoded_signed_transaction = Transaction.encode(signed_transaction)

{:ok, Utils.hex_encode(encoded_signed_transaction)}
end
end

@impl true
def accounts(_opts), do: {:error, :secp256k1_module_not_loaded}
end

@impl true
def sign_transaction(transaction, opts) do
with {:ok, private_key} <- private_key(opts),
:ok <- validate_private_key(private_key, Keyword.get(opts, :from)),
encoded = Transaction.encode(transaction),
sign_hash = keccak_module().hash_256(encoded),
{:ok, {r, s, recovery_id}} <- secp256k1_module().sign(sign_hash, private_key) do
signed_transaction =
%Signed{
payload: transaction,
signature_r: r,
signature_s: s,
signature_y_parity_or_v: Signed.calculate_y_parity_or_v(transaction, recovery_id)
}

encoded_signed_transaction = Transaction.encode(signed_transaction)

{:ok, Utils.hex_encode(encoded_signed_transaction)}
def accounts(opts) do
with {:ok, private_key} <- private_key(opts),
{:ok, address} <- do_get_address(private_key) do
{:ok, [address]}
end
end
end
else
@impl true
def sign_transaction(_tx, _opts), do: {:error, :secp256k1_module_not_loaded}

@impl true
def accounts(opts) do
with {:ok, private_key} <- private_key(opts),
{:ok, address} <- do_get_address(private_key) do
{:ok, [address]}
end
@impl true
def accounts(_opts), do: {:error, :secp256k1_module_not_loaded}
end

defp do_get_address(private_key) do
Expand Down
2 changes: 1 addition & 1 deletion lib/ethers/transaction/signed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule Ethers.Transaction.Signed do

defp maybe_add_chain_id(%__MODULE__{payload: %Legacy{chain_id: nil} = legacy_tx} = signed_tx) do
{chain_id, _recovery_id} = extract_chain_id_and_recovery_id(signed_tx)
%__MODULE__{signed_tx | payload: %Legacy{legacy_tx | chain_id: chain_id}}
%{signed_tx | payload: %{legacy_tx | chain_id: chain_id}}
end

defp maybe_add_chain_id(%__MODULE__{} = tx), do: tx
Expand Down