- I'm building a dApp and would like to treat the contract address as a String.
- I was able to find a way to convert a String to an AccountId, but no example exists for the reverse case.
- I also tried encoding with "parity-scale-codec" as shown below, but it is not guaranteed to be UTF-8, so it cannot be converted to String.
If anyone knows how to convert please help.
#[ink(message)]
pub fn convert_accountid_to_string(&self, account_id: AccountId) -> Vec<u8> {
let tmp :Vec<u8> = account_id.encode();
tmp
}
- I implemented the following as advised, but got some errors and don't know how to work around it,
Source Code:
#[ink(message)]
pub fn convert_accountid_to_string2(&self, account_id: AccountId) -> String {
let account_id = H256::from(account_id);
// Convert the AccountId to an SS58 encoded string
let account_id_string = account_id.to_ss58check();
account_id_string
}
Error Information:
error[E0277]: the trait bound `H256: From<ink::ink_primitives::AccountId>` is not satisfied
--> /Users/shin.takahashi/develop/work/account_id_test/lib.rs:39:41
|
39 | let account_id = H256::from(account_id);
| ---------- ^^^^^^^^^^ the trait `From<ink::ink_primitives::AccountId>` is not implemented for `H256`
| |
| required by a bound introduced by this call
error[E0599]: no method named `to_ss58check` found for struct `H256` in the current scope
--> /Users/shin.takahashi/develop/work/account_id_test/lib.rs:42:48
|
42 | let account_id_string = account_id.to_ss58check();
| ^^^^^^^^^^^^ method not found in `H256`
use ink_env::AccountId; use sp_core::H256; use core::convert::From; impl From<AccountId> for H256 { fn from(account_id: AccountId) -> Self { let mut h256 = H256::default(); h256.as_mut().copy_from_slice(&account_id[..]); h256 } }