-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose API to get the shared origin keys (#292)
Fixes #158
- Loading branch information
Showing
7 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/archethic_web/controllers/api/origin_key_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule ArchEthicWeb.API.OriginKeyController do | ||
use ArchEthicWeb, :controller | ||
|
||
alias ArchEthic.Crypto | ||
alias ArchEthic.SharedSecrets | ||
|
||
alias ArchEthic.TransactionChain | ||
alias ArchEthic.TransactionChain.TransactionData.Ownership | ||
|
||
def origin_key(conn, params) do | ||
with %{"origin_public_key" => origin_public_key} <- params, | ||
{:ok, origin_public_key} <- Base.decode16(origin_public_key, case: :mixed), | ||
true <- Crypto.valid_public_key?(origin_public_key), | ||
<<_curve_id::8, origin_id::8, _rest::binary>> <- origin_public_key, | ||
{first_origin_family_public_key, _} <- | ||
SharedSecrets.get_origin_family_from_origin_id(origin_id) | ||
|> SharedSecrets.get_origin_family_seed() | ||
|> Crypto.derive_keypair(0), | ||
{:ok, tx} <- | ||
Crypto.derive_address(first_origin_family_public_key) | ||
|> TransactionChain.get_last_transaction(data: [:ownerships]), | ||
ownership when ownership != nil <- | ||
Enum.find(tx.data.ownerships, fn ownership -> | ||
Ownership.authorized_public_key?(ownership, origin_public_key) | ||
end) do | ||
res = %{ | ||
encrypted_origin_private_keys: Base.encode16(ownership.secret), | ||
encrypted_secret_key: | ||
Ownership.get_encrypted_key(ownership, origin_public_key) |> Base.encode16() | ||
} | ||
|
||
conn | ||
|> put_status(:ok) | ||
|> json(res) | ||
else | ||
er when er in [:error, false] -> | ||
conn | ||
|> put_status(400) | ||
|> json(%{ | ||
error: "Invalid public key" | ||
}) | ||
|
||
{:error, _} -> | ||
conn | ||
|> put_status(404) | ||
|> json(%{ | ||
error: "Public key not found" | ||
}) | ||
|
||
nil -> | ||
conn | ||
|> put_status(404) | ||
|> json(%{ | ||
error: "Public key not found" | ||
}) | ||
|
||
_ -> | ||
conn | ||
|> put_status(404) | ||
|> json(%{ | ||
error: "Invalid parameters" | ||
}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
test/archethic_web/controllers/api/origin_key_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule ArchEthicWeb.API.OriginKeyControllerTest do | ||
use ArchEthicCase | ||
use ArchEthicWeb.ConnCase | ||
|
||
alias ArchEthic.P2P | ||
alias ArchEthic.P2P.Node | ||
alias ArchEthic.Crypto | ||
|
||
alias ArchEthic.TransactionChain.Transaction | ||
alias ArchEthic.TransactionChain.TransactionData | ||
alias ArchEthic.TransactionChain.TransactionData.Ownership | ||
|
||
import Mox | ||
|
||
setup do | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {127, 0, 0, 1}, | ||
port: 3000, | ||
first_public_key: Crypto.last_node_public_key(), | ||
last_public_key: Crypto.last_node_public_key(), | ||
network_patch: "AAA", | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now() | ||
}) | ||
|
||
:ok | ||
end | ||
|
||
describe "origin_key/2" do | ||
test "should send not_found response for invalid params", %{conn: conn} do | ||
conn = | ||
post(conn, "/api/origin_key", %{ | ||
origin_public_key: "0001540315" | ||
}) | ||
|
||
assert %{ | ||
"error" => "Invalid public key" | ||
} = json_response(conn, 400) | ||
end | ||
|
||
test "should send not_found response when public key isn't found in owner transactions", %{ | ||
conn: conn | ||
} do | ||
MockDB | ||
|> stub(:get_transaction, fn _, _ -> | ||
{:ok, | ||
%Transaction{ | ||
data: %TransactionData{ | ||
ownerships: [ | ||
%Ownership{ | ||
secret: Base.decode16!("0001AAAAAA"), | ||
authorized_keys: %{Base.decode16!("0001BBBBBB") => Base.decode16!("0001CCCCCC")} | ||
} | ||
] | ||
} | ||
}} | ||
end) | ||
|
||
MockDB | ||
|> stub(:get_last_chain_address, fn _ -> | ||
Base.decode16!("0001DDDDDD") | ||
end) | ||
|
||
conn = | ||
post(conn, "/api/origin_key", %{ | ||
origin_public_key: | ||
"00015403152aeb59b1b584d77c8f326031815674afeade8cba25f18f02737d599c39" | ||
}) | ||
|
||
assert %{ | ||
"error" => "Public key not found" | ||
} = json_response(conn, 404) | ||
end | ||
|
||
test "should send json secret values response when public key is found in owner transactions", | ||
%{conn: conn} do | ||
MockDB | ||
|> stub(:get_transaction, fn _, _ -> | ||
{:ok, | ||
%Transaction{ | ||
data: %TransactionData{ | ||
ownerships: [ | ||
%Ownership{ | ||
secret: Base.decode16!("0001AAAAAA"), | ||
authorized_keys: %{ | ||
Base.decode16!( | ||
"00015403152aeb59b1b584d77c8f326031815674afeade8cba25f18f02737d599c39", | ||
case: :mixed | ||
) => Base.decode16!("0001CCCCCC") | ||
} | ||
} | ||
] | ||
} | ||
}} | ||
end) | ||
|
||
MockDB | ||
|> stub(:get_last_chain_address, fn _ -> | ||
Base.decode16!("0001DDDDDD") | ||
end) | ||
|
||
conn = | ||
post(conn, "/api/origin_key", %{ | ||
origin_public_key: | ||
"00015403152aeb59b1b584d77c8f326031815674afeade8cba25f18f02737d599c39" | ||
}) | ||
|
||
assert %{ | ||
"encrypted_origin_private_keys" => "0001AAAAAA", | ||
"encrypted_secret_key" => "0001CCCCCC" | ||
} = json_response(conn, 200) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters