How to hard-code an account address into smart contract

Hi,

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?

Thanks

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(())
}
}

Hi @ulapcyber

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 :blush:.

If you run into trouble, then let me know and we’ll look at it :wink:

Best regards,
Kasper

1 Like

Thanks @Kasper! I will try it