Skip to content
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

Fix the shared secret keystore loading #279

Merged
merged 1 commit into from
Apr 13, 2022
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
61 changes: 30 additions & 31 deletions lib/archethic/crypto/keystore/shared_secrets/software_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,8 @@ defmodule ArchEthic.Crypto.SharedSecretsKeystore.SoftwareImpl do

load_storage_nonce()

node_shared_secrets_chain =
TransactionChain.list_transactions_by_type(
:node_shared_secrets,
[
:address,
data: [:ownerships],
validation_stamp: [:timestamp]
]
)

nb_node_shared_secrets_keys = Enum.count(node_shared_secrets_chain)
nb_node_shared_secrets_keys =
TransactionChain.count_transactions_by_type(:node_shared_secrets)

Logger.info("Node shared secrets keys positioned at #{nb_node_shared_secrets_keys}")

Expand All @@ -54,7 +45,11 @@ defmodule ArchEthic.Crypto.SharedSecretsKeystore.SoftwareImpl do
:ets.insert(@keystore_table, {:shared_secrets_index, nb_node_shared_secrets_keys})
:ets.insert(@keystore_table, {:network_pool_index, nb_network_pool_keys})

load_node_shared_secrets_chain(node_shared_secrets_chain)
:node_shared_secrets
|> TransactionChain.list_addresses_by_type()
|> Enum.at(-1)
|> load_node_shared_secrets_tx()

{:ok, %{}}
end

Expand All @@ -68,29 +63,33 @@ defmodule ArchEthic.Crypto.SharedSecretsKeystore.SoftwareImpl do
end
end

defp load_node_shared_secrets_chain(chain) do
case Enum.at(chain, 0) do
nil ->
:ok
defp load_node_shared_secrets_tx(nil), do: :ok

defp load_node_shared_secrets_tx(address) do
{:ok,
%Transaction{
data: %TransactionData{
ownerships: [ownership = %Ownership{secret: secret}]
},
validation_stamp: %ValidationStamp{timestamp: timestamp}
}} =
TransactionChain.get_transaction(address, [
:address,
data: [:ownerships],
validation_stamp: [:timestamp]
])

%Transaction{
address: address,
data: %TransactionData{ownerships: [ownership = %Ownership{secret: secret}]},
validation_stamp: %ValidationStamp{timestamp: timestamp}
} ->
if Ownership.authorized_public_key?(ownership, Crypto.last_node_public_key()) do
encrypted_secret_key =
Ownership.get_encrypted_key(ownership, Crypto.last_node_public_key())
if Ownership.authorized_public_key?(ownership, Crypto.last_node_public_key()) do
encrypted_secret_key = Ownership.get_encrypted_key(ownership, Crypto.last_node_public_key())

daily_nonce_date = SharedSecrets.next_application_date(timestamp)
daily_nonce_date = SharedSecrets.next_application_date(timestamp)

:ok = do_unwrap_secrets(secret, encrypted_secret_key, daily_nonce_date)
:ok = do_unwrap_secrets(secret, encrypted_secret_key, daily_nonce_date)

Logger.info("Node shared secrets loaded",
transaction_address: Base.encode16(address),
transaction_type: :node_shared_secrets
)
end
Logger.info("Node shared secrets loaded",
transaction_address: Base.encode16(address),
transaction_type: :node_shared_secrets
)
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/archethic/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ defmodule ArchEthic.DB do
Enumerable.t()
@callback count_transactions_by_type(type :: Transaction.transaction_type()) ::
non_neg_integer()

@callback list_addresses_by_type(Transaction.transaction_type()) ::
Enumerable.t() | list(binary())
@callback get_last_chain_address(binary()) :: binary()
@callback get_last_chain_address(binary(), DateTime.t()) :: binary()
@callback get_first_chain_address(binary()) :: binary()
Expand Down
8 changes: 8 additions & 0 deletions lib/archethic/db/embedded_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ defmodule ArchEthic.DB.EmbeddedImpl do
end)
end

@doc """
Stream all the addresses for a transaction type
"""
@spec list_addresses_by_type(Transaction.transaction_type()) :: Enumerable.t() | list(binary())
def list_addresses_by_type(type) when is_atom(type) do
ChainIndex.get_addresses_by_type(type, db_path())
end

@doc """
Count the number of transactions for a given type
"""
Expand Down
6 changes: 6 additions & 0 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ defmodule ArchEthic.TransactionChain do
@spec count_transactions_by_type(type :: Transaction.transaction_type()) :: non_neg_integer()
defdelegate count_transactions_by_type(type), to: DB

@doc """
Stream all the addresses for a transaction type
"""
@spec list_addresses_by_type(Transaction.transaction_type()) :: Enumerable.t() | list(binary())
defdelegate list_addresses_by_type(type), to: DB

@doc """
Get the last transaction address from a transaction chain
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ defmodule ArchEthic.Crypto.SharedSecrets.SoftwareImplTest do
timestamp = ~U[2021-10-10 00:00:00Z]

MockDB
|> expect(:count_transactions_by_type, fn
|> stub(:count_transactions_by_type, fn
:node_rewards -> 1
:node_shared_secrets -> 1
end)
|> expect(:list_transactions_by_type, fn :node_shared_secrets, _ ->
[
%Transaction{
data: %TransactionData{
ownerships: [Ownership.new(secrets, aes_key, [Crypto.last_node_public_key()])]
},
validation_stamp: %ValidationStamp{
timestamp: timestamp
}
}
]
|> expect(:list_addresses_by_type, fn :node_shared_secrets ->
[:crypto.strong_rand_bytes(32)]
end)
|> expect(:get_transaction, fn _, _ ->
{:ok,
%Transaction{
data: %TransactionData{
ownerships: [Ownership.new(secrets, aes_key, [Crypto.last_node_public_key()])]
},
validation_stamp: %ValidationStamp{
timestamp: timestamp
}
}}
end)

{:ok, _pid} = Keystore.start_link()
Expand Down
1 change: 1 addition & 0 deletions test/support/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ defmodule ArchEthicCase do
|> stub(:chain_size, fn _ -> 0 end)
|> stub(:list_transactions_by_type, fn _, _ -> [] end)
|> stub(:count_transactions_by_type, fn _ -> 0 end)
|> stub(:list_addresses_by_type, fn _ -> [] end)
|> stub(:list_transactions, fn _ -> [] end)
|> stub(:transaction_exists?, fn _ -> false end)
|> stub(:register_p2p_summary, fn _, _, _, _ -> :ok end)
Expand Down