static KeyPair CreateRandomAccount(Account source, long nativeAmount) { var dest = KeyPair.Random(); var operation = new CreateAccountOperation.Builder(dest, nativeAmount) .SetSourceAccount(source.KeyPair) .Build(); source.IncrementSequenceNumber(); Stellar.Transaction transaction = new Stellar.Transaction.Builder(source) .AddOperation(operation) .Build(); transaction.Sign(source.KeyPair); var tx = transaction.ToEnvelopeXdrBase64(); var response = PostResult(tx); Console.WriteLine("response:" + response.ReasonPhrase); Console.WriteLine(dest.Address); Console.WriteLine(dest.Seed); return(dest); }
public void TestCreateAccountOperation() { // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF var source = KeyPair.FromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK"); // GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR var destination = KeyPair.FromSecretSeed("SDHZGHURAYXKU2KMVHPOXI6JG2Q4BSQUQCEOY72O3QQTCLR2T455PMII"); const string startingAmount = "1000"; var operation = new CreateAccountOperation.Builder(destination, startingAmount) .SetSourceAccount(source) .Build(); var xdr = operation.ToXdr(); var parsedOperation = (CreateAccountOperation)Operation.FromXdr(xdr); Assert.AreEqual(10000000000L, xdr.Body.CreateAccountOp.StartingBalance.InnerValue); Assert.AreEqual(source.AccountId, parsedOperation.SourceAccount.AccountId); Assert.AreEqual(destination.AccountId, parsedOperation.Destination.AccountId); Assert.AreEqual(startingAmount, parsedOperation.StartingBalance); Assert.AreEqual(OperationThreshold.Medium, parsedOperation.Threshold); Assert.AreEqual( "AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAAAAAAA7eBSYbzcL5UKo7oXO24y1ckX+XuCtkDsyNHOp1n1bxAAAAACVAvkAA==", operation.ToXdrBase64()); }
public void CreateAccountOperation() { // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF KeyPair source = KeyPair.FromSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK"); // GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR KeyPair destination = KeyPair.FromSeed("SDHZGHURAYXKU2KMVHPOXI6JG2Q4BSQUQCEOY72O3QQTCLR2T455PMII"); var balance = 1000; CreateAccountOperation operation = new CreateAccountOperation.Builder(destination, balance) .SetSourceAccount(source) .Build(); Assert.AreEqual(source.Address, operation.SourceAccount.Address); Assert.AreEqual(destination.Address, operation.Destination.Address); Assert.AreEqual(balance, operation.StartingBalance); Assert.AreEqual("AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAAAAAAA7eBSYbzcL5UKo7oXO24y1ckX+XuCtkDsyNHOp1n1bxAAAAAAAAAD6A==", operation.ToXdrBase64()); Stellar.Generated.Operation xdr = operation.ToXDR(); CreateAccountOperation parsedOperation = Stellar.CreateAccountOperation.FromXDR(xdr); Assert.AreEqual(source.Address, parsedOperation.SourceAccount.Address); Assert.AreEqual(destination.Address, parsedOperation.Destination.Address); Assert.AreEqual(1000, parsedOperation.StartingBalance); }
static KeyPair CreateRandomAccount(Account source, long nativeAmount) { var master = KeyPair.Master(); var dest = KeyPair.Random(); var operation = new CreateAccountOperation.Builder(dest, nativeAmount) .SetSourceAccount(source.KeyPair) .Build(); source.IncrementSequenceNumber(); Stellar.Transaction transaction = new Stellar.Transaction.Builder(source) .AddOperation(operation) .Build(); transaction.Sign(source.KeyPair); string message = transaction.ToEnvelopeXdrBase64(); string response = GetResult(message); Console.WriteLine(response); Console.WriteLine(dest.AccountId); Console.WriteLine(dest.Seed); return(dest); }
//创建随机账户 static KeyPair CreateRandomAccount(Account source, long nativeAmount) { var dest = KeyPair.Random(); var operation = new CreateAccountOperation.Builder(dest, nativeAmount) .SetSourceAccount(source.KeyPair) .Build(); source.IncrementSequenceNumber(); Transaction transaction = new Transaction.Builder(source) .AddOperation(operation) .Build(); transaction.Sign(source.KeyPair); var tx = transaction.ToEnvelopeXdrBase64(); var response = PostResult(tx); Console.WriteLine("response:" + response.ReasonPhrase); Console.WriteLine(dest.Address); Console.WriteLine(dest.Seed); Random rd = new Random(); System.IO.StreamWriter sw = new System.IO.StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "\\syslog" + DateTime.Now.ToString("yyyy-MM-dd") + "-" + rd.Next(99999) + ".txt"); sw.WriteLine("公钥地址:" + dest.Address); sw.WriteLine("私钥密码:" + dest.Seed); sw.Close(); return(dest); }
public ITransactionBuilder AddCreateAccount(string destinationAddress, double startingBalance) { KeyPair keyPair = KeyPair.FromAccountId(destinationAddress); CreateAccountOperation.Builder builder = new CreateAccountOperation.Builder(keyPair, startingBalance.ToString(CultureInfo.InvariantCulture)); builder.SetSourceAccount(_baseKeyPair); _operations.Add(builder.Build()); return(this); }
public async Task <bool> ActivateUserAccount(string accountId, string secretSeed) { //Set network and server Network.UseTestNetwork(); Server server = new Server("https://horizon-testnet.stellar.org"); var sourceKeyPair = KeyPair.FromSecretSeed(DistributionAccount.SecretSeed); //Destination keypair from the account id KeyPair destinationKeyPair = KeyPair.FromAccountId(accountId); AccountResponse sourceAccountResponse = await server.Accounts.Account(DistributionAccount.PublicKey); //Create source account object Account sourceAccount = new Account(DistributionAccount.PublicKey, sourceAccountResponse.SequenceNumber); Asset asset = new AssetTypeNative(); var operationStartSponsor = new BeginSponsoringFutureReservesOperation.Builder(destinationKeyPair) .SetSourceAccount(sourceKeyPair) .Build(); //Create payment operation CreateAccountOperation operation = new CreateAccountOperation.Builder(destinationKeyPair, "0").SetSourceAccount(sourceAccount.KeyPair).Build(); var sponsoredSource = KeyPair.FromSecretSeed(secretSeed); var operationEndSponsoring = new EndSponsoringFutureReservesOperation.Builder() .SetSourceAccount(sponsoredSource) .Build(); Transaction transaction = new TransactionBuilder(sourceAccount) .SetFee(100) .AddOperation(operationStartSponsor) .AddOperation(operation) .AddOperation(operationEndSponsoring) .Build(); //Sign Transaction transaction.Sign(sourceKeyPair); transaction.Sign(sponsoredSource); try { var response = await server.SubmitTransaction(transaction); return(response.IsSuccess()); } catch (Exception exception) { return(false); } }
public Stellar.Transaction SampleTransaction(string destAccountId) { Stellar.Network.CurrentNetwork = ""; var master = KeyPair.Master(); var dest = string.IsNullOrEmpty(destAccountId) ? KeyPair.Random() : KeyPair.FromAccountId(destAccountId); var sourceAccount = new Account(master, 1); CreateAccountOperation operation = new CreateAccountOperation.Builder(dest, 1000) //.SetSourceAccount(master) .Build(); Stellar.Transaction transaction = new Stellar.Transaction.Builder(sourceAccount) .AddOperation(operation) .Build(); return(transaction); }
private async Task <SubmitTransactionResponse> GetCreateAccountTransaction(string destinationAddress) { KeyPair destinationKeyPair = KeyPair.FromAccountId(destinationAddress); AccountResponse sourceAccount = null; AccountResponse destinationAccount = null; try { sourceAccount = await GetAccount(_keyPair).ConfigureAwait(false); destinationAccount = await GetAccount(destinationKeyPair).ConfigureAwait(false); } catch (Exception ex) {} if (sourceAccount == null) { throw new Exception("Source account doesn't exists"); } if (destinationAccount != null) { throw new Exception("Account already exists"); } CreateAccountOperation.Builder createAccountOperationBuilder = new CreateAccountOperation.Builder(destinationKeyPair, "0"); createAccountOperationBuilder.SetSourceAccount(_keyPair); Transaction.Builder transactionBuilder = new Transaction.Builder(new Account(_keyPair, sourceAccount.SequenceNumber)); transactionBuilder.AddOperation(createAccountOperationBuilder.Build()); transactionBuilder.AddMemo(new MemoText($"1-{_app_id}")); Transaction transaction = transactionBuilder.Build(); transaction.Sign(_keyPair); return(await _server.SubmitTransaction(transaction).ConfigureAwait(false)); }
public async Task CreateAccount(KeyPair destAccount) { var sourceKeypair = KeyPair.FromSecretSeed(AccountSeed); var sourceAccountResp = await server.Accounts.Account(sourceKeypair); var sourceAccount = new Account(sourceKeypair, sourceAccountResp.SequenceNumber); var operation = new CreateAccountOperation.Builder(destAccount, "20").SetSourceAccount(sourceKeypair).Build(); var transaction = new Transaction.Builder(sourceAccount).AddOperation(operation).AddMemo(Memo.Text("Hello Memo")).Build(); transaction.Sign(sourceKeypair); try { var resp = await server.SubmitTransaction(transaction); if (resp.IsSuccess()) { Console.WriteLine("transaction completed successfully!"); await GetAccountBalance(destAccount); } else { Console.WriteLine("transaction failed."); var c = resp.SubmitTransactionResponseExtras.ExtrasResultCodes; Console.WriteLine(c.TransactionResultCode); foreach (var x in c.OperationsResultCodes) { Console.WriteLine(x); } } } catch (Exception e) { Console.WriteLine(e.Message); } }
public async Task <string> BuildTransactionAsync(Guid operationId, AddressBalance from, string toAddress, string memoText, long amount) { var fromKeyPair = KeyPair.FromAccountId(from.Address); var fromAccount = new Account(fromKeyPair, from.Sequence); var toKeyPair = KeyPair.FromAccountId(toAddress); var transferableBalance = from.Balance - from.MinBalance; Operation operation; if (await _horizonService.AccountExists(toAddress)) { if (amount <= transferableBalance) { var asset = new AssetTypeNative(); operation = new PaymentOperation.Builder(toKeyPair, asset, Operation.FromXdrAmount(amount)) .SetSourceAccount(fromKeyPair) .Build(); } else if (!_balanceService.IsDepositBaseAddress(from.Address)) { operation = new AccountMergeOperation.Builder(toKeyPair) .SetSourceAccount(fromKeyPair) .Build(); } else { throw new BusinessException($"It isn't allowed to merge the entire balance from the deposit base into another account! Transfer less funds. transferable={transferableBalance}"); } } else { if (amount <= transferableBalance) { operation = new CreateAccountOperation.Builder(toKeyPair, Operation.FromXdrAmount(amount)) .SetSourceAccount(fromKeyPair) .Build(); } else { throw new BusinessException($"It isn't possible to merge the entire balance into an unused account! Use a destination in existance. transferable={transferableBalance}"); } } var builder = new TransactionBuilder(fromAccount) .AddOperation(operation) .SetFee(_appSettings.StellarApiService.OperationFee); if (!string.IsNullOrWhiteSpace(memoText)) { var memo = new MemoText(memoText); builder = builder.AddMemo(memo); } var tx = builder.Build(); var xdr = tx.ToUnsignedEnvelopeXdr(TransactionBase.TransactionXdrVersion.V1); var expirationDate = (DateTime.UtcNow + _transactionExpirationTime); var maxUnixTimeDouble = expirationDate.ToUnixTime() / 1000;//ms to seconds var maxTimeUnix = (ulong)maxUnixTimeDouble; xdr.V1.Tx.TimeBounds = new TimeBounds() { MaxTime = new TimePoint(new Uint64(maxTimeUnix)), MinTime = new TimePoint(new Uint64(0)), }; var writer = new XdrDataOutputStream(); stellar_dotnet_sdk.xdr.TransactionEnvelope.Encode(writer, xdr); var xdrBase64 = Convert.ToBase64String(writer.ToArray()); _log.Info("Transaction has been built", new { OperationId = operationId, From = from, To = toAddress, Memo = memoText, Amount = amount, Fee = tx.Fee, Sequence = tx.SequenceNumber, MaxTimeUnix = maxTimeUnix }); var build = new TxBuild { OperationId = operationId, XdrBase64 = xdrBase64 }; await _buildRepository.AddAsync(build); return(xdrBase64); }
public async Task <string> BuildTransactionAsync(Guid operationId, AddressBalance from, string toAddress, long amount) { var fromKeyPair = KeyPair.FromAddress(from.Address); var fromAccount = new Account(fromKeyPair, from.Sequence); var toKeyPair = KeyPair.FromAddress(toAddress); var transferableBalance = from.Balance - from.MinBalance; StellarBase.Operation operation; if (await _horizonService.AccountExists(toAddress)) { if (amount <= transferableBalance) { var asset = new StellarBase.Asset(); operation = new PaymentOperation.Builder(toKeyPair, asset, amount) .SetSourceAccount(fromKeyPair) .Build(); } else { operation = new AccountMergeOperation.Builder(toKeyPair) .SetSourceAccount(fromKeyPair) .Build(); } } else { if (amount <= transferableBalance) { operation = new CreateAccountOperation.Builder(toKeyPair, amount) .SetSourceAccount(fromKeyPair) .Build(); } else { throw new BusinessException($"Currently not possible to transfer entire balance to an unused account! Use a destination in existance. transferable={transferableBalance}"); } } fromAccount.IncrementSequenceNumber(); var tx = new StellarBase.Transaction.Builder(fromAccount) .AddOperation(operation) .Build(); var xdr = tx.ToXDR(); var writer = new ByteWriter(); StellarBase.Generated.Transaction.Encode(writer, xdr); var xdrBase64 = Convert.ToBase64String(writer.ToArray()); var build = new TxBuild { OperationId = operationId, XdrBase64 = xdrBase64 }; await _buildRepository.AddAsync(build); return(xdrBase64); }