From abdb67f6caea2f4c3d8d8082fe6202a16a591357 Mon Sep 17 00:00:00 2001 From: Kurdistan Tech Ministry Date: Fri, 27 Feb 2026 03:57:36 +0300 Subject: [PATCH] fix: convert hex AccountId to SS58 in referral/KYC queries toJSON() returns hex format for AccountId fields but comparison was against SS58 addresses, causing referrer matching to always fail. - citizenship-workflow: encodeAddress for referrer in getPendingApprovals - citizenship-workflow: handle both PascalCase and camelCase KycStatus - referral: encodeAddress for referrer in getMyReferrals and getReferralInfo --- shared/lib/citizenship-workflow.ts | 21 ++++++++++++++++++--- shared/lib/referral.ts | 17 +++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/shared/lib/citizenship-workflow.ts b/shared/lib/citizenship-workflow.ts index df5d0a86..fd9060ea 100644 --- a/shared/lib/citizenship-workflow.ts +++ b/shared/lib/citizenship-workflow.ts @@ -4,6 +4,7 @@ // Handles citizenship verification, status checks, and workflow logic import type { ApiPromise } from '@pezkuwi/api'; +import { encodeAddress } from '@pezkuwi/util-crypto'; import { getSigner } from '@/lib/get-signer'; import type { InjectedAccountWithMeta } from '@pezkuwi/extension-inject/types'; @@ -785,16 +786,30 @@ export async function getPendingApprovalsForReferrer( // An application is "pending" if it exists in applications but is NOT yet // approved in kycStatuses. Check referrer matches current user, or current // user is the founder (can approve any application). - const isReferrer = appData.referrer?.toString() === referrerAddress; + // Note: toJSON() returns hex for AccountId fields, convert to SS58 for comparison + let referrerSS58 = ''; + try { + if (appData.referrer) { + referrerSS58 = encodeAddress(appData.referrer as string, 42); + } + } catch { + referrerSS58 = appData.referrer?.toString() ?? ''; + } + const isReferrer = referrerSS58 === referrerAddress; const isFounder = referrerAddress === FOUNDER_ADDRESS; if (isReferrer || isFounder) { // Check if already approved via kycStatuses const kycStatus = await api.query.identityKyc.kycStatuses(applicantAddress); - const statusStr = kycStatus.isEmpty ? null : kycStatus.toString(); + const statusJson = kycStatus.isEmpty ? null : kycStatus.toJSON?.() ?? kycStatus.toString(); + const statusStr = typeof statusJson === 'string' ? statusJson : null; // Pending = not yet approved (no status or PendingReferral) - if (!statusStr || statusStr === 'PendingReferral') { + // Check both PascalCase and camelCase variants for safety + const isPending = !statusStr || + statusStr === 'PendingReferral' || + statusStr === 'pendingReferral'; + if (isPending) { pending.push({ applicantAddress, identityHash: (appData.identityHash as string) || '' diff --git a/shared/lib/referral.ts b/shared/lib/referral.ts index fcdacbc6..87fbda40 100644 --- a/shared/lib/referral.ts +++ b/shared/lib/referral.ts @@ -1,4 +1,5 @@ import type { ApiPromise } from '@pezkuwi/api'; +import { encodeAddress } from '@pezkuwi/util-crypto'; import type { InjectedAccountWithMeta } from '@pezkuwi/extension-inject/types'; import type { Signer } from '@pezkuwi/api/types'; @@ -162,8 +163,13 @@ export async function getReferralInfo( } const data = result.toJSON() as any; + // toJSON() returns hex for AccountId, convert to SS58 + let referrerSS58 = data.referrer ?? ''; + try { + if (data.referrer) referrerSS58 = encodeAddress(data.referrer, 42); + } catch { /* keep hex as fallback */ } return { - referrer: data.referrer, + referrer: referrerSS58, createdAt: parseInt(data.createdAt), }; } catch (error) { @@ -265,7 +271,14 @@ export async function getMyReferrals( .filter(([_key, value]) => { if (value.isEmpty) return false; const data = value.toJSON() as any; - return data.referrer === referrerAddress; + // toJSON() returns hex for AccountId, convert to SS58 for comparison + let refSS58 = ''; + try { + if (data.referrer) refSS58 = encodeAddress(data.referrer, 42); + } catch { + refSS58 = data.referrer ?? ''; + } + return refSS58 === referrerAddress; }) .map(([key]) => { // Extract the referred address from the storage key