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