6

Say you have two account ids Aid(A) and Aid(B) and you want to combine them.e.g. XOR( Aid(A), Aid(B) ).

Basically, I want to achieve this;

T::Hashing::hash_of(&(idA, idB)) == T::Hashing::hash_of(&(idB, idA))

1 Answer 1

3

We can always represent types inside of the runtime as their encoded blobs (Vec<u8>), so this question breaks down to "How do you XOR two Vec<u8>"?

For that, I see this thread:

https://users.rust-lang.org/t/how-to-xor-two-vec-u8/31071

So a full solution would look like:

let idA: T::AccountId = account_a;
let idB: T::AccountId = account_b;

let mut bytesA: Vec<u8> = idA.encode();
let bytesB: Vec<u8> = idB.encode();

// `bytesA` is being modified in place.
// You could also allocate a new vector if you want.
bytesA.iter_mut()
    .zip(bytesB.iter())
    .for_each(|(x1, x2)| *x1 ^= *x2);

let new_account = T::AccountId::decode(bytesA)
    .expect(
        "all bytes are valid accounts, and the source of
        these bytes is the xor of two other accounts, thus
        this must also be a valid account. qed"
    );
1
  • 1
    Very clear answer. Thanks Shawn! Commented Feb 10, 2022 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.