Exemplo n.º 1
0
        public void ShouldBeModifiable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());
                Assert.True(received.Verify(key.Address));

                // verify ok if header changed
                received.Headers.Set("timestamp", DateTime.Now);
                Assert.True(received.Verify(key.Address));

                // verify failed if body changed
                received.Body.Set("id", RndProvider.Next(100));
                Assert.False(received.Verify(key.Address));
            }
        }
Exemplo n.º 2
0
        public void ShouldBeVerifiable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                PrivateKey wrongKey = key.CKD(key.Bytes);
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());

                Assert.True(received.Verify(key.Address));
                Assert.False(received.Verify(wrongKey.Address));

                // verify failed if body has changed
                received.Body.Set("rndBytes", RndProvider.GetNonZeroBytes(128));
                Assert.False(received.Verify(key.Address));
            }
        }
Exemplo n.º 3
0
        public async Task <(string key, string error)> GetUserKeyAsync(string token, int id = 0)
        {
            try
            {
                PrivateKey nonce = PrivateKey.CreateKey();

                (JsonRpc response, string error) = await PostAsync(new JsonRpc.Request(cyprus_getUserKey, id, token, Hex.ToString(nonce.PublicKey.CompressedKey)));

                if (ReferenceEquals(response, null))
                {
                    return(null, error);
                }

                string encrypted = response.Result <string>(0);
                string openKey   = response.Result <string>(1);
                string passKey   = nonce.CreateEcdhKey(openKey);

                // decrypt user key
                return(Aes256.TryDecrypt(Hex.ToByteArray(passKey), Hex.ToByteArray(encrypted), out var plain) ? (Hex.ToString(plain), (string)null) : (null, "can't decrypt key"));
            }
            catch (Exception ex)
            {
                return(null, ex.Message);
            }
        }
Exemplo n.º 4
0
        public void ShouldBeSerializable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());

                Assert.Equal(message.Hash, received.Hash);
                Assert.Equal(rndBytes, message.Value <byte[]>("rndBytes"));
                Assert.Equal(rndBytes, received.Value <byte[]>("rndBytes"));
                Assert.Equal(message.Value <byte[]>("rndBytes"), received.Value <byte[]>("rndBytes"));
                Assert.Equal(message.Headers.Hash, received.Headers.Hash);
                Assert.Equal(message.Body.Hash, received.Body.Hash);
            }
        }
        public void Test2()
        {
            PrivateKey key = PrivateKey.CreateKey();

            ElasticLayout layout = ElasticLayout.DefineLayout(1024);
            Elastic3D     to     = new Elastic3D((byte)(1 + RndProvider.Next(layout.X)), (byte)(1 + RndProvider.Next(layout.Y)), (byte)(1 + RndProvider.Next(layout.Z)));

            Message message = new Message.Builder()
                              .Body("rndBytes", RndProvider.GetNonZeroBytes(128))
                              .Build(key);

            PrivateKey routerKey = PrivateKey.CreateKey();

            message.RouteTo(to.TimeToLive(), to, layout, routerKey);

            Payload routes = message.Routes();

            Assert.Equal(to.TimeToLive(), routes.Value <byte>("ttl"));
            Assert.Equal(to, routes.Value <Elastic3D>("to"));
            Assert.Equal(layout, routes.Value <ElasticLayout>("layout"));

            Address router = message.Router();

            Assert.Equal(routerKey.Address, router);
            Assert.True(message.VerifyRouter());

            message.TimeToLive(0);

            Assert.Equal(0, message.TimeToLive());

            Assert.Equal(0, new Elastic3D(0, 0, 0).TimeToLive());
            Assert.Equal(1, new Elastic3D(16, 0, 0).TimeToLive());
            Assert.Equal(2, new Elastic3D(13, 15, 0).TimeToLive());
            Assert.Equal(3, new Elastic3D(1, 2, 3).TimeToLive());
        }
        public void ShouldBeParsableElastic3D()
        {
            PrivateKey messageKey = PrivateKey.CreateKey();

            Elastic3D     to     = new Elastic3D(1, 2, 3);
            ElasticLayout layout = new ElasticLayout(16, 16, 16);


            Message message = new Message.Builder()
                              .Body("rndBytes", RndProvider.GetNonZeroBytes(32))
                              .Build(messageKey);
        }
        public void DateTimeTest()
        {
            PrivateKey key = PrivateKey.CreateKey();

            Message ping = new Message.Builder()
                           .Body("rndBytes", RndProvider.GetNonZeroBytes(32))
                           .Build(key);

            Message received = Message.Parse(ping.ToBytes());

            Assert.Equal(ping.TimeStamp, received.TimeStamp);
        }
Exemplo n.º 8
0
        public void SignerShouldBeDeterministic()
        {
            for (int i = 0; i < repeats; i++)
            {
                H256 messageHash = SecureRandom.GetBytes(H256.BYTE_LENGTH);

                PrivateKey privateKey = PrivateKey.CreateKey();

                Signature sig1 = privateKey.Sign(messageHash);
                Signature sig2 = privateKey.Sign(messageHash);

                Assert.Equal(sig1, sig2);
            }
        }
