示例#1
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
    }
示例#2
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();
    }
示例#3
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");
    }