-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule BlockKeys.Solana.Address do | ||
@moduledoc """ | ||
Converts a public extended key into a Solana Address | ||
""" | ||
|
||
alias BlockKeys.Base58 | ||
|
||
def from_xpub(xpub) do | ||
xpub | ||
|> maybe_decode() | ||
|> Base58.encode() | ||
end | ||
|
||
def valid_address?(address) when byte_size(address) in 32..44 do | ||
public_key = address |> BlockKeys.Base58.decode() |> :binary.encode_unsigned() | ||
Ed25519.on_curve?(public_key) | ||
end | ||
|
||
def valid_address?(_address), do: false | ||
|
||
defp maybe_decode(<<"xpub", encoded_key::binary>> = _xpub) do | ||
encoded_key | ||
|> decode_extended_key() | ||
|> Map.fetch!(:key) | ||
end | ||
|
||
defp maybe_decode(key), do: key | ||
|
||
defp decode_extended_key(key) do | ||
decoded_key = | ||
Base58.decode(key) | ||
|> :binary.encode_unsigned() | ||
|
||
<< | ||
version_number::binary-4, | ||
depth::binary-1, | ||
fingerprint::binary-4, | ||
index::binary-4, | ||
chain_code::binary-32, | ||
key::binary-32, | ||
_checksum::binary-4 | ||
>> = decoded_key | ||
|
||
%{ | ||
version_number: version_number, | ||
depth: depth, | ||
fingerprint: fingerprint, | ||
index: index, | ||
chain_code: chain_code, | ||
key: key | ||
} | ||
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,13 @@ | ||
defmodule BlockKeys.Solana do | ||
@moduledoc """ | ||
Helper module to derive and convert to a Solana Address | ||
""" | ||
|
||
alias BlockKeys.Solana.Address | ||
alias BlockKeys.CKD | ||
|
||
def address(key, path) do | ||
CKD.derive(key, path, network: :solana) | ||
|> Address.from_xpub() | ||
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 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,27 @@ | ||
defmodule SolanaAddressTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias BlockKeys.Solana.Address | ||
alias BlockKeys.CKD | ||
|
||
test "address from mnemonic" do | ||
root_key = | ||
BlockKeys.from_mnemonic( | ||
"nurse grid sister metal flock choice system control about mountain sister rapid hundred render shed chicken print cover tape sister zero bronze tattoo stairs" | ||
) | ||
|
||
assert root_key == | ||
"xprv9s21ZrQH143K35qGjQ6GG1wGHFZP7uCZA1WBdUJA8vBZqESQXQGA4A9d4eve5JqWB5m8YTMcNe8cc7c3FVzDGNcmiabi9WQycbFeEvvJF2D" | ||
|
||
assert CKD.derive(root_key, "M/44'/501'/0'/0/0", network: :solana) | ||
|> Address.from_xpub() == | ||
"4U76rEGDx595M46rWgoA7LwtA821BWCU9CkwG8zbJ6xa" | ||
end | ||
|
||
test "check if address is valid" do | ||
valid_address = "4U76rEGDx595M46rWgoA7LwtA821BWCU9CkwG8zbJ6xa" | ||
assert Address.valid_address?(valid_address) == true | ||
invalid_address = "ABCDEFG1234567" | ||
assert Address.valid_address?(invalid_address) == false | ||
end | ||
end |