encodeKey method
String
encodeKey(
- List<
int> publicKey, { - ICPAddressType addrType = ICPAddressType.principal,
- List<
int> ? subaccount, - CryptoKeyType? curveType,
override
Encodes a public key or address bytes into a blockchain address.
Throws AddressConverterException on invalid input.
Implementation
@override
String encodeKey(
List<int> publicKey, {
ICPAddressType addrType = ICPAddressType.principal,
List<int>? subaccount,
CryptoKeyType? curveType,
}) {
final CryptoKeyType type = AddrKeyValidator.getConfigArg(
curveType,
'curveType',
);
// 1. Validate & normalize the public key
final pubkey = switch (type) {
CryptoKeyType.secp256k1 => AddrKeyValidator.validateAndGetSecp256k1Key(
publicKey,
),
CryptoKeyType.ed25519 => AddrKeyValidator.validateAndGetEd25519PubKey(
publicKey,
),
CryptoKeyType.ed25519Kholaw =>
AddrKeyValidator.validateAndGetEd25519PubKey(
publicKey,
type: CryptoKeyType.ed25519Kholaw,
),
CryptoKeyType.nist256p1 => AddrKeyValidator.validateAndGetNist256p1Key(
publicKey,
),
_ =>
throw CoreErrors.missingOrInvalidAddressArguments(
reason: "Unsupported curve type.",
),
};
// 2. DER-encode (required by the ICP self-authenticating principal formula)
final BuBytes derBytes = switch (pubkey) {
Secp256k1PublicKey secp => secp.toDer(
pubKeyMode: CryptoKeyEncoding.uncompressed,
),
Secp256r1PublicKey secp => secp.toDer(
pubKeyMode: CryptoKeyEncoding.uncompressed,
),
ED25519KholawPublicKey kh => kh.toDer(),
ED25519PublicKey ed => ed.toDer(),
_ => throw CoreErrors.addressKeyValidationFailed(),
};
final BuBytes principalBytes = ICPAddrUtils.selfAuthenticated(derBytes);
// 4. Encode according to requested type
final addr = switch (addrType) {
ICPAddressType.principal => _encodePrincipal(principalBytes),
ICPAddressType.accountIdentifier => _encodeAccountIdentifier(
principalBytes,
subaccount,
),
};
return addr.address;
}