Packages

Various modules and helpers for working with Nimiq primitives in the Gleam programming language or as a CLI

Current section

Files

Jump to
nimiq_gleam src cli@new_transaction_command.erl
Raw

src/cli@new_transaction_command.erl

-module(cli@new_transaction_command).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([run/0]).
-spec fee_flag() -> glint:flag(integer()).
fee_flag() ->
_pipe = glint:int_flag(<<"fee"/utf8>>),
_pipe@1 = glint:flag_default(_pipe, 0),
glint:flag_help(
_pipe@1,
<<"Set the transaction's fee in luna. Default is 0."/utf8>>
).
-spec msg_flag() -> glint:flag(binary()).
msg_flag() ->
_pipe = glint:string_flag(<<"msg"/utf8>>),
_pipe@1 = glint:flag_help(
_pipe,
<<"Add a message to the transaction, maximum 64 bytes."/utf8>>
),
glint:flag_constraint(
_pipe@1,
fun(Msg) -> case erlang:byte_size(Msg) =< 64 of
true ->
{ok, Msg};
false ->
{error,
snag:new(
<<"msg must not be longer than 64 bytes"/utf8>>
)}
end end
).
-spec network_flag() -> glint:flag(integer()).
network_flag() ->
_pipe = glint:int_flag(<<"network"/utf8>>),
_pipe@1 = glint:flag_default(_pipe, 5),
_pipe@3 = glint:flag_constraint(
_pipe@1,
begin
_pipe@2 = [5, 24],
glint@constraint:one_of(_pipe@2)
end
),
glint:flag_help(
_pipe@3,
<<"Set the network ID. Albatross Testnet => 5, Albatross Mainnet => 24. Default is 5."/utf8>>
).
-spec private_key_flag() -> glint:flag(binary()).
private_key_flag() ->
_pipe = glint:string_flag(<<"sign-with"/utf8>>),
_pipe@1 = glint:flag_help(
_pipe,
<<"Set the private key to sign the transaction with."/utf8>>
),
glint:flag_constraint(
_pipe@1,
fun(Key) -> case gleam_stdlib:base16_decode(Key) of
{ok, _} ->
{ok, Key};
{error, _} ->
{error,
snag:new(
<<"private key must be 32 bytes in hex format"/utf8>>
)}
end end
).
-spec run() -> glint:command(nil).
run() ->
glint:command_help(
<<"Creates Nimiq transactions"/utf8>>,
fun() ->
glint:named_arg(
<<"SENDER"/utf8>>,
fun(Sender) ->
glint:named_arg(
<<"RECIPIENT"/utf8>>,
fun(Recipient) ->
glint:named_arg(
<<"VALUE_LUNA"/utf8>>,
fun(Value) ->
glint:flag(
fee_flag(),
fun(Fee) ->
glint:flag(
msg_flag(),
fun(Msg) ->
glint:named_arg(
<<"VALIDITY_START_HEIGHT"/utf8>>,
fun(
Validity_start_height
) ->
glint:flag(
network_flag(),
fun(Network_id) ->
glint:flag(
private_key_flag(
),
fun(
Private_key
) ->
glint:command(
fun(
Named,
_,
Flags
) ->
_assert_subject = begin
_pipe = Sender(
Named
),
account@address:from_string(
_pipe
)
end,
{ok,
Sender@1} = case _assert_subject of
{ok,
_} -> _assert_subject;
_assert_fail ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 74}
)
end,
_assert_subject@1 = begin
_pipe@1 = Recipient(
Named
),
account@address:from_string(
_pipe@1
)
end,
{ok,
Recipient@1} = case _assert_subject@1 of
{ok,
_} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 75}
)
end,
_assert_subject@2 = begin
_pipe@2 = Value(
Named
),
_pipe@3 = gleam@int:parse(
_pipe@2
),
gleam@result:map(
_pipe@3,
fun(Field@0) -> {coin, Field@0} end
)
end,
{ok,
Value@1} = case _assert_subject@2 of
{ok,
_} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 76}
)
end,
_assert_subject@3 = begin
_pipe@4 = Fee(
Flags
),
gleam@result:map(
_pipe@4,
fun(Field@0) -> {coin, Field@0} end
)
end,
{ok,
Fee@1} = case _assert_subject@3 of
{ok,
_} -> _assert_subject@3;
_assert_fail@3 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@3,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 77}
)
end,
Data = begin
_pipe@5 = Msg(
Flags
),
_pipe@6 = gleam@result:map(
_pipe@5,
fun gleam_stdlib:identity/1
),
gleam@result:unwrap(
_pipe@6,
<<>>
)
end,
_assert_subject@4 = begin
_pipe@7 = Validity_start_height(
Named
),
gleam@int:parse(
_pipe@7
)
end,
{ok,
Validity_start_height@1} = case _assert_subject@4 of
{ok,
_} -> _assert_subject@4;
_assert_fail@4 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@4,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 80}
)
end,
_assert_subject@5 = begin
_pipe@8 = Network_id(
Flags
),
gleam@result:map(
_pipe@8,
fun(
Num
) ->
_pipe@9 = transaction@enums:to_network_id(
Num
),
utils@misc:unwrap(
_pipe@9
)
end
)
end,
{ok,
Network_id@1} = case _assert_subject@5 of
{ok,
_} -> _assert_subject@5;
_assert_fail@5 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@5,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 82}
)
end,
Tx = transaction@transaction:new_basic_with_data(
Sender@1,
Recipient@1,
Data,
Value@1,
Fee@1,
Validity_start_height@1,
Network_id@1,
none
),
_pipe@11 = case Private_key(
Flags
) of
{error,
_} ->
Tx;
{ok,
Private_key@1} ->
_assert_subject@6 = begin
_pipe@10 = Private_key@1,
key@ed25519@private_key:from_hex(
_pipe@10
)
end,
{ok,
Private_key@2} = case _assert_subject@6 of
{ok,
_} -> _assert_subject@6;
_assert_fail@6 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@6,
module => <<"cli/new_transaction_command"/utf8>>,
function => <<"run"/utf8>>,
line => 102}
)
end,
Public_key = key@ed25519@public_key:derive_key(
Private_key@2
),
Signature = key@ed25519@signature:create(
Private_key@2,
Public_key,
transaction@transaction:serialize_content(
Tx
)
),
Proof = transaction@signature_proof:single_sig(
{ed_dsa_public_key,
Public_key},
{ed_dsa_signature,
Signature}
),
transaction@transaction:set_proof(
Tx,
transaction@signature_proof:serialize(
Proof
)
)
end,
_pipe@12 = transaction@transaction:to_hex(
_pipe@11
),
_pipe@13 = utils@misc:unwrap(
_pipe@12
),
gleam@io:println(
_pipe@13
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).