Exemple #1
0
        public void TestSign()
        {
            txManager = new TransactionManager(rpcClientMock.Object, sender);

            TransactionAttribute[] attributes = new TransactionAttribute[1]
            {
                new TransactionAttribute
                {
                    Usage = TransactionAttributeUsage.Url,
                    Data  = "53616d706c6555726c".HexToBytes() // "SampleUrl"
                }
            };

            byte[] script = new byte[1];
            txManager.MakeTransaction(script, attributes)
            .AddSignature(keyPair1)
            .Sign();

            // get signature from Witnesses
            var tx = txManager.Tx;

            byte[] signature = tx.Witnesses[0].InvocationScript.Skip(1).ToArray();

            Assert.IsTrue(Crypto.Default.VerifySignature(tx.GetHashData(), signature, keyPair1.PublicKey.EncodePoint(false).Skip(1).ToArray()));

            // duplicate sign should not add new witness
            txManager.AddSignature(keyPair1).Sign();
            Assert.AreEqual(1, txManager.Tx.Witnesses.Length);

            // throw exception when the KeyPair is wrong
            Assert.ThrowsException <Exception>(() => txManager.AddSignature(keyPair2));
        }
Exemple #2
0
        public void TestAddWitness()
        {
            txManager = new TransactionManager(rpcClientMock.Object, sender);

            // Cosigner as contract scripthash
            Cosigner[] cosigners = new Cosigner[1]
            {
                new Cosigner
                {
                    Account = UInt160.Zero,
                    Scopes  = WitnessScope.Global
                }
            };

            byte[] script = new byte[1];
            txManager.MakeTransaction(script, null, cosigners);
            txManager.AddWitness(UInt160.Zero);
            txManager.AddSignature(keyPair1);
            txManager.Sign();

            var tx = txManager.Tx;

            Assert.AreEqual(2, tx.Witnesses.Length);
            Assert.AreEqual(0, tx.Witnesses[0].VerificationScript.Length);
            Assert.AreEqual(0, tx.Witnesses[0].InvocationScript.Length);
        }
        public async Task TestAddWitness()
        {
            // Cosigner as contract scripthash
            Signer[] signers = new Signer[2]
            {
                new Signer
                {
                    Account = sender,
                    Scopes  = WitnessScope.Global
                },
                new Signer
                {
                    Account = UInt160.Zero,
                    Scopes  = WitnessScope.Global
                }
            };

            byte[] script = new byte[1];
            txManager = await TransactionManager.MakeTransactionAsync(rpcClientMock.Object, script, signers);

            txManager.AddWitness(UInt160.Zero);
            txManager.AddSignature(keyPair1);
            await txManager.SignAsync();

            var tx = txManager.Tx;

            Assert.AreEqual(2, tx.Witnesses.Length);
            Assert.AreEqual(41, tx.Witnesses[0].VerificationScript.Length);
            Assert.AreEqual(66, tx.Witnesses[0].InvocationScript.Length);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");


            /// Instruction: https://docs.neo.org/v3/docs/en-us/tooldev/sdk/transaction.html#transaction-construction-process
            // choose a neo node with rpc opened
            RpcClient client = new RpcClient("http://seed1t.neo.org:20332");



            // construct the script
            ReadOnlySpan <byte> myContractSpan = Encoding.Default.GetBytes(myContractScriptHash);
            UInt160             scriptHash     = new UInt160(myContractSpan);

            byte[] script = scriptHash.MakeScript("addAgText", "String for test");


            // get ScriptHash of KeyPair account
            KeyPair sendKey = Neo.Network.RPC.Utility.GetKeyPair(privateKey);
            UInt160 sender  = Contract.CreateSignatureContract(sendKey.PublicKey).ScriptHash;


            // add Cosigners, which is a collection of scripthashs that need to be signed
            Cosigner[] cosigners = new[] { new Cosigner {
                                               Scopes = WitnessScope.CalledByEntry, Account = sender
                                           } };

            // initialize the TransactionManager with rpc client and sender scripthash
            TransactionManager txManager = new TransactionManager(client, sender);

            // fill the script, attributes and cosigners
            txManager.MakeTransaction(script, null, cosigners);

            // add signature for the transaction with sendKey
            txManager.AddSignature(sendKey);

            // sign transaction with the added signature
            txManager.Sign();

            Transaction tx = txManager.Tx;


            // broadcasts the transaction over the Neo network
            client.SendRawTransaction(tx);

            Console.WriteLine("Done");

            // print a message after the transaction is on chain
            WalletAPI neoAPI = new WalletAPI(client);

            neoAPI.WaitTransaction(tx)
            .ContinueWith(async(p) => Console.WriteLine($"Transaction vm state is  {(await p).VMState}"));

            Console.ReadKey();
        }
