encodeKey method
Encodes an Ada Byron Legacy address with the provided public key, chain code, and optional HD path information.
The pubKey
parameter is the public key to be encoded.
The optional kwargs
parameter is a map of additional arguments, including:
- "hd_path": A string or Bip32Path specifying the hierarchical deterministic (HD) path.
- "chain_code": A bytes or Bip32ChainCode representing the chain code.
- "hd_path_key": An optional bytes for the HD path key (must be 32 bytes).
Returns a string representing the encoded Ada Byron Legacy address.
Throws an ArgumentException if the provided HD path, chain code, or HD path key is invalid.
Implementation
@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
Bip32Path hdPath;
if (kwargs["hd_path"] is String) {
hdPath = Bip32PathParser.parse(kwargs["hd_path"]);
} else {
if (kwargs["hd_path"] is! Bip32Path) {
throw ArgumentException("hd path must be string or Bip32Path");
}
hdPath = kwargs["hd_path"];
}
List<int> chainCodeBytes;
if (kwargs["chain_code"] is List<int>) {
chainCodeBytes = kwargs["chain_code"];
} else {
if (kwargs["chain_code"] is! Bip32ChainCode) {
throw ArgumentException("chain code must be bytes or Bip32ChainCode");
}
chainCodeBytes = (kwargs["chain_code"] as Bip32ChainCode).toBytes();
}
List<int>? hdPathKeyBytes;
if (kwargs["hd_path_key"] != null) {
if (kwargs["hd_path_key"] is! List<int>) {
throw ArgumentException("hd path key must be bytes");
}
hdPathKeyBytes = kwargs["hd_path_key"];
if (hdPathKeyBytes!.length != QuickCrypto.chacha20Polu1305Keysize) {
throw ArgumentException(
"HD path key shall be ${QuickCrypto.chacha20Polu1305Keysize}-byte long");
}
}
final pubKeyBytes =
AddrKeyValidator.validateAndGetEd25519Key(pubKey).compressed;
return _AdaByronAddrUtils.encodeKey(
pubKeyBytes, chainCodeBytes, AdaByronAddrTypes.publicKey,
hdPathEncBytes: hdPathKeyBytes == null
? null
: _AdaByronAddrHdPath.encrypt(hdPath, hdPathKeyBytes));
}