dispatchToChain
dispatchToChain(
schema,authorAccount):Promise<SchemaId>
Dispatches a schema to the blockchain for storage, ensuring its uniqueness, immutability, and verifiability. This function encodes the schema, creates a blockchain transaction, and submits it using the author’s account for signing and submission.
Functionality:
Section titled “Functionality:”- Checks for existing schema: Verifies if the schema is already registered on the blockchain.
- Encodes schema in CBOR: Ensures schema data is serialized efficiently.
- Creates and signs the extrinsic: Uses the blockchain’s
createmethod for schema storage. - Transaction submission: Signs and submits the extrinsic to the blockchain using the provided author’s account.
Parameters:
Section titled “Parameters:”Parameters
Section titled “Parameters”• schema: ISchema
An ISchema object representing the structured data definition for the Cord network.
This object defines the schema’s structure and requirements.
• authorAccount: CordKeyringPair
A CordKeyringPair representing the blockchain account of the author,
used to sign and submit the schema transaction.
Returns:
Section titled “Returns:”Returns
Section titled “Returns”Promise<SchemaId>
A promise that resolves to the unique schema ID (SchemaId) upon successful storage.
If the schema is already stored, it returns the existing schema’s $id.
Throws:
Section titled “Throws:”Throws
Section titled “Throws”If an error occurs during the dispatch process, such as:
- Schema creation issues.
- Network connectivity problems.
- Transaction signing or submission failure.
Example Usage:
Section titled “Example Usage:”async function exampleSchemaDispatch() { const schema = { title: 'Example Schema', properties: { id: { type: 'string' } } }; const authorAccount = cord.createFromUri('//Alice'); // Example keyring pair
try { const schemaId = await dispatchToChain(schema, authorAccount); console.log('Schema dispatched with ID:', schemaId); } catch (error) { console.error('Error dispatching schema:', error); }}
exampleSchemaDispatch();