public string SignTransaction(string privateKey, string serializedTransactionBase64)
        {
            try
            {
                var serializedTransaction = System.Text.Encoding.UTF8.DecodeBase64(serializedTransactionBase64);
                var pkBytes     = new Bytes(privateKey);
                var wallet      = Lykke.Icon.Sdk.KeyWallet.Load(pkBytes);
                var transaction = TransactionDeserializer.Deserialize(serializedTransaction);
                var signedTr    = new SignedTransaction(transaction, wallet);
                var props       = signedTr.GetProperties();
                var serializedSignedTransaction = SignedTransaction.Serialize(props);
                var base64 = System.Text.Encoding.UTF8.EncodeBase64(serializedSignedTransaction);

                return(base64);
            }
            catch (Exception e)
            {
                if (e is FormatException || e is ArgumentOutOfRangeException)
                {
                    throw new ArgumentException(e.Message);
                }

                throw;
            }
        }
        public void CheckVerificationMalformedSignature()
        {
            var randomKey = KeyWallet.Create();
            var from      = wallet.GetAddress();
            var to        = new Address("hx5bfdb090f43a808005ffc27c25b213145e80b7cd");

            var transaction = TransactionBuilder.CreateBuilder()
                              .Nid(NetworkId.Main)
                              .From(from)
                              .To(to)
                              .Value(BigInteger.Parse("0de0b6b3a7640000", NumberStyles.AllowHexSpecifier))
                              .StepLimit(BigInteger.Parse("12345", NumberStyles.AllowHexSpecifier))
                              .Timestamp(BigInteger.Parse("563a6cf330136", NumberStyles.AllowHexSpecifier))
                              .Nonce(BigInteger.Parse("1"))
                              .Build();

            var signedTransaction = new SignedTransaction(transaction, wallet);
            var transactionProps  = signedTransaction.GetTransactionProperties();
            var allProps          = signedTransaction.GetProperties();
            var hash      = SignedTransaction.GetTransactionHash(transactionProps);
            var signature = Base64.Decode(allProps.GetItem("signature").ToString());

            signature[64] = 212;
            var result = EcdsaSignature.VerifySignature(randomKey.GetAddress(), signature, hash);

            Assert.False(result);
        }
예제 #3
0
        public void TestIcxDeserialize()
        {
            var from = wallet.GetAddress();
            var to   = new Address("hx5bfdb090f43a808005ffc27c25b213145e80b7cd");

            var transaction = TransactionBuilder.CreateBuilder()
                              .Nid(NetworkId.Main)
                              .From(from)
                              .To(to)
                              .Value(BigInteger.Parse("0de0b6b3a7640000", NumberStyles.AllowHexSpecifier))
                              .StepLimit(BigInteger.Parse("12345", NumberStyles.AllowHexSpecifier))
                              .Timestamp(BigInteger.Parse("563a6cf330136", NumberStyles.AllowHexSpecifier))
                              .Nonce(BigInteger.Parse("1"))
                              .Build();

            var signedTransaction       = new SignedTransaction(transaction, wallet);
            var properties              = signedTransaction.GetProperties();
            var serialize               = SignedTransaction.Serialize(properties);
            var deserializedTransaction = SignedTransaction.Deserialize(serialize);

            TransactionAssertion.CompareTransactions(transaction, deserializedTransaction);
        }
        public async Task BroadcastTransactionAsync__Transaction_Is_Broadcasted__Transaction_Is_Sent()
        {
            var        blockchainService = _container.Resolve <IBlockchainService>();
            string     from        = _secretWallet.GetAddress().ToString();
            string     to          = _toWallet.GetAddress().ToString();
            BigInteger amount      = BigInteger.Parse("100000000000000");
            BigInteger gasAmount   = BigInteger.Parse("1000000");
            BigInteger gasPrice    = 1;
            var        transaction = blockchainService.BuildTransaction(from, to, amount, gasAmount, gasPrice, 0);

            string txDataStr                         = System.Text.Encoding.UTF8.DecodeBase64(transaction);
            var    iconTransaction                   = TransactionDeserializer.Deserialize(txDataStr);
            var    signedTransaction                 = new SignedTransaction(iconTransaction, _secretWallet);
            var    properties                        = signedTransaction.GetProperties();
            var    transactionProperties             = signedTransaction.GetTransactionProperties();
            var    transactionHash                   = SignedTransaction.GetTransactionHash(transactionProperties);
            var    serializedSignedTransaction       = SignedTransaction.Serialize(properties);
            string serializedSignedTransactionBase64 = System.Text.Encoding.UTF8.EncodeBase64(serializedSignedTransaction);

            var expectedTxHash = (new Bytes(transactionHash)).ToHexString(true);
            var txHash         = await blockchainService.BroadcastTransactionAsync(serializedSignedTransactionBase64);

            Assert.Equal(expectedTxHash, txHash);
        }