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) {
    final keyPairData = await keyPair.extract();
    if (keyPairData is! SimpleKeyPairData) {
      throw ArgumentError.value(
        keyPair,
        'keyPair',
        'Extracted key pair should be `SimpleKeyPairData`, but got: `$keyPairData`',
      );
    }
    final result = await channel.invokeMethod(
      'ed25519_sign',
      {
        'data': Uint8List.fromList(data),
        'secretKey': Uint8List.fromList(keyPairData.bytes),
      },
    );
    final error = result['error'];
    if (error is String) {
      throw StateError(
          'error in "package:better_cryptography_flutter": $error');
    }
    final bytes = result['signature'] as Uint8List;
    final publicKey = base64Decode(result['publicKey'] as String);
    return Signature(
      bytes,
      publicKey: SimplePublicKey(publicKey, type: KeyPairType.ed25519),
    );
  }
  return super.sign(data, keyPair: keyPair);
}