Skip to content

Commit

Permalink
Adds wallet tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesduncombe committed Feb 1, 2024
1 parent c1dfb78 commit b078120
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions test/support/test_token.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule TTEth.TestToken do
@moduledoc false
use TTEth.Contract,
abi_file: Application.app_dir(:tt_eth, "priv/fixtures/contract_abis/TestToken.abi")
end
4 changes: 2 additions & 2 deletions test/tt_eth/transactions/eip1559_transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TTEth.Transactions.EIP1559TransactionTest do
alias TTEth.Wallet

# Polygon Mumbai.
@chain_id 80001
@chain_id 80_001

@private_key_human "0x62aa6ec41b56439d2c5df352c45a00389cef262b3761e13c6481e35ab027d262"
@to_address_human "0x38f153fdd399ff2cf64704c6a4b16d3fd9ddcd69"
Expand Down Expand Up @@ -113,7 +113,7 @@ defmodule TTEth.Transactions.EIP1559TransactionTest do
data: @tx_data,
chain_id: @chain_id,
nonce: @nonce,
gas_limit: 21000,
gas_limit: 21_000,
max_fee_per_gas: 3_000_000_000,
max_priority_fee_per_gas: 3_000_000_001,
value: 0
Expand Down
61 changes: 61 additions & 0 deletions test/tt_eth/wallet_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,64 @@ defmodule TTEth.WalletTest do
end
end

describe "sign/2" do
setup :build_wallet

test "given a wallet and digest, signs the digest", %{wallet: wallet} do
wallet
|> Wallet.sign("some plaintext" |> TTEth.keccak())
|> assert_match({:ok, {<<_signature::512>>, recovery_id}} when recovery_id in [0, 1])
end

test "returns an error tuple if a failure happens", %{wallet: wallet} do
wallet
|> Wallet.sign("some plaintext")
|> assert_equal({:error, :wrong_message_size})
end
end

describe "sign!/2" do
setup :build_wallet

test "same as sign/2", %{wallet: wallet} do
digest = "some plaintext" |> TTEth.keccak()

{:ok, compact_signature} =
wallet
|> Wallet.sign(digest)

wallet
|> Wallet.sign!(digest)
|> assert_match(^compact_signature)
end
end

describe "personal_sign/2" do
setup :build_wallet

test "signs a plaintext using the EIP-191 standard", %{wallet: wallet} do
wallet
|> Wallet.personal_sign("some plaintext")
|> assert_match({:ok, {v, _r, _s}} when v in [27, 28])
end

test "returns an error tuple if a failure happens"

Check failure on line 83 in test/tt_eth/wallet_test.exs

View workflow job for this annotation

GitHub Actions / OTP 22.x / Elixir 1.11.4

test personal_sign/2 returns an error tuple if a failure happens (TTEth.WalletTest)

Check failure on line 83 in test/tt_eth/wallet_test.exs

View workflow job for this annotation

GitHub Actions / OTP 22.x / Elixir 1.13.4

test personal_sign/2 returns an error tuple if a failure happens (TTEth.WalletTest)

Check failure on line 83 in test/tt_eth/wallet_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.11.4

test personal_sign/2 returns an error tuple if a failure happens (TTEth.WalletTest)

Check failure on line 83 in test/tt_eth/wallet_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.13.4

test personal_sign/2 returns an error tuple if a failure happens (TTEth.WalletTest)

Check failure on line 83 in test/tt_eth/wallet_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.13.4

test personal_sign/2 returns an error tuple if a failure happens (TTEth.WalletTest)

Check failure on line 83 in test/tt_eth/wallet_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.5

test personal_sign/2 returns an error tuple if a failure happens (TTEth.WalletTest)
end

describe "personal_sign!/2" do
setup :build_wallet

test "same as personal_sign/2", %{wallet: wallet} do
{:ok, components} =
wallet
|> Wallet.personal_sign("some plaintext")

wallet
|> Wallet.personal_sign!("some plaintext")
|> assert_match(^components)
end
end

## Private.

defp assert_match_ternary_wallet(wallet) do
Expand All @@ -65,4 +123,7 @@ defmodule TTEth.WalletTest do
}
})
end

defp build_wallet(_),
do: %{wallet: Wallet.new()}
end
2 changes: 1 addition & 1 deletion test/tt_eth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TTEthTest do
import TTEth, only: [binary_to_hex!: 1]
doctest TTEth, import: true

@chain_id 12345
@chain_id 12_345

describe "new_keypair/0" do
test "generates a public and private key" do
Expand Down

0 comments on commit b078120

Please sign in to comment.