Hi Difinity Team && Community Developers, I meet some questions when I try to use the principal id to generate secp256identity, the code as the follows:
I use the principal id as the input, then I can get an account id array, denoted as secret.
Then I use Secp256Identity.fromSecretKey() to generate identity, it works, but there is a problem:
When I use the new identity to call getPrincipal(), the result is different from the input principal id, I don’t know why
export const fromHexString = (hexString: string) => {
return Uint8Array.from(Buffer.from(hexString, ‘base64’));
}
export const getAidArrByPid = (pid: Principal) => {
let aid = getAccountId(pid, SUB_ACCOUNT_ZERO);
let aidArr = Uint8Array.from(fromHexString(aid));
return aidArr;
}
let identity1 = Secp256k1KeyIdentity.fromSecretKey(aidArr);
A principal is basically an encoded public key. If I understand correctly what you’re trying to do, then you’re trying to generate the private key from a principal. This is not possible. If it were, public key cryptography as we know it would break.
You have to start with a private key, which you can then load into a Secp256k1 identity, which can then display its public key in the form of its principal. From that you can then derive the account id.
Thx, you’re right, I’m trying to generate the private key from a principal.
So, it only can generate a secp256identity from a secret key.
And thank you again