Esempio n. 1
0
        ///<summary>Private: Generate changeTrust transaction with given parameters.</summary>
        ///<param name="account">Stellar account</param>
        ///<param name="asset">Stellar asset</param>
        ///<returns>Returns transaction.</returns>
        private Stellar.Transaction Tx(Stellar.Account account, Stellar.Asset asset)
        {
            string assetXdr    = asset.ToXdr().ToString();
            string opXdrAmount = Stellar.Operation.ToXdrAmount(assetXdr).ToString();

            var operation = new Stellar.ChangeTrustOperation.Builder(asset, opXdrAmount).Build();

            return(new Stellar.Transaction.Builder(account).AddOperation(operation).Build());
        }
        ///<summary>Add a cosigner to a given account.</summary>
        ///<param name="keypair">Account keypair</param>
        ///<param name="cosignerKeypair">Cosigner keypair</param>
        ///<param name="weight">Cosigner weight = 1 (default)</param>
        ///<returns>Returns submitted transaction response</returns>
        public StellarResponses.SubmitTransactionResponse Call(
            Stellar.KeyPair keypair,
            Stellar.xdr.SignerKey cosignerKeypair,
            int weight = 1
            )
        {
            var client  = new Client().HorizonClient;
            var account = new Stellar.Account(keypair, null);  // null sequnece number for now
            var tx      = this.Tx(account, cosignerKeypair, weight);

            tx.Sign(account.KeyPair);

            return(client.SubmitTransaction(tx).Result);
        }
Esempio n. 3
0
        async private Task <Stellar.Transaction> GenerateSignedTransaction(Stellar.KeyPair keypair)
        {
            long randomSequence = (long)(99999999 - Math.Floor((decimal) new Random().Next() * 65536));

            Stellar.Account             account   = new Stellar.Account(keypair, randomSequence);
            Stellar.Transaction.Builder txBuilder = new Stellar.Transaction.Builder(account);

            StellarResponses.AssetResponse asset = await new Library.Client().StellarAsset();

            Stellar.Operation   op = new Stellar.PaymentOperation.Builder(keypair, asset.Asset, "0.000001").Build();
            Stellar.Transaction tx = txBuilder.AddOperation(op).Build();

            tx.Sign(keypair);

            return(Stellar.Transaction.FromEnvelopeXdr(tx.ToEnvelopeXdr()));
        }
Esempio n. 4
0
        ///<summary>Submit transaction</summary>
        ///<param name="buildFn">Callback to build the transaction</param>
        ///<returns>Promise that resolves or rejects with response of horizon</returns>
        async public Task <StellarResponses.SubmitTransactionResponse> SubmitTx(Action <Stellar.Transaction.Builder> buildFn)
        {
            var account = new Stellar.Account(UserAccount().KeyPair(), UserAccount().Info().SequenceNumber);

            var builder = new Stellar.Transaction.Builder(account);

            buildFn(builder);

            var tx = builder.Build();

            tx.Sign(AppKeypair());

            var response = await this.ClientInstance.SubmitTransaction(tx);

            await this.Reload();

            return(response);
        }
        ///<summary>Private: Generate setOptions transaction with given parameters.</summary>
        ///<param name="account">Stellar account</param>
        ///<param name="cosignerKeypair">Cosigner keypair</param>
        ///<param name="weight">Cosigner weight</param>
        ///<returns>Returns new Stellar Transaction.</returns>
        private Stellar.Transaction Tx(
            Stellar.Account account,
            Stellar.xdr.SignerKey cosignerKeypair,
            int weight
            )
        {
            var operation =
                new Stellar.SetOptionsOperation.Builder()
                .SetHighThreshold(10)
                .SetLowThreshold(1)
                .SetMasterKeyWeight(10)
                .SetMediumThreshold(1)
                .SetSigner(cosignerKeypair, weight)
                .Build();

            return(new Stellar.Transaction.Builder(account)
                   .AddOperation(operation)
                   .Build());
        }
