示例#1
0
 void OnClickWatch()
 {
     lastWatcherId = nanoManager.Watch(NanoUtils.PrivateKeyToAddress(watcherPrivateKey), (watcherInfo) =>
     {
         WatchedUI.text = watcherInfo.hash;
     });
 }
示例#2
0
    void OnClickNextPrivateKey()
    {
        var privateKeyFiles = NanoUtils.GetPrivateKeyFiles();

        if (privateKeyFiles.Length > 0)
        {
            var privateKeyFile = privateKeyFiles[privateKeyIndex];
            privateKey = NanoUtils.LoadPrivateKey(privateKeyFile, password);
            if (!String.IsNullOrEmpty(privateKey))
            {
                PrivateKeyChanged();
            }
            ++privateKeyIndex;
            if (privateKeyIndex >= privateKeyFiles.Length - 1)
            {
                privateKeyIndex = 0;
            }
        }
        if (privateKeyFiles.Length > 1)
        {
            LastWorkUI.text       = ""; // Clear some fields
            BalanceUI.text        = "";
            PendingBalanceUI.text = "";
        }
    }
示例#3
0
    void OnClickCreatePrivateKey()
    {
        privateKey = NanoUtils.ByteArrayToHexString(NanoUtils.GeneratePrivateKey());
        TimeSpan t        = DateTime.UtcNow - new DateTime(1970, 1, 1);
        var      filename = "encrypted_privateKey_" + (int)t.TotalSeconds + ".nano";

        // Save the private key to disk
        NanoUtils.SavePrivateKey(privateKey, filename, password);
        PrivateKeyChanged();
        LastWorkUI.text = ""; // Clear last work
    }
示例#4
0
    void PrivateKeyChanged()
    {
        // Update the public key text element
        address          = NanoUtils.PrivateKeyToAddress(privateKey);
        PublicKeyUI.text = address;

        // Update QRcode for topping up funds
        var qrCodeAsTexture2D = NanoUtils.GenerateQRCodeTextureOnlyAccount(10, address, 10);

        QRCodeTopUpUI.sprite = Sprite.Create(qrCodeAsTexture2D, new Rect(0.0f, 0.0f, qrCodeAsTexture2D.width, qrCodeAsTexture2D.height), new Vector2(0.5f, 0.5f));
    }
示例#5
0
 private IEnumerator SendHandler()
 {
     yield return(nanoManager.Send(NanoUtils.PrivateKeyToAddress(watcherPrivateKey), new NanoAmount("1000000000000000000000000"), privateKey, (error, hash) =>
     {
         if (!error)
         {
             Debug.Log("Send confirmed!!");
         }
         else
         {
             Debug.Log("Error with Send");
         }
     }));
 }
示例#6
0
    IEnumerator PaidArcadeHandler()
    {
        // Recieve this block

        List <PendingBlock> pendingBlocks = null;
        var arcadeAddress = NanoUtils.PrivateKeyToAddress(arcadePrivateKey);

        yield return(nanoManager.PendingBlocks(arcadeAddress, (responsePendingBlocks) =>
        {
            pendingBlocks = responsePendingBlocks;
        }));

        // Just get the first one as it will have the highest amount
        if (pendingBlocks != null && pendingBlocks.Count > 0)
        {
            // Returns the one with the highest amount
            var pendingBlock = pendingBlocks[0];
            yield return(nanoManager.ReceiveWaitConf(arcadeAddress, pendingBlock, arcadePrivateKey, (error, hash) =>
            {
                if (!error)
                {
                    var qrCodePayoutAsTexture2D = NanoUtils.GenerateQRCodeTextureWithPrivateKey(10, arcadePrivateKey, 10);
                    QRCodePayoutArcadeUI.sprite = Sprite.Create(qrCodePayoutAsTexture2D, new Rect(0.0f, 0.0f, qrCodePayoutAsTexture2D.width, qrCodePayoutAsTexture2D.height), new Vector2(0.5f, 0.5f));
                    QRCodePayArcadeUI.sprite = null;

                    // Start the payout listener
                    var expirySecs = 30;
                    nanoManager.ListenForPayoutWaitConfirmation(arcadeAddress, expirySecs, (errorPayout) =>
                    {
                        if (!errorPayout)
                        {
                            PayoutArcadeUI.text = "Extracted privateKey";
                        }
                        else
                        {
                            PayoutArcadeUI.text = "PrivateKey expired";
                            Debug.Log("Did not retrieve the payout fast enough");
                        }
                        QRCodePayoutArcadeUI.sprite = null;
                    });
                }
                else
                {
                    Debug.Log("Error with ReceiveWaitConf");
                }
            }));
        }
    }
