Skip to content

refactor: make pubkey optional for decoded_json in keystore in decode_str! function #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2024
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
8 changes: 6 additions & 2 deletions lib/keystore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ defmodule Keystore do

privkey = decrypt!(decoded_json["crypto"], password)

pubkey = Map.fetch!(decoded_json, "pubkey") |> parse_binary!()

{:ok, derived_pubkey} = Bls.derive_pubkey(privkey)

pubkey =
case Map.has_key?(decoded_json, "pubkey") do
true -> Map.get(decoded_json, "pubkey") |> parse_binary!()
false -> derived_pubkey
end

if derived_pubkey != pubkey do
raise("Keystore secret and public keys don't form a valid pair")
end
Expand Down
32 changes: 32 additions & 0 deletions test/unit/keystore_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,36 @@ defmodule Unit.KeystoreTest do
{:ok, signature} = Bls.sign(privkey, digest)
assert Bls.valid?(pubkey, digest, signature)
end

test "eip scrypt without pubkey test vector" do
scrypt_json =
Jason.decode!(@pbkdf2_json)
|> Map.delete("pubkey")
|> Jason.encode!()

{pubkey, privkey} = Keystore.decode_str!(scrypt_json, @eip_password)

assert privkey == @eip_secret
assert pubkey == @pubkey

digest = :crypto.hash(:sha256, "test message")
{:ok, signature} = Bls.sign(privkey, digest)
assert Bls.valid?(pubkey, digest, signature)
end

test "eip pbkdf2 without pubkey test vector" do
pbkdf2_json =
Jason.decode!(@pbkdf2_json)
|> Map.delete("pubkey")
|> Jason.encode!()

{pubkey, privkey} = Keystore.decode_str!(pbkdf2_json, @eip_password)

assert privkey == @eip_secret
assert pubkey == @pubkey

digest = :crypto.hash(:sha256, "test message")
{:ok, signature} = Bls.sign(privkey, digest)
assert Bls.valid?(pubkey, digest, signature)
end
end
Loading