encodeMultiKey static method
encode Multi Public keys to MultiKey address
Implementation
static List<int> encodeMultiKey(
List<IPublicKey> publicKeys,
int requiredSignature,
) {
try {
final pubkeyLayoutStruct =
publicKeys.map((e) {
return switch (e.curve) {
EllipticCurveTypes.secp256k1 => {e.curve.name: e.uncompressed},
EllipticCurveTypes.ed25519 => {
e.curve.name: e.compressed.sublist(1),
},
_ =>
throw AddressConverterException.addressKeyValidationFailed(
reason: "Unsupported ${e.curve.name} public key.",
),
};
}).toList();
final keys = publicKeys.toSet();
if (keys.length != publicKeys.length) {
throw AddressConverterException.addressKeyValidationFailed(
network: "Aptos",
reason: "Duplicate public key detected.",
);
}
if (publicKeys.length < AptosAddrConst.multikeyMinPublicKey ||
publicKeys.length > AptosAddrConst.multikeyMaxPublicKey) {
throw AddressConverterException.addressKeyValidationFailed(
network: "Aptos",
reason:
"The number of public keys provided is invalid. It must be between ${AptosAddrConst.multikeyMinPublicKey} and ${AptosAddrConst.multikeyMaxPublicKey}.",
);
}
if (requiredSignature < AptosAddrConst.minthreshold ||
requiredSignature > AptosAddrConst.multiKeyMaxSignature) {
throw AddressConverterException.addressKeyValidationFailed(
network: "Aptos",
reason:
"Invalid threshold. The threshold must be between ${AptosAddrConst.minthreshold} and ${AptosAddrConst.multiKeyMaxSignature}.",
);
}
if (publicKeys.length < requiredSignature) {
throw AddressConverterException.addressKeyValidationFailed(
network: "Aptos",
reason:
"The number of public keys must be at least equal to the required signatures.",
);
}
final layoutStruct = {
"requiredSignature": requiredSignature,
"publicKeys": pubkeyLayoutStruct,
};
final encode = AptosAddrConst.multiKeyAddressLayout.serialize(
layoutStruct,
);
return hashKeyBytes(
bytes: encode,
scheme: AptosAddrConst.multikeyAddressFlag,
);
} on AddressConverterException {
rethrow;
} catch (e) {
throw AddressConverterException.addressBytesValidationFailed(
network: "Aptos",
details: {"error": e.toString()},
);
}
}