示例#7
0
    IEnumerator SendWaitConfHandler()
    {
        var amount = new NanoAmount(NanoUtils.NanoToRaw("0.000001"));

        yield return(nanoManager.SendWaitConf(NanoUtils.PrivateKeyToAddress(watcherPrivateKey), amount, privateKey, (error, hash) =>
        {
            if (!error)
            {
                Debug.Log("Send wait confirmed!!");
            }
            else
            {
                Debug.Log("Error with SendWaitConf");
            }
        }));
    }
示例#8
0
    private IEnumerator ReceiveNeedsWorkHandler()
    {
        if (LastWorkUI.text.Equals(""))
        {
            yield break;
        }

        // First we get the frontier
        string previous = null;
        string rep      = null;

        yield return(nanoManager.AccountInfo(address, (accountInfo) =>
        {
            previous = accountInfo.frontier;
            rep = accountInfo.representative;
        }));

        List <PendingBlock> pendingBlocks = null;

        yield return(nanoManager.PendingBlocks(address, (responsePendingBlocks) =>
        {
            pendingBlocks = responsePendingBlocks;
        }));

        if (pendingBlocks.Count != 0)
        {
            // Just get the first one as it will have the highest amount
            var pendingBlock = pendingBlocks[0];

            // Create the block to receive
            var newBalance = currentBalance + pendingBlock.amount;
            var block      = nanoManager.CreateBlock(address, NanoUtils.HexStringToByteArray(privateKey), newBalance, pendingBlock.source, previous, rep != null ? rep : defaultRep, LastWorkUI.text);
            yield return(nanoManager.Process(block, previous == null ? BlockType.open : BlockType.receive, (hash) =>
            {
                if (hash != null)
                {
                    LastWorkUI.text = ""; // Clear it
                }

                Debug.Log(hash);
            }));
        }
        else
        {
            Debug.Log("There are no pending blocks to receive");
        }
    }
示例#9
0
    // Send to yourself
    private IEnumerator SendNeedsWorkHandler()
    {
        if (LastWorkUI.text.Equals(""))
        {
            yield break;
        }

        // First we get the frontier
        string previous = null;
        string rep      = null;

        yield return(nanoManager.AccountInfo(address, (accountInfo) =>
        {
            previous = accountInfo.frontier;
            rep = accountInfo.representative;
        }));

        if (previous != null)
        {
            // Create the block to send
            var newBalance = currentBalance - new NanoAmount(System.Numerics.BigInteger.Parse("1000000000000000000000000"));
            var block      = nanoManager.CreateBlock(address, NanoUtils.HexStringToByteArray(privateKey), newBalance, NanoUtils.PrivateKeyToPublicKeyHexString(watcherPrivateKey), previous, rep, LastWorkUI.text);
            yield return(nanoManager.Process(block, BlockType.send, (hash) =>
            {
                if (hash != null)
                {
                    LastWorkUI.text = ""; // Clear it
                }

                Debug.Log(hash);
            }));
        }
        else
        {
            Debug.Log("Account does not exist yet");
        }
    }
