Ejemplo n.º 1
0
        public static bool CallContract(string method, object[] values)
        {
            try
            {
                var key        = new KeyPair(privateKey.HexToBytes());
                var scriptHash = new UInt160(contractScriptHash.HexToBytes());

                var api      = NeoRPC.ForTestNet();
                var response = api.CallContract(key, scriptHash, method, values);
                if (response != null)
                {
                    return(true);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Null response received on CallContract");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public static UserCredentials GetUserCredentials(byte[] encodedUsername)
        {
            UserCredentials uc = null;

            var api = NeoRPC.ForTestNet();
            //Console.WriteLine("guc.encodedUsername: '******'");
            var response2 = api.TestInvokeScript(NeoDraw_ContractHash, new object[] { "get", "user", new object[] { encodedUsername } });

            object[]   resultsArray2    = (object[])response2.result;
            byte[]     bencodedUsername = (byte[])(((object[])((object[])resultsArray2[0])[0])[0]);
            byte[]     bencodedPassword = (byte[])(((object[])((object[])resultsArray2[0])[0])[1]);
            BigInteger status           = (BigInteger)(((object[])((object[])resultsArray2[0])[0])[2]);
            //string sencodedUsername = Encoding.ASCII.GetString(bencodedUsername).Replace("\0", "");
            //string sencodedPassword = Encoding.ASCII.GetString(bencodedPassword).Replace("\0", "");
            string sencodedUsername = bencodedUsername.ToHexString();
            string sencodedPassword = bencodedPassword.ToHexString();

            //Console.WriteLine("guc.sencodedUsername,sencodedPassword: '******' '" + sencodedPassword + "'");
            //Console.WriteLine("guc.sencodedUsername,sencodedPassword: "******" " + sencodedPassword.Length + "");
            //Console.WriteLine("guc.status: " + status.ToString());

            if (status != 5 && sencodedUsername.Length > 0 && sencodedPassword.Length > 0)
            {
                uc = new UserCredentials {
                    encodedUsername = bencodedUsername, encodedPassword = bencodedPassword
                };
            }

            return(uc);
        }
Ejemplo n.º 3
0
        public static UserPoint[] GetAllPoints(byte[] encodedUsername)
        {
            List <UserPoint> points = new List <UserPoint>();

            var api      = NeoRPC.ForTestNet();
            var response = api.TestInvokeScript(NeoDraw_ContractHash, new object[] { "getall", "point", new object[] { encodedUsername } });

            object[] resultsArray = (object[])response.result;
            if (resultsArray != null)
            {
                //Console.WriteLine("resultsArray.length: " + resultsArray.Length);
                for (int element = 0; element < resultsArray.Length; element++)
                {
                    byte[] bx = (byte[])(((object[])((object[])resultsArray[element])[0])[0]);
                    byte[] by = (byte[])(((object[])((object[])resultsArray[element])[0])[1]);
                    string sx = Encoding.ASCII.GetString(bx);
                    string sy = Encoding.ASCII.GetString(by);
                    //Console.WriteLine("sx,sy: " + sx + " " + sy);
                    int ix = Int32.Parse(sx);
                    int iy = Int32.Parse(sy);
                    //Console.WriteLine("ix,iy: " + ix.ToString() + " " + iy.ToString());

                    points.Add(new UserPoint {
                        x = ix, y = iy
                    });
                }
            }

            return(points.ToArray <UserPoint>());
        }
Ejemplo n.º 4
0
    void Start()
    {
        this.api = NeoRPC.ForTestNet();

        this.keys = new KeyPair("a9e2b5436cab6ff74be2d5c91b8a67053494ab5b454ac2851f872fb0fd30ba5e".HexToBytes());

        this.addressLabel.text = keys.address;
        this.balanceLabel.text = "Please wait, syncing balance...";
    }
Ejemplo n.º 5
0
        public static bool AddUser(byte[] encodedUsername, byte[] encodedPassword)
        {
            var  api = NeoRPC.ForTestNet();
            var  TestNetAccount_KeyPair = new KeyPair(TestNetAccount_PrivateKey.HexToBytes());
            bool response5 = api.CallContract(TestNetAccount_KeyPair, NeoDraw_ContractHash, new object[] { "add", "user", new object[] { encodedPassword, encodedUsername } });

            //Console.WriteLine("response5: " + ((bool)response5).ToString());
            return((bool)response5);
        }
Ejemplo n.º 6
0
        public static bool AddPoint(byte[] encodedUsername, UserPoint up)
        {
            //Console.WriteLine("AddPoint:Adding point ( " + up.x.ToString() + ", " + up.y.ToString() + ") for " + encodedUsername.ToHexString());
            var  api = NeoRPC.ForTestNet();
            var  TestNetAccount_KeyPair = new KeyPair(TestNetAccount_PrivateKey.HexToBytes());
            bool response5 = api.CallContract(TestNetAccount_KeyPair, NeoDraw_ContractHash, new object[] { "add", "point", new object[] { up.y, up.x, encodedUsername } });

            //Console.WriteLine("response5: " + ((bool)response5).ToString());
            return((bool)response5);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            // NOTE - You can also create an API instance for a specific private net
            var api = NeoRPC.ForTestNet();

            // NOTE - Private keys should not be hardcoded in the code, this is just for demonstration purposes!
            var privateKey = "a9e2b5436cab6ff74be2d5c91b8a67053494ab5b454ac2851f872fb0fd30ba5e";

            Console.WriteLine("*Loading NEO address...");
            var keys = new KeyPair(privateKey.HexToBytes());

            Console.WriteLine("Got :" + keys.address);

            // it is possible to optionally obtain also token balances with this method
            Console.WriteLine("*Syncing balances...");
            var balances = api.GetBalancesOf(keys.address, false);

            foreach (var entry in balances)
            {
                Console.WriteLine(entry.Value + " " + entry.Key);
            }

            // TestInvokeScript let's us call a smart contract method and get back a result
            // NEP5 https://github.com/neo-project/proposals/issues/3

            api = NeoRPC.ForMainNet();
            var redPulse = api.GetToken("RPX");

            // you could also create a NEP5 from a contract script hash
            //var redPulse_contractHash = "ecc6b20d3ccac1ee9ef109af5a7cdb85706b1df9";
            //var redPulse = new NEP5(api, redPulse_contractHash);

            Console.WriteLine("*Querying Symbol from RedPulse contract...");
            //var response = api.TestInvokeScript(redPulse_contractHash, "symbol", new object[] { "" });
            //var symbol = System.Text.Encoding.ASCII.GetString((byte[])response.result);
            var symbol = redPulse.Symbol;

            Console.WriteLine(symbol); // should print "RPX"

            // here we get the RedPulse token balance from an address
            Console.WriteLine("*Querying BalanceOf from RedPulse contract...");
            //var balance = api.GetTokenBalance("AVQ6jAQ3Prd32BXU5r2Vb3QL1gYzTpFhaf", "RPX");
            var balance = redPulse.BalanceOf("AVQ6jAQ3Prd32BXU5r2Vb3QL1gYzTpFhaf");

            Console.WriteLine(balance);

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the Neo RPC for the specified network type.  Defaults to Testnet
        /// </summary>
        /// <param name="type">NetworkType - network to use</param>
        /// <returns>NeoRPC - the RPC for the requested network</returns>
        public static NeoRPC GetNeoRPCForType(NetworkType type)
        {
            switch (type)
            {
            case NetworkType.Mainnet:
                return(NeoRPC.ForMainNet());

            case NetworkType.Testnet:
                return(NeoRPC.ForTestNet());

            case NetworkType.CozTestnet:
                throw new Exception("COZ Testnet not implemented yet");

            case NetworkType.Privnet:
                return(NeoRPC.ForPrivateNet());
            }

            return(NeoRPC.ForTestNet());
        }
Ejemplo n.º 9
0
        private static NeoRPC BaseRpcInstance()
        {
            if (Configuration["Neo:Network"] == "Main")
            {
                return(NeoRPC.ForMainNet());
            }

            if (Configuration["Neo:Network"] == "Test")
            {
                return(NeoRPC.ForTestNet());
            }

            if (Configuration["Neo:Network"] == "AllCode")
            {
                return(new NeoFluxNeoRPC(Configuration["Neo:DefaultScanUrl"]));
            }

            return(null);
        }
Ejemplo n.º 10
0
 public void Init()
 {
     api   = NeoRPC.ForTestNet();
     token = api.GetToken("RPX");
 }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            string address;

            do
            {
                Console.WriteLine("Write an NEO address to listen:");
                address = Console.ReadLine();
            } while (!address.IsValidAddress());

            var api = NeoRPC.ForTestNet();

            var oldBlockCount = api.GetBlockHeight();

            var targetScriptHash = new UInt160(address.AddressToScriptHash());

            Console.WriteLine("Now listening for transactions...");

            do
            {
                // wait for block generation
                Thread.Sleep(10000);

                var newBlockCount = api.GetBlockHeight();

                if (newBlockCount != oldBlockCount)
                {
                    Console.WriteLine($"Fetching block {newBlockCount}");

                    // retrieve latest block
                    var block = api.GetBlock(newBlockCount);

                    if (block == null)
                    {
                        Console.WriteLine($"Failed...");
                        continue;
                    }

                    oldBlockCount = newBlockCount;

                    // inspect each tx in the block for inputs sent to the target address
                    foreach (var tx in block.transactions)
                    {
                        var amounts = new Dictionary <string, decimal>();

                        foreach (var output in tx.outputs)
                        {
                            if (output.scriptHash.Equals(targetScriptHash))
                            {
                                var asset = NeoAPI.SymbolFromAssetID(output.assetID);

                                if (amounts.ContainsKey(asset))
                                {
                                    amounts[asset] += output.value;
                                }
                                else
                                {
                                    amounts[asset] = output.value;
                                }
                            }
                        }

                        if (amounts.Count > 0)
                        {
                            foreach (var entry in amounts)
                            {
                                Console.WriteLine($"{entry.Value} {entry.Key} was sent to {address}, in tx {tx.Hash}");
                            }
                        }
                    }
                }
            } while (true);
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            if (args.Length < 5)
            {
                Console.WriteLine("neo-sender <Net> <PrivateKey> <DestAddress> <Symbol> <Amount>");
                Console.WriteLine("Net          Can be Main, Test or custom URL");
                Console.WriteLine("PrivateKey   Can be a full hex private key or a WIF private key");
                Console.WriteLine("DestAddress  Must be a valid Neo address");
                Console.WriteLine("Symbol       Must be Neo, Gas or any support token (eg: RPX, DBC)");
                return;
            }

            var keyStr        = args[1];
            var outputAddress = args[2];

            var symbol = args[3];   //"GAS"
            var amount = decimal.Parse(args[4]);

            var fromKey = keyStr.Length == 52 ? KeyPair.FromWIF(keyStr) : new KeyPair(keyStr.HexToBytes());

            Console.WriteLine($"Sending {amount} {symbol} from {fromKey.address} to {outputAddress}");

            var    net = args[0].ToLowerInvariant();
            NeoAPI api;

            switch (net)
            {
            case "main": api = NeoRPC.ForMainNet(); break;

            case "test": api = NeoRPC.ForTestNet(); break;

            default: api = new NeoRPC(net); break;
            }

            bool result = false;

            try
            {
                if (api.IsToken(symbol))
                {
                    var token = api.GetToken(symbol);
                    result = token.Transfer(fromKey, outputAddress, amount);
                }
                else
                if (api.IsAsset(symbol))
                {
                    result = api.SendAsset(fromKey, outputAddress, symbol, amount);
                }
                else
                {
                    Console.WriteLine("Unknown symbol.");
                    Environment.Exit(-1);
                }
            }
            catch
            {
                Console.WriteLine("Error executing transaction.");
                Environment.Exit(-1);
            }

            Console.WriteLine("Transaction result: " + result);
        }