Exemplo n.º 1
0
        public void CallContractOnLocalNodeTest()
        {
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            ulong   nonce   = account.Nonce + 1;

            Calldata callData = nativeClient.EncodeCalldata(logger, TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, TestConstants.TestContractFunctionParams);

            UnsignedTx unsignedTxNative = CreateUnsignedContractCallTx(nonce, callData.CallData, null);
            Tx         signedTxNative   = nativeClient.SignTransaction(unsignedTxNative, baseKeyPair.PrivateKey);

            // post the signed contract call tx
            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);

            Assert.AreEqual(postTxResponse.TXHash, Encoding.ComputeTxHash(signedTxNative.TX));
            logger.LogInformation("CreateContractTx hash: " + postTxResponse.TXHash);

            // get the tx info object to resolve the result
            TxInfoObject txInfoObject = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);


            // decode the result to json

            JToken json = nativeClient.DecodeCallResult(TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, txInfoObject.CallInfo.ReturnType, txInfoObject.CallInfo.ReturnValue, CompileOptsBackend.Fate);

            Assert.AreEqual(TestConstants.TestContractFunctionParam, json.Value <string>());
        }
Exemplo n.º 2
0
    public async Task <SignedTx> SignTx(string sk, UnsignedTx tx)
    {
        var req = new SignTxRequest();

        req.SecretKey  = sk;
        req.UnsignedTx = tx;
        var res = await client.SignTxAsync(req);

        return(res);
    }
