Skip to content

Commit 0c7a500

Browse files
authored
feat: update SorobanServer.SimulateTransaction to add support for non-root authorization (#68)
1 parent 34b17cf commit 0c7a500

3 files changed

Lines changed: 48 additions & 14 deletions

File tree

StellarDotnetSdk.Tests/SorobanServerTest.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public async Task TestSimulateSignedTransaction()
343343
{
344344
using var sorobanServer = Utils.CreateTestSorobanServerWithContent("");
345345
await Assert.ThrowsExceptionAsync<TooManySignaturesException>(() =>
346-
sorobanServer.SimulateTransaction(CreateDummyTransaction()));
346+
sorobanServer.SimulateTransaction(CreateDummyTransaction(sign: true)));
347347
}
348348

349349
[TestMethod]
@@ -1061,7 +1061,11 @@ public async Task TestSimulateTransactionSuccess()
10611061
""";
10621062
using var sorobanServer = Utils.CreateTestSorobanServerWithContent(json);
10631063

1064-
var response = await sorobanServer.SimulateTransaction(CreateDummyTransaction(false));
1064+
var response = await sorobanServer.SimulateTransaction(
1065+
CreateDummyTransaction(sign: false),
1066+
resourceConfig: null,
1067+
authMode: AuthMode.RECORD_ALLOW_NONROOT
1068+
);
10651069

10661070
Assert.IsNotNull(response);
10671071

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace StellarDotnetSdk.Soroban;
2+
3+
public enum AuthMode
4+
{
5+
ENFORCE,
6+
RECORD,
7+
RECORD_ALLOW_NONROOT,
8+
}

StellarDotnetSdk/Soroban/SorobanServer.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net.Http;
45
using System.Reflection;
@@ -240,24 +241,45 @@ public Task<GetLedgerEntriesResponse> GetLedgerEntry(LedgerKey key)
240241
/// </remarks>
241242
/// </param>
242243
/// <param name="resourceConfig">Contains configuration for how resources will be calculated when simulating transactions.</param>
244+
/// <param name="authMode">Explicitly allows users to opt in to non-root authorization in recording mode.
245+
/// <p>Leaving this field unset will default to <see cref="AuthMode.ENFORCE"/> if auth entries are present,
246+
/// <see cref="AuthMode.RECORD"/> otherwise.</p>
247+
/// </param>
243248
/// <returns>A <see cref="SimulateTransactionResponse" /> object.</returns>
244-
public Task<SimulateTransactionResponse> SimulateTransaction(Transaction transaction, uint? resourceConfig = null)
249+
public Task<SimulateTransactionResponse> SimulateTransaction(
250+
Transaction transaction,
251+
uint? resourceConfig = null,
252+
AuthMode? authMode = null)
253+
{
254+
var requestParams = BuildSimulateTransactionRequest(transaction, resourceConfig, authMode);
255+
256+
return SendRequest<object, SimulateTransactionResponse>(
257+
"simulateTransaction",
258+
requestParams
259+
);
260+
}
261+
262+
private static Dictionary<string, object> BuildSimulateTransactionRequest(
263+
Transaction transaction,
264+
uint? resourceConfig,
265+
AuthMode? authMode)
245266
{
267+
var request = new Dictionary<string, object>
268+
{
269+
["transaction"] = transaction.ToUnsignedEnvelopeXdrBase64()
270+
};
271+
246272
if (resourceConfig != null)
247273
{
248-
return SendRequest<object, SimulateTransactionResponse>("simulateTransaction",
249-
new
250-
{
251-
transaction = transaction.ToUnsignedEnvelopeXdrBase64(),
252-
resourceConfig = new { instructionLeeway = resourceConfig },
253-
});
274+
request["resourceConfig"] = new { instructionLeeway = resourceConfig };
275+
}
276+
277+
if (authMode != null)
278+
{
279+
request["authMode"] = authMode;
254280
}
255281

256-
return SendRequest<object, SimulateTransactionResponse>("simulateTransaction",
257-
new
258-
{
259-
transaction = transaction.ToUnsignedEnvelopeXdrBase64(),
260-
});
282+
return request;
261283
}
262284

263285
/// <summary>

0 commit comments

Comments
 (0)