Ejemplo n.º 1
0
        async Task <MdContainer> LoadDbContainer()
        {
            var appContainerInfo = await _dataOps.Session.AccessContainer.GetMDataInfoAsync(APP_CONTAINER_PATH);

            var mdKeyCipherBytes = await _dataOps.Session.MDataInfoActions.EncryptEntryKeyAsync(appContainerInfo, MD_CONTAINER_KEY_BYTES);

            var cipherTxtEntryVal = await _dataOps.Session.MData.GetValueAsync(appContainerInfo, mdKeyCipherBytes);

            var plainTxtEntryVal = await _dataOps.Session.MDataInfoActions.DecryptAsync(appContainerInfo, cipherTxtEntryVal.Item1);

            var mdContainerJson = plainTxtEntryVal.ToUtfString();
            var locator         = mdContainerJson.Parse <MdLocator>();

            var dataTreeFactory = new Factories.DataTreeFactory(_nodeFactory);
            var result          = await dataTreeFactory.LocateAsync(locator, (s) => throw new ArgumentOutOfRangeException("Can only add 999k items to this collection."));

            _mdContainerSource = result.Value; // makes sense to check for HasValue?
            var value = await _mdContainerSource.GetValueAsync();

            if (value.HasValue)
            {
                return(value.Value.Parse <MdContainer>());
            }
            return(new MdContainer());
        }
Ejemplo n.º 2
0
        public async Task InitializeManager()
        {
            if (!await ExistsManagerAsync())
            {
                // Create new md head container
                _mdContainer = new MdContainer();

                var mdContainerRoot = await GetNewMdNodeAsync();

                var f = new Factories.DataTreeFactory(_nodeFactory);
                _mdContainerSource = await f.CreateAsync((s) => throw new ArgumentOutOfRangeException("Can only add 999k items to this collection.")); // no expansion func, means we can only create 999 databases with this account.

                var serializedDbContainer = _mdContainerSource.MdLocator.Json();

                // Insert a serialized mdContainer into App Container
                var appContainer = await _dataOps.Session.AccessContainer.GetMDataInfoAsync(APP_CONTAINER_PATH);

                var dbIdCipherBytes = await _dataOps.Session.MDataInfoActions.EncryptEntryKeyAsync(appContainer, MD_CONTAINER_KEY_BYTES);

                var dbCipherBytes = await _dataOps.Session.MDataInfoActions.EncryptEntryValueAsync(appContainer, serializedDbContainer.ToUtfBytes());

                using (var appContEntryActionsH = await _dataOps.Session.MDataEntryActions.NewAsync())
                {
                    await _dataOps.Session.MDataEntryActions.InsertAsync(appContEntryActionsH, dbIdCipherBytes, dbCipherBytes);

                    await _dataOps.Session.MData.MutateEntriesAsync(appContainer, appContEntryActionsH); // <----------------------------------------------    Commit ------------------------
                }

                // Set Permissions
                var version = await _dataOps.Session.MData.GetVersionAsync(appContainer);

                foreach (var pair in _permissions.AppContainerPermissions)
                {
                    var userSignKeyH = await _dataOps.Session.Crypto.SignPubKeyNewAsync(pair.Key);

                    await _dataOps.Session.MData.SetUserPermissionsAsync(appContainer, userSignKeyH, pair.Value, ++version);
                }
            }
            else
            {
                _mdContainer = await LoadDbContainer();
            }
        }