signMessage method

Future<SignMessage> signMessage(
  1. String msg,
  2. String privateKey
)

Signs a message with the provided private key.

This function takes a message and an encoded private key, performs validation, and uses the Ed25519 cryptographic algorithm to generate a signature for the message. It returns a SignMessage object containing the encoded public key and the generated signature.

Implementation

Future<SignMessage> signMessage(String msg, String privateKey) async {
  if (msg.isEmpty || privateKey.isEmpty) {
    throw Exception('require message or encPrivateKey');
  }

  List<int> msgList = msg.codeUnits;
  Uint8List msgByte = Uint8List.fromList(msgList);

  Uint8List privateKeyByte = parsePrivateKey(privateKey);

  final algorithm = crypto.Ed25519();
  // Generate a key pair
  final keyPair = await algorithm.newKeyPairFromSeed(privateKeyByte);

  // Sign a message
  final signature = await algorithm.sign(
    msgByte,
    keyPair: keyPair,
  );

  SignMessage resp = SignMessage();
  resp.publicKey = await getEncPublicKey(privateKey);
  resp.signData = HEX.encode(signature.bytes);

  return resp;
}