newKeychainTransaction method

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

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
  • blockchainTxVersion: The blockchain transaction version to be used.
  • servicesMap: (Optional) A map where:
    • Keys are service names (as String),
    • Values are derivation paths (as String) associated with the respective services. If provided, the keychain will include these services with their respective derivation paths.

Implementation

Transaction newKeychainTransaction(
  String seed,
  List<String> authorizedPublicKeys,
  Uint8List originPrivateKey,
  int blockchainTxVersion, {
  Map<String, String>? servicesMap,
}) {
  var keychain = Keychain(seed: hexToUint8List(seed));
  if (servicesMap!.isNotEmpty) {
    servicesMap.forEach((serviceName, derivationPath) {
      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));
}