Exemplo n.º 3
0
        public Tx SignTransaction(UnsignedTx unsignedTx, string privateKey)
        {
            byte[] networkData  = System.Text.Encoding.UTF8.GetBytes(Configuration.Network);
            byte[] binaryTx     = Encoding.DecodeCheckWithIdentifier(unsignedTx.TX);
            byte[] txAndNetwork = networkData.Concatenate(binaryTx);
            byte[] sig          = Signing.Sign(txAndNetwork, privateKey);
            Tx     tx           = new Tx();

            tx.TX = Encoding.EncodeSignedTransaction(sig, binaryTx);
            return(tx);
        }
        /**
         * create an unsigned native namepreclaim transaction
         *
         *
         */
        public void BuildNativeNamePreclaimTransaction()
        {
            string     sender = BaseKeyPair.Generate().PublicKey;
            BigInteger salt   = Crypto.GenerateNamespaceSalt();
            ulong      nonce  = 1;
            ulong      ttl    = 100;
            NamePreclaimTransaction namePreclaimTx      = nativeClient.CreateNamePreClaimTransaction(sender, validDomain, salt, nonce, ttl);
            NamePreclaimTransaction namePreclaimTxDebug = debugClient.CreateNamePreClaimTransaction(sender, validDomain, salt, nonce, ttl);
            UnsignedTx unsignedTxNative = namePreclaimTx.CreateUnsignedTransaction();
            UnsignedTx unsignedTx       = namePreclaimTxDebug.CreateUnsignedTransaction();

            Assert.AreEqual(unsignedTxNative.TX, unsignedTx.TX);
        }
        public void PostUpdateTx()
        {
            BaseKeyPair keyPair = new BaseKeyPair(TestConstants.BENEFICIARY_PRIVATE_KEY);
            Account     account = nativeClient.GetAccount(keyPair.PublicKey);
            ulong       nonce   = account.Nonce + 1;
            BigInteger  salt    = Crypto.GenerateNamespaceSalt();
            ulong       ttl     = 0;
            string      domain  = TestConstants.DOMAIN + random.Next() + TestConstants.NAMESPACE;

            /** create a new namespace to update later */
            NamePreclaimTransaction namePreclaimTx = nativeClient.CreateNamePreClaimTransaction(keyPair.PublicKey, domain, salt, nonce, ttl);
            UnsignedTx     unsignedTx     = namePreclaimTx.CreateUnsignedTransaction();
            Tx             signedTx       = nativeClient.SignTransaction(unsignedTx, keyPair.PrivateKey);
            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTx);

            Assert.AreEqual(postTxResponse.TXHash, Encoding.ComputeTxHash(signedTx.TX));
            BigInteger           fee             = NameServiceBiding.GetDefaultBidFee(domain);
            NameClaimTransaction nameClaimTx     = nativeClient.CreateNameClaimTransaction(keyPair.PublicKey, domain, salt, fee, Constants.BaseConstants.NAME_FEE, nonce + 1, ttl);
            UnsignedTx           unsignedClaimTx = nameClaimTx.CreateUnsignedTransaction();
            Tx             signedClaimTx         = nativeClient.SignTransaction(unsignedClaimTx, keyPair.PrivateKey);
            PostTxResponse postClaimTxResponse   = nativeClient.PostTx(logger, signedClaimTx);
            NameEntry      nameEntry             = nativeClient.GetNameId(domain);
            ulong          initialTTL            = nameEntry.Ttl;

            logger.LogInformation($"Created namespace {domain} with salt {salt} and nameEntry {nameEntry} in tx {postClaimTxResponse.TXHash} for update test");
            /** finished creating namespace */
            ulong nameTtl   = 10000;
            ulong clientTtl = 50;

            account = nativeClient.GetAccount(keyPair.PublicKey);
            nonce   = account.Nonce + 1;
            NameUpdateTransaction nameUpdateTx     = nativeClient.CreateNameUpdateTransaction(keyPair.PublicKey, nameEntry.Id, nonce, ttl, clientTtl, nameTtl, new List <NamePointer>());
            UnsignedTx            unsignedUpdateTx = nameUpdateTx.CreateUnsignedTransaction();
            Tx             signedUpdateTx          = nativeClient.SignTransaction(unsignedUpdateTx, keyPair.PrivateKey);
            PostTxResponse postUpdateTxResponse    = nativeClient.PostTx(logger, signedUpdateTx);

            Assert.AreEqual(postUpdateTxResponse.TXHash, Encoding.ComputeTxHash(signedUpdateTx.TX));
            nameEntry = nativeClient.GetNameId(domain);
            logger.LogInformation($"Updated namespace {domain} with salt {salt} and nameEntry {nameEntry} in tx {postClaimTxResponse.TXHash} for update test");

            ulong updatedTTL = nameEntry.Ttl;
            // subtract 40000 because initial default ttl is 50000 and updated ttl was 10000
            ulong diffTtl = initialTTL - updatedTTL;

            Assert.IsTrue(diffTtl <= 40000);
            if (diffTtl < 40000)
            {
                logger.LogInformation($"Diff of Ttl is {diffTtl}, this happens when meanwhile new blocks are mined");
            }
        }
        public void BuildNativeSpendTransactionTest()
        {
            string     sender    = BaseKeyPair.Generate().PublicKey;
            string     recipient = BaseKeyPair.Generate().PublicKey;
            BigInteger amount    = 1000;
            string     payload   = "";
            ulong      ttl       = 100;
            ulong      nonce     = 5;

            SpendTransaction spendTx          = nativeClient.CreateSpendTransaction(sender, recipient, amount, payload, ttl, nonce);
            SpendTransaction spendTxDebug     = debugClient.CreateSpendTransaction(sender, recipient, amount, payload, ttl, nonce);
            UnsignedTx       unsignedTxNative = spendTx.CreateUnsignedTransaction();
            UnsignedTx       unsignedTx       = spendTxDebug.CreateUnsignedTransaction();

            Assert.AreEqual(unsignedTx.TX, unsignedTxNative.TX);
        }
        public void PostNameClaimTx()
        {
            BaseKeyPair             keyPair        = new BaseKeyPair(TestConstants.BENEFICIARY_PRIVATE_KEY);
            Account                 account        = nativeClient.GetAccount(keyPair.PublicKey);
            BigInteger              salt           = Crypto.GenerateNamespaceSalt();
            ulong                   nonce          = account.Nonce + 1;
            ulong                   ttl            = 0;
            NamePreclaimTransaction namePreclaimTx = nativeClient.CreateNamePreClaimTransaction(keyPair.PublicKey, validDomain, salt, nonce, ttl);
            UnsignedTx              unsignedTx     = namePreclaimTx.CreateUnsignedTransaction();


            NamePreclaimTransaction namePreclaimTxDebug = debugClient.CreateNamePreClaimTransaction(keyPair.PublicKey, validDomain, salt, nonce, ttl);
            UnsignedTx unsignedTxDebug = namePreclaimTxDebug.CreateUnsignedTransaction();

            Assert.AreEqual(unsignedTxDebug.TX, unsignedTx.TX);


            Tx signedTx = nativeClient.SignTransaction(unsignedTx, keyPair.PrivateKey);


            logger.LogInformation("Signed NamePreclaimTx: " + signedTx.TX);
            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTx);

            logger.LogInformation("NamePreclaimTx hash: " + postTxResponse.TXHash);
            Assert.AreEqual(postTxResponse.TXHash, Encoding.ComputeTxHash(signedTx.TX));

            BigInteger           fee             = NameServiceBiding.GetDefaultBidFee(validDomain);
            NameClaimTransaction nameClaimTx     = nativeClient.CreateNameClaimTransaction(keyPair.PublicKey, validDomain, salt, fee, Constants.BaseConstants.NAME_FEE, nonce + 1, ttl);
            UnsignedTx           unsignedClaimTx = nameClaimTx.CreateUnsignedTransaction();

            NameClaimTransaction nameClaimTxDebug     = debugClient.CreateNameClaimTransaction(keyPair.PublicKey, validDomain, salt, fee, Constants.BaseConstants.NAME_FEE, nonce + 1, ttl);
            UnsignedTx           unsignedClaimTxDebug = nameClaimTxDebug.CreateUnsignedTransaction();


            Assert.AreEqual(unsignedClaimTxDebug.TX, unsignedClaimTx.TX);


            Tx signedClaimTx = nativeClient.SignTransaction(unsignedClaimTx, keyPair.PrivateKey);

            logger.LogInformation("Signed NameClaimTx: " + signedClaimTx.TX);
            postTxResponse = nativeClient.PostTx(logger, signedClaimTx);
            logger.LogInformation($"Using namespace {validDomain} and salt {salt} for committmentId {Encoding.GenerateCommitmentHash(validDomain, salt)}");
            logger.LogInformation("NameClaimTx hash: " + postTxResponse.TXHash);
        }
        /** @param context */

        public void PostRevokeTx()
        {
            BaseKeyPair           keyPair      = new BaseKeyPair(TestConstants.BENEFICIARY_PRIVATE_KEY);
            string                nameId       = nativeClient.GetNameId(validDomain).Id;
            Account               account      = nativeClient.GetAccount(keyPair.PublicKey);
            ulong                 nonce        = account.Nonce + 1;
            ulong                 ttl          = 0;
            NameRevokeTransaction nameRevokeTx = nativeClient.CreateNameRevokeTransaction(keyPair.PublicKey, nameId, nonce, ttl);
            UnsignedTx            unsignedTx   = nameRevokeTx.CreateUnsignedTransaction();
            Tx signedTx = nativeClient.SignTransaction(unsignedTx, keyPair.PrivateKey);

            logger.LogInformation("Signed NameRevokeTx: " + signedTx.TX);
            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTx);

            logger.LogInformation("NameRevokeTx hash: " + postTxResponse.TXHash);
            Assert.AreEqual(postTxResponse.TXHash, Encoding.ComputeTxHash(signedTx.TX));
            Assert.ThrowsException <ApiException <Error> >(() => nativeClient.GetNameIdAsync(validDomain).TimeoutAsync(TestConstants.NUM_TRIALS_DEFAULT).RunAndUnwrap(), "Not Found");
            logger.LogInformation("Validated, that namespace {validDomain} is revoked");
        }
        public void PostSpendTxTest()
        {
            // get the currents accounts nonce in case a transaction is already
            // created and increase it by one
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);

            BaseKeyPair kp = BaseKeyPair.Generate();

            string           recipient        = kp.PublicKey;
            BigInteger       amount           = 1000000000000000000;
            string           payload          = "";
            ulong            ttl              = 0;
            ulong            nonce            = account.Nonce + 1;
            SpendTransaction spendTx          = nativeClient.CreateSpendTransaction(baseKeyPair.PublicKey, recipient, amount, payload, ttl, nonce);
            UnsignedTx       unsignedTxNative = spendTx.CreateUnsignedTransaction();
            Tx             signedTx           = nativeClient.SignTransaction(unsignedTxNative, baseKeyPair.PrivateKey);
            PostTxResponse txResponse         = nativeClient.PostTransaction(signedTx);

            logger.LogInformation("SpendTx hash: " + txResponse.TXHash);
            Assert.AreEqual(txResponse.TXHash, Encoding.ComputeTxHash(signedTx.TX));
        }
