Is address
when serialized to BCS always the same length?
#525
-
Discord user IDNo response Describe your question in detail.I have this code: /// Sort two addresses and get an object seed based on these sorted addresses.
/// Do not use this function if the two addresses are the same.
fun sort_addresses(a: address, b: address): (address, address, vector<u8>) {
let a_bcs = bcs::to_bytes(&a);
let b_bcs = bcs::to_bytes(&b);
let a_len = vector::length(&a_bcs);
let b_len = vector::length(&b_bcs);
if (a_len < b_len) {
return (a, b, build_object_seed_from_bytes(a_bcs, b_bcs))
} else if (a_len > b_len) {
return (b, a, build_object_seed_from_bytes(b_bcs, a_bcs))
};
let i = 0;
while (i < a_len && i < b_len) {
let a_byte = *vector::borrow(&a_bcs, i);
let b_byte = *vector::borrow(&b_bcs, i);
if (a_byte < b_byte) {
return (a, b, build_object_seed_from_bytes(a_bcs, b_bcs))
} else if (a_byte > b_byte) {
return (b, a, build_object_seed_from_bytes(b_bcs, a_bcs))
};
i = i + 1;
};
// This should never happen
abort 0
} Is there any point in the length check? I assume not because addresses are always 32 bytes (ref), but I figured I'd confirm. What error, if any, are you getting?No response What have you tried or looked at? Or how can we reproduce the error?No response Which operating system are you using?macOS Which SDK or tool are you using? (if any)N/A Describe your environment or tooling in detailNo response |
Beta Was this translation helpful? Give feedback.
Answered by
gregnazario
Nov 11, 2024
Replies: 1 comment
-
Addresses are always fixed length 32-bytes according to the BCS spec. There is no Uleb leading it to determine the length. It's essentially a u256 but interpreted as an address |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
gregnazario
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Addresses are always fixed length 32-bytes according to the BCS spec. There is no Uleb leading it to determine the length.
It's essentially a u256 but interpreted as an address