selfSign method

Future<void> selfSign({
  1. String? passphrase,
  2. String? recoveryKey,
  3. String? keyOrPassphrase,
  4. OpenSSSS? openSsss,
})

Implementation

Future<void> selfSign(
    {String? passphrase,
    String? recoveryKey,
    String? keyOrPassphrase,
    OpenSSSS? openSsss}) async {
  var handle = openSsss;
  if (handle == null) {
    handle = encryption.ssss.open(EventTypes.CrossSigningMasterKey);
    await handle.unlock(
      passphrase: passphrase,
      recoveryKey: recoveryKey,
      keyOrPassphrase: keyOrPassphrase,
      postUnlock: false,
    );
    await handle.maybeCacheAll();
  }
  final masterPrivateKey = base64decodeUnpadded(
      await handle.getStored(EventTypes.CrossSigningMasterKey));
  final keyObj = olm.PkSigning();
  String? masterPubkey;
  try {
    masterPubkey = keyObj.init_with_seed(masterPrivateKey);
  } catch (e) {
    masterPubkey = null;
  } finally {
    keyObj.free();
  }
  final userDeviceKeys =
      client.userDeviceKeys[client.userID]?.deviceKeys[client.deviceID];
  if (masterPubkey == null || userDeviceKeys == null) {
    throw Exception('Master or user keys not found');
  }
  final masterKey = client.userDeviceKeys[client.userID]?.masterKey;
  if (masterKey == null || masterKey.ed25519Key != masterPubkey) {
    throw Exception('Master pubkey key doesn\'t match');
  }
  // master key is valid, set it to verified
  await masterKey.setVerified(true, false);
  // and now sign both our own key and our master key
  await sign([
    masterKey,
    userDeviceKeys,
  ]);
}