Ejemplo n.º 1
0
        public void SignHashTest()
        {
            var a = 1056;

            var hashOfa = HashingHelper.HashObject(a);

            var rsa = RSA.Create();

            var key = rsa.ExportParameters(true);

            byte[] signature = DigitalSignatureHelper.SignHash(hashOfa, key);

            var ok = DigitalSignatureHelper.VerifySignedHash(hashOfa, signature, key);

            Assert.True(ok);
        }
Ejemplo n.º 2
0
        public void AddTransaction(SycoinTransactionContent transactionContent, string privateKey)
        {
            #region Verify transaction input and output

            // Input UTXO check
            transactionContent.Input.All(input =>
            {
                var UTXOInfo = UTXOManager.GetUTXOInfo(input.TransactionHash, input.PrevOutputIndex);
                if (UTXOInfo != null)
                {
                    throw new Exceptions.NonUTXOException(input);
                }
                return(true);
            });

            var allInputTransactions = DataProvider.GetTransactions(transactionContent.Input.Select(i => i.TransactionHash));

            #region Output validation
            var inputAmount = transactionContent.Input.Select(input =>
            {
                var prev_trans  = allInputTransactions.FirstOrDefault(x => x.Hash == input.TransactionHash);
                var prev_output = prev_trans.Content.Outputs.ElementAt(input.PrevOutputIndex);
                return(prev_output.Amount);
            }).Sum();

            var outputAmount = transactionContent.Outputs.Sum(output => output.Amount);

            if (inputAmount < outputAmount)
            {
                throw new Exceptions.InputNotEnoughTokenException();
            }
            #endregion

            #endregion

            var contentHash = HashingHelper.HashObject(transactionContent);
            var rsa         = RSAHelper.CreateRsaProviderFromPrivateKey(privateKey);
            var publicKey   = RSAHelper.ExportPublicKeyToPEMFormat(rsa);

            DataProvider.AddTransaction(new SyCoinTransaction()
            {
                Signature = HashingHelper.ByteArrayToHexDigit(DigitalSignatureHelper.SignHash(contentHash, rsa.ExportParameters(true))),
                Hash      = HashingHelper.ByteArrayToHexDigit(contentHash),
                PublicKey = publicKey,
                Content   = transactionContent
            });
        }