From d5ff77b2782ccb29a3d5c446e51421144d2894c7 Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Fri, 12 Dec 2025 09:53:24 +0900 Subject: [PATCH 1/3] WIP --- src/hz_grids.erl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/hz_grids.erl b/src/hz_grids.erl index 4fc9381..17482c7 100644 --- a/src/hz_grids.erl +++ b/src/hz_grids.erl @@ -44,8 +44,8 @@ -spec url(Instruction, HTTP) -> Result when Instruction :: spend | transfer | sign, HTTP :: uri_string:uri_string(), - GRIDS :: uri_string:uri_string(), - Result :: {ok, GRIDS} | uri_string:uri_error(). + Result :: {ok, GRIDS} | uri_string:uri_error(), + GRIDS :: uri_string:uri_string(). %% @doc %% Takes @@ -66,6 +66,19 @@ url2(Instruction, URL = #{path := Path}) -> {ok, uri_string:recompose(GRIDS)}. +-spec url(Instruction, Context, Recipient, Payload, Amount) -> Result + when Instruction :: spend | transfer, + Context :: uri_string:uri_string() | string(), % + Recipient :: string(), + Payload :: binary(), + Amount :: non_neg_integer(), + Result :: {ok, GRIDS} | error | uri_string:uri_error(). + +url(spend, Context, Recipient, Payload, Amount) -> + url(spend, +url(transfer, Context, Recipient, Payload, Amount) -> + + -spec parse(GRIDS) -> Result when GRIDS :: string(), Result :: {ok, Instruction} | uri_string:error(), -- 2.30.2 From 88c6f6dcc7d31876146f4c71a3cd801a97573a75 Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Thu, 18 Dec 2025 04:13:01 +0900 Subject: [PATCH 2/3] Spend/Transfer URLs --- src/hz_grids.erl | 58 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/src/hz_grids.erl b/src/hz_grids.erl index 17482c7..7e54aa1 100644 --- a/src/hz_grids.erl +++ b/src/hz_grids.erl @@ -38,7 +38,7 @@ -module(hz_grids). -vsn("0.7.0"). --export([url/2, parse/1, req/2, req/3]). +-export([url/2, url/3, url/4, parse/1, req/2, req/3]). -spec url(Instruction, HTTP) -> Result @@ -66,17 +66,57 @@ url2(Instruction, URL = #{path := Path}) -> {ok, uri_string:recompose(GRIDS)}. --spec url(Instruction, Context, Recipient, Payload, Amount) -> Result - when Instruction :: spend | transfer, - Context :: uri_string:uri_string() | string(), % +-spec url(Instruction, Recipient, Amount) -> GRIDS + when Instruction :: {spend, Network} | {transfer, Node}, + Network :: string(), + Node :: {inet:ip_address() | inet:hostname(), inet:port_number()} + | uri_string:uri_string(), Recipient :: string(), - Payload :: binary(), Amount :: non_neg_integer(), - Result :: {ok, GRIDS} | error | uri_string:uri_error(). + GRIDS :: uri_string:uri_string(). +%% @doc +%% Forms a GRIDS URL for spends or transfers. +%% @equiv uri(Instruction, Recipient, Amount, ""). -url(spend, Context, Recipient, Payload, Amount) -> - url(spend, -url(transfer, Context, Recipient, Payload, Amount) -> +url(Instruction, Recipient, Amount) -> + url(Instruction, Recipient, Amount, ""). + + +-spec url(Instruction, Recipient, Amount, Payload) -> GRIDS + when Instruction :: {spend, Network} | {transfer, Node}, + Network :: string(), + Node :: {inet:ip_address() | inet:hostname(), inet:port_number()} + | uri_string:uri_string(), % "http://..." | "https://..." + Recipient :: string(), + Amount :: non_neg_integer(), + Payload :: binary(), + GRIDS :: uri_string:uri_string(). +%% @doc +%% Forms a GRIDS URL for spends or transfers. + +url({spend, Network}, Recipient, Amount, Payload) -> + Elements = ["grids://", Network, "/1/s/", Recipient, qwargs(Amount, Payload)], + unicode:characters_to_list(Elements); +url({transfer, Node}, Recipient, Amount, Payload) -> + Prefix = + case Node of + {H, P} -> ["grid://", h_to_s(H), ":", integer_to_list(P)]; + "https://" ++ H -> ["grids://", H]; + "http://" ++ H -> ["grid://", H]; + <<"https://", H/binary>> -> ["grids://", H]; + <<"http://", H/binary>> -> ["grid://", H] + end, + unicode:characters_to_list([Prefix, "/1/t/", Recipient, qwargs(Amount, Payload)]). + +h_to_s(Host) when is_list(Host) -> Host; +h_to_s(Host) when is_binary(Host) -> Host; +h_to_s(Host) when is_tuple(Host) -> inet:ntoa(Host); +h_to_s(Host) when is_atom(Host) -> atom_to_list(Host). + +qwargs(Amount, "") -> + ["?a=", integer_to_list(Amount)]; +qwargs(Amount, Payload) -> + [$? | uri_string:compose_query([{"a", integer_to_list(Amount)}, {"p", Payload}])]. -spec parse(GRIDS) -> Result -- 2.30.2 From f5e955b5836ab56b7dd7fd9c60bcb5af167c47cf Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Fri, 19 Dec 2025 21:34:06 +0900 Subject: [PATCH 3/3] Allow for a 'none' amount to prompt users for an amount. --- src/hz_grids.erl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/hz_grids.erl b/src/hz_grids.erl index 7e54aa1..6a9c6de 100644 --- a/src/hz_grids.erl +++ b/src/hz_grids.erl @@ -88,7 +88,7 @@ url(Instruction, Recipient, Amount) -> Node :: {inet:ip_address() | inet:hostname(), inet:port_number()} | uri_string:uri_string(), % "http://..." | "https://..." Recipient :: string(), - Amount :: non_neg_integer(), + Amount :: non_neg_integer() | none, Payload :: binary(), GRIDS :: uri_string:uri_string(). %% @doc @@ -113,8 +113,12 @@ h_to_s(Host) when is_binary(Host) -> Host; h_to_s(Host) when is_tuple(Host) -> inet:ntoa(Host); h_to_s(Host) when is_atom(Host) -> atom_to_list(Host). +qwargs(none, "") -> + []; qwargs(Amount, "") -> ["?a=", integer_to_list(Amount)]; +qwargs(none, Payload) -> + [$? | uri_string:compose_query([{"p", Payload}])]; qwargs(Amount, Payload) -> [$? | uri_string:compose_query([{"a", integer_to_list(Amount)}, {"p", Payload}])]. -- 2.30.2