Exemplo n.º 10
0
        public void CallContractAfterDryRunOnLocalNode()
        {
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            ulong   nonce   = account.Nonce + 1;

            // Compile the call contract
            Calldata calldata = nativeClient.EncodeCalldata(logger, TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, TestConstants.TestContractFunctionParams);
            List <Dictionary <AccountParameter, object> > dict = new List <Dictionary <AccountParameter, object> >();

            dict.Add(new Dictionary <AccountParameter, object> {
                { AccountParameter.PUBLIC_KEY, baseKeyPair.PublicKey }
            });
            DryRunResults results = nativeClient.PerformDryRunTransactions(logger, dict, null, new List <UnsignedTx> {
                CreateUnsignedContractCallTx(nonce, calldata.CallData, null)
            });

            logger.LogInformation("callContractAfterDryRunOnLocalNode: " + JsonConvert.SerializeObject(results));
            foreach (DryRunResult result in results.Results)
            {
                Assert.AreEqual("ok", result.Result);
                var contractAfterDryRunTx = nativeClient.CreateContractCallTransaction(Constants.BaseConstants.ABI_VERSION, calldata.CallData, localDeployedContractId, result.CallObj.GasUsed, result.CallObj.GasPrice, nonce, baseKeyPair.PublicKey, 0);

                UnsignedTx unsignedTxNative = contractAfterDryRunTx.CreateUnsignedTransaction();
                Tx         signedTxNative   = nativeClient.SignTransaction(unsignedTxNative, baseKeyPair.PrivateKey);

                // post the signed contract call tx
                PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);
                Assert.AreEqual(postTxResponse.TXHash, Encoding.ComputeTxHash(signedTxNative.TX));
                logger.LogInformation("CreateContractTx hash: " + postTxResponse.TXHash);

                // get the tx info object to resolve the result
                TxInfoObject txInfoObject = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);

                // decode the result to json
                JToken json = nativeClient.DecodeCallResult(TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, txInfoObject.CallInfo.ReturnType, txInfoObject.CallInfo.ReturnValue, CompileOptsBackend.Fate);


                Assert.AreEqual(TestConstants.TestContractFunctionParam, json.Value <string>());
            }
        }
        public void DeployPaymentSplitter()
        {
            ByteCode byteCode = nativeClient.Compile(paymentSplitterSource, null, null);
            Calldata calldata = nativeClient.EncodeCalldata(logger, paymentSplitterSource, "init", new List <string> {
                GenerateMapParam(initialWeights)
            });

            logger.LogInformation("contract bytecode: " + byteCode.Bytecode);
            logger.LogInformation("contract calldata: " + calldata.CallData);
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            string  ownerId = baseKeyPair.PublicKey;

            ushort     abiVersion = Constants.BaseConstants.ABI_VERSION;
            ushort     vmVersion  = Constants.BaseConstants.VM_VERSION;
            BigInteger amount     = 0;
            BigInteger deposit    = 0;
            ulong      ttl        = 0;
            ulong      gas        = 4800000;
            BigInteger gasPrice   = Constants.BaseConstants.MINIMAL_GAS_PRICE;
            ulong      nonce      = account.Nonce + 1;

            ContractCreateTransaction contractTx = nativeClient.CreateContractCreateTransaction(abiVersion, amount, calldata.CallData, byteCode.Bytecode, deposit, gas, gasPrice, nonce, ownerId, ttl, vmVersion);

            UnsignedTx unsignedTx = contractTx.CreateUnsignedTransaction();

            logger.LogInformation("Unsigned Tx - hash - dryRun: " + unsignedTx.TX);
            Tx signedTxNative = nativeClient.SignTransaction(unsignedTx, baseKeyPair.PrivateKey);

            logger.LogInformation("CreateContractTx hash (native signed): " + signedTxNative);

            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);
            TxInfoObject   txInfoObject   = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);

            localDeployedContractId = txInfoObject.CallInfo.ContractId;
            logger.LogInformation("Deployed contract - hash " + postTxResponse.TXHash + " - " + txInfoObject);
            if ("revert".Equals(txInfoObject.CallInfo.ReturnType))
            {
                Assert.Fail("transaction reverted: " + nativeClient.DecodeCalldata(logger, txInfoObject.CallInfo.ReturnValue, "string"));
            }
        }
