Exemplo n.º 1
0
        public IActionResult DecryptWantedSystemMessageAsync([FromBody] DecryptWantedSystemMessageRequest request)
        {
            Guard.NotNull(request, nameof(request));

            // checks the request is valid
            if (!this.ModelState.IsValid)
            {
                return(ModelStateErrors.BuildErrorResponse(this.ModelState));
            }

            try
            {
                Script        scriptPubKey  = this.network.CreateTransaction(request.TransactionHex).Outputs[request.MessageOutputIndex].ScriptPubKey;
                RsaPrivateKey rsaPrivateKey = null;
                if (!String.IsNullOrEmpty(request.RsaPrivateKeyHex))
                {
                    try
                    {
                        rsaPrivateKey = RsaPrivateKey.FromHex(request.RsaPrivateKeyHex);
                    }
                    catch
                    {
                        throw new Exception("The RSA private key you provided was not in the correct form.");
                    }
                }

                NBitcoin.Messaging.WantedSystemMessage sm = WantedSystemMessageTemplate.Instance.GetWantedSystemMessage(scriptPubKey, rsaPrivateKey);

                var model = new DecryptedWantedSystemMessageModel
                {
                    Version      = sm.Version,
                    Compression  = sm.Compression.ToString(),
                    ChecksumType = sm.ChecksumType.ToString(),
                    Encryption   = sm.Encryption.ToString(),
                    Metadata     = sm.Metadata.ToString(),
                    Text         = sm.Text
                };

                return(this.Json(model));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
Exemplo n.º 2
0
        private void AddMessage(TransactionBuildContext context)
        {
            if (context.Message == null)
            {
                return;
            }

            // load rsa keys from the address book
            Wallet        wallet = this.walletManager.GetWallet(context.AccountReference.WalletName);
            WalletManager wm     = (WalletManager)this.walletManager;

            if (context.EncryptMessage)
            {
                if (!wm.ReviewerAddresses.ContainsKey(context.MessageRecipient))
                {
                    throw new Exception($"The recipient '{context.MessageRecipient}' was not found in the address book.");
                }

                var reviewerAddress = wm.ReviewerAddresses[context.MessageRecipient];

                if (String.IsNullOrEmpty(reviewerAddress.RsaPrivateKeyHex))
                {
                    throw new Exception($"The reviewers' address '{reviewerAddress.Address}' was recognized, but its private key information was not received from the '{reviewerAddress.PublicApiUrl}' address. This is usually means that the current, requesting node has no access rights to it, so: (A) you must use a different address from your local node OR (B) call this method on the node where the address was originally created on.");
                }

                context.TransactionBuilder.SendMessage(context.Message, context.MessageRecipient, context.MessageReplyToAddress, context.MessageRewardAddress, RsaPublicKey.FromHex(reviewerAddress.RsaPublicKeyHex), RsaPrivateKey.FromHex(reviewerAddress.RsaPrivateKeyHex));
            }
            else
            {
                context.TransactionBuilder.SendMessage(context.Message, context.MessageRecipient, context.MessageReplyToAddress, context.MessageRewardAddress);
            }
        }