UNPKG

@attestprotocol/stellar-sdk

Version:

Stellar implementation of the Attest Protocol SDK

594 lines (576 loc) 20.7 kB
import { IProtocolConfig, SchemaDefinition, AttestProtocolResponse, Schema, ListSchemasByIssuerParams, PaginatedResponse, AttestationDefinition, Attestation, ListAttestationsByWalletParams, ListAttestationsBySchemaParams, RevocationDefinition, DelegatedAttestationDefinition, DelegatedRevocationDefinition, Authority, AttestProtocolBase } from '@attestprotocol/core'; export { AttestProtocolErrorType, AttestProtocolResponse, Attestation, AttestationDefinition, Authority, IAttestProtocol, RevocationDefinition, Schema, SchemaDefinition, createAttestProtocolError, createErrorResponse, createSuccessResponse } from '@attestprotocol/core'; import { Client } from '@attestprotocol/stellar/dist/bindings/src/protocol'; export { AttestationRecord as ProtocolAttestationRecord, Authority as ProtocolAuthority, Client as ProtocolClient, networks as ProtocolNetworks, Schema as ProtocolSchema, ResolverAttestationRecord, StoredAttestation } from '@attestprotocol/stellar/dist/bindings/src/protocol'; import { Client as Client$1, SchemaRules, AttestationRecord } from '@attestprotocol/stellar/dist/bindings/src/authority'; export { AttestationRecord as AuthorityAttestationRecord, Client as AuthorityClient, networks as AuthorityNetworks, RegisteredAuthorityData, SchemaRules } from '@attestprotocol/stellar/dist/bindings/src/authority'; /** * Stellar-specific types for the Attest Protocol SDK */ /** * Custom signer interface for Stellar transactions */ interface StellarCustomSigner { signTransaction: (xdr: string) => Promise<{ signedTxXdr: string; signerAddress?: string; }>; } /** * Stellar-specific SDK configuration */ interface StellarConfig extends IProtocolConfig { /** * Either a secret key string or a custom signer implementation */ secretKeyOrCustomSigner: string | StellarCustomSigner; /** * Public key address */ publicKey: string; /** * Network passphrase (defaults to TESTNET) */ networkPassphrase?: string; /** * Contract addresses for protocol and authority contracts */ contractAddresses?: { protocol?: string; authority?: string; }; /** * Whether to allow HTTP connections (for development) */ allowHttp?: boolean; } /** * Stellar-specific schema configuration */ interface StellarSchemaConfig { name: string; content: string; resolverAddress?: string; revocable?: boolean; } /** * Stellar-specific attestation configuration */ interface StellarAttestationConfig { schemaUID: string; subject: string; reference?: string; } /** * Stellar attestation configuration with value field */ interface StellarAttestationConfigWithValue extends StellarAttestationConfig { value: string; reference: string; } /** * Stellar authority fetch result */ interface StellarFetchAuthorityResult { address: string; metadata: string; } /** * Stellar schema fetch result */ interface StellarFetchSchemaResult { uid: string; definition: string; authority: string; revocable: boolean; resolver: string | null; } /** * Stellar schema creation result */ interface StellarCreateSchemaResult { schemaUID: string; hash: string; } /** * Stellar attestation fetch result */ interface StellarFetchAttestationResult { schemaUid: string; subject: string; value: string; reference: string | null; revoked: boolean; } /** * Stellar revocation configuration */ interface StellarRevokeAttestationConfig { attestationUID: string; recipient: string; reference?: string | null; } /** * Schema operations for the Stellar Attest Protocol */ declare class StellarSchemaService { private protocolClient; private publicKey; constructor(config: StellarConfig, protocolClient: Client); /** * Create a new schema on the Stellar network */ createSchema(config: SchemaDefinition): Promise<AttestProtocolResponse<Schema>>; /** * Fetch a schema by its UID */ fetchSchemaById(id: string): Promise<AttestProtocolResponse<Schema | null>>; /** * Generate a deterministic ID from schema definition */ generateIdFromSchema(schema: SchemaDefinition): Promise<AttestProtocolResponse<string>>; /** * List schemas by issuer */ listSchemasByIssuer(params: ListSchemasByIssuerParams): Promise<AttestProtocolResponse<PaginatedResponse<Schema>>>; /** * Validate schema definition */ private validateSchemaDefinition; } /** * Attestation operations for the Stellar Attest Protocol */ declare class StellarAttestationService { private protocolClient; private publicKey; constructor(config: StellarConfig, protocolClient: Client); /** * Issue a new attestation on the Stellar network */ issueAttestation(config: AttestationDefinition): Promise<AttestProtocolResponse<Attestation>>; /** * Fetch an attestation by its ID */ fetchAttestationById(id: string): Promise<AttestProtocolResponse<Attestation | null>>; /** * Get an attestation by schema UID, subject, and reference */ getAttestation(schemaUid: string, subject: string, reference?: string): Promise<AttestProtocolResponse<Attestation | null>>; /** * List attestations by wallet address */ listAttestationsByWallet(params: ListAttestationsByWalletParams): Promise<AttestProtocolResponse<PaginatedResponse<Attestation>>>; /** * List attestations by schema UID */ listAttestationsBySchema(params: ListAttestationsBySchemaParams): Promise<AttestProtocolResponse<PaginatedResponse<Attestation>>>; /** * Revoke an attestation */ revokeAttestation(config: RevocationDefinition): Promise<AttestProtocolResponse<void>>; /** * Revoke an attestation by its components (Stellar-specific) */ revokeAttestationByComponents(schemaUid: string, subject: string, reference?: string): Promise<AttestProtocolResponse<void>>; /** * Attest by delegation (not implemented in current Stellar contracts) */ attestByDelegation(config: DelegatedAttestationDefinition): Promise<AttestProtocolResponse<Attestation>>; /** * Revoke by delegation (not implemented in current Stellar contracts) */ revokeByDelegation(config: DelegatedRevocationDefinition): Promise<AttestProtocolResponse<void>>; /** * Validate attestation definition */ private validateAttestationDefinition; /** * Validate revocation definition */ private validateRevocationDefinition; } /** * Authority operations for the Stellar Attest Protocol */ declare class StellarAuthorityService { private authorityClient; private publicKey; constructor(config: StellarConfig, authorityClient: Client$1); /** * Initialize the authority contract */ initialize(admin: string, tokenContractId: string): Promise<AttestProtocolResponse<void>>; /** * Register an authority (admin function) */ adminRegisterAuthority(authToReg: string, metadata: string): Promise<AttestProtocolResponse<void>>; /** * Register an authority (public function with fees) */ registerAuthority(authorityToReg: string, metadata: string): Promise<AttestProtocolResponse<void>>; /** * Check if an address is an authority */ isAuthority(authority: string): Promise<AttestProtocolResponse<boolean>>; /** * Register schema rules (admin function) */ adminRegisterSchema(schemaUid: Buffer, rules: SchemaRules): Promise<AttestProtocolResponse<void>>; /** * Set schema levy (admin function) */ adminSetSchemaLevy(schemaUid: Buffer, levyAmount: bigint, levyRecipient: string): Promise<AttestProtocolResponse<void>>; /** * Set registration fee (admin function) */ adminSetRegistrationFee(feeAmount: bigint, tokenId: string): Promise<AttestProtocolResponse<void>>; /** * Process attestation through authority contract */ attest(attestation: AttestationRecord): Promise<AttestProtocolResponse<boolean>>; /** * Process revocation through authority contract */ revoke(attestation: AttestationRecord): Promise<AttestProtocolResponse<boolean>>; /** * Withdraw collected levies */ withdrawLevies(): Promise<AttestProtocolResponse<void>>; /** * Get schema rules */ getSchemaRules(schemaUid: Buffer): Promise<AttestProtocolResponse<SchemaRules | null>>; /** * Get collected levies for an authority */ getCollectedLevies(authority: string): Promise<AttestProtocolResponse<bigint>>; /** * Get token ID */ getTokenId(): Promise<AttestProtocolResponse<string>>; /** * Get admin address */ getAdminAddress(): Promise<AttestProtocolResponse<string>>; /** * Fetch authority information */ fetchAuthority(id: string): Promise<AttestProtocolResponse<Authority | null>>; } /** * Stellar implementation of the Attest Protocol SDK */ /** * Stellar implementation of the Attest SDK */ declare class StellarAttestProtocol extends AttestProtocolBase { protected config: StellarConfig; private server; private networkPassphrase; private protocolClient; private authorityClient; private schemaService; private attestationService; private authorityService; /** * Creates a new instance of the Stellar Attest SDK * @param config SDK configuration options */ constructor(config: StellarConfig); protected getDefaultNetworkUrl(): string; /** * Initialize the protocol contract by setting the admin */ initialize(): Promise<AttestProtocolResponse<void>>; registerAuthority(): Promise<AttestProtocolResponse<string>>; fetchAuthority(id: string): Promise<AttestProtocolResponse<Authority | null>>; isIssuerAnAuthority(issuer: string): Promise<AttestProtocolResponse<boolean>>; createSchema(config: SchemaDefinition): Promise<AttestProtocolResponse<Schema>>; fetchSchemaById(id: string): Promise<AttestProtocolResponse<Schema | null>>; generateIdFromSchema(schema: SchemaDefinition): Promise<AttestProtocolResponse<string>>; listSchemasByIssuer(params: ListSchemasByIssuerParams): Promise<AttestProtocolResponse<PaginatedResponse<Schema>>>; issueAttestation(config: AttestationDefinition): Promise<AttestProtocolResponse<Attestation>>; fetchAttestationById(id: string): Promise<AttestProtocolResponse<Attestation | null>>; listAttestationsByWallet(params: ListAttestationsByWalletParams): Promise<AttestProtocolResponse<PaginatedResponse<Attestation>>>; listAttestationsBySchema(params: ListAttestationsBySchemaParams): Promise<AttestProtocolResponse<PaginatedResponse<Attestation>>>; revokeAttestation(config: RevocationDefinition): Promise<AttestProtocolResponse<void>>; attestByDelegation(config: DelegatedAttestationDefinition): Promise<AttestProtocolResponse<Attestation>>; revokeByDelegation(config: DelegatedRevocationDefinition): Promise<AttestProtocolResponse<void>>; /** * Get the underlying protocol contract client */ getProtocolClient(): Client; /** * Get the underlying authority contract client */ getAuthorityClient(): Client$1; /** * Get the schema service for direct access */ getSchemaService(): StellarSchemaService; /** * Get the attestation service for direct access */ getAttestationService(): StellarAttestationService; /** * Get the authority service for direct access */ getAuthorityService(): StellarAuthorityService; /** * Get an attestation by schema UID, subject, and reference */ getAttestation(schemaUid: string, subject: string, reference?: string): Promise<AttestProtocolResponse<Attestation | null>>; } /** * Stellar Schema Encoder - Standardized schema definition and data encoding for Stellar attestations * * Inspired by EAS (Ethereum Attestation Service) but adapted for Stellar/Soroban contracts. * Provides type-safe schema definitions and encoding/decoding utilities. */ /** * Supported Stellar attestation data types */ declare enum StellarDataType { STRING = "string", BOOL = "bool", U32 = "u32", U64 = "u64", I32 = "i32", I64 = "i64", I128 = "i128", ADDRESS = "address", BYTES = "bytes", SYMBOL = "symbol", ARRAY = "array", OPTION = "option", MAP = "map", TIMESTAMP = "timestamp", AMOUNT = "amount" } /** * Schema field definition */ interface SchemaField { name: string; type: StellarDataType | string; optional?: boolean; description?: string; validation?: { min?: number; max?: number; pattern?: string; enum?: string[]; }; } /** * Complete schema definition with metadata */ interface StellarSchemaDefinition { name: string; version: string; description: string; fields: SchemaField[]; metadata?: { category?: string; tags?: string[]; authority?: string; revocable?: boolean; expirable?: boolean; }; } /** * Encoded attestation data ready for contract submission */ interface EncodedAttestationData { schemaHash: string; encodedData: string; decodedData: Record<string, any>; schema: StellarSchemaDefinition; } /** * Schema validation error */ declare class SchemaValidationError extends Error { field?: string | undefined; constructor(message: string, field?: string | undefined); } /** * Stellar Schema Encoder - Provides standardized schema definition and data encoding */ declare class StellarSchemaEncoder { private schema; constructor(schema: StellarSchemaDefinition); /** * Get the schema definition */ getSchema(): StellarSchemaDefinition; /** * Generate a unique hash for this schema */ getSchemaHash(): string; /** * Encode attestation data according to the schema */ encodeData(data: Record<string, any>): Promise<EncodedAttestationData>; /** * Decode attestation data from encoded format */ decodeData(encodedData: string): Record<string, any>; /** * Validate data against the schema */ validateData(data: Record<string, any>): void; /** * Generate default values for a schema */ generateDefaults(): Record<string, any>; /** * Convert schema to JSON Schema format for external compatibility */ toJSONSchema(): object; /** * Create a schema encoder from JSON Schema */ static fromJSONSchema(jsonSchema: any): StellarSchemaEncoder; /** * Validate schema definition */ private validateSchema; /** * Validate individual field value */ private validateFieldValue; /** * Process data for encoding (type conversions, etc.) */ private processDataForEncoding; /** * Process data for decoding (reverse of encoding) */ private processDataForDecoding; /** * Get default value for a type */ private getDefaultValue; /** * Check if a type is valid Stellar type */ private isValidStellarType; /** * Convert Stellar type to JSON Schema type */ private stellarTypeToJSONSchemaType; /** * Convert JSON Schema type to Stellar type */ private static jsonSchemaTypeToStellarType; /** * Validate Stellar address format */ private isValidStellarAddress; } /** * Pre-defined schema encoders for common use cases */ declare class StellarSchemaRegistry { private static schemas; /** * Register a schema encoder */ static register(name: string, encoder: StellarSchemaEncoder): void; /** * Get a registered schema encoder */ static get(name: string): StellarSchemaEncoder | undefined; /** * List all registered schema names */ static list(): string[]; /** * Initialize with common schemas */ static initializeDefaults(): void; } /** * Schema-related utility functions */ /** * Generate a schema UID using the same algorithm as the Stellar protocol contract. * * This function implements the exact same logic as the `generate_uid` function * in the protocol contract (contracts/stellar/protocol/src/instructions/schema.rs) * * The UID is derived from: * - Schema definition (as string) * - Authority address (who is registering the schema) * - Optional resolver address * * All values are serialized to XDR format and concatenated before hashing with SHA-256. * * @param schemaDefinition - The string representation of the schema * @param authority - The address of the authority registering the schema * @param resolver - Optional resolver address * @returns Promise<string> - The 64-character hex string schema UID */ declare function generateSchemaUid(schemaDefinition: string, authority: string, resolver?: string | null): Promise<string>; /** * Generate a deterministic schema UID from a SchemaDefinition object. * * This function takes a SchemaDefinition and generates the same UID that would * be created by the contract when registering the schema. * * @param schema - The schema definition object * @param authority - The authority address (defaults to empty string if not provided) * @returns Promise<string> - The 64-character hex string schema UID */ declare function generateIdFromSchema(schema: SchemaDefinition, authority?: string): Promise<string>; /** * Format a schema UID for display (with dashes for readability). * * @param uid - The 64-character hex UID * @returns Formatted UID string */ declare function formatSchemaUid(uid: string): string; /** * Parse a formatted schema UID back to raw hex string. * * @param formattedUid - The formatted UID with dashes * @returns Raw 64-character hex string */ declare function parseFormattedUid(formattedUid: string): string; /** * Internal utilities for the Stellar Attest Protocol SDK * * This module exports all internal utilities organized by context: * - Schema utilities (UID generation, formatting) * - Test data generation functions * - Stellar-specific utilities (keypairs, addresses) * - Standardized schema encoders * - Validation functions * - Schema encoder core functionality */ type index_EncodedAttestationData = EncodedAttestationData; type index_SchemaField = SchemaField; type index_SchemaValidationError = SchemaValidationError; declare const index_SchemaValidationError: typeof SchemaValidationError; type index_StellarDataType = StellarDataType; declare const index_StellarDataType: typeof StellarDataType; type index_StellarSchemaDefinition = StellarSchemaDefinition; type index_StellarSchemaEncoder = StellarSchemaEncoder; declare const index_StellarSchemaEncoder: typeof StellarSchemaEncoder; type index_StellarSchemaRegistry = StellarSchemaRegistry; declare const index_StellarSchemaRegistry: typeof StellarSchemaRegistry; declare const index_formatSchemaUid: typeof formatSchemaUid; declare const index_generateIdFromSchema: typeof generateIdFromSchema; declare const index_generateSchemaUid: typeof generateSchemaUid; declare const index_parseFormattedUid: typeof parseFormattedUid; declare namespace index { export { type index_EncodedAttestationData as EncodedAttestationData, type index_SchemaField as SchemaField, index_SchemaValidationError as SchemaValidationError, index_StellarDataType as StellarDataType, type index_StellarSchemaDefinition as StellarSchemaDefinition, index_StellarSchemaEncoder as StellarSchemaEncoder, index_StellarSchemaRegistry as StellarSchemaRegistry, index_formatSchemaUid as formatSchemaUid, index_generateIdFromSchema as generateIdFromSchema, index_generateSchemaUid as generateSchemaUid, index_parseFormattedUid as parseFormattedUid }; } /** * @attestprotocol/stellar-sdk * * Stellar implementation of the Attest Protocol SDK */ export { type EncodedAttestationData, type SchemaField, SchemaValidationError, StellarAttestProtocol, type StellarAttestationConfig, type StellarAttestationConfigWithValue, StellarAttestationService, StellarAuthorityService, type StellarConfig, type StellarCreateSchemaResult, type StellarCustomSigner, StellarDataType, type StellarFetchAttestationResult, type StellarFetchAuthorityResult, type StellarFetchSchemaResult, type StellarRevokeAttestationConfig, type StellarSchemaConfig, type StellarSchemaDefinition, StellarSchemaEncoder, StellarSchemaRegistry, StellarSchemaService, index as common, StellarAttestProtocol as default };