public async Task RandomPrivateMutableDataUpdateAction() { Utils.InitialiseSessionForRandomTestApp(); const ulong tagType = 15001; const string actKey = "sample_key"; const string actValue = "sample_value"; using (var mdInfoHandle = await MDataInfo.RandomPrivateAsync(tagType)) { using (var permissionSetH = await MDataPermissionSet.NewAsync()) { await MDataPermissionSet.AllowAsync(permissionSetH, MDataAction.Insert); await MDataPermissionSet.AllowAsync(permissionSetH, MDataAction.ManagePermissions); using (var permissionsH = await MDataPermissions.NewAsync()) { using (var appSignKeyH = await Crypto.AppPubSignKeyAsync()) { await MDataPermissions.InsertAsync(permissionsH, appSignKeyH, permissionSetH); await MData.MData.PutAsync(mdInfoHandle, permissionsH, NativeHandle.Zero); } } } using (var entryActionsH = await MDataEntryActions.NewAsync()) { var key = Encoding.Default.GetBytes(actKey).ToList(); var value = Encoding.Default.GetBytes(actValue).ToList(); key = await MDataInfo.EncryptEntryKeyAsync(mdInfoHandle, key); value = await MDataInfo.EncryptEntryValueAsync(mdInfoHandle, value); await MDataEntryActions.InsertAsync(entryActionsH, key, value); await MData.MData.MutateEntriesAsync(mdInfoHandle, entryActionsH); } using (var keysHandle = await MData.MData.ListKeysAsync(mdInfoHandle)) { var len = await MDataKeys.LenAsync(keysHandle); Assert.AreEqual(1, len.ToInt32()); } using (var entriesHandle = await MData.MData.ListEntriesAsync(mdInfoHandle)) { var entries = await MDataEntries.ForEachAsync(entriesHandle); Assert.AreEqual(1, entries.Count); var entry = entries.First(); var key = await MDataInfo.DecryptAsync(mdInfoHandle, entry.Item1); var value = await MDataInfo.DecryptAsync(mdInfoHandle, entry.Item2); var encoding = new ASCIIEncoding(); Assert.AreEqual(actKey, encoding.GetString(key.ToArray())); Assert.AreEqual(actValue, encoding.GetString(value.ToArray())); } } }
public async Task RandomPublicMutableDataInsertAction() { Utils.InitialiseSessionForRandomTestApp(); const ulong tagType = 15001; using (var mdInfoHandle = await MDataInfo.RandomPublicAsync(tagType)) { using (var permissionSetH = await MDataPermissionSet.NewAsync()) { await MDataPermissionSet.AllowAsync(permissionSetH, MDataAction.Insert); await MDataPermissionSet.AllowAsync(permissionSetH, MDataAction.ManagePermissions); using (var permissionsH = await MDataPermissions.NewAsync()) { using (var appSignKeyH = await Crypto.AppPubSignKeyAsync()) { await MDataPermissions.InsertAsync(permissionsH, appSignKeyH, permissionSetH); await MData.MData.PutAsync(mdInfoHandle, permissionsH, NativeHandle.Zero); } } } using (var entryActionsH = await MDataEntryActions.NewAsync()) { var key = Encoding.Default.GetBytes("sample_key").ToList(); var value = Encoding.Default.GetBytes("sample_value").ToList(); await MDataEntryActions.InsertAsync(entryActionsH, key, value); await MData.MData.MutateEntriesAsync(mdInfoHandle, entryActionsH); } using (var entryActionsH = await MDataEntryActions.NewAsync()) { var key = Encoding.Default.GetBytes("sample_key_2").ToList(); var value = Encoding.Default.GetBytes("sample_value_2").ToList(); await MDataEntryActions.InsertAsync(entryActionsH, key, value); await MData.MData.MutateEntriesAsync(mdInfoHandle, entryActionsH); } using (var keysHandle = await MData.MData.ListKeysAsync(mdInfoHandle)) { var len = await MDataKeys.LenAsync(keysHandle); Assert.AreEqual(2, len.ToInt32()); } } }
// Creates db with address to category MD public async Task CreateDbAsync(string databaseId) { databaseId = DbIdForProtocol(databaseId); if (databaseId.Contains(".") || databaseId.Contains("@")) { throw new NotSupportedException("Unsupported characters '.' and '@'."); } // Check if account exits first and return error var dstPubIdDigest = await GetMdXorName(databaseId); var pubKey = await _crypto.AppPubEncKeyAsync(); var dstPubIdMDataInfoH = new MDataInfo { Name = dstPubIdDigest.ToArray(), TypeTag = 15001 }; var accountExists = false; try { await _mData.ListKeysAsync(dstPubIdMDataInfoH); accountExists = true; } catch (Exception) { // ignored - acct not found } if (accountExists) { throw new Exception("Id already exists."); } var permissions = new PermissionSet { Delete = true, Insert = true, ManagePermissions = true, Read = true, Update = true }; // Create Self Permissions using (var streamTypesPermH = await _mDataPermissions.NewAsync()) { using (var appSignPkH = await _crypto.AppPubSignKeyAsync()) { await _mDataPermissions.InsertAsync(streamTypesPermH, appSignPkH, permissions); } // Create Md for holding categories var categoriesMDataInfoH = await _mDataInfo.RandomPrivateAsync(15001); await _mData.PutAsync(categoriesMDataInfoH, streamTypesPermH, NativeHandle.Zero); // <---------------------------------------------- Commit ------------------------ var serializedCategoriesMdInfo = await _mDataInfo.SerialiseAsync(categoriesMDataInfoH); // Finally update App Container (store db info to it) var database = new Database { DbId = databaseId, Categories = new DataArray { Type = "Buffer", Data = serializedCategoriesMdInfo }, // Points to Md holding stream types //Archive = new DataArray {Type = "Buffer", Data = serializedDatabaseMdInfo}, //DataEncPk = categoryEncPk.ToHexString(), //DataEncSk = categoryEncSk.ToHexString() }; var serializedDb = JsonConvert.SerializeObject(database); var appContH = await _accessContainer.GetMDataInfoAsync(AppContainerPath); // appContainerHandle var dbIdCipherBytes = await _mDataInfo.EncryptEntryKeyAsync(appContH, database.DbId.ToUtfBytes()); var dbCipherBytes = await _mDataInfo.EncryptEntryValueAsync(appContH, serializedDb.ToUtfBytes()); using (var appContEntryActionsH = await _mDataEntryActions.NewAsync()) { await _mDataEntryActions.InsertAsync(appContEntryActionsH, dbIdCipherBytes, dbCipherBytes); await _mData.MutateEntriesAsync(appContH, appContEntryActionsH); // <---------------------------------------------- Commit ------------------------ } } }