Exemple #1
0
        public void TestMessageTransactionBuilder()
        {
            var builder = new MessageTransactionBuilder();

            builder.NID        = 2;
            builder.PrivateKey = PrivateKey.Random();
            builder.To         = "hx54f7853dc6481b670caf69c5a27c7c8fe5be8269";
            builder.StepLimit  = NumericsHelper.ICX2Loop("0.1");
            builder.Value      = NumericsHelper.ICX2Loop("1");
            builder.Timestamp  = 100000000000;
            builder.Message    = "testMessage";

            var tx = builder.Build();

            Assert.True(Signer.Verify(tx.Signature, tx.Hash, tx.From));

            var hashSource = new Dictionary <string, object>()
            {
                ["version"]   = "0x3",
                ["nid"]       = "0x2",
                ["from"]      = Addresser.Create(builder.PrivateKey).ToString(),
                ["to"]        = "hx54f7853dc6481b670caf69c5a27c7c8fe5be8269",
                ["stepLimit"] = "0x16345785d8a0000",
                ["value"]     = "0xde0b6b3a7640000",
                ["timestamp"] = "0x174876e800",
                ["dataType"]  = "message",
                ["data"]      = new Bytes(Encoding.UTF8.GetBytes(builder.Message)).ToString()
            };

            var hash = Hasher.Digest(hashSource);

            Assert.AreEqual(tx.Hash, hash);
        }
Exemple #2
0
        public Transaction Build()
        {
            Address   from      = Addresser.Create(PrivateKey);
            Hash32    hash      = BuildHash(from);
            Signature signature = Signer.Sign(hash, PrivateKey);

            return(new Transaction(
                       Version, from, To, Value, StepLimit, Nonce, NID, Timestamp, DataType, Data, hash, signature
                       ));
        }
Exemple #3
0
        public void TestCallTransactionBuilder()
        {
            var builder = new CallTransactionBuilder();

            builder.NID             = 2;
            builder.PrivateKey      = PrivateKey.Random();
            builder.To              = "cx54f7853dc6481b670caf69c5a27c7c8fe5be8269";
            builder.StepLimit       = NumericsHelper.ICX2Loop("0.10000000000");
            builder.Value           = NumericsHelper.ICX2Loop("1.00000");
            builder.Timestamp       = 100000000000;
            builder.Method          = "transfer";
            builder.Params["to"]    = new ExternalAddress("hx54f7853dc6481b670caf69c5a27c7c8fe5be8269");
            builder.Params["value"] = new BigInteger(10);

            var tx = builder.Build();

            Assert.True(Signer.Verify(tx.Signature, tx.Hash, tx.From));

            var hashSource = new Dictionary <string, object>()
            {
                ["version"]   = "0x3",
                ["nid"]       = "0x2",
                ["from"]      = Addresser.Create(builder.PrivateKey).ToString(),
                ["to"]        = "cx54f7853dc6481b670caf69c5a27c7c8fe5be8269",
                ["stepLimit"] = "0x16345785d8a0000",
                ["value"]     = "0xde0b6b3a7640000",
                ["timestamp"] = "0x174876e800",
                ["dataType"]  = "call",
                ["data"]      = new Dictionary <string, object>()
                {
                    ["method"] = "transfer",
                    ["params"] = new Dictionary <string, object>()
                    {
                        ["to"]    = "hx54f7853dc6481b670caf69c5a27c7c8fe5be8269",
                        ["value"] = "0xa"
                    }
                }
            };

            var hash = Hasher.Digest(hashSource);

            Assert.AreEqual(tx.Hash, hash);
        }
