This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move NUT01 code it it's own folder
- Loading branch information
Showing
10 changed files
with
179 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
defmodule Cashubrew.Nuts.Nut01.Keyset do | ||
@moduledoc """ | ||
Contains the logic required for the Mint to generate keys and keysets | ||
""" | ||
alias Cashubrew.Nuts.Nut00.BDHKE | ||
|
||
import Bitwise | ||
|
||
defp max_order do | ||
64 | ||
end | ||
|
||
@doc """ | ||
Generates a keyset from a seed and derivation path. | ||
## Parameters | ||
- seed: A binary representing the seed. | ||
- derivation_path: A string representing the derivation path (e.g., "m/0'/0'/0'"). | ||
## Returns | ||
- A map containing amounts mapped to their corresponding private and public keys. | ||
""" | ||
def generate_keys(seed, derivation_path \\ "m/0'/0'/0'") do | ||
encoded_seed = | ||
seed | ||
|> :unicode.characters_to_binary(:utf8) | ||
|> Base.encode16(case: :lower) | ||
|
||
{master_private_key, master_chain_code} = BlockKeys.CKD.master_keys(encoded_seed) | ||
root_key = BlockKeys.CKD.master_private_key({master_private_key, master_chain_code}) | ||
|
||
Enum.map(0..(max_order() - 1), fn i -> | ||
amount = 1 <<< i | ||
child_path = "#{derivation_path}/#{i}" | ||
child_key = BlockKeys.CKD.derive(root_key, child_path) | ||
|
||
private_key = BlockKeys.Encoding.decode_extended_key(child_key) | ||
private_key_bytes = :binary.part(private_key.key, 1, 32) | ||
|
||
{_, public_key} = BDHKE.generate_keypair(private_key_bytes) | ||
|
||
%{ | ||
amount: amount, | ||
private_key: private_key_bytes, | ||
public_key: public_key | ||
} | ||
end) | ||
end | ||
|
||
@spec derive_keyset_id([map()]) :: <<_::16, _::_*8>> | ||
@doc """ | ||
Derives a keyset ID from a set of public keys. | ||
## Parameters | ||
- keys: A list of maps containing public keys. | ||
## Returns | ||
- A string representing the keyset ID. | ||
""" | ||
def derive_keyset_id(keys) do | ||
sorted_keys = Enum.sort_by(keys, & &1.amount) | ||
pubkeys_concat = Enum.map(sorted_keys, & &1.public_key) |> IO.iodata_to_binary() | ||
|
||
"00" <> | ||
(:crypto.hash(:sha256, pubkeys_concat) |> Base.encode16(case: :lower) |> binary_part(0, 14)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Cashubrew.Nuts.Nut01.Routes do | ||
@moduledoc """ | ||
List the rest routes defined in the NUT-01 | ||
""" | ||
|
||
@doc """ | ||
The route to query active keys from the Mint | ||
""" | ||
def v1_keys do | ||
"/v1/keys" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
defmodule Cashubrew.Nuts.Nut01.Serde.GetKeysResponse do | ||
@moduledoc """ | ||
The body of the get keys rest request | ||
""" | ||
@enforce_keys [:keysets] | ||
defstruct [:keysets] | ||
|
||
def from_keysets(keysets) do | ||
%__MODULE__{keysets: inner_from_keysets(keysets, [])} | ||
end | ||
|
||
defp inner_from_keysets([], accumulator) do | ||
accumulator | ||
end | ||
|
||
defp inner_from_keysets([%{id: id, unit: unit, keys: keys}, tail], accumulator) do | ||
inner_from_keysets(tail, [ | ||
Cashubrew.Nuts.Nut01.Serde.Keyset.from_keyset(id, unit, keys) | accumulator | ||
]) | ||
end | ||
end | ||
|
||
defmodule Cashubrew.Nuts.Nut01.Serde.Keys do | ||
@moduledoc """ | ||
Keys for the Cashubrew mint. | ||
""" | ||
defstruct [:pairs] | ||
end | ||
|
||
defimpl Jason.Encoder, for: Cashubrew.Nuts.Nut01.Serde.Keys do | ||
def encode(%Cashubrew.Nuts.Nut01.Keys{pairs: pairs}, opts) do | ||
# Convert amounts to strings and sort them in ascending order | ||
sorted_pairs = | ||
pairs | ||
|> Enum.map(fn {amount, pubkey} -> {to_string(amount), pubkey} end) | ||
|> Enum.sort_by( | ||
fn {amount_str, _} -> | ||
{amount_int, ""} = Integer.parse(amount_str) | ||
amount_int | ||
end, | ||
:asc | ||
) | ||
|
||
# Encode the sorted pairs as JSON | ||
json_pairs = | ||
sorted_pairs | ||
|> Enum.map(fn {key, value} -> | ||
[Jason.Encode.string(key, opts), ?:, Jason.Encode.string(value, opts)] | ||
end) | ||
|
||
json_pairs = Enum.intersperse(json_pairs, ?,) | ||
["{", json_pairs, "}"] | ||
end | ||
end | ||
|
||
defmodule Cashubrew.Nuts.Nut01.Serde.Keyset do | ||
@moduledoc """ | ||
A keyset | ||
""" | ||
@enforce_keys [:id, :unit, :keys] | ||
defstruct [:id, :unit, :keys] | ||
|
||
def from_keyset(id, unit, keys) do | ||
keys_list = | ||
Enum.map(keys, fn key -> {key.amount, Base.encode16(key.public_key, case: :lower)} end) | ||
|
||
%__MODULE__{ | ||
id: id, | ||
unit: unit, | ||
keys: %Cashubrew.Nuts.Nut01.Serde.Keys{pairs: keys_list} | ||
} | ||
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.