Пример #1
0
        public void Should_be_able_to_use_key_store()
        {
            var store = new KeyStore(DatabaseGateway, new KeyStoreQueryFactory());

            var id = Guid.NewGuid();

            var value        = string.Concat("value=", id.ToString());
            var anotherValue = string.Concat("anotherValue=", id.ToString());

            using (DatabaseContextFactory.Create(EventStoreConnectionStringName))
            {
                store.Add(id, value);

                Assert.Throws <SqlException>(() => store.Add(id, value));

                var idGet = store.Get(value);

                Assert.IsNotNull(idGet);
                Assert.AreEqual(id, idGet);

                idGet = store.Get(anotherValue);

                Assert.IsNull(idGet);
            }
        }
        public async Task ListPrivateKeys(int repeat)
        {
            var generatedProtectedPrivateKeys = new List <ProtectedPrivateKey>();

            foreach (var _ in Enumerable.Range(0, repeat))
            {
                var(protectedPrivateKey, _) = CreateProtectedPrivateKey();
                generatedProtectedPrivateKeys.Add(protectedPrivateKey);
                KeyStore.Add(protectedPrivateKey);
            }

            var result = await ExecuteQueryAsync("query { keyStore { protectedPrivateKeys { address } } }");

            var data           = (Dictionary <string, object>)result.Data;
            var keyStoreResult = (Dictionary <string, object>)data["keyStore"];
            var protectedPrivateKeyAddresses =
                keyStoreResult["protectedPrivateKeys"].As <List <object> >()
                .Cast <Dictionary <string, object> >()
                .Select(x => x["address"].As <string>())
                .ToImmutableList();

            foreach (var protectedPrivateKey in generatedProtectedPrivateKeys)
            {
                Assert.Contains(protectedPrivateKey.Address.ToString(), protectedPrivateKeyAddresses);
            }

            var(notStoredProtectedPrivateKey, _) = CreateProtectedPrivateKey();
            Assert.DoesNotContain(notStoredProtectedPrivateKey.Address.ToString(), protectedPrivateKeyAddresses);
        }
Пример #3
0
        public async Task KeyStore_Get()
        {
            var store    = new Mock <IBlobStore>();
            var policy   = new EncryptionPolicy();
            var idPolicy = policy.Id;
            var keyStore = new KeyStore(store.Object, new PassThruEncryptor());
            var key      = new Key(policy);
            var cbResult = "";

            store.Setup(s => s.Put(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Encoding>())).Callback <string, string, Encoding>((id, data, encoding) => { cbResult = data; });

            await keyStore.Add(key);

            var store2 = new Mock <IBlobStore>();

            keyStore = new KeyStore(store2.Object, new PassThruEncryptor());

            store2.Setup(s => s.Get(It.IsAny <string>(), It.IsAny <Encoding>())).ReturnsAsync(cbResult);

            var result = await keyStore.Get(idPolicy);

            Assert.AreEqual(policy.Id, result.Id);
            Assert.AreEqual(policy.Id, result.Policy.Id);
            Assert.AreEqual(policy.KeySize, result.Policy.KeySize);
            Assert.AreEqual(policy.BlockSize, result.Policy.BlockSize);
            Assert.AreEqual(policy.Padding, result.Policy.Padding);
            Assert.AreEqual(policy.Mode, result.Policy.Mode);
            Assert.AreEqual(policy.Algorithm, result.Policy.Algorithm);
            Assert.AreEqual(policy.Expires, result.Policy.Expires);
        }
Пример #4
0
        public void Should_be_able_to_use_key_store()
        {
            var store = new KeyStore(DatabaseGateway,
                                     new KeyStoreQueryFactory(new ScriptProvider(new ScriptProviderConfiguration())));

            using (DatabaseContextFactory.Create(EventStoreConnectionStringName))
            {
                var id = OrderId;

                var value        = string.Concat("value=", id.ToString());
                var anotherValue = string.Concat("anotherValue=", id.ToString());

                store.Add(id, value);

                Assert.Throws <DuplicateKeyException>(() => store.Add(id, value),
                                                      $"Should not be able to add duplicate key / id = {id} / key = '{value}' / (ensure that your implementation throws a `DuplicateKeyException`)");

                var idGet = store.Get(value);

                Assert.IsNotNull(idGet,
                                 $"Should be able to retrieve the id of the associated key / id = {id} / key = '{value}'");
                Assert.AreEqual(id, idGet,
                                $"Should be able to retrieve the correct id of the associated key / id = {id} / key = '{value}' / id retrieved = {idGet}");

                idGet = store.Get(anotherValue);

                Assert.IsNull(idGet, $"Should not be able to get id of non-existent / id = {id} / key = '{anotherValue}'");

                store.Remove(id);

                idGet = store.Get(value);

                Assert.IsNull(idGet,
                              $"Should be able to remove association using id (was not removed) / id = {id} / key = '{value}'");

                store.Add(id, value);
                store.Remove(value);

                idGet = store.Get(value);

                Assert.IsNull(idGet,
                              $"Should be able to remove association using key (was not removed) / id = {id} / key = '{value}'");
            }
        }
        public async Task DecryptedPrivateKey()
        {
            var(protectedPrivateKey, passphrase) = CreateProtectedPrivateKey();
            var privateKey = protectedPrivateKey.Unprotect(passphrase);

            KeyStore.Add(protectedPrivateKey);

            var result = await ExecuteQueryAsync($"query {{ keyStore {{ decryptedPrivateKey(address: \"{privateKey.ToAddress()}\", passphrase: \"{passphrase}\") }} }}");

            var data                      = (Dictionary <string, object>)result.Data;
            var keyStoreResult            = (Dictionary <string, object>)data["keyStore"];
            var decryptedPrivateKeyResult = (string)keyStoreResult["decryptedPrivateKey"];

            Assert.Equal(ByteUtil.Hex(privateKey.ByteArray), decryptedPrivateKeyResult);
        }
Пример #6
0
        public async Task KeyStore_Put()
        {
            var store    = new Mock <IBlobStore>();
            var policy   = new EncryptionPolicy();
            var idPolicy = policy.Id;
            var keyStore = new KeyStore(store.Object, new PassThruEncryptor());
            var key      = new Key(policy);
            var cbResult = "";

            store.Setup(s => s.Put(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Encoding>())).Callback <string, string, Encoding>((id, data, encoding) => { cbResult = data; });

            await keyStore.Add(key);

            Assert.IsFalse(string.IsNullOrWhiteSpace(cbResult));
        }
Пример #7
0
        public async Task RevokePrivateKey()
        {
            var privateKey = new PrivateKey();
            var passphrase = "";

            var protectedPrivateKey = ProtectedPrivateKey.Protect(privateKey, passphrase);

            KeyStore.Add(protectedPrivateKey);

            var address = privateKey.ToAddress();

            var result = await ExecuteQueryAsync(
                $"mutation {{ keyStore {{ revokePrivateKey(address: \"{address.ToHex()}\") {{ address }} }} }}");

            var revokedPrivateKeyAddress = result.Data.As <Dictionary <string, object> >()["keyStore"]
                                           .As <Dictionary <string, object> >()["revokePrivateKey"]
                                           .As <Dictionary <string, object> >()["address"].As <string>();

            Assert.DoesNotContain(KeyStore.List(),
                                  t => t.Item2.Address.ToString() == revokedPrivateKeyAddress);
            Assert.Equal(address.ToString(), revokedPrivateKeyAddress);
        }