Jesse
January 28, 2022, 11:49pm
1
@kpeacock i have a follow up question to this comment
I’m learning this now! It’s a little non-trivial, although it is covered in the examples Roman provided.
You can get an account-identifier by hashing the principal of the canister together with a “subaccount” which can be pretty much any other value converted into a blob. Any account-identifier that starts with your canister’s principal can be controlled by your canister.
You’ll need to convert it into a string, for people to send money to it, though.
Because this isn’t very easy to drop into…
Do you know where i can find an example of converting an accounting-identifier
To a Text
? I’m trying to do so by calling Text.DecodeUtf8(accountId)
where accountId
Is a Blob
, but I’m just getting null
returned.
1 Like
It is all binary. You can convert them to hex, I have an example here .
For reference to others, here’s an example to do that in typescript
...padding,
...Principal.fromText(p).toBlob(),
...getSubAccountArray(s)
]);
const hash = sha224(array);
const checksum = to32bits(getCrc32(hash));
const array2 = new Uint8Array([
...checksum,
...hash
]);
return toHexString(array2);
};
const getSubAccountArray = (s) => {
if (Array.isArray(s)){
return s.concat(Array(32-s.length).fill(0));
} else {
//32 bit number only
return Array(28).fill(0).concat(to32bits(s ? s : 0))
}
};
const from32bits = ba => {
4 Likes
Jesse
January 29, 2022, 10:39pm
4
These were helpful. Got the job done