Exemplo n.º 12
0
        public void BuildCreateContractTransactionTest()
        {
            string     ownerId    = baseKeyPair.PublicKey;
            ushort     abiVersion = Constants.BaseConstants.ABI_VERSION;
            ushort     vmVersion  = 4;
            BigInteger amount     = 100;
            BigInteger deposit    = 100;
            ulong      ttl        = 20000;
            ulong      gas        = 1000;
            BigInteger gasPrice   = 1100000000;

            ulong nonce = 1;
            ContractCreateTransaction contractTx      = nativeClient.CreateContractCreateTransaction(abiVersion, amount, TestConstants.TestContractCallData, TestConstants.TestContractByteCode, deposit, gas, gasPrice, nonce, ownerId, ttl, vmVersion);
            ContractCreateTransaction contractTxDebug = debugClient.CreateContractCreateTransaction(abiVersion, amount, TestConstants.TestContractCallData, TestConstants.TestContractByteCode, deposit, gas, gasPrice, nonce, ownerId, ttl, vmVersion);

            contractTx.Fee = contractTxDebug.Fee = 1098660000000000;

            UnsignedTx unsignedTxNative = contractTx.CreateUnsignedTransaction();
            UnsignedTx unsignedTxDebug  = contractTxDebug.CreateUnsignedTransaction();

            Assert.AreEqual(unsignedTxNative.TX, unsignedTxDebug.TX);
        }
