Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private async Task <BlockGraphProto> Sign(SignedBlockGraphMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.BlockGraph == null)
            {
                throw new ArgumentNullException(nameof(message.BlockGraph));
            }

            if (message.Round <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(message.Round));
            }

            try
            {
                var blockHash    = BlockHash(new SignedBlockHashMessage(message.BlockGraph.Block.SignedBlock.Coin.Stamp, message.Node, message.Round, message.PublicKey));
                var coinHash     = HashCoin(new SignedHashCoinMessage(message.BlockGraph.Block.SignedBlock.Coin, message.PublicKey));
                var combinedHash = Util.Combine(blockHash, coinHash);
                var signedHash   = await onionServiceClient.SignHashAsync(combinedHash);

                var signed = new BlockGraphProto
                {
                    Block = new BlockIDProto
                    {
                        Hash        = message.BlockGraph.Block.SignedBlock.Coin.Stamp,
                        Node        = message.Node,
                        Round       = message.Round,
                        SignedBlock = new BlockProto
                        {
                            Key       = message.BlockGraph.Block.SignedBlock.Coin.Stamp,
                            Coin      = message.BlockGraph.Block.SignedBlock.Coin,
                            PublicKey = signedHash.PublicKey.ToHex(),
                            Signature = signedHash.Signature.ToHex()
                        }
                    }
                };

                return(signed);
            }
            catch (Exception ex)
            {
                logger.Error($"<<< SigningProvider.Sign >>>: {ex.ToString()}");
            }

            return(null);
        }
        public static async Task <SignedSwimMessage> CreateAsync(MessageBase message, IOnionServiceClient client)
        {
            var serialized = JsonConvert.SerializeObject(message);
            var hash       = Cryptography.GenericHashNoKey(serialized);

            var signatureResponse = await client.SignHashAsync(hash);

            return(new SignedSwimMessage
            {
                Hash = hash,
                Message = message,
                Signature = signatureResponse.Signature,
                PublicKey = signatureResponse.PublicKey
            });
        }
        public SignedSwimMessage SignMessage(MessageBase message)
        {
            var serialized = JsonConvert.SerializeObject(message);
            var hash       = Cryptography.GenericHashNoKey(serialized);

            var signedHashResponse = _onionServiceClient.SignHashAsync(hash)
                                     .ConfigureAwait(false)
                                     .GetAwaiter()
                                     .GetResult();

            return(new SignedSwimMessage
            {
                Hash = hash,
                Message = message,
                Signature = signedHashResponse.Signature,
                PublicKey = signedHashResponse.PublicKey
            });
        }