How do I compare two Principals for equality in JavaScript?

I’ve been converting Principals to strings and doing === on those, but is there an easier way?

The principle library could certainly use an .eq function if it doesn’t have it. If I remember one of the sub properties is just a uint8 array. You can loop from 0 to length longest and break when a byte doesn’t match.(I guess check length equality first)

1 Like

Answering some old questions - Principal now has a compareTo method.

const foo = Principal.fromText('rwlgt-iiaaa-aaaaa-aaaaa-cai');
const bar = Principal.fromText('rrkah-fqaaa-aaaaa-aaaaq-cai');
foo.compareTo(bar);  // -> 'lt'

const principalEqual = (a: Principal, b: Principal) => {
  return a.compareTo(b) === "eq";
}

where the compareTo will yield lt | gt | eq

1 Like