forked from samcamwilliams/HyperBEAM
-
Notifications
You must be signed in to change notification settings - Fork 84
feat: token preloaded lib #1016
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
Open
charmful0x
wants to merge
4
commits into
edge
Choose a base branch
from
feat/exported-token-util
base: edge
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,82 @@ | ||
| %%% @doc Shared token account helpers. | ||
| %%% These helpers define token balance account semantics: external IDs are | ||
| %%% validated as raw binaries, then canonicalized only when mapped onto balance | ||
| %%% trie keys. | ||
| -module(lib_token). | ||
|
|
||
| -export([validate_address/3, account_key/1]). | ||
|
|
||
| %% @doc Validate an address before it is used as a token balance account. | ||
| %% Validation checks the raw external ID and its canonical balance key form so | ||
| %% mixed-case input cannot collide with AO/trie control keys after | ||
| %% canonicalization. | ||
| validate_address(Address, CustomList, Opts) when is_binary(Address), is_list(CustomList) -> | ||
| AccountKey = account_key(Address), | ||
| CanonicalCustomKeys = [account_key(Key) || Key <- CustomList, is_binary(Key)], | ||
| case byte_size(Address) of | ||
| 0 -> {error, <<"Address cannot be empty.">>}; | ||
| N when N > 128 -> {error, <<"Address is too long.">>}; | ||
| _ -> | ||
| TrieReservedKeys = trie_reserved_keys(Opts), | ||
| maybe | ||
| true ?= (AccountKey =/= <<"path">>) | ||
| orelse {error, <<"Address uses the reserved path key.">>}, | ||
| true ?= (not is_device_key(AccountKey, Opts)) | ||
| orelse {error, <<"Address uses a reserved device key.">>}, | ||
| true ?= (not is_reserved_trie_key(Address, TrieReservedKeys)) | ||
| orelse {error, <<"Address uses a reserved trie internal key.">>}, | ||
| true ?= (not is_reserved_trie_key(AccountKey, TrieReservedKeys)) | ||
| orelse {error, <<"Address uses a reserved trie internal key.">>}, | ||
| true ?= (not is_reserved_custom_key(Address, CustomList)) | ||
| orelse {error, <<"Address is a reserved custom key.">>}, | ||
| true ?= (not is_reserved_custom_key(AccountKey, CanonicalCustomKeys)) | ||
| orelse {error, <<"Address is a reserved custom key.">>}, | ||
| true ?= valid_address_chars(Address) | ||
| orelse {error, <<"Address contains unsupported characters.">>} | ||
| end | ||
| end; | ||
| validate_address(_, _, _) -> | ||
| {error, <<"Address must be a binary.">>}. | ||
|
|
||
| %% @doc Canonical token balance trie key for an external account ID. | ||
| account_key(Address) -> | ||
| hb_util:to_lower(hb_ao:normalize_key(Address)). | ||
|
|
||
| is_device_key(Key, Opts) -> | ||
| lists:any( | ||
| fun(Device) -> | ||
| case hb_device:message_to_fun( | ||
| #{<<"device">> => Device}, Key, Opts | ||
| ) of | ||
| {ok, _, _} -> true; | ||
| {add_key, _, _} -> false | ||
| end | ||
| end, | ||
| [<<"message@1.0">>, <<"trie@1.0">>] | ||
| ). | ||
|
|
||
| %% Trie metadata keys are not exposed as device operations. | ||
| trie_reserved_keys(Opts) -> | ||
| {ok, Trie} = hb_device_load:reference(<<"trie@1.0">>, Opts), | ||
| maps:get(reserved, Trie:info(), []). | ||
|
speeddragon marked this conversation as resolved.
|
||
|
|
||
| is_reserved_trie_key(Key, ReservedKeys) -> | ||
| lists:member(Key, ReservedKeys). | ||
|
|
||
| is_reserved_custom_key(Key, List) when is_binary(Key), is_list(List) -> | ||
| lists:member(Key, List); | ||
| is_reserved_custom_key(_, _) -> | ||
| false. | ||
|
|
||
| %% @doc Return true when every byte is in the supported account alphabet. | ||
| valid_address_chars(<<>>) -> | ||
| true; | ||
| valid_address_chars(<<Char, Rest/binary>>) when | ||
| Char >= $A, Char =< $Z; | ||
| Char >= $a, Char =< $z; | ||
| Char >= $0, Char =< $9; | ||
| Char =:= $_; | ||
| Char =:= $- -> | ||
| valid_address_chars(Rest); | ||
| valid_address_chars(_) -> | ||
| false. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have -- if we don't already -- a way to programmatically generate the list of device keys. in
hb_device. Themessage_to_funflow must use it somewhere, so we should just be able to extract it. Perhaps simple: Just usemessage_to_funwith#{ <<"device">> => DEV_NAME }, ... [key:] Address, ...)? If you get backfun dev_message:get/4then you know there were no hits. I think you can also specify the default, at least in theneo/edge-1.0branch.