toMultiSigAddress static method

String toMultiSigAddress(
  1. List<PubkeyWeightPair> pks,
  2. int threshold
)

Derives a multisig address from a list of pk and weights and threshold. || ... || flag_n || pk_n || weight_n`

Implementation

// It is the 32-byte Blake2b hash of the serializd bytes of `flag_MultiSig || threshold || flag_1 || pk_1 || weight_1
/// || ... || flag_n || pk_n || weight_n`
static String toMultiSigAddress(
  List<PubkeyWeightPair> pks,
  int threshold
) {
  if (pks.length > MAX_SIGNER_IN_MULTISIG) {
    throw ArgumentError(
      "Max number of signers in a multisig is $MAX_SIGNER_IN_MULTISIG",
    );
  }
  // max length = 1 flag byte + (max pk size + max weight size (u8)) * max signer size + 2 threshold bytes (u16)
  const maxLength = 1 + (64 + 1) * MAX_SIGNER_IN_MULTISIG + 2;
  final tmp = Uint8List(maxLength);
  tmp.setAll(0, [SIGNATURE_SCHEME_TO_FLAG.MultiSig]);
  tmp.setAll(1, toLittleEndianTwoBytes(threshold));

  int i = 3;
  for (var pk in pks) {
    tmp.setAll(i, [pk.pubKey.flag()]);
    tmp.setAll(i + 1, pk.pubKey.toRawBytes());
    tmp.setAll(i + 1 + pk.pubKey.toRawBytes().length, [pk.weight]);
    i += pk.pubKey.toRawBytes().length + 2;
  }
  return normalizeSuiAddress(
    Hex.encode(blake2b(tmp.sublist(0, i))),
  );
}