예제 #1
0
 public void TestSetup()
 {
     keyPair1      = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p"));
     sender        = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash();
     address1      = Neo.Wallets.Helper.ToAddress(sender);
     rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]);
     walletAPI     = new WalletAPI(rpcClientMock.Object);
 }
예제 #2
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();
        }
예제 #3
0
 public void TestSetup()
 {
     keyPair1      = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p"));
     sender        = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash();
     multiSender   = Contract.CreateMultiSigContract(1, new ECPoint[] { keyPair1.PublicKey }).ScriptHash;
     rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]);
     client        = rpcClientMock.Object;
     address1      = Wallets.Helper.ToAddress(sender, client.protocolSettings.AddressVersion);
     walletAPI     = new WalletAPI(rpcClientMock.Object);
 }
예제 #4
0
        /*WaitTransaction*/
        public void WaitTransaction()
        {
            WalletAPI walletAPI = new WalletAPI(rpcClient);

            Console.WriteLine("********************************");
            Console.WriteLine("WaitTransaction");
            var sendFromResult = rpcClient.SendFromAsync("0x254b9decd76080ef368e7a6b0a065938dfbc31cf", "NLtDqwnj9s7wQVyaiD5ohjV3e9fUVkZxDp", "NedjwsfAJYFas9rn8UHWQftTW4oKAQyW9h", "2").Result;

            Console.WriteLine("send: " + sendFromResult.ToString());
            Transaction tx     = Utility.TransactionFromJson(sendFromResult);
            var         waitTx = walletAPI.WaitTransactionAsync(tx).Result.ToJson();

            Console.WriteLine();
            Console.WriteLine("confirmed: " + waitTx);
            Console.WriteLine("********************************");
            Console.WriteLine();
        }
예제 #5
0
        public static async Task BroadcastTransaction(string senderPrivateKey, string operation, params object[] param)
        {
            try
            {
                // Create a new RPC client that is connected to a private test network
                RpcClient client = new RpcClient("http://localhost:10332");

                // Initialize the script that will be executed by the neo VM
                UInt160 contractHash = UInt160.Parse(SmartContractHash);
                byte[]  script       = contractHash.MakeScript(operation, param);

                // Generate a keypair from the sender's private key
                KeyPair  senderKeyPair = Neo.Network.RPC.Utility.GetKeyPair(senderPrivateKey);
                UInt160  sender        = Contract.CreateSignatureContract(senderKeyPair.PublicKey).ScriptHash;
                Signer[] signers       = new[]
                {
                    new Signer()
                    {
                        Scopes  = WitnessScope.CalledByEntry,
                        Account = sender
                    }
                };

                // Create and broadcast the transaction
                Transaction transaction = new TransactionManager(client)
                                          .MakeTransaction(script, signers, null)
                                          .AddSignature(senderKeyPair)
                                          .Sign()
                                          .Tx;
                client.SendRawTransaction(transaction);

                // Wait until transaction is confirmed on the chain
                WalletAPI wallet = new WalletAPI(client);
                await wallet.WaitTransaction(transaction).ContinueWith(async(p) =>
                {
                    Console.WriteLine($"Transaction vm state is  {(await p).VMState}");
                });
            }
            catch (Exception e)
            {
                throw e;
            }
        }