Ejemplo n.º 1
0
        public async Task KeyStoreEncryptAsync(IPrivateKey privateKey,
                                               NetworkType networkType,
                                               KeyRegistryTypes keyIdentifier)
        {
            try
            {
                var publicKeyHash = _hashProvider.ComputeMultiHash(privateKey.GetPublicKey().Bytes).ToArray()
                                    .ToByteString();
                var address = new Address
                {
                    PublicKeyHash = publicKeyHash,
                    AccountType   = AccountType.PublicAccount,
                    NetworkType   = networkType
                };

                var securePassword = _passwordManager.RetrieveOrPromptPassword(_defaultNodePassword,
                                                                               "Please create a password for this node");

                var password = StringFromSecureString(securePassword);

                var json = EncryptAndGenerateDefaultKeyStoreAsJson(
                    password,
                    _cryptoContext.ExportPrivateKey(privateKey),
                    address.RawBytes.ToBase32());

                _passwordManager.AddPasswordToRegistry(_defaultNodePassword, securePassword);

                await _fileSystem.WriteTextFileToCddSubDirectoryAsync(keyIdentifier.Name, Constants.KeyStoreDataSubDir,
                                                                      json);
            }
            catch (Exception e)
            {
                _logger.Error(e.Message);
            }
        }
        public async Task <IPrivateKey> KeyStoreGenerate(KeyRegistryTypes keyIdentifier)
        {
            var privateKey = _cryptoContext.GeneratePrivateKey();

            await KeyStoreEncryptAsync(privateKey, keyIdentifier);

            return(privateKey);
        }
        private ISignature Sign(byte[] data, SigningContext signingContext, KeyRegistryTypes keyIdentifier)
        {
            var privateKey = _keyRegistry.GetItemFromRegistry(keyIdentifier);

            if (privateKey == null && !TryPopulateRegistryFromKeyStore(keyIdentifier, out privateKey))
            {
                throw new SignatureException("The signature cannot be created because the key does not exist");
            }

            return(Sign(data, signingContext, privateKey));
        }
Ejemplo n.º 4
0
        private IPrivateKey GetPrivateKey(KeyRegistryTypes keyIdentifier)
        {
            var privateKey = _keyRegistry.GetItemFromRegistry(keyIdentifier);

            if (privateKey == null && !TryPopulateRegistryFromKeyStore(keyIdentifier, out privateKey))
            {
                throw new SignatureException("The signature cannot be created because the key does not exist");
            }

            return(privateKey);
        }
Ejemplo n.º 5
0
        public IPrivateKey KeyStoreDecrypt(KeyRegistryTypes keyIdentifier)
        {
            var json = GetJsonFromKeyStore(keyIdentifier);

            if (string.IsNullOrEmpty(json))
            {
                _logger.Error("No keystore exists for the given key");
                return(null);
            }

            var         keyBytes   = KeyStoreDecrypt(_defaultNodePassword, json);
            IPrivateKey privateKey = null;

            try
            {
                privateKey = _cryptoContext.GetPrivateKeyFromBytes(keyBytes);
            }
            catch (ArgumentException)
            {
                _logger.Error("Keystore did not contain a valid key");
            }

            return(privateKey);
        }
Ejemplo n.º 6
0
        public async Task <IPrivateKey> KeyStoreGenerateAsync(NetworkType networkType, KeyRegistryTypes keyIdentifier)
        {
            var privateKey = _cryptoContext.GeneratePrivateKey();

            await KeyStoreEncryptAsync(privateKey, networkType, keyIdentifier).ConfigureAwait(false);

            return(privateKey);
        }
        private bool TryPopulateRegistryFromKeyStore(KeyRegistryTypes keyIdentifier, out IPrivateKey key)
        {
            key = _keyStore.KeyStoreDecrypt(keyIdentifier);

            return(key != null && (_keyRegistry.RegistryContainsKey(keyIdentifier) || _keyRegistry.AddItemToRegistry(keyIdentifier, key)));
        }