newKeychainTransaction method
Create a new keychain and build a transaction
seed
: Keychain's seedauthorizedPublicKeys
: Authorized public keys able to decrypt the keychainoriginPrivateKey
: Origin private key to attest the transactionderivationPath
: 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));
}