I want to hard code an account address into the smart contract so I can use it for checking against sender/init_origin for authorization. I can see that an account address is a 32-byte sequence that is displayed as Base58check encoded. How can I make a constant variable for base58 encoded account address, e.g., 3ZFGxLtnUUSJGW2WqjMh1DDjxyq5rnytCwkSqxFTpsWSFdQnNn? I’ve tried to decode this one to hex and then use the macro hex! to convert it to a [u8, 32] array but the hex literal is 37 elements. What is the correct way to do this?
Saw below code in types.rs. So i copied and tailored fit it for my needs where I parse a const literal base58 address string to create an AccounAddress. It seems to work but I don’t think it is the most efficient. So still waiting for a simpler solution from you guys. Thanks.
fn from_str(v: &str) → Result<Self, Self::Err> {
let (version, body) = v.from_base58check().map_err(|_| ())?;
if version == 1 && body.len() == ACCOUNT_ADDRESS_SIZE {
let mut buf = [0u8; ACCOUNT_ADDRESS_SIZE];
buf.copy_from_slice(&body);
Ok(AccountAddress(buf))
} else {
Err(())
}
}
You can convert the base58 string to hex with this little tool that I wrote a while ago (which just uses the base58 python package): Concordium Base58 - Replit
If you run it and enter your account address you will get it in hex format. The one you wrote becomes 509c67903ada59268584cf2321810daffffbab32621eea0e18ff3c341e011962.
You can then use this to create the AccountAddress, either with the hex! macro or by manually writing out the bytes .
If you run into trouble, then let me know and we’ll look at it
@Kasper
Why base58 encoding and decoding is different in concordium from “bs58” npmJs implementation (tailored for bitcoin). mostly other encodings produces 5 extra bytes(1 in front and 4 in back). serializing input with serializeUpdateContractParameters() and schema.bin can handle encoding but how to decode hex to base58 received from contract events(in blockSummary) in nodeJs.
We do use the same encoding, which includes a version byte up front, and four bytes of the payload hashed twice in the end (as described here).
But we use the version byte 1 instead of 0, which is used for Bitcoin addresses (at least according to the wiki I just linked).
As for your second question. I assume that you want to extract some account address that is emitted in the event? For that, you need to extract the 32 bytes that encode the account address from the event and then convert it to hex. Are you looking at a Mint event from a CIS-2 contract? Or what kind of events are you trying to parse?