Exemple #4
0
        public void TestDelopyTransaction()
        {
            var builder = new DeployTransactionBuilder();

            builder.NID             = 2;
            builder.PrivateKey      = PrivateKey.Random();
            builder.To              = "cx0000000000000000000000000000000000000000";
            builder.StepLimit       = NumericsHelper.ICX2Loop("0.1");
            builder.Timestamp       = 100000000000;
            builder.ContentType     = "application/zip";
            builder.Content         = new Bytes("0x1212121212");
            builder.Params["to"]    = new ExternalAddress("hx54f7853dc6481b670caf69c5a27c7c8fe5be8269");
            builder.Params["value"] = new BigInteger(10);

            var tx = builder.Build();

            Assert.True(Signer.Verify(tx.Signature, tx.Hash, tx.From));

            var hashSource = new Dictionary <string, object>()
            {
                ["version"]   = "0x3",
                ["nid"]       = "0x2",
                ["from"]      = Addresser.Create(builder.PrivateKey).ToString(),
                ["to"]        = "cx0000000000000000000000000000000000000000",
                ["stepLimit"] = "0x16345785d8a0000",
                ["timestamp"] = "0x174876e800",
                ["dataType"]  = "deploy",
                ["data"]      = new Dictionary <string, object>()
                {
                    ["contentType"] = "application/zip",
                    ["content"]     = "0x1212121212",
                    ["params"]      = new Dictionary <string, object>()
                    {
                        ["to"]    = "hx54f7853dc6481b670caf69c5a27c7c8fe5be8269",
                        ["value"] = "0xa"
                    }
                }
            };

            var hash = Hasher.Digest(hashSource);

            Assert.AreEqual(tx.Hash, hash);
        }
Exemple #5
0
        public void Test_KeyStore()
        {
            Random random = new Random();

            byte[] passwordBytes = new byte[random.Next() % 256];
            random.NextBytes(passwordBytes);
            string password = Convert.ToBase64String(passwordBytes);

            PrivateKey      privateKey = PrivateKey.Random();
            ExternalAddress address    = Addresser.Create(privateKey);

            KeyStore keyStore = KeyStore.Create(privateKey, address);
            string   fileName = keyStore.Store(password);

            keyStore = KeyStore.Load(password, fileName);

            File.Delete(fileName);

            Assert.AreEqual(privateKey, keyStore.PrivateKey);
            Assert.AreEqual(address, keyStore.Address);
        }
Exemple #6
0
        public async Task Test_Call()
        {
            var getScoreApi = new GetScoreApi(Consts.ApiUrl.TestNet);
            var scoreApi    = await getScoreApi.Invoke("cx0000000000000000000000000000000000000001");

            Console.WriteLine(JsonConvert.SerializeObject(scoreApi, Formatting.Indented));

            var privateKey = PrivateKey.Random();
            var address    = Addresser.Create(privateKey);

            var call   = new Call(Consts.ApiUrl.TestNet);
            var result = await call.Invoke(
                address,
                "cx0000000000000000000000000000000000000001",
                "isDeployer",
                ("address", address)
                );

            // 0x0
            Console.WriteLine(result);

            var call0   = new Call <bool>(Consts.ApiUrl.TestNet);
            var result0 = await call0.Invoke(
                address,
                "cx0000000000000000000000000000000000000001",
                "isDeployer",
                ("address", address)
                );

            // false
            Console.WriteLine(result0);

            var call1   = new Call <IsDeployerRequestParam, bool>(Consts.ApiUrl.TestNet);
            var result1 = await call1.Invoke(
                address,
                "cx0000000000000000000000000000000000000001",
                "isDeployer",
                new IsDeployerRequestParam()
            {
                Address = address
            }
                );

            // false
            Console.WriteLine(result1);

            var call2   = new Call <BigInteger>(Consts.ApiUrl.TestNet);
            var result2 = await call2.Invoke(
                address,
                "cx0000000000000000000000000000000000000001",
                "getStepPrice"
                );

            Console.WriteLine(result2);

            var call3   = new Call <GetRevisionResponseParam>(Consts.ApiUrl.TestNet);
            var result3 = await call3.Invoke(
                address,
                "cx0000000000000000000000000000000000000001",
                "getRevision"
                );

            Console.WriteLine(result3.Code);
            Console.WriteLine(result3.Name);

            var call4   = new Call <Dictionary <string, BigInteger> >(Consts.ApiUrl.TestNet);
            var result4 = await call4.Invoke(
                address,
                "cx0000000000000000000000000000000000000001",
                "getStepCosts"
                );

            Console.WriteLine(JsonConvert.SerializeObject(result4));
        }
Exemple #7
0
 public static Wallet Create(PrivateKey privateKey)
 {
     return(new Wallet(privateKey, Addresser.Create(privateKey)));
 }