diff --git a/src/hz.erl b/src/hz.erl index 086dbb7..4388ff2 100644 --- a/src/hz.erl +++ b/src/hz.erl @@ -792,11 +792,20 @@ contract_code(ID) -> contract_source(ID) -> case request(["/v3/contracts/", ID, "/source"]) of - {ok, #{"source" := Source}} -> {ok, Source}; + {ok, #{"source" := Source}} -> extract(Source); {ok, #{"reason" := Reason}} -> {error, Reason}; Error -> Error end. +extract(TarBaby) -> + case erl_tar:extract({binary, TarBaby}, [memory, compressed]) of + {ok, Project} -> + {ok, Project}; + Error -> + tell(info, "non-compressed thingy returned: ~tp", [Error]), + {ok, TarBaby} + end. + -spec contract_poi(ID) -> {ok, Bytecode} | {error, Reason} when ID :: contract_id(), @@ -1049,6 +1058,7 @@ contract_create(CreatorID, Path, InitArgs) -> contract_create(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Path, InitArgs) -> case file:read_file(Path) of {ok, Source} -> + Name = filename:basename(Path), Dir = filename:dirname(Path), {ok, CWD} = file:get_cwd(), SrcDir = so_utils:canonical_dir(Path), @@ -1058,17 +1068,18 @@ contract_create(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Path, InitArgs) -> {src_dir, SrcDir}, {include, {file_system, [CWD, so_utils:canonical_dir(Dir)]}}], contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, - Source, Options, InitArgs); + Name, Source, Options, InitArgs); Error -> Error end. -contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Source, Options, InitArgs) -> +contract_create2(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Name, Source, Options, InitArgs) -> case so_compiler:from_string(Source, Options) of {ok, Compiled} -> + Named = maps:put(contract_name, Name, Compiled), contract_create_built(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, - Compiled, InitArgs); + Named, InitArgs); Error -> Error end. @@ -1146,7 +1157,7 @@ assemble_calldata(CreatorID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallDa assemble_calldata2(OwnerID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallData) -> Code = gmser_contract_code:serialize(Compiled), - Source = unicode:characters_to_binary(maps:get(contract_source, Compiled, <<>>)), + Source = bundle_source(Compiled), VM = 1, ABI = 1, <> = <>, @@ -1183,6 +1194,29 @@ assemble_calldata2(OwnerID, Nonce, Gas, GasPrice, Amount, TTL, Compiled, CallDat error:Reason -> {error, Reason} end. +bundle_source(Compiled) -> + case maps:find(contract_source, Compiled) of + {ok, Source} -> bundle_source2(unicode:characters_to_binary(Source), Compiled); + error -> <<>> + end. + +bundle_source2(Source, Compiled) -> + File = unicode:characters_to_binary(maps:get(contract_name, Compiled, <<"contract.aes">>)), + <> = crypto:strong_rand_bytes(8), + Rand = integer_to_binary(RN, 36), + TmpDir = filename:join(zx_lib:path(tmp), Rand), + TgzName = <>, + TarGzPath = filename:join(TmpDir, TgzName), + ok = filelib:ensure_dir(TarGzPath), + {ok, CWD} = file:get_cwd(), + ok = file:set_cwd(TmpDir), + ok = file:write_file(File, Source), + ok = erl_tar:create(TarGzPath, [File], [compressed]), + {ok, TgzBin} = file:read_file(TarGzPath), + ok = file:set_cwd(CWD), + ok = file:del_dir_r(TmpDir), + TgzBin. + -spec read_aci(Path) -> Result when Path :: file:filename(),