diff --git a/Components/Pages/Send.razor b/Components/Pages/Send.razor index 6852c18..1fe81a1 100644 --- a/Components/Pages/Send.razor +++ b/Components/Pages/Send.razor @@ -34,9 +34,9 @@ else }
- - - @{ var destErr = IdentityValidation.Validate(_destination); } + + + @{ var destErr = IdentityValidation.ValidateDestination(_destination); } @if (destErr != null) {
@destErr
@@ -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; } @@ -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, diff --git a/Helpers/IdentityValidation.cs b/Helpers/IdentityValidation.cs index 734d958..d5778fd 100644 --- a/Helpers/IdentityValidation.cs +++ b/Helpers/IdentityValidation.cs @@ -1,3 +1,4 @@ +using Qubic.Core; using Qubic.Core.Entities; namespace Qubic.Net.Wallet.Helpers; @@ -23,7 +24,7 @@ public static class IdentityValidation } /// - /// 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. /// public static string? ValidateDestination(string? value) @@ -31,10 +32,23 @@ public static class IdentityValidation 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); } + /// + /// Resolves a destination string to a . + /// Accepts either a 60-character identity or a contract index (1-1023). + /// Throws if the value is invalid. + /// + 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); + } + /// /// Returns Bootstrap CSS class for identity input validation state. ///