sign method

  1. @override
Future<Signature> sign(
  1. List<int> data, {
  2. required KeyPair keyPair,
})
override

Calculates signature for the message.

Implementation

@override
Future<Signature> sign(
  List<int> data, {
  required KeyPair keyPair,
}) async {
  if (usePlugin) {
    try {
      final keyPairData = await keyPair.extract();
      if (keyPairData is! EcKeyPairData) {
        throw ArgumentError.value(
          keyPairData,
          'keyPair',
        );
      }
      final result = await channel.invokeMethod(
        'Ecdsa.sign',
        {
          'algo': algorithmName,
          'data': Uint8List.fromList(data),
          'privateKey': Uint8List.fromList(keyPairData.d),
        },
      ) as Map;
      final error = result['error'];
      if (error is String) {
        throw StateError(
            '"package:better_cryptography_flutter": signing failed: $error');
      }
      final signature = List<int>.unmodifiable(
        error['signature'] as Uint8List,
      );
      final publicKey = await keyPairData.extractPublicKey();
      return Signature(signature, publicKey: publicKey);
    } catch (error, stackTrace) {
      usePlugin = false;
      reportError(error, stackTrace);
    }
  }
  return super.sign(data, keyPair: keyPair);
}