From f770bc299ed88adfaae6b3249f69b28c1b8079a2 Mon Sep 17 00:00:00 2001 From: Jarvis Carroll Date: Thu, 9 Oct 2025 09:46:45 +1100 Subject: [PATCH] add {raw, binary()} case for all chain objects --- src/hz.erl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/hz.erl b/src/hz.erl index d255c24..df9663b 100644 --- a/src/hz.erl +++ b/src/hz.erl @@ -1855,9 +1855,11 @@ coerce({_, _, contract}, {contract, Bin}, from_fate) -> Address = gmser_api_encoder:encode(contract_pubkey, Bin), {ok, unicode:characters_to_list(Address)}; coerce({_, _, signature}, S, to_fate) when is_binary(S) andalso (byte_size(S) =:= 64) -> - % If it is a binary of 64 bytes then it can be used as is... If it is an - % sg_... string of 64 bytes, then it is too short to be valid, so just - % interpret it as a binary directly. + % Usually to pass a binary in, you need to wrap it as {raw, Binary}, but + % since sg_... strings OR hex blobs can be used as signatures in Sophia, we + % special case this case based on the length. Even if a binary starts with + % "sg_", 64 characters is not enough to represent a 64 byte signature, so + % the most optimistic interpretation is to use the binary directly. {ok, S}; coerce({O, N, signature}, S, to_fate) -> coerce_chain_object(O, N, signature, signature, S); @@ -1973,6 +1975,8 @@ coerce_bytes(O, N, Count, Bytes) when byte_size(Bytes) /= Count -> coerce_bytes(_, _, _, Bytes) -> {ok, Bytes}. +coerce_chain_object(_, _, _, _, {raw, Binary}) -> + {ok, Binary}; coerce_chain_object(O, N, T, Tag, S) -> case decode_chain_object(Tag, S) of {ok, Data} -> {ok, coerce_chain_object2(T, Data)}; @@ -2563,6 +2567,7 @@ coerce_signature_binary_test() -> 169,85,212,142,14,12,233,252,97,50,193,158,229,51,123,206,222, 249,2,3,85,173,106,150,243,253,89,128,248,52,195,140,95,114, 233,110,119,143,206,137,124,36,63,154,85,7>>, + {ok, Binary} = coerce(Type, {raw, Binary}, to_fate), {ok, Binary} = coerce(Type, Binary, to_fate), ok.