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 ArgumentException 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 {}]) {
final addrType = kwargs['addr_type'] ?? XlmAddrTypes.pubKey;
if (addrType is! XlmAddrTypes) {
throw ArgumentException(
'Address type is not an enumerative of XlmAddrTypes');
}
IPublicKey pubKeyObj = AddrKeyValidator.validateAndGetEd25519Key(pubKey);
List<int> payloadBytes =
List<int>.from([addrType.value, ...pubKeyObj.compressed.sublist(1)]);
List<int> checksumBytes = _XlmAddrUtils.computeChecksum(payloadBytes);
return Base32Encoder.encodeNoPaddingBytes(
List<int>.from([...payloadBytes, ...checksumBytes]));
}