encodeKey method
Encode a Stellar (XLM) public key as a Stellar address.
This method encodes a Stellar public key as a Stellar address, which can be used for transactions.
pubKey
: The Stellar public key to encode.kwargs
: A map of optional keyword arguments.addr_type
: The address type, either XlmAddrTypes.pubKey or XlmAddrTypes.privKey.
Throws an AddressConverterException if the address type is not valid or if there's a validation error.
Example usage:
final encoder = XlmAddrEncoder();
final publicKey = List<int>.from([6, ...bytes]); // Replace 'bytes' with the actual public key bytes.
final addr = encoder.encodeKey(publicKey, {'addr_type': XlmAddrTypes.pubKey});
Implementation
@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
if (pubKey.length ==
Ed25519KeysConst.pubKeyByteLen + Ed25519KeysConst.pubKeyPrefix.length) {
pubKey = pubKey.sublist(1);
}
final addrType = AddrKeyValidator.nullOrValidateAddressArgs<XlmAddrTypes>(
kwargs, "addr_type") ??
XlmAddrTypes.pubKey;
AddrDecUtils.validateBytesLength(pubKey, Ed25519KeysConst.pubKeyByteLen);
if (addrType == XlmAddrTypes.pubKey) {
AddrKeyValidator.validateAndGetEd25519Key(pubKey);
} else if (addrType == XlmAddrTypes.privKey) {
Ed25519PrivateKey.fromBytes(pubKey);
}
if (addrType == XlmAddrTypes.muxed) {
final BigInt? muxedId = BigintUtils.tryParse(kwargs["account_id"]);
if (muxedId == null || muxedId > maxU64 || muxedId < BigInt.zero) {
throw AddressConverterException(
"Missing or invalid 'account_id'. An accountId is required for a muxed address.",
details: {"accounts_id": kwargs["account_id"]});
}
final idBytes =
BigintUtils.toBytes(muxedId, length: XlmAddrConst.muxedIdLength);
pubKey = [...pubKey, ...idBytes];
}
List<int> payloadBytes = List<int>.from([addrType.value, ...pubKey]);
List<int> checksumBytes = _XlmAddrUtils.computeChecksum(payloadBytes);
return Base32Encoder.encodeNoPaddingBytes(
List<int>.from([...payloadBytes, ...checksumBytes]));
}