Schemas

Define structure and validation rules for attestations

Schemas

A schema defines the structure and validation rules for credentials in the Solana Attestation System. Schemas serve as templates that specify what fields a credential should contain and how they should be formatted. Each schema is associated with a credential type and can be versioned to support evolution of the data structure.

Structure

The Schema struct represents a schema in the Solana Attestation System. Each schema defines a template for credentials that can be created under it.

Type Definitions

Schema

export type Schema = {
    discriminator: number;           // Internal discriminator
    credential: Address;             // Associated credential address
    name: ReadonlyUint8Array;       // Schema name
    description: ReadonlyUint8Array; // Schema description
    layout: ReadonlyUint8Array;     // Schema layout definition
    fieldNames: ReadonlyUint8Array; // Names of fields in the schema
    isPaused: boolean;              // Pause status
    version: number;                // Schema version
};

Methods

Fetching Schemas

MethodDescriptionParametersReturns
fetchSchemaFetches a single schema by its addressrpc: RPC context, address: Schema's address, config?: Fetch configPromise<Account<Schema>>
fetchMaybeSchemaSafely fetches a schema, returns null if not foundrpc: RPC context, address: Schema's address, config?: Fetch configPromise<MaybeAccount<Schema>>
fetchAllSchemaFetches multiple schemas by their addressesrpc: RPC context, addresses: Array of schema addresses, config?: Fetch configPromise<Account<Schema>[]>
fetchAllMaybeSchemaSafely fetches multiple schemas, skips not foundrpc: RPC context, addresses: Array of schema addresses, config?: Fetch configPromise<MaybeAccount<Schema>[]>

Serialization

MethodDescriptionParametersReturns
getSchemaEncoderGets the encoder for schema dataNoneEncoder<SchemaArgs>
getSchemaDecoderGets the decoder for schema dataNoneDecoder<Schema>
getSchemaCodecGets the codec for schema dataNoneCodec<SchemaArgs, Schema>

Usage Examples

Fetching a Single Schema

const schema = await fetchSchema(rpc, schemaAddress);
console.log('Schema name:', schema.name);

Fetching Multiple Schemas

const schemas = await fetchAllSchema(rpc, [schema1Address, schema2Address]);
schemas.forEach(schema => console.log('Schema:', schema.name));

Safe Fetching

const schema = await fetchMaybeSchema(rpc, schemaAddress);
if (schema) {
    console.log('Schema found:', schema.name);
} else {
    console.log('Schema not found');
}

Schema Layout Data Types

The layout field uses numeric values to specify data types for each field in the schema:

ValueData TypeDescription
0U8Unsigned 8-bit integer
1U16Unsigned 16-bit integer
2U32Unsigned 32-bit integer
3U64Unsigned 64-bit integer
4U128Unsigned 128-bit integer
5I8Signed 8-bit integer
6I16Signed 16-bit integer
7I32Signed 32-bit integer
8I64Signed 64-bit integer
9I128Signed 128-bit integer
10BoolBoolean value
11CharSingle character
12StringVariable-length string
13VecU8Vector of unsigned 8-bit integers
14VecU16Vector of unsigned 16-bit integers
15VecU32Vector of unsigned 32-bit integers
16VecU64Vector of unsigned 64-bit integers
17VecU128Vector of unsigned 128-bit integers
18VecI8Vector of signed 8-bit integers
19VecI16Vector of signed 16-bit integers
20VecI32Vector of signed 32-bit integers
21VecI64Vector of signed 64-bit integers
22VecI128Vector of signed 128-bit integers
23VecBoolVector of boolean values
24VecCharVector of characters
25VecStringVector of strings

Example Usage

For example, a layout of [12, 0, 12] would define three fields: a String, followed by a U8 integer, followed by another String. The fieldNames array provides human-readable names that correspond positionally to each layout value, so with field names ["name", "age", "country"], the first String field would be named "name", the U8 field would be "age", and the second String field would be "country".

Here's an example of how those would be utilized when creating a Schema:

const createSchemaInstruction = getCreateSchemaInstruction({
    authority,
    payer,
    credential: credentialPda,
    schema: schemaPda,
    name: "Test Schema",
    description: "Just an example",
    fieldNames: ["name", "age", "country"],
    layout: Buffer.from([12, 0, 12]),
});

Important Notes

  • The discriminator field is used internally and should not be modified
  • The credential field links the schema to its associated credential type
  • isPaused can be used to temporarily disable schema usage
  • The version field allows for schema evolution while maintaining backward compatibility
  • layout and fieldNames define the structure of credentials using this schema
  • All byte array fields (name, description, layout, fieldNames) should be properly encoded/decoded according to your application's needs