示例#10
0
    void Start()
    {
        // Initialize RPC & Websocket
        nanoManager            = gameObject.AddComponent <NanoManager>();
        nanoManager.rpcURL     = "http://95.216.164.23:28103"; // Update this url to point to your JSON-RPC server
        nanoManager.defaultRep = defaultRep;

        nanoWebsocket         = gameObject.AddComponent <NanoWebSocket>();
        nanoWebsocket.url     = "ws://95.216.164.23:28104"; // Update this url to point to your websocket server
        nanoManager.Websocket = nanoWebsocket;

        Debug.Log("Private key files located at: " + Path.Combine(Application.persistentDataPath, "Nano"));

        // Update QR codes for arcade
        arcadePrivateKey = NanoUtils.ByteArrayToHexString(NanoUtils.GeneratePrivateKey());

        nanoManager.AddOnWebsocketConnectListener((bool isError, bool isReconnect) =>
        {
            // Called when the connection is successfully opened (or failed), it will automatically keep trying to connect if there is a failure
            if (!isError)
            {
                nanoManager.ListenForPaymentWaitConfirmation(NanoUtils.PrivateKeyToAddress(arcadePrivateKey), new NanoAmount("1000000000000000000000000"), true, (error) =>
                {
                    if (!error)
                    {
                        PayArcadeUI.text = "Paid!";
                        // Wait until the account has a balance
                        StartCoroutine(PaidArcadeHandler());
                    }
                    else
                    {
                        Debug.Log("Error with payment");
                    }
                });

                Debug.Log("Successfully connected to websocket!!");
            }
            else
            {
                Debug.Log("Failed to connect to websocket!!");
            }
        });

        nanoManager.AddConfirmationListener((websocketConfirmationResponse) =>
        {
            Debug.Log("Confirmation received");
            string output = "";
            var block     = websocketConfirmationResponse.message.block;
            output       += "type: " + block.type + "\n";
            output       += "account: " + block.account + "\n";
            output       += "previous: " + block.previous + "\n";
            output       += "representative: " + block.representative + "\n";
            output       += "balance: " + block.balance + "\n";
            output       += "link: " + block.link + "\n";
            output       += "link_as_account: " + block.link_as_account + "\n";
            output       += "signature: " + block.signature + "\n";
            output       += "work: " + block.work + "\n";
            output       += "subtype: " + block.subtype;
            WebsockConfirmationResponseUI.text = output;
        });

        nanoManager.AddFilteredConfirmationListener((websocketConfirmationResponse) =>
        {
            Debug.Log("Confirmation received");
        });

        // Add event listeners for all the buttons
        CreatePrivateKeyUI.onClick.AddListener(OnClickCreatePrivateKey);
        NextPrivateKeyUI.onClick.AddListener(OnClickNextPrivateKey);
        GenerateWorkUI.onClick.AddListener(OnClickGenerateWork);
        SendNeedsWorkUI.onClick.AddListener(OnClickSendNeedsWork);
        ReceiveNeedsWorkUI.onClick.AddListener(OnClickReceiveNeedsWork);
        SendUI.onClick.AddListener(OnClickSend);
        ReceiveUI.onClick.AddListener(OnClickReceive);
        SendWaitConfUI.onClick.AddListener(OnClickSendWaitConf);
        ReceiveWaitConfUI.onClick.AddListener(OnClickReceiveWaitConf);
        AutomatePocketingUI.onClick.AddListener(OnClickAutomatePocketing);
        UnautomatePocketingUI.onClick.AddListener(OnClickUnautomatePocketing);
        ListenAllConfirmationsUI.onClick.AddListener(OnClickListenAllConfirmations);
        UnlistenAllConfirmationsUI.onClick.AddListener(OnClickUnlistenAllConfirmations);
        WatchUI.onClick.AddListener(OnClickWatch);
        UnwatchUI.onClick.AddListener(OnClickUnwatch);

        var numRawPayToPlay      = "1000000000000000000000000";
        var qrCodePayAsTexture2D = NanoUtils.GenerateQRCodeTextureWithAmount(10, NanoUtils.PrivateKeyToAddress(arcadePrivateKey), numRawPayToPlay, 10);

        QRCodePayArcadeUI.sprite = Sprite.Create(qrCodePayAsTexture2D, new Rect(0.0f, 0.0f, qrCodePayAsTexture2D.width, qrCodePayAsTexture2D.height), new Vector2(0.5f, 0.5f));

        watcherPrivateKey = NanoUtils.ByteArrayToHexString(NanoUtils.GeneratePrivateKey());

        OnClickNextPrivateKey();
    }
示例#11
0
 void OnClickUnwatch()
 {
     nanoManager.Unwatch(NanoUtils.PrivateKeyToAddress(watcherPrivateKey), lastWatcherId);
 }
