newKeychainTransaction method

Transaction newKeychainTransaction(
  1. String seed,
  2. List<String> authorizedPublicKeys,
  3. Uint8List originPrivateKey,
  4. int blockchainTxVersion, {
  5. String? serviceName,
  6. String? derivationPath,
})

Create a new keychain and build a transaction

  • seed : Keychain's seed
  • authorizedPublicKeys : Authorized public keys able to decrypt the keychain
  • originPrivateKey : Origin private key to attest the transaction
  • derivationPath : derivation path associated to service name

Implementation

Transaction newKeychainTransaction(
  String seed,
  List<String> authorizedPublicKeys,
  Uint8List originPrivateKey,
  int blockchainTxVersion, {
  String? serviceName,
  String? derivationPath,
}) {
  var keychain = Keychain(seed: hexToUint8List(seed));
  if (serviceName!.isNotEmpty && derivationPath!.isNotEmpty) {
    keychain = keychain.copyWithService(serviceName, derivationPath);
  }

  final aesKey = uint8ListToHex(
    Uint8List.fromList(
      List<int>.generate(32, (int i) => Random.secure().nextInt(256)),
    ),
  );

  final authorizedKeys = List<AuthorizedKey>.empty(growable: true);
  for (final key in authorizedPublicKeys) {
    authorizedKeys.add(
      AuthorizedKey(
        encryptedSecretKey: uint8ListToHex(ecEncrypt(aesKey, key)),
        publicKey: key,
      ),
    );
  }

  return Transaction(
    type: 'keychain',
    version: blockchainTxVersion,
    data: Transaction.initData(),
  )
      .setContent(jsonEncode(keychain.toDID()))
      .addOwnership(
        uint8ListToHex(aesEncrypt(keychain.encode(), aesKey)),
        authorizedKeys,
      )
      .build(seed, 0)
      .transaction
      .originSign(uint8ListToHex(originPrivateKey));
}