sign method

  1. @override
Uint8List? sign(
  1. SignKey privateKey
)
override

Signs the TAI data with the entity's private key.

Encodes all properties to a data string, signs it with the private key, and stores the signature for later verification.

sKey: Private key matching the entity's Meta public key

Returns: Signature as Uint8List if successful, null on error

Implementation

@override
Uint8List? sign(SignKey privateKey) {
  Uint8List? signature;
  // if (_status > 0) {
  //   // already signed/verified
  //   assert(_json != null, 'document data error: $this');
  //   signature = _getSignature();
  //   assert(signature != null, 'document signature error: $this');
  //   return signature;
  // }
  // 1. update sign time
  setProperty('time', DateTime.now().millisecondsSinceEpoch / 1000.0);
  // 2. encode & sign
  Map? dict = properties;
  if (dict == null) {
    assert(false, 'document invalid: ${toMap()}');
    return null;
  }
  String data = JSONMap.encode(dict);
  assert(data.isNotEmpty, 'should not happen: $dict');
  signature = privateKey.sign(UTF8.encode(data));
  assert(signature.isNotEmpty, 'should not happen: $dict');
  TransportableData ted = Base64Data.createWithBytes(signature);
  // 3. update 'data' & 'signature' fields
  this['data'] = data;                 // JSON string
  this['signature'] = ted.serialize();  // BASE-64
  _json = data;
  _sig = ted;
  // 4. update status
  _status = 1;
  return signature;
}