Esempio n. 6
0
        ///<summary>Generates challenge transaction signed by developers private key.</summary>
        ///<param name="developerSecret">Developers secret seed</param>
        ///<param name="expireIn">Session expiration time in seconds from now. Default is Client.ChallengeExpiresIn.</param>
        ///<returns>Returns base64-encoded transaction envelope</returns>
        public string Call(string developerSecret, int expireIn = 0)
        {
            if (expireIn == 0)
            {
                expireIn = Client.ChallengeExpiresIn;
            }

            var keypair   = this.Keypair(developerSecret);
            var account   = new Stellar.Account(keypair, this.RandomSequence());
            var operation =
                new Stellar.PaymentOperation.Builder(keypair, new Stellar.AssetTypeNative(), "0.000001")
                .SetSourceAccount(Stellar.KeyPair.Random())
                .Build();

            var tx = new Stellar.Transaction.Builder(account)
                     .AddMemo(this.Memo())
                     .AddTimeBounds(this.BuildTimeBounds(expireIn))
                     .AddOperation(operation)
                     .Build();

            tx.Sign(keypair);

            return(tx.ToEnvelopeXdrBase64());
        }
        public async Task TxCommitQuantumTest(int cursor, int amount, int asset, Type excpectedException)
        {
            context.AppState.State = ApplicationState.Ready;

            long apex = context.QuantumStorage.CurrentApex;

            var client1StartBalanceAmount = (long)0;

            var account1 = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair).Account;

            var clientAccountBalance = account1.GetBalance(asset);

            var withdrawalDest = KeyPair.Random();
            var txHash         = new byte[] { };

            if (clientAccountBalance != null && amount > 0)
            {
                client1StartBalanceAmount = clientAccountBalance.Amount;


                context.Constellation.TryFindAssetSettings(asset, out var assetSettings);

                var account   = new stellar_dotnet_sdk.Account(TestEnvironment.Client1KeyPair.AccountId, 1);
                var txBuilder = new TransactionBuilder(account);
                txBuilder.SetFee(10_000);
                txBuilder.AddTimeBounds(new stellar_dotnet_sdk.TimeBounds(DateTimeOffset.UtcNow, new TimeSpan(0, 5, 0)));
                txBuilder.AddOperation(
                    new PaymentOperation.Builder(withdrawalDest, assetSettings.ToAsset(), Amount.FromXdr(amount).ToString())
                    .SetSourceAccount((KeyPair)context.Constellation.Vault)
                    .Build()
                    );
                var tx = txBuilder.Build();
                txHash = tx.Hash();

                var txV1     = tx.ToXdrV1();
                var txStream = new XdrDataOutputStream();
                stellar_dotnet_sdk.xdr.Transaction.Encode(txStream, txV1);

                var accountWrapper = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair);

                var withdrawal = new WithdrawalRequest
                {
                    Account        = accountWrapper.Account.Id,
                    TransactionXdr = txStream.ToArray(),
                    RequestId      = DateTime.UtcNow.Ticks,
                    AccountWrapper = accountWrapper
                };

                MessageEnvelope quantum = withdrawal.CreateEnvelope();
                quantum.Sign(TestEnvironment.Client1KeyPair);
                if (!context.IsAlpha)
                {
                    quantum = new RequestQuantum {
                        Apex = ++apex, RequestEnvelope = quantum, Timestamp = DateTime.UtcNow.Ticks
                    }.CreateEnvelope();
                    quantum.Sign(TestEnvironment.AlphaKeyPair);
                }
                //create withdrawal
                await context.QuantumHandler.HandleAsync(quantum);
            }

            var depositAmount = new Random().Next(10, 1000);

            var ledgerNotification = new TxNotification
            {
                TxCursor = (uint)cursor,
                Payments = new List <PaymentBase>
                {
                    new Deposit
                    {
                        Amount      = depositAmount,
                        Destination = TestEnvironment.Client1KeyPair,
                        Asset       = asset
                    },
                    new Withdrawal
                    {
                        TransactionHash = txHash,
                        PaymentResult   = PaymentResults.Success
                    }
                }
            };
            var ledgerNotificationEnvelope = ledgerNotification.CreateEnvelope();

            ledgerNotificationEnvelope.Sign(TestEnvironment.Auditor1KeyPair);

            var ledgerCommitEnv = new TxCommitQuantum
            {
                Source = ledgerNotificationEnvelope,
                Apex   = ++apex
            }.CreateEnvelope();

            if (!context.IsAlpha)
            {
                var msg = ((TxCommitQuantum)ledgerCommitEnv.Message);
                msg.Timestamp   = DateTime.UtcNow.Ticks;
                ledgerCommitEnv = msg.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);
            }

            await AssertQuantumHandling(ledgerCommitEnv, excpectedException);

            if (excpectedException == null)
            {
                Assert.AreEqual(context.TxCursorManager.TxCursor, ledgerNotification.TxCursor);

                Assert.AreEqual(account1.GetBalance(asset).Liabilities, 0);
                Assert.AreEqual(account1.GetBalance(asset).Amount, client1StartBalanceAmount - amount + depositAmount); //acc balance + deposit - withdrawal
            }
        }