signTransaction method

  1. @override
SuiMultisigSignature signTransaction(
  1. List<int> txbytes, {
  2. List<SuiBasePrivateKey<SuiCryptoPublicKey<IPublicKey>>>? signers,
  3. bool hashDigest = true,
})
override

Signs a transaction using the account's private key.

Implementation

@override
SuiMultisigSignature signTransaction(List<int> txbytes,
    {List<SuiBasePrivateKey>? signers, bool hashDigest = true}) {
  signers = List<SuiBasePrivateKey>.from(signers ?? privateKeys);
  signers = signers
      .where((e) {
        final key = e.publicKey;
        return publicKey.publicKeys.any((i) => i.publicKey == key);
      })
      .toSet()
      .toList();
  signers.sort((a, b) {
    final firstKey = a.publicKey;
    final secoundKey = b.publicKey;
    return publicKey.publicKeys
        .indexWhere((e) => e.publicKey == firstKey)
        .compareTo(publicKey.publicKeys
            .indexWhere((e) => e.publicKey == secoundKey));
  });
  final List<SuiGenericSignature> signatures = [];
  final digest = SuiCryptoUtils.generateTransactionDigest(
      txBytes: txbytes, hashDigest: hashDigest);
  int weight = 0;
  List<int> bits = [];
  int bitMap = 0;
  for (final i in signers) {
    final pubKey = i.publicKey;
    final index =
        publicKey.publicKeys.indexWhere((e) => e.publicKey == pubKey);
    if (index >= 0 && !bits.contains(index)) {
      final pubKeyInfo = publicKey.publicKeys.elementAt(index);
      final signature = i.sign(digest);
      signatures.add(signature);
      weight += pubKeyInfo.weight;
      bitMap |= 1 << index;
      if (weight >= publicKey.threshold) break;
    }
  }
  if (weight < publicKey.threshold) {
    throw DartSuiPluginException(
      "Insufficient signatures.",
      details: {"threshold": publicKey.threshold, "weight": weight},
    );
  }
  return SuiMultisigSignature(
      publicKey: publicKey, signatures: signatures, bitmap: bitMap);
}