From 2d4350fa4907f153553e0437cedc2b525eba4911 Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Tue, 4 Mar 2025 20:11:11 +0900 Subject: [PATCH] Initial --- .gitignore | 15 +++++++++ counter.aes | 13 ++++++++ toy_caller.erl | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 .gitignore create mode 100644 counter.aes create mode 100644 toy_caller.erl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..20177b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.eunit +deps +*.o +*.beam +*.plt +*.swp +erl_crash.dump +ebin/*.beam +doc/*.html +doc/*.css +doc/edoc-info +doc/erlang.png +rel/example_project +.concrete/DEV_MODE +.rebar diff --git a/counter.aes b/counter.aes new file mode 100644 index 0000000..11234a4 --- /dev/null +++ b/counter.aes @@ -0,0 +1,13 @@ +@compiler >= 9.0.0 + +contract Counter = + record state = {value : int} + + stateful entrypoint init() = + {value = 1} + + stateful entrypoint increment() = + put({value = state.value + 1}) + + entrypoint value() = + state.value \ No newline at end of file diff --git a/toy_caller.erl b/toy_caller.erl new file mode 100644 index 0000000..66617ed --- /dev/null +++ b/toy_caller.erl @@ -0,0 +1,84 @@ +-module(toy_caller). + +-export([new_keys/0, get_source/0, deploy/3]). + + +new_keys() -> + #{public := PubKey, secret := SecKey = <>} = ecu_eddsa:sign_keypair(), + Phrase = gd_key_master:encode(K), + ID = gmser_api_encoder:encode(account_pubkey, PubKey), + {ID, SecKey, Phrase}. + +get_source() -> + file:read_file("counter.aes"). + + +deploy(Source, PubKey, SecKey) -> + case compile(Source) of + {ok, Build} -> deploy2(Build, PubKey, SecKey); + Error -> Error + end. + +deploy2(Build, PubKey, SecKey) -> + ChainID = <<"groot.testnet">>, % NOTE: This obviously should come from the chain + {ok, Height} = hz:top_height(), + {ok, Nonce} = hz:next_nonce(PubKey), + TTL = Height + 10000, + GasPrice = hz:min_gas_price(), + Gas = 5000000, + Amount = 0, + InitArgs = [], % NOTE: This would be a list of strings, had this contract arguments + case hz:contract_create_built(PubKey, + Nonce, Amount, TTL, Gas, GasPrice, + Build, InitArgs) of + {ok, CreateTX} -> deploy3(SecKey, CreateTX, ChainID); + Error -> gmc_v_devman:trouble(Error) + end. + +deploy3(SecKey, CreateTX, ChainID) -> + SignedTX = sign_tx_hash(CreateTX, SecKey, ChainID), + case hz:post_tx(SignedTX) of + {ok, Data = #{"tx_hash" := TXHash}} -> + ok = io:format("The transaction hash is: ~p", [TXHash]), + deploy4(Data); + {ok, WTF} -> + gmc_v_devman:trouble({error, WTF}); + Error -> + gmc_v_devman:trouble(Error) + end. + +deploy4(#{"tx_hash" := TXHash}) -> + case hz:tx_info(TXHash) of + {ok, #{"call_info" := #{"return_type" := "ok", "contract_id" := ConID}}} -> + gmc_v_devman:open_contract(ConID); + {error, "Tx not mined"} -> + gmc_v_devman:trouble({tx_hash, TXHash}); + {ok, Reason = #{"call_info" := #{"return_type" := "revert"}}} -> + gmc_v_devman:trouble({error, Reason}); + Error -> + gmc_v_devman:trouble(Error) + end. + + + +compile(Source) -> + Options = [{aci, json}], + so_compiler:from_string(Source, Options). + + + +sign_tx_hash(Unsigned, SecKey, NetworkID) -> + {ok, TX_Data} = gmser_api_encoder:safe_decode(transaction, Unsigned), + {ok, Hash} = eblake2:blake2b(32, TX_Data), + NetworkHash = <>, + Signature = ecu_eddsa:sign_detached(NetworkHash, SecKey), + SigTxType = signed_tx, + SigTxVsn = 1, + SigTemplate = + [{signatures, [binary]}, + {transaction, binary}], + TX = + [{signatures, [Signature]}, + {transaction, TX_Data}], + SignedTX = gmser_chain_objects:serialize(SigTxType, SigTxVsn, SigTemplate, TX), + gmser_api_encoder:encode(transaction, SignedTX).