Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions StellarDotnetSdk/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public KeyPair KeyPair
}
}

/// <summary>
/// Gets the muxed account identifier associated with this account.
/// </summary>
public IAccountId MuxedAccount { get; }

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions StellarDotnetSdk/Accounts/IAccountId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ namespace StellarDotnetSdk.Accounts;
/// </summary>
public interface IAccountId
{
/// <summary>Gets the XDR muxed account representation.</summary>
Xdr.MuxedAccount MuxedAccount { get; }

/// <summary>Gets the <see cref="KeyPair" /> used for signing transactions.</summary>
KeyPair SigningKey { get; }

/// <summary>Gets the raw Ed25519 public key bytes.</summary>
byte[] PublicKey { get; }

/// <summary>Gets the StrKey-encoded address (G... for standard accounts, M... for muxed accounts).</summary>
string Address { get; }

/// <summary>Gets the StrKey-encoded account ID (G... for standard accounts, M... for muxed accounts).</summary>
string AccountId { get; }

/// <summary>Gets a value indicating whether this is a muxed (multiplexed) account.</summary>
bool IsMuxedAccount { get; }
}
6 changes: 6 additions & 0 deletions StellarDotnetSdk/Accounts/ITransactionBuilderAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
/// </summary>
public interface ITransactionBuilderAccount
{
/// <summary>Gets the StrKey-encoded account ID.</summary>
string AccountId { get; }

/// <summary>Gets the <see cref="Accounts.KeyPair" /> of the account.</summary>
KeyPair KeyPair { get; }

/// <summary>Gets the muxed account identifier associated with this account.</summary>
IAccountId MuxedAccount { get; }

/// <summary>Gets the current sequence number of the account on the Stellar ledger.</summary>
long SequenceNumber { get; }

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions StellarDotnetSdk/Accounts/KeyPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ public static KeyPair FromAccountId(string accountId)
return FromPublicKey(decoded);
}

/// <summary>
/// Derives a Stellar <see cref="KeyPair" /> from a BIP-39 mnemonic seed using the
/// standard Stellar derivation path (<c>m/44'/148'/{accountIndex}'</c>).
/// </summary>
/// <param name="seed">The hex-encoded BIP-39 seed.</param>
/// <param name="accountIndex">The account index in the derivation path.</param>
/// <returns>A <see cref="KeyPair" /> derived from the given seed and account index.</returns>
public static KeyPair FromBIP39Seed(string seed, uint accountIndex)
{
var bip32 = new BIP32();
Expand All @@ -295,6 +302,13 @@ public static KeyPair FromBIP39Seed(string seed, uint accountIndex)
return FromSecretSeed(bip32.DerivePath(path, seed).Key);
}

