@@ -21,41 +21,41 @@ defmodule Ethers.Signer.Local do
2121 alias Ethers.Transaction.Signed
2222 alias Ethers.Utils
2323
24- if not Code . ensure_loaded? ( secp256k1_module ( ) ) do
24+ if Code . ensure_loaded? ( secp256k1_module ( ) ) do
2525 @ impl true
26- def sign_transaction ( _tx , _opts ) , do: { :error , :secp256k1_module_not_loaded }
26+ def sign_transaction ( transaction , opts ) do
27+ with { :ok , private_key } <- private_key ( opts ) ,
28+ :ok <- validate_private_key ( private_key , Keyword . get ( opts , :from ) ) ,
29+ encoded = Transaction . encode ( transaction ) ,
30+ sign_hash = keccak_module ( ) . hash_256 ( encoded ) ,
31+ { :ok , { r , s , recovery_id } } <- secp256k1_module ( ) . sign ( sign_hash , private_key ) do
32+ signed_transaction =
33+ % Signed {
34+ payload: transaction ,
35+ signature_r: r ,
36+ signature_s: s ,
37+ signature_y_parity_or_v: Signed . calculate_y_parity_or_v ( transaction , recovery_id )
38+ }
39+
40+ encoded_signed_transaction = Transaction . encode ( signed_transaction )
41+
42+ { :ok , Utils . hex_encode ( encoded_signed_transaction ) }
43+ end
44+ end
2745
2846 @ impl true
29- def accounts ( _opts ) , do: { :error , :secp256k1_module_not_loaded }
30- end
31-
32- @ impl true
33- def sign_transaction ( transaction , opts ) do
34- with { :ok , private_key } <- private_key ( opts ) ,
35- :ok <- validate_private_key ( private_key , Keyword . get ( opts , :from ) ) ,
36- encoded = Transaction . encode ( transaction ) ,
37- sign_hash = keccak_module ( ) . hash_256 ( encoded ) ,
38- { :ok , { r , s , recovery_id } } <- secp256k1_module ( ) . sign ( sign_hash , private_key ) do
39- signed_transaction =
40- % Signed {
41- payload: transaction ,
42- signature_r: r ,
43- signature_s: s ,
44- signature_y_parity_or_v: Signed . calculate_y_parity_or_v ( transaction , recovery_id )
45- }
46-
47- encoded_signed_transaction = Transaction . encode ( signed_transaction )
48-
49- { :ok , Utils . hex_encode ( encoded_signed_transaction ) }
47+ def accounts ( opts ) do
48+ with { :ok , private_key } <- private_key ( opts ) ,
49+ { :ok , address } <- do_get_address ( private_key ) do
50+ { :ok , [ address ] }
51+ end
5052 end
51- end
53+ else
54+ @ impl true
55+ def sign_transaction ( _tx , _opts ) , do: { :error , :secp256k1_module_not_loaded }
5256
53- @ impl true
54- def accounts ( opts ) do
55- with { :ok , private_key } <- private_key ( opts ) ,
56- { :ok , address } <- do_get_address ( private_key ) do
57- { :ok , [ address ] }
58- end
57+ @ impl true
58+ def accounts ( _opts ) , do: { :error , :secp256k1_module_not_loaded }
5959 end
6060
6161 defp do_get_address ( private_key ) do
0 commit comments