From ddec3bfa7477a84ea8844a8996a026444c32858a Mon Sep 17 00:00:00 2001 From: Jarvis Carroll Date: Tue, 24 Feb 2026 06:12:00 +0000 Subject: [PATCH] add more format options to decode_bytearray I reversed the argument order here, since the Format option is sort of kind of almost optional, but I am not sure if that was a good idea. --- src/hz.erl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/hz.erl b/src/hz.erl index 84d1535..0ed98b6 100644 --- a/src/hz.erl +++ b/src/hz.erl @@ -71,7 +71,7 @@ contract_call/5, contract_call/6, contract_call/10, - decode_bytearray_fate/1, decode_bytearray/2, + decode_bytearray/2, spend/5, spend/10, sign_tx/2, sign_tx/3, sign_message/2, verify_signature/3, @@ -701,8 +701,9 @@ decode_bytearray_fate(EncodedStr) -> {ok, Object} end. --spec decode_bytearray(Type, EncodedStr) -> {ok, Result} | {error, Reason} - when Type :: term(), +-spec decode_bytearray(EncodedStr, Format) -> {ok, Result} | {error, Reason} + when Format :: fate | sophia | {sophia, Type} | {erlang, Type}, + Type :: term(), EncodedStr :: binary() | string(), Result :: none | term(), Reason :: term(). @@ -713,13 +714,18 @@ decode_bytearray_fate(EncodedStr) -> %% must be the result type of the same function in the same AACI that was used %% to create the transaction that EncodedStr came from. -decode_bytearray(Type, EncodedStr) -> +decode_bytearray(EncodedStr, Format) -> case decode_bytearray_fate(EncodedStr) of {ok, none} -> {ok, none}; - {ok, Object} -> hz_aaci:fate_to_erlang(Type, Object); + {ok, FATE} -> decode_bytearray2(FATE, Format); {error, Reason} -> {error, Reason} end. +decode_bytearray2(FATE, fate) -> FATE; +decode_bytearray2(FATE, sophia) -> hz_sophia:fate_to_list(FATE); +decode_bytearray2(FATE, {sophia, Type}) -> hz_sophia:fate_to_list(Type, FATE); +decode_bytearray2(FATE, {erlang, Type}) -> hz_aaci:fate_to_erlang(Type, FATE). + to_binary(S) when is_binary(S) -> S; to_binary(S) when is_list(S) -> list_to_binary(S).