/// <summary>
/// Derives a Stellar <see cref="KeyPair" /> from a BIP-39 mnemonic seed using the
/// standard Stellar derivation path (<c>m/44'/148'/{accountIndex}'</c>).
/// </summary>
/// <param name="seedBytes">The raw BIP-39 seed bytes.</param>
/// <param name="accountIndex">The account index in the derivation path.</param>
/// <returns>A <see cref="KeyPair" /> derived from the given seed and account index.</returns>
public static KeyPair FromBIP39Seed(byte[] seedBytes, uint accountIndex)
{
var seed = seedBytes.ToStringHex();
Expand Down
10 changes: 10 additions & 0 deletions StellarDotnetSdk/Accounts/MuxedAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@

namespace StellarDotnetSdk.Accounts;

/// <summary>
/// Provides factory methods for creating account identifiers from XDR muxed account representations.
/// </summary>
public static class MuxedAccount
{
/// <summary>
/// Creates an <see cref="IAccountId" /> from an XDR <see cref="Xdr.MuxedAccount" />.
/// Returns a <see cref="KeyPair" /> for standard Ed25519 accounts, or a
/// <see cref="MuxedAccountMed25519" /> for multiplexed accounts.
/// </summary>
/// <param name="muxedAccount">The XDR muxed account to convert.</param>
/// <returns>An <see cref="IAccountId" /> representing the account.</returns>
public static IAccountId FromXdrMuxedAccount(Xdr.MuxedAccount muxedAccount)
{
return muxedAccount.Discriminant.InnerValue switch
Expand Down
5 changes: 5 additions & 0 deletions StellarDotnetSdk/Accounts/MuxedAccountMed25519.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ public MuxedAccountMed25519(KeyPair key, ulong id)
Key = key ?? throw new ArgumentNullException(nameof(key));
}

/// <summary>Gets the 64-bit numeric identifier that distinguishes this muxed account.</summary>
public ulong Id { get; }

/// <summary>Gets the underlying Ed25519 <see cref="KeyPair" /> of the muxed account.</summary>
public KeyPair Key { get; }

/// <summary>Gets the raw Ed25519 public key bytes of the underlying account.</summary>
public byte[] PublicKey => Key.PublicKey;

/// <summary>
Expand Down
19 changes: 19 additions & 0 deletions StellarDotnetSdk/Amount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@

namespace StellarDotnetSdk;

/// <summary>
/// Utility class for converting between Stellar amount representations. Stellar stores amounts as
/// 64-bit integers with 7 decimal places of precision (1 XLM = 10,000,000 stroops).
/// </summary>
public static class Amount
{
private static readonly decimal One = new(10000000);


/// <summary>
/// Converts a decimal value to its culture-invariant string representation.
/// </summary>
/// <param name="d">The decimal value to convert.</param>
/// <returns>A culture-invariant string representation of the decimal.</returns>
public static string DecimalToString(decimal d)
{
return d.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Converts an XDR amount (in stroops) to a human-readable decimal string.
/// </summary>
/// <param name="value">The XDR amount in stroops (1 unit = 10,000,000 stroops).</param>
/// <returns>A decimal string representation of the amount.</returns>
public static string FromXdr(long value)
{
var amount = decimal.Divide(new decimal(value), One);
return DecimalToString(amount);
}

/// <summary>
/// Converts a human-readable decimal string to an XDR amount (in stroops).
/// </summary>
/// <param name="value">The decimal string to convert (e.g., "10.5").</param>
/// <returns>The amount in stroops as a 64-bit integer.</returns>
public static long ToXdr(string value)
{
if (string.IsNullOrEmpty(value))
Expand Down
13 changes: 13 additions & 0 deletions StellarDotnetSdk/Assets/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public abstract class Asset
/// </summary>
public abstract string Type { get; }

/// <summary>
/// Creates an <see cref="Asset" /> from its canonical string form.
/// Native assets use <c>"native"</c>; credit assets use <c>"CODE:ISSUER"</c>.
/// </summary>
/// <param name="canonicalForm">The canonical asset string (e.g., <c>"native"</c> or <c>"USD:GABC..."</c>).</param>
/// <returns>The corresponding <see cref="Asset" /> instance.</returns>
/// <exception cref="ArgumentException">Thrown when the canonical form is invalid.</exception>
public static Asset Create(string canonicalForm)
{
if (canonicalForm == "native")
Expand Down Expand Up @@ -119,5 +126,11 @@ public static Asset FromXdr(Xdr.Asset thisXdr)
/// </summary>
public abstract string CanonicalName();

/// <summary>
/// Compares this asset to another <see cref="Asset" /> for ordering.
/// Assets are ordered: native &lt; credit_alphanum4 &lt; credit_alphanum12.
/// </summary>
/// <param name="asset">The asset to compare with.</param>
/// <returns>A negative value, zero, or positive value if this asset is less than, equal to, or greater than <paramref name="asset" />.</returns>
public abstract int CompareTo(Asset asset);
}
9 changes: 9 additions & 0 deletions StellarDotnetSdk/Assets/AssetAmount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ public class AssetAmount(
Asset Asset,
string Amount)
{
/// <summary>
/// Gets the Stellar asset.
/// </summary>
public Asset Asset { get; } = Asset;

/// <summary>
/// Gets the amount of the asset as a string (in stroops-compatible decimal format).
/// </summary>
public string Amount { get; } = Amount;

/// <inheritdoc />
public override int GetHashCode()
{
return Asset.GetHashCode().Hash(Amount);
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
if (obj is not AssetAmount other)
Expand Down
3 changes: 3 additions & 0 deletions StellarDotnetSdk/Assets/AssetExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace StellarDotnetSdk.Assets;

/// <summary>
/// Provides extension methods for the <see cref="Asset" /> class.
/// </summary>
public static class AssetExtensions
{
/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions StellarDotnetSdk/Assets/AssetTypeCreditAlphaNum12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ namespace StellarDotnetSdk.Assets;
/// </summary>
public class AssetTypeCreditAlphaNum12 : AssetTypeCreditAlphaNum
{
/// <summary>
/// The Horizon REST API type identifier for alphanumeric 12-character assets.
/// </summary>
public const string RestApiType = "credit_alphanum12";

/// <summary>
/// Initializes a new instance of <see cref="AssetTypeCreditAlphaNum12" /> with the specified asset code and issuer.
/// </summary>
/// <param name="code">The asset code (5-12 alphanumeric characters).</param>
/// <param name="issuer">The Stellar account ID of the asset issuer.</param>
/// <exception cref="StellarDotnetSdk.Exceptions.AssetCodeLengthInvalidException">Thrown when the code length is not between 5 and 12.</exception>
public AssetTypeCreditAlphaNum12(string code, string issuer) : base(code, issuer)
{
if (code.Length is < 5 or > 12)
Expand All @@ -20,8 +29,10 @@ public AssetTypeCreditAlphaNum12(string code, string issuer) : base(code, issuer
}
}

/// <inheritdoc />
public override string Type => RestApiType;

/// <inheritdoc />
public override Xdr.Asset ToXdr()
{
var thisXdr = new Xdr.Asset
Expand All @@ -41,6 +52,7 @@ public override Xdr.Asset ToXdr()
return thisXdr;
}

/// <inheritdoc />
public override int CompareTo(Asset asset)
{
if (asset.Type != RestApiType)
Expand Down
12 changes: 12 additions & 0 deletions StellarDotnetSdk/Assets/AssetTypeCreditAlphaNum4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ namespace StellarDotnetSdk.Assets;
/// </summary>
public class AssetTypeCreditAlphaNum4 : AssetTypeCreditAlphaNum
{
/// <summary>
/// The Horizon REST API type identifier for alphanumeric 4-character assets.
/// </summary>
public const string RestApiType = "credit_alphanum4";

/// <summary>
/// Initializes a new instance of <see cref="AssetTypeCreditAlphaNum4" /> with the specified asset code and issuer.
/// </summary>
/// <param name="code">The asset code (1-4 alphanumeric characters).</param>
/// <param name="issuer">The Stellar account ID of the asset issuer.</param>
/// <exception cref="StellarDotnetSdk.Exceptions.AssetCodeLengthInvalidException">Thrown when the code length is not between 1 and 4.</exception>
public AssetTypeCreditAlphaNum4(string code, string issuer) : base(code, issuer)
{
if (code.Length is < 1 or > 4)
Expand All @@ -21,8 +30,10 @@ public AssetTypeCreditAlphaNum4(string code, string issuer) : base(code, issuer)
}
}

/// <inheritdoc />
public override string Type => RestApiType;

/// <inheritdoc />
public override Xdr.Asset ToXdr()
{
var thisXdr = new Xdr.Asset
Expand All @@ -42,6 +53,7 @@ public override Xdr.Asset ToXdr()
return thisXdr;
}

/// <inheritdoc />
public override int CompareTo(Asset asset)
{
switch (asset.Type)
Expand Down
8 changes: 8 additions & 0 deletions StellarDotnetSdk/Assets/AssetTypeNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ namespace StellarDotnetSdk.Assets;
/// </summary>
public class AssetTypeNative : Asset
{
/// <summary>
/// The Horizon REST API type identifier for the native asset.
/// </summary>
public const string RestApiType = "native";

/// <inheritdoc />
public override string Type => RestApiType;

/// <inheritdoc />
public override int GetHashCode()
{
return 0;
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
if (obj is not AssetTypeNative)
Expand All @@ -25,6 +31,7 @@ public override bool Equals(object? obj)
return GetHashCode() == obj.GetHashCode();
}

/// <inheritdoc />
public override Xdr.Asset ToXdr()
{
var thisXdr = new Xdr.Asset
Expand All @@ -40,6 +47,7 @@ public override string CanonicalName()
return "native";
}

/// <inheritdoc />
public override int CompareTo(Asset asset)
{
if (asset.Type == RestApiType)
Expand Down
Loading
Loading