The Account Identifier is used in Ledger Canister, so it’s important to get the account identifier in motoko for those canisters integrating with Ledger Canister.
I find the source code of get account identifier in golang:
the blob (Principal’s bytes format) is principal.NewSelfAuthenticatingId(der).Bytes(), and to convert to account identifier, it involves complex functions such as sha224.
How to get the Account Identifier from Principal in Motoko.
@flyq You can find a sha256 implementation here: Motoko Packages
This isn’t enough for your use case however, as to generate an AccountIdentifier from a principal, sha224 is used. Unless that is added it may not be possible to generate AccountIdentifiers in mo
Wikipedia suggests there’s few changes needed to the sha256 implementation to calculate sha244 instead (assuming that implementation is based on the wikipedia algorithm)
Damn I must have started my lib an hour before you posted this, and only just checked in now to post it. It’s the same as yours (change IV and skipped the last block in the sum stage).
I’ll move onto working on the AccountIdentifiers now. The python gist makes it easy to follow
The sub account can be any Nat256, allowing for sub accounts from the same principal. E.g. in NNS if you add a new account I believe it’s just the next account number (so the second one would be 1 as a 32byte blob)
The problem is not solved perfectly, Account Identifier is CRC-32(X) + X, in which X is currently available in motoko, but CRC-32 is not. So the next work is to implement CRC32 in motoko.
Could you post the output for that principal ID in your gist? I dont have python handy to run it. Trying to see if I can get my motoko script to generate the same thing.
Currently have a working crc32, which should be the same as the one from the binascii python library so hoping it should match
Awesome, so my accountid script is working, using the sha224 lib and a custom crc32 motoko lib:
Currently this is hard coded for the sub account (SUBACCOUNT_ZERO), I’ll add the ability for custom sub accounts and then complete some more testing locally and get something uploaded today.
It’s a bit unusual though, both CRC32 and SHA224 are pretty non-standard, and unlikely to be re-used much. Does it make sense to condense all of this into a single library?
I’ve uploaded a library for the AccountIdentifiers: GitHub - stephenandrews/motoko-accountid - this seems to hold true for the examples posted above, and I will continue with a few more.
This will allow us to communicate with the ledger canister directly for transfers etc. I am currently working on a wrapping canister as well.