Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Components/Pages/Send.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ else
}
</div>
<div class="mb-2">
<label class="form-label small">Destination (60-char identity)</label>
<AddressInput @bind-Value="_destination" Placeholder="BAAAAA..." />
@{ var destErr = IdentityValidation.Validate(_destination); }
<label class="form-label small">Destination</label>
<AddressInput @bind-Value="_destination" Placeholder="BAAAAA... or contract index" AllowContractIndex="true" />
@{ var destErr = IdentityValidation.ValidateDestination(_destination); }
@if (destErr != null)
{
<div class="small text-danger mt-1">@destErr</div>
Expand Down Expand Up @@ -163,8 +163,8 @@ else
private async Task Preview()
{
_error = null; _result = null;
if (string.IsNullOrWhiteSpace(_destination) || _destination.Trim().Length != 60)
{ _error = "Destination must be a 60-character identity."; return; }
if (IdentityValidation.ValidateDestination(_destination) != null)
{ _error = "Destination must be a valid 60-character identity or contract index (1-1023)."; return; }
if (_amount <= 0) { _error = "Amount must be positive."; return; }
if (BalanceSvc.Balance != null && _amount > BalanceSvc.Balance.Value)
{ _error = $"Insufficient balance ({FormatQu(BalanceSvc.Balance.Value)} QU)."; return; }
Expand All @@ -188,10 +188,10 @@ else
_sending = true; _error = null;
try
{
if (IdentityValidation.Validate(_destination) != null) { _error = "Invalid destination."; return; }
if (IdentityValidation.ValidateDestination(_destination) != null) { _error = "Invalid destination."; return; }
_resolvedTick = await TransactionHelper.ResolveTargetTickAsync(Backend, TickMonitor, Settings, _targetTick);

var dest = Qubic.Core.Entities.QubicIdentity.FromIdentity(_destination!.Trim());
var dest = IdentityValidation.ResolveIdentity(_destination!);
var tx = Seed.CreateAndSignTransfer(dest, _amount, _resolvedTick);
_result = await TransactionHelper.BroadcastAndTrackAsync(
Backend, TxTracker, Seed, tx,
Expand Down
18 changes: 16 additions & 2 deletions Helpers/IdentityValidation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Qubic.Core;
using Qubic.Core.Entities;

namespace Qubic.Net.Wallet.Helpers;
Expand All @@ -23,18 +24,31 @@ public static class IdentityValidation
}

/// <summary>
/// Validates a destination that can be either a 60-char identity or a contract index (0-23).
/// Validates a destination that can be either a 60-char identity or a contract index (1-1023).
/// Returns null if valid, error message if invalid.
/// </summary>
public static string? ValidateDestination(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return null;
var v = value.Trim();
if (int.TryParse(v, out var idx))
return idx is >= 0 and <= 23 ? null : "Contract index must be 0-23";
return idx is >= 1 and <= 1023 ? null : "Contract index must be 1-1023";
return Validate(v);
}

/// <summary>
/// Resolves a destination string to a <see cref="QubicIdentity"/>.
/// Accepts either a 60-character identity or a contract index (1-1023).
/// Throws <see cref="ArgumentException"/> if the value is invalid.
/// </summary>
public static QubicIdentity ResolveIdentity(string value)
{
var v = value.Trim();
if (int.TryParse(v, out var idx))
return QubicIdentity.FromPublicKey(QubicContracts.GetContractPublicKey(idx));
return QubicIdentity.FromIdentity(v);
}

/// <summary>
/// Returns Bootstrap CSS class for identity input validation state.
/// </summary>
Expand Down