public void TestSetOptionsOperationSignerSha256() { // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF var source = KeyPair.FromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK"); var preimage = Encoding.UTF8.GetBytes("stellar.org"); var hash = Util.Hash(preimage); var operation = new SetOptionsOperation.Builder() .SetSigner(Signer.Sha256Hash(hash), 10) .SetSourceAccount(source) .Build(); var xdr = operation.ToXdr(); var parsedOperation = (SetOptionsOperation)Operation.FromXdr(xdr); Assert.AreEqual(null, parsedOperation.InflationDestination); Assert.AreEqual(null, parsedOperation.ClearFlags); Assert.AreEqual(null, parsedOperation.SetFlags); Assert.AreEqual(null, parsedOperation.MasterKeyWeight); Assert.AreEqual(null, parsedOperation.LowThreshold); Assert.AreEqual(null, parsedOperation.MediumThreshold); Assert.AreEqual(null, parsedOperation.HighThreshold); Assert.AreEqual(null, parsedOperation.HomeDomain); Assert.IsTrue(hash.SequenceEqual(parsedOperation.Signer.HashX.InnerValue)); Assert.AreEqual(10, parsedOperation.SignerWeight); Assert.AreEqual(source.AccountId, parsedOperation.SourceAccount.AccountId); Assert.AreEqual( "AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAACbpRqMkaQAfCYSk/n3xIl4fCoHfKqxF34ht2iuvSYEJQAAAAK", operation.ToXdrBase64()); }
/// <summary> /// Builds and configures Centaurus vault /// </summary> /// <returns>Transaction cursor</returns> private async Task <long> BuildAndConfigureVault(stellar_dotnet_sdk.responses.AccountResponse vaultAccount) { var majority = MajorityHelper.GetMajorityCount(constellationInitInfo.Auditors.Count()); var sourceAccount = await StellarAccountHelper.GetStellarAccount(vaultAccount.KeyPair); var transactionBuilder = new TransactionBuilder(sourceAccount); transactionBuilder.SetFee(10_000); var existingTrustlines = vaultAccount.Balances .Where(b => b.Asset is stellar_dotnet_sdk.AssetTypeCreditAlphaNum) .Select(b => b.Asset) .Cast <stellar_dotnet_sdk.AssetTypeCreditAlphaNum>(); foreach (var a in constellationInitInfo.Assets) { var asset = a.ToAsset() as stellar_dotnet_sdk.AssetTypeCreditAlphaNum; if (asset == null) //if null than asset is stellar_dotnet_sdk.AssetTypeNative { throw new InvalidOperationException("Native assets are supported by default."); //better to throw exception to avoid confusions with id } if (existingTrustlines.Any(t => t.Code == asset.Code && t.Issuer == asset.Issuer)) { continue; } var trustOperation = new ChangeTrustOperation.Builder(asset, "922337203685.4775807"); transactionBuilder.AddOperation(trustOperation.Build()); } var optionOperationBuilder = new SetOptionsOperation.Builder() .SetMasterKeyWeight(0) .SetLowThreshold(majority) .SetMediumThreshold(majority) .SetHighThreshold(majority); transactionBuilder.AddOperation(optionOperationBuilder.Build()); foreach (var signer in constellationInitInfo.Auditors) { transactionBuilder.AddOperation(new SetOptionsOperation.Builder().SetSigner(Signer.Ed25519PublicKey(signer), 1).Build()); } var transaction = transactionBuilder.Build(); transaction.Sign(Global.Settings.KeyPair); var result = await Global.StellarNetwork.Server.SubmitTransaction(transaction); if (!result.IsSuccess()) { throw new Exception($"Transaction failed. Result Xdr: {result.ResultXdr}"); } var tx = await Global.StellarNetwork.Server.Transactions.Transaction(result.Hash); return(long.Parse(tx.PagingToken)); }
public void TestSetOptionsOperationSingleField() { // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF var source = KeyPair.FromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK"); var homeDomain = "stellar.org"; var operation = new SetOptionsOperation.Builder() .SetHomeDomain(homeDomain) .SetSourceAccount(source) .Build(); var xdr = operation.ToXdr(); var parsedOperation = (SetOptionsOperation)Operation.FromXdr(xdr); Assert.AreEqual(null, parsedOperation.InflationDestination); Assert.AreEqual(null, parsedOperation.ClearFlags); Assert.AreEqual(null, parsedOperation.SetFlags); Assert.AreEqual(null, parsedOperation.MasterKeyWeight); Assert.AreEqual(null, parsedOperation.LowThreshold); Assert.AreEqual(null, parsedOperation.MediumThreshold); Assert.AreEqual(null, parsedOperation.HighThreshold); Assert.AreEqual(homeDomain, parsedOperation.HomeDomain); Assert.AreEqual(null, parsedOperation.Signer); Assert.AreEqual(null, parsedOperation.SignerWeight); Assert.AreEqual(source.AccountId, parsedOperation.SourceAccount.AccountId); Assert.AreEqual( "AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAtzdGVsbGFyLm9yZwAAAAAA", operation.ToXdrBase64()); }
/// <summary> /// Builds and configures Centaurus vault /// </summary> /// <returns>Transaction cursor</returns> private async Task <long> BuildAndConfigureVault(AccountModel vaultAccount) { var majority = MajorityHelper.GetMajorityCount(constellationInitInfo.Auditors.Count()); var sourceAccount = await Context.StellarDataProvider.GetAccountData(vaultAccount.KeyPair.AccountId); var transactionBuilder = new TransactionBuilder(sourceAccount.ToITransactionBuilderAccount()); transactionBuilder.SetFee(10_000); foreach (var a in constellationInitInfo.Assets) { if (a.IsXlm) { throw new InvalidOperationException("Native assets are supported by default."); //better to throw exception to avoid confusions with id } if (vaultAccount.ExistingTrustLines.Any(ta => ta == a.ToString())) { continue; } var trustOperation = new ChangeTrustOperation.Builder(a.ToAsset(), "922337203685.4775807"); transactionBuilder.AddOperation(trustOperation.Build()); } var optionOperationBuilder = new SetOptionsOperation.Builder() .SetMasterKeyWeight(0) .SetLowThreshold(majority) .SetMediumThreshold(majority) .SetHighThreshold(majority); transactionBuilder.AddOperation(optionOperationBuilder.Build()); foreach (var signer in constellationInitInfo.Auditors) { transactionBuilder.AddOperation(new SetOptionsOperation.Builder().SetSigner(Signer.Ed25519PublicKey(signer), 1).Build()); } var transaction = transactionBuilder.Build(); transaction.Sign(Context.Settings.KeyPair); var result = await Context.StellarDataProvider.SubmitTransaction(transaction); if (!result.IsSuccess) { throw new Exception($"Transaction failed. Result Xdr: {result.ResultXdr}"); } var tx = await Context.StellarDataProvider.GetTransaction(result.Hash); return(tx.PagingToken); }
public void TestSetOptionsOperation() { // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF var source = KeyPair.FromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK"); // GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR var inflationDestination = KeyPair.FromSecretSeed("SDHZGHURAYXKU2KMVHPOXI6JG2Q4BSQUQCEOY72O3QQTCLR2T455PMII"); // GBCP5W2VS7AEWV2HFRN7YYC623LTSV7VSTGIHFXDEJU7S5BAGVCSETRR var signer = Signer.Ed25519PublicKey(KeyPair.FromSecretSeed("SA64U7C5C7BS5IHWEPA7YWFN3Z6FE5L6KAMYUIT4AQ7KVTVLD23C6HEZ")); var clearFlags = 1; var setFlags = 1; var masterKeyWeight = 1; var lowThreshold = 2; var mediumThreshold = 3; var highThreshold = 4; var homeDomain = "stellar.org"; var signerWeight = 1; var operation = new SetOptionsOperation.Builder() .SetInflationDestination(inflationDestination) .SetClearFlags(clearFlags) .SetSetFlags(setFlags) .SetMasterKeyWeight(masterKeyWeight) .SetLowThreshold(lowThreshold) .SetMediumThreshold(mediumThreshold) .SetHighThreshold(highThreshold) .SetHomeDomain(homeDomain) .SetSigner(signer, signerWeight) .SetSourceAccount(source) .Build(); var xdr = operation.ToXdr(); var parsedOperation = (SetOptionsOperation)Operation.FromXdr(xdr); Assert.AreEqual(inflationDestination.AccountId, parsedOperation.InflationDestination.AccountId); Assert.AreEqual(clearFlags, parsedOperation.ClearFlags); Assert.AreEqual(setFlags, parsedOperation.SetFlags); Assert.AreEqual(masterKeyWeight, parsedOperation.MasterKeyWeight); Assert.AreEqual(lowThreshold, parsedOperation.LowThreshold); Assert.AreEqual(mediumThreshold, parsedOperation.MediumThreshold); Assert.AreEqual(highThreshold, parsedOperation.HighThreshold); Assert.AreEqual(homeDomain, parsedOperation.HomeDomain); Assert.AreEqual(signer.Discriminant.InnerValue, parsedOperation.Signer.Discriminant.InnerValue); Assert.AreEqual(signer.Ed25519.InnerValue, parsedOperation.Signer.Ed25519.InnerValue); Assert.AreEqual(signerWeight, parsedOperation.SignerWeight); Assert.AreEqual(source.AccountId, parsedOperation.SourceAccount.AccountId); Assert.AreEqual(OperationThreshold.High, parsedOperation.Threshold); Assert.AreEqual( "AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAUAAAABAAAAAO3gUmG83C+VCqO6FztuMtXJF/l7grZA7MjRzqdZ9W8QAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAIAAAABAAAAAwAAAAEAAAAEAAAAAQAAAAtzdGVsbGFyLm9yZwAAAAABAAAAAET+21WXwEtXRyxb/GBe1tc5V/WUzIOW4yJp+XQgNUUiAAAAAQ==", operation.ToXdrBase64()); }
public void TestSetOptionsOperationPreAuthTxSigner() { Network.UseTestNetwork(); // GBPMKIRA2OQW2XZZQUCQILI5TMVZ6JNRKM423BSAISDM7ZFWQ6KWEBC4 var source = KeyPair.FromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS"); var destination = KeyPair.FromAccountId("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR"); var sequenceNumber = 2908908335136768L; var account = new Account(source, sequenceNumber); var transaction = new Transaction.Builder(account) .AddOperation(new CreateAccountOperation.Builder(destination, "2000").Build()) .Build(); // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF var opSource = KeyPair.FromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK"); var operation = new SetOptionsOperation.Builder() .SetSigner(Signer.PreAuthTx(transaction), 10) .SetSourceAccount(opSource) .Build(); var xdr = operation.ToXdr(); var parsedOperation = (SetOptionsOperation)Operation.FromXdr(xdr); Assert.AreEqual(null, parsedOperation.InflationDestination); Assert.AreEqual(null, parsedOperation.ClearFlags); Assert.AreEqual(null, parsedOperation.SetFlags); Assert.AreEqual(null, parsedOperation.MasterKeyWeight); Assert.AreEqual(null, parsedOperation.LowThreshold); Assert.AreEqual(null, parsedOperation.MediumThreshold); Assert.AreEqual(null, parsedOperation.HighThreshold); Assert.AreEqual(null, parsedOperation.HomeDomain); Assert.IsTrue(transaction.Hash().SequenceEqual(parsedOperation.Signer.PreAuthTx.InnerValue)); Assert.AreEqual(10, parsedOperation.SignerWeight); Assert.AreEqual(opSource.AccountId, parsedOperation.SourceAccount.AccountId); Assert.AreEqual( "AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAB1vRBIRC3w7ZH5rQa17hIBKUwZTvBP4kNmSP7jVyw1fQAAAAK", operation.ToXdrBase64()); }
//I DO NOT RECOMMEND SENDING SECRET SEEDS OVER A NETWORK (ESPECIALLY UNENCRYPTED), THIS IS JUST AN EXAMPLE OF HOW YOU MIGHT FORM AN API USING THE SDK public async Task <IActionResult> SetInflationDestination([FromBody] SetInflationDestinationRequest request) { try { Network.UseTestNetwork(); var source = KeyPair.FromAccountId(request.AccountId); var signer = KeyPair.FromSecretSeed(request.Seed); var inflationDestination = KeyPair.FromAccountId(request.InflationDestination); //For Livenet use https://horizon.stellar.org using (var server = new Server("https://horizon-testnet.stellar.org")) { AccountResponse sourceAccount = await server.Accounts.Account(source); var sequenceNumber = sourceAccount.SequenceNumber; var account = new Account(source, sequenceNumber); var operation = new SetOptionsOperation.Builder() .SetInflationDestination(inflationDestination) .SetSourceAccount(source) .Build(); var memo = Memo.Text("Sample Memo"); Transaction transaction = new Transaction.Builder(account).AddOperation(operation).AddMemo(memo).Build(); var transactionXDR = transaction.ToUnsignedEnvelopeXdr(); transaction.Sign(signer); var test = transaction.ToEnvelopeXdrBase64(); await server.SubmitTransaction(test); return(Ok()); } } catch (Exception Ex) { return(StatusCode(500, "Something went wrong")); } }
//static void MergeAccount(KeyPair from,uint clearflag,uint setflag,uint masterwgt,uint lowthreshold,uint mediumthreshold,uint highthreshold,string domain,Stellar.Generated.SignerKey to,uint wgt) static void MergeAccount(KeyPair from, uint masterwgt, uint lowthreshold, uint mediumthreshold, uint highthreshold, Stellar.Generated.SignerKey to, uint wgt) { Account source = new Account(from, GetSequenceNum(from.Address)); // load asset Asset asset = new Asset(); var operation = new SetOptionsOperation.Builder() .SetSourceAccount(from) .SetMasterKeyWeight(masterwgt) .SetLowThreshold(lowthreshold) .SetMediumThreshold(mediumthreshold) .SetHighThreshold(highthreshold) .SetSigner(to, wgt) //.SetInflationDestination(from) //.SetClearFlags(clearflag) //.SetSetFlags(setflag) //.SetHomeDomain(domain) .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.ReasonPhrase); }
/// Generates a multi-signed transaction /// and transfer amount to a 3rd account, if signed correctly public async Task MultiSigTransfer() { // Horizon settings Network.UseTestNetwork(); Server server = new Server(HorizonUrl); // master account Console.WriteLine("Generating key pairs..."); KeyPair masterKeyPair = KeyPair.FromSecretSeed(MasterSecret); AccountResponse masterAccountResponse = await server.Accounts.Account(masterKeyPair); Account masterAccount = new Account(masterAccountResponse.KeyPair, masterAccountResponse.SequenceNumber); // generating keypairs KeyPair signerKeyPair = KeyPair.FromAccountId(SignerAccount); KeyPair signerSecretKeyPair = KeyPair.FromSecretSeed(SignerSecret);; KeyPair destinationKeyPair = KeyPair.FromAccountId(DestinationAccount); var signerKey = stellar_dotnet_sdk.Signer.Ed25519PublicKey(signerKeyPair); // set signer operation SetOptionsOperation signerOperation = new SetOptionsOperation.Builder().SetSigner(signerKey, 1).Build(); // set flag // for clearing flags -> SetOptionsOperation flagOperation = new SetOptionsOperation.Builder().SetClearFlags(1).Build(); SetOptionsOperation flagOperation = new SetOptionsOperation.Builder().SetSetFlags(1).Build(); // set medium threshold SetOptionsOperation thresholdOperation = new SetOptionsOperation.Builder().SetMediumThreshold(2).Build(); // payment operation string amountToTransfer = "35"; Asset asset = new AssetTypeNative(); PaymentOperation paymentOperation = new PaymentOperation.Builder(destinationKeyPair, asset, amountToTransfer).SetSourceAccount(masterKeyPair).Build(); // create transaction Transaction transaction = new Transaction.Builder(masterAccount) .AddOperation(flagOperation) .AddOperation(thresholdOperation) .AddOperation(signerOperation) .AddOperation(paymentOperation) .Build(); // sign Transaction transaction.Sign(masterKeyPair); transaction.Sign(signerSecretKeyPair); // try to send transaction try { Console.WriteLine("Sending Transaction..."); await server.SubmitTransaction(transaction); Console.WriteLine("Success!"); await this.GetBalance(MasterAccount); await this.GetBalance(SignerAccount); await this.GetBalance(DestinationAccount); } catch (Exception exception) { Console.WriteLine("Send Transaction Failed"); Console.WriteLine("Exception: " + exception.Message); } }