add {raw, binary()} case for all chain objects

This commit is contained in:
Jarvis Carroll 2025-10-09 09:46:45 +11:00
parent c934510859
commit f770bc299e

View File

@ -1855,9 +1855,11 @@ coerce({_, _, contract}, {contract, Bin}, from_fate) ->
Address = gmser_api_encoder:encode(contract_pubkey, Bin), Address = gmser_api_encoder:encode(contract_pubkey, Bin),
{ok, unicode:characters_to_list(Address)}; {ok, unicode:characters_to_list(Address)};
coerce({_, _, signature}, S, to_fate) when is_binary(S) andalso (byte_size(S) =:= 64) -> 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 % Usually to pass a binary in, you need to wrap it as {raw, Binary}, but
% sg_... string of 64 bytes, then it is too short to be valid, so just % since sg_... strings OR hex blobs can be used as signatures in Sophia, we
% interpret it as a binary directly. % 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}; {ok, S};
coerce({O, N, signature}, S, to_fate) -> coerce({O, N, signature}, S, to_fate) ->
coerce_chain_object(O, N, signature, signature, S); 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) -> coerce_bytes(_, _, _, Bytes) ->
{ok, Bytes}. {ok, Bytes}.
coerce_chain_object(_, _, _, _, {raw, Binary}) ->
{ok, Binary};
coerce_chain_object(O, N, T, Tag, S) -> coerce_chain_object(O, N, T, Tag, S) ->
case decode_chain_object(Tag, S) of case decode_chain_object(Tag, S) of
{ok, Data} -> {ok, coerce_chain_object2(T, Data)}; {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, 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, 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>>, 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, Binary} = coerce(Type, Binary, to_fate),
ok. ok.