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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useWalletContext } from '@/providers/wallet.provider';
import { useDidContext } from '@/providers/did.provider';
import { useCredential } from '@acta-team/credentials';
import { toast } from 'sonner';
import { isStellarTestnetDid } from '@/lib/validation';
import type { CredentialFormState, VerifiableCredential } from '@/@types/credentials';

export function useCredentialForm() {
Expand All @@ -13,7 +14,7 @@ export function useCredentialForm() {
const { issue } = useCredential();
const [state, setState] = useState<CredentialFormState>({
issuerName: '',
subjectDid: 'did:pkh:stellar:testnet:GAGPI5M5M4CZHQPZSTXOWX4J6UQMUJWFKACPXDRQMZTK43GPOSPW6NVU',
subjectDid: '',
degreeType: '',
degreeName: '',
validFrom: new Date().toISOString(),
Expand Down Expand Up @@ -46,6 +47,10 @@ export function useCredentialForm() {
toast.error('Please fill all required fields');
return;
}
if (!isStellarTestnetDid(state.subjectDid)) {
toast.error('Invalid Subject DID format');
return;
}
if (!signTransaction) {
toast.error('Signer unavailable');
return;
Expand Down
9 changes: 9 additions & 0 deletions src/components/modules/credential/ui/CredentialForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import {
DialogDescription,
} from '@/components/ui/dialog';
import { useCredentialForm } from '../hooks/use-credential-form';
import { isStellarTestnetDid } from '@/lib/validation';

export function CredentialForm() {
const { state, updateField, handleCreate, fillExample, walletAddress, ownerDid } =
useCredentialForm();

const subjectDidInvalid = state.subjectDid.length > 0 && !isStellarTestnetDid(state.subjectDid);

return !walletAddress ? (
<div className="rounded border p-6 md:p-8 min-h-[20vh] flex flex-col items-center justify-center text-center">
<p className="text-base">Connect your wallet to begin.</p>
Expand Down Expand Up @@ -102,7 +105,13 @@ export function CredentialForm() {
value={state.subjectDid}
onChange={(e) => updateField('subjectDid', e.target.value)}
placeholder="did:pkh:stellar:testnet:GAGPI5M5M4CZHQPZSTXOWX4J6UQMUJWFKACPXDRQMZTK43GPOSPW6NVU"
aria-invalid={subjectDidInvalid}
/>
{subjectDidInvalid && (
<p className="text-xs text-red-500 mt-1">
Invalid Subject DID format. Expected did:pkh:stellar:testnet:&lt;Stellar address&gt;.
</p>
)}
<Dialog
open={state.openSubjectDidInfo}
onOpenChange={(open) => updateField('openSubjectDidInfo', open)}
Expand Down
10 changes: 10 additions & 0 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const STELLAR_ADDRESS_REGEX = /^G[A-Z2-7]{55}$/;
const STELLAR_TESTNET_DID_REGEX = /^did:pkh:stellar:testnet:G[A-Z2-7]{55}$/;

export function isStellarAddress(addr: string): boolean {
return STELLAR_ADDRESS_REGEX.test(addr);
}

export function isStellarTestnetDid(did: string): boolean {
return STELLAR_TESTNET_DID_REGEX.test(did);
}
4 changes: 3 additions & 1 deletion src/providers/did.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { useWalletContext } from '@/providers/wallet.provider';
import { isStellarAddress } from '@/lib/validation';

type DidContextValue = {
ownerDid: string | null;
Expand All @@ -10,7 +11,8 @@ type DidContextValue = {

const DidContext = createContext<DidContextValue | undefined>(undefined);

export function makeDidForAddress(address: string): string {
export function makeDidForAddress(address: string): string | null {
if (!isStellarAddress(address)) return null;
// Always use testnet for this demo
return `did:pkh:stellar:testnet:${address}`;
}
Expand Down
Loading