Exemplo n.º 9
0
        public void EcdhTest()
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey a = PrivateKey.CreateKey();
                PrivateKey b = PrivateKey.CreateKey();

                string aa = a.PublicKey;
                string bb = b.PublicKey;

                var expected = a.CreateEcdhKey(bb);
                var actual   = b.CreateEcdhKey(aa);

                Assert.Equal(expected, actual);
            }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            ILoggable logger = new BLog.Builder()
                               .WithConsole(true)
                               .WithFilter(LogFilter.All)
                               .Build();

            PrivateKey key = PrivateKey.CreateKey();

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                NodeServer server = new NodeServer(logger);
                server.Start(PORT, cts);

                Console.CancelKeyPress += (sender, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                byte[] rndBytes = RndProvider.GetNonZeroBytes(1024 * 1024);

                while (!cts.IsCancellationRequested)
                {
                    Thread.Sleep(10);

                    rndBytes = RndProvider.GetNonZeroBytes(1024 * 1024);
                    Message message = new Message.Builder()
                                      .Body("rndBytes", rndBytes)
                                      .Build(key);

                    byte[] data = message.ToBytes();
                    //                    ElasticNode.SendTo(data, "127.0.0.1", PORT);
                    NodeClient client = new NodeClient(logger);
                    client.Start("127.0.0.1", PORT);

                    Thread.Sleep(1000);

//                    client.Write(data);
                    client.Stop();
                }
            }
        }
Exemplo n.º 11
0
        public void SignerShouldRecoverableWithValidRecoverId()
        {
            for (int i = 0; i < repeats; i++)
            {
                H256 messageHash = SecureRandom.GetBytes(H256.BYTE_LENGTH);

                PrivateKey privateKey = PrivateKey.CreateKey();

                Signature sig = privateKey.Sign(messageHash);

                Assert.True(sig.V < 4);
                Assert.Equal(privateKey.PublicKey, sig.GetPublicKey(messageHash));

                byte[] bytes = sig;
                bytes[64] = 99;

                Signature wrong = Signature.Parse(bytes);
                Assert.NotEqual(privateKey.PublicKey, wrong.GetPublicKey(messageHash));
            }
        }
Exemplo n.º 12
0
        public void SignerShouldGenerateValidKey()
        {
            for (int i = 0; i < repeats; i++)
            {
                H256 messageHash = SecureRandom.GetBytes(H256.BYTE_LENGTH);

                PrivateKey secKey = PrivateKey.CreateKey();
                PublicKey  pubKey = secKey.PublicKey;
                Signature  sig    = secKey.Sign(messageHash);

                // private key verify
                Assert.True(Secp256k1Helper.SecretKeyVerify(secKey.Key));

                // private key, public key, signature length verify
                Assert.Equal(PrivateKey.KEY_LENGTH, secKey.Key.Length);
                Assert.Equal(PublicKey.KEY_LENGTH, pubKey.Length);
                Assert.Equal(Signature.LENGTH, sig.Length);

                // verify signature
                Assert.True(pubKey.Verify(sig, messageHash));
                Assert.Equal(pubKey, sig.GetPublicKey(messageHash));
            }
        }
Exemplo n.º 13
0
        private void OnCommandAccountsNew(string[] args)
        {
            string name = args.Length > 0 ? args[0] : BConsole.ReadLine("name: ");

            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            if (wallets.Contains(name))
            {
                BConsole.WriteLine("account name already existing!");
                return;
            }

            string password = BConsole.ReadPassword("password: "******"confirm: ");

            if (password != confirm)
            {
                BConsole.WriteLine("password mismatch");
                return;
            }

            // encrypted key
            string keystore = KeyStoreService.EncryptKeyStoreV3(PrivateKey.CreateKey(), password);

            var account = wallets.Add(name, keystore);

            BConsole.WriteLine("new account(", name, ")! address=", account.Address);
        }
        public void ShouldBeSerializableObject()
        {
            Person jade = new Person()
            {
                id     = 100,
                name   = "JADE KIM",
                female = false,
                age    = 43,
                height = 174.5m,
                weight = 77.01,
                hobby  = new string[] { "Basketball", "Movie", "Music" },
                birth  = new DateTime(1977, 3, 19),
                key    = PrivateKey.CreateKey().Bytes
            };

            // serialize & deserialize
            Person person = Payload.Parse(
                new Payload.Builder(jade)
                .Build()
                .ToBytes()
                ).ToObject <Person>();

            // assert
            Assert.Equal(jade.id, person.id);
            Assert.Equal(jade.name, person.name);
            Assert.Equal(jade.female, person.female);
            Assert.Equal(jade.age, person.age);
            Assert.Equal(jade.height, person.height);
            Assert.Equal(jade.weight, person.weight);
            Assert.Equal(jade.birth, person.birth);
            Assert.Equal(jade.key.ToHex(), person.key.ToHex());
            Assert.Null(person.context);

            for (int i = 0; i < jade.hobby.Length; i++)
            {
                Assert.Equal(jade.hobby[i], person.hobby[i]);
            }

            // serialize & deserialize
            person = Payload.Parse(
                new Payload.Builder()
                .Value("person", jade)
                .Build()
                .ToBytes()
                ).Value <Person>("person");

            // assert
            Assert.Equal(jade.id, person.id);
            Assert.Equal(jade.name, person.name);
            Assert.Equal(jade.female, person.female);
            Assert.Equal(jade.age, person.age);
            Assert.Equal(jade.height, person.height);
            Assert.Equal(jade.weight, person.weight);
            Assert.Equal(jade.birth, person.birth);
            Assert.Equal(jade.key.ToHex(), person.key.ToHex());
            Assert.Null(person.context);

            for (int i = 0; i < jade.hobby.Length; i++)
            {
                Assert.Equal(jade.hobby[i], person.hobby[i]);
            }
        }