Exemplo n.º 13
0
        public void BuildCallContractTransactionTest()
        {
            Account    account                      = nativeClient.GetAccount(baseKeyPair.PublicKey);
            string     callerId                     = baseKeyPair.PublicKey;
            ushort     abiVersion                   = Constants.BaseConstants.ABI_VERSION;
            ulong      ttl                          = 20000;
            ulong      gas                          = 1000;
            BigInteger gasPrice                     = 1000000000;
            ulong      nonce                        = account.Nonce + 1;
            string     callContractCalldata         = TestConstants.EncodedServiceCall;
            ContractCallTransaction contractTx      = nativeClient.CreateContractCallTransaction(abiVersion, callContractCalldata, localDeployedContractId, gas, gasPrice, nonce, callerId, ttl);
            ContractCallTransaction contractTxDebug = debugClient.CreateContractCallTransaction(abiVersion, callContractCalldata, localDeployedContractId, gas, gasPrice, nonce, callerId, ttl);

            contractTx.Fee = contractTxDebug.Fee = 1454500000000000;

            UnsignedTx unsignedTxNative = contractTx.CreateUnsignedTransaction();

            logger.LogInformation("CreateContractTx hash (native unsigned): " + unsignedTxNative.TX);

            UnsignedTx unsignedTxDebug = contractTxDebug.CreateUnsignedTransaction();

            logger.LogInformation("CreateContractTx hash (debug unsigned): " + unsignedTxDebug.TX);
            Assert.AreEqual(unsignedTxNative.TX, unsignedTxDebug.TX);
        }
        public void CallPayAndSplitMethod()
        {
            Account    account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            BigInteger balanceRecipient1;
            BigInteger balanceRecipient2;
            BigInteger balanceRecipient3;

            try
            {
                balanceRecipient1 = nativeClient.GetAccount(initialReceiver1.PublicKey).Balance;
                balanceRecipient2 = nativeClient.GetAccount(initialReceiver2.PublicKey).Balance;
                balanceRecipient3 = nativeClient.GetAccount(initialReceiver3.PublicKey).Balance;
                // if one of the accounts wasn't active we get an error and know that the
                // accounts
                // don't have any balance
            }
            catch (Exception)
            {
                balanceRecipient1 = 0;
                balanceRecipient2 = 0;
                balanceRecipient3 = 0;
            }

            ulong    nonce        = account.Nonce + 1;
            decimal  paymentValue = "1".ToAettos(Unit.AE);
            Calldata calldata     = nativeClient.EncodeCalldata(logger, paymentSplitterSource, "payAndSplit", null);

            logger.LogInformation("Contract ID: " + localDeployedContractId);
            List <Dictionary <AccountParameter, object> > accounts = new List <Dictionary <AccountParameter, object> >();
            Dictionary <AccountParameter, object>         ac       = new Dictionary <AccountParameter, object>()
            {
                { AccountParameter.PUBLIC_KEY, baseKeyPair.PublicKey }
            };

            accounts.Add(ac);
            DryRunResults dryRunResults = nativeClient.PerformDryRunTransactions(logger, accounts, null, new List <UnsignedTx>()
            {
                nativeClient.CreateUnsignedContractCallTx(logger, baseKeyPair.PublicKey, nonce, calldata.CallData, null, localDeployedContractId, new BigInteger(paymentValue))
            });

            logger.LogInformation("callContractAfterDryRunOnLocalNode: " + JsonConvert.SerializeObject(dryRunResults));
            Assert.AreEqual(1, dryRunResults.Results.Count);
            DryRunResult dryRunResult = dryRunResults.Results.First();

            Assert.AreEqual("ok", dryRunResult.Result);
            ContractCallTransaction contractAfterDryRunTx = nativeClient.CreateContractCallTransaction(Constants.BaseConstants.ABI_VERSION, calldata.CallData, localDeployedContractId, dryRunResult.CallObj.GasUsed, dryRunResult.CallObj.GasPrice, nonce, baseKeyPair.PublicKey, 0);

            contractAfterDryRunTx.Model.Amount = new BigInteger(paymentValue);

            UnsignedTx unsignedTxNative = contractAfterDryRunTx.CreateUnsignedTransaction();
            Tx         signedTxNative   = nativeClient.SignTransaction(unsignedTxNative, baseKeyPair.PrivateKey);

            // post the signed contract call tx
            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);

            Assert.AreEqual(postTxResponse.TXHash, Utils.Encoding.ComputeTxHash(signedTxNative.TX));
            logger.LogInformation("CreateContractTx hash: " + postTxResponse.TXHash);

            // we wait until the tx is available and the payment should have been splitted
            TxInfoObject txInfoObject = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);

            logger.LogInformation("PayAndSplit transaction - hash " + postTxResponse.TXHash + " - " + txInfoObject);
            if ("revert".Equals(txInfoObject.CallInfo.ReturnType))
            {
                Assert.Fail("transaction reverted: " + nativeClient.DecodeCalldata(logger, txInfoObject.CallInfo.ReturnValue, "string"));
            }

            Assert.AreEqual(balanceRecipient1 + new BigInteger(paymentValue * 0.4m), nativeClient.GetAccount(initialReceiver1.PublicKey).Balance);
            Assert.AreEqual(balanceRecipient2 + new BigInteger(paymentValue * 0.4m), nativeClient.GetAccount(initialReceiver2.PublicKey).Balance);
            Assert.AreEqual(balanceRecipient3 + new BigInteger(paymentValue * 0.2m), nativeClient.GetAccount(initialReceiver3.PublicKey).Balance);
        }