Exemple #5
0
        public void TestSign()
        {
            txManager = new TransactionManager(rpcClientMock.Object, sender);

            TransactionAttribute[] attributes = new TransactionAttribute[1]
            {
                new TransactionAttribute
                {
                    Usage = TransactionAttributeUsage.Url,
                    Data  = "53616d706c6555726c".HexToBytes() // "SampleUrl"
                }
            };

            Cosigner[] cosigners = new Cosigner[1] {
                new Cosigner {
                    Account = sender,
                    Scopes  = WitnessScope.Global
                }
            };

            byte[] script = new byte[1];
            txManager.MakeTransaction(script, attributes, cosigners)
            .AddSignature(keyPair1)
            .Sign();

            // get signature from Witnesses
            var tx = txManager.Tx;

            byte[] signature = tx.Witnesses[0].InvocationScript.Skip(2).ToArray();

            Assert.IsTrue(Crypto.VerifySignature(tx.GetHashData(), signature, keyPair1.PublicKey.EncodePoint(false).Skip(1).ToArray()));
            // verify network fee
            long networkFee = tx.Size * (long)1000 + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Crypto.ECDsaVerify, null, null);

            Assert.AreEqual(networkFee, tx.NetworkFee);

            // duplicate sign should not add new witness
            txManager.AddSignature(keyPair1).Sign();
            Assert.AreEqual(1, txManager.Tx.Witnesses.Length);

            // throw exception when the KeyPair is wrong
            Assert.ThrowsException <Exception>(() => txManager.AddSignature(keyPair2).Sign());
        }
Exemple #6
0
        public void TestSign()
        {
            txManager = new TransactionManager(rpcClientMock.Object, sender);

            var attributes = new TransactionAttribute[1]
            {
                new Cosigner
                {
                    Account = sender,
                    Scopes  = WitnessScope.Global
                }
            };

            byte[] script = new byte[1];
            txManager.MakeTransaction(script, attributes)
            .AddSignature(keyPair1)
            .Sign();

            // get signature from Witnesses
            var tx = txManager.Tx;

            byte[] signature = tx.Witnesses[0].InvocationScript.Skip(2).ToArray();

            Assert.IsTrue(Crypto.VerifySignature(tx.GetHashData(), signature, keyPair1.PublicKey));
            // verify network fee and system fee
            long networkFee = tx.Size * (long)1000 + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice * 1;

            Assert.AreEqual(networkFee, tx.NetworkFee);
            Assert.AreEqual(100, tx.SystemFee);

            // duplicate sign should not add new witness
            txManager.AddSignature(keyPair1).Sign();
            Assert.AreEqual(1, txManager.Tx.Witnesses.Length);

            // throw exception when the KeyPair is wrong
            Assert.ThrowsException <Exception>(() => txManager.AddSignature(keyPair2).Sign());
        }
        public async Task TestSign()
        {
            Signer[] signers = new Signer[1]
            {
                new Signer
                {
                    Account = sender,
                    Scopes  = WitnessScope.Global
                }
            };

            byte[] script = new byte[1];
            txManager = await TransactionManager.MakeTransactionAsync(rpcClientMock.Object, script, signers);

            await txManager
            .AddSignature(keyPair1)
            .SignAsync();

            // get signature from Witnesses
            var tx = txManager.Tx;

            byte[] signature = tx.Witnesses[0].InvocationScript.Skip(2).ToArray();

            Assert.IsTrue(Crypto.VerifySignature(tx.GetHashData(), signature, keyPair1.PublicKey));
            // verify network fee and system fee
            Assert.AreEqual(100000000 /*Mock*/, tx.NetworkFee);
            Assert.AreEqual(100, tx.SystemFee);

            // duplicate sign should not add new witness
            await txManager.AddSignature(keyPair1).SignAsync();

            Assert.AreEqual(1, txManager.Tx.Witnesses.Length);

            // throw exception when the KeyPair is wrong
            await ThrowsAsync <Exception>(async() => await txManager.AddSignature(keyPair2).SignAsync());
        }
Exemple #8
0
        public async Task TestSign()
        {
            Signer[] signers = new Signer[1]
            {
                new Signer
                {
                    Account = sender,
                    Scopes  = WitnessScope.Global
                }
            };

            byte[] script = new byte[1];
            txManager = await TransactionManager.MakeTransactionAsync(client, script, signers);

            await txManager
            .AddSignature(keyPair1)
            .SignAsync();

            // get signature from Witnesses
            var tx = txManager.Tx;
            ReadOnlyMemory <byte> signature = tx.Witnesses[0].InvocationScript[2..];