Exemple #1
0
        private static Transaction CreateValidTx(DataCache snapshot, NEP6Wallet wallet, UInt160 account, uint nonce)
        {
            var tx = wallet.MakeTransaction(snapshot, new TransferOutput[]
            {
                new TransferOutput()
                {
                    AssetId    = NativeContract.GAS.Hash,
                    ScriptHash = account,
                    Value      = new BigDecimal(BigInteger.One, 8)
                }
            },
                                            account);

            tx.Nonce = nonce;

            var data = new ContractParametersContext(snapshot, tx, ProtocolSettings.Default.Network);

            Assert.IsNull(data.GetSignatures(tx.Sender));
            Assert.IsTrue(wallet.Sign(data));
            Assert.IsTrue(data.Completed);
            Assert.AreEqual(1, data.GetSignatures(tx.Sender).Count());

            tx.Witnesses = data.GetWitnesses();
            return(tx);
        }
        private static Transaction SignWithWallet(Transaction tx)
        {
            if (tx == null)
            {
                throw new ArgumentNullException("tx");
            }
            try
            {
                tx.ToJson();
            }
            catch (Exception)
            {
                throw new FormatException("交易格式错误");
            }


            var wallet = new NEP6Wallet(new WalletIndexer("D:\\PrivateNet2\\node1\\Index_0001E240"), "1.json");

            try
            {
                wallet.Unlock("11111111");
            }
            catch (Exception)
            {
                Console.WriteLine("password error");
            }

            //Signature
            var context = new ContractParametersContext(tx);

            wallet.Sign(context);
            if (context.Completed)
            {
                Console.WriteLine("签名成功");
                tx.Witnesses = context.GetWitnesses();
            }
            else
            {
                Console.WriteLine("签名失败");
            }
            //Console.WriteLine(tx.ToArray().ToHexString());
            Console.WriteLine("交易验证:" + tx.Verify(Blockchain.Singleton.GetSnapshot(), new List <Transaction> {
                tx
            }));
            Console.WriteLine(tx.ToArray().ToHexString());
            return(tx);
        }
Exemple #3
0
        public void TestCreateAccount()
        {
            var uut = new NEP6Wallet(wPath, "123", ProtocolSettings.Default);
            var acc = uut.CreateAccount("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632549".HexToBytes());
            var tx  = new Transaction()
            {
                Attributes = Array.Empty <TransactionAttribute>(),
                Script     = new byte[1],
                Signers    = new Signer[] { new Signer()
                                            {
                                                Account = acc.ScriptHash
                                            } },
            };
            var ctx = new ContractParametersContext(TestBlockchain.GetTestSnapshot(), tx, ProtocolSettings.Default.Network);
            var sig = uut.Sign(ctx);

            tx.Witnesses = ctx.GetWitnesses();
            Assert.IsTrue(tx.VerifyWitnesses(ProtocolSettings.Default, TestBlockchain.GetTestSnapshot(), long.MaxValue));
            Assert.ThrowsException <ArgumentNullException>(() => uut.CreateAccount((byte[])null));
            Assert.ThrowsException <ArgumentException>(() => uut.CreateAccount("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551".HexToBytes()));
        }
Exemple #4
0
        private Transaction CreateValidTx(NEP6Wallet wallet, UInt160 account, uint nonce)
        {
            var tx = wallet.MakeTransaction(new TransferOutput[]
            {
                new TransferOutput()
                {
                    AssetId    = NativeContract.GAS.Hash,
                    ScriptHash = account,
                    Value      = new BigDecimal(1, 8)
                }
            },
                                            account);

            tx.Nonce = nonce;

            var data = new ContractParametersContext(tx);

            Assert.IsTrue(wallet.Sign(data));
            Assert.IsTrue(data.Completed);

            tx.Witnesses = data.GetWitnesses();
            return(tx);
        }