示例#12
0
    // Start is called before the first frame update
    void Start()
    {
        // Validate nano/raw functions
        if (!NanoUtils.ValidateNano("100.123"))
        {
            Debug.Log("Error validating nano");
            return;
        }

        if (!NanoUtils.ValidateNano("100,123"))
        {
            Debug.Log("Error validating nano");
            return;
        }

        if (!NanoUtils.ValidateNano("340282366.920938463463374607431768211455"))
        {
            Debug.Log("Error validating nano");
            return;
        }

        if (!NanoUtils.ValidateNano("0.1231231"))
        {
            Debug.Log("Error validating nano");
            return;
        }

        if (!NanoUtils.ValidateNano(".1223"))
        {
            Debug.Log("Error validating nano");
            return;
        }

        if (!NanoUtils.ValidateNano(",1223"))
        {
            Debug.Log("Error validating nano");
            return;
        }

        if (NanoUtils.ValidateNano(".122.3"))
        {
            Debug.Log("Error validating nano, 2 decimal points");
            return;
        }

        if (!NanoUtils.ValidateRaw("100"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        if (!NanoUtils.ValidateRaw("340282366920938463463374607431768211455"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        if (!NanoUtils.ValidateRaw("0001"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        if (NanoUtils.ValidateRaw("100.123"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        if (NanoUtils.ValidateRaw("100@123"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        if (NanoUtils.ValidateRaw("1111111111111111111111111111111111111111111111111111111111"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        // 1 above max raw
        if (NanoUtils.ValidateRaw("340282366920938463463374607431768211456"))
        {
            Debug.Log("Error validating raw");
            return;
        }

        // Raw to nano
        var raw  = "10000000000000000000000000000000";
        var nano = NanoUtils.RawToNano(raw);

        if (!nano.Equals("10.0"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        raw  = "1000000000000000000000000000000";
        nano = NanoUtils.RawToNano(raw);
        if (!nano.Equals("1.0"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        raw  = "100000000000000000000000000000";
        nano = NanoUtils.RawToNano(raw);
        if (!nano.Equals("0.1"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        raw  = "10000000000000000000000000000";
        nano = NanoUtils.RawToNano(raw);
        if (!nano.Equals("0.01"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        raw  = "100000000000000000000000000";
        nano = NanoUtils.RawToNano(raw);
        if (!nano.Equals("0.0001"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        raw  = "100";
        nano = NanoUtils.RawToNano(raw);
        if (!nano.Equals("0.0000000000000000000000000001"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        raw  = "1";
        nano = NanoUtils.RawToNano(raw);
        if (!nano.Equals("0.000000000000000000000000000001"))
        {
            Debug.Log("Error converting raw to nano");
            return;
        }

        // Nano to raw
        nano = "10.0";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("10000000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = "10";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("10000000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = "1.0";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("1000000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = "0.1";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("100000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = ".1";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("100000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = "00000.1";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("100000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = "0.01";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("10000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        nano = "0.000000000000000000000000000001";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("1"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        // Test localization of using a comma
        nano = "0,01";
        raw  = NanoUtils.NanoToRaw(nano);
        if (!raw.Equals("10000000000000000000000000000"))
        {
            Debug.Log("Error converting nano to raw");
            return;
        }

        // Test NanoAmount
        if (!((new NanoAmount("1") + new NanoAmount("2")).Equals(new NanoAmount("3"))))
        {
            Debug.Log("Error with adding");
            return;
        }

        if (!((new NanoAmount("2") - new NanoAmount("1")).Equals(new NanoAmount("1"))))
        {
            Debug.Log("Error with subtracting");
            return;
        }

        if (!(new NanoAmount("3000") > new NanoAmount("2000")))
        {
            Debug.Log("Error with greater");
            return;
        }

        if ((new NanoAmount("2000") > new NanoAmount("2000")))
        {
            Debug.Log("Error with greater");
            return;
        }

        if ((new NanoAmount("1999") > new NanoAmount("2000")))
        {
            Debug.Log("Error with greater");
            return;
        }

        if (!(new NanoAmount("3000") >= new NanoAmount("2000")))
        {
            Debug.Log("Error with greater or equal");
            return;
        }

        if (!(new NanoAmount("2000") >= new NanoAmount("2000")))
        {
            Debug.Log("Error with greater or equal");
            return;
        }

        if ((new NanoAmount("1999") > new NanoAmount("2000")))
        {
            Debug.Log("Error with greater or equal");
            return;
        }

        var amount = new NanoAmount(100);

        byte[] bytes = NanoUtils.HexStringToByteArray("E989DE925A4EDEE45447158557AD1409450315491F147F4AAA8F37DCA355354A");

        byte[] b  = NanoUtils.AddressToPublicKeyByteArray("nano_3kqdiqmqiojr1aqqj51aq8bzz5jtwnkmhb38qwf3ppngo8uhhzkdkn7up7rp");
        string s1 = NanoUtils.SignHash("E989DE925A4EDEE45447158557AD1409450315491F147F4AAA8F37DCA355354A", bytes);
        string s2 = NanoUtils.PublicKeyToAddress(bytes);
        string s3 = NanoUtils.ByteArrayToHexString(bytes);

        var prvKey   = NanoUtils.GeneratePrivateKey();
        var password = "******" + Random.value;
        var filename = "privateKey1.nano";

        NanoUtils.SavePrivateKey(NanoUtils.ByteArrayToHexString(prvKey), filename, password);
        var originalPrivateKey  = NanoUtils.ByteArrayToHexString(prvKey);
        var extractedPrivateKey = NanoUtils.LoadPrivateKey(filename, password);

        if (!originalPrivateKey.Equals(extractedPrivateKey))
        {
            Debug.Log("Error decrypting privateKey");
            return;
        }

        Debug.Log("Successfully tested Utils");
    }