{"version":3,"file":"module.cjs","sources":["../../../../../src/modules/abstract/schema/module.js"],"sourcesContent":["import { canonicalize } from 'json-canonicalize';\nimport { validate } from 'jsonschema';\n\n// Supported schemas\nimport JSONSchema07 from '../../../vc/schemas/schema-draft-07';\nimport jsonFetch from '../../../utils/json-fetch';\nimport { Blob, BlobWithId } from '../../../types';\nimport { ensureInstanceOf } from '../../../utils';\nimport AbstractBlobModule from '../blob/module';\n\nexport default class Schema {\n  /**\n   * Creates a new `Schema` object\n   * @constructor\n   * @param {string} [id] - schema ID\n   */\n  constructor(id) {\n    this.id = id;\n  }\n\n  static fromJSON(json) {\n    const { id, schema } = json;\n\n    const schemaObj = new Schema(id);\n\n    if (schema) {\n      schemaObj.schema = schema;\n    }\n\n    return schemaObj;\n  }\n\n  /**\n   * Add the JSON schema to this object after checking that `json` is a valid JSON schema. Check if JSON is valid.\n   * @param {object} json - the schema JSON\n   */\n  async setJSONSchema(json) {\n    await Schema.validateSchema(json);\n    this.schema = json;\n    return this;\n  }\n\n  /**\n   * Serializes the object using `getSerializedBlob` and then signs it using the given\n   * polkadot-js pair. The object will be updated with key `signature`. Repeatedly calling it will\n   * keep resetting the `signature` key\n   * @param {object} pair - Key pair to sign with\n   * @param blobModule\n   */\n  sign(pair) {\n    this.signature = pair.sign(this.toBlob().blob.bytes);\n    return this;\n  }\n\n  /**\n   * Serializes schema object to JSON\n   * @returns {object}\n   */\n  toJSON() {\n    const { signature: _signature, ...rest } = this;\n\n    return rest;\n  }\n\n  /**\n   * Serializes the schema to a blob object to send to the node\n   * @returns {object}\n   */\n  toBlob() {\n    if (!this.schema) {\n      throw new Error(\n        'Schema requires schema property to be serialized to blob',\n      );\n    }\n\n    return new BlobWithId(this.id, new Blob(canonicalize(this.schema)));\n  }\n\n  /**\n   * Prepares a transaction to write this schema object to the dock chain using the blob module\n   * @param {AbstractBlobModule} blobModule - The dock API\n   * @param targetDid\n   * @param keyPair\n   * @param params\n   * @return {Promise<object>} The extrinsic to sign and send.\n   */\n  async writeToChain(blobModule, keyPair, params) {\n    return await ensureInstanceOf(blobModule, AbstractBlobModule).new(\n      this.toBlob(),\n      keyPair,\n      params,\n    );\n  }\n\n  /**\n   * Check that the given JSON schema is compliant with JSON schema spec mentioned in RFC\n   * @param {object} json - The JSON schema to validate\n   * @returns {Promise<object>} - Returns promise to an object or throws error\n   */\n  static async validateSchema(json) {\n    // Get the JSON schema spec to check against.\n    const jsonSchemaSpec = await this.getJSONSchemaSpec(json);\n    return validate(json, jsonSchemaSpec, {\n      throwError: true,\n    });\n  }\n\n  /**\n   * Get schema from the chain using the given id, by querying the blob storage.\n   * Accepts a full blob id like blob:dock:0x... or just the hex identifier and the `DockAPI` object.\n   * The returned schema would be formatted as specified in the RFC (including author DID, schema id) or an error is\n   * returned if schema is not found on the chain or in JSON format.\n   * @param {string} id - The Schema ID\n   * @param {AbstractBlobModule} blobModule\n   * @returns {Promise<object>}\n   */\n  static async get(id, blobModule) {\n    const [author, chainBlob] = await blobModule.get(id);\n\n    return {\n      ...chainBlob.toObject(),\n      id: String(id),\n      author: String(author),\n    };\n  }\n\n  /**\n   * Gets the JSON schema spec from given JSON. Will either return the stored JSON schema or get\n   * it using HTTP or will throw error if cannot get.\n   * @param {object} json\n   * @returns {Promise<object>}\n   */\n  static async getJSONSchemaSpec(json) {\n    const schemaKey = '$schema';\n    const schemaUrl = json[schemaKey];\n    if (schemaUrl) {\n      // The URL might be 'http://json-schema.org/draft-07/schema' or 'http://json-schema.org/draft-07/schema#'\n      // In that case, the schema is already stored in the SDK as this is the latest JSON schema spec\n      if (\n        schemaUrl === 'http://json-schema.org/draft-07/schema'\n        || schemaUrl === 'http://json-schema.org/draft-07/schema#'\n      ) {\n        // Return stored JSON schema\n        return JSONSchema07;\n      }\n      // Fetch the URI and expect a JSON response\n      const doc = await jsonFetch(schemaUrl);\n      if (typeof doc === 'object') {\n        return doc;\n      }\n      // If MIME type did not indicate JSON, try to parse the response as JSON\n      try {\n        return JSON.parse(doc);\n      } catch (e) {\n        throw new Error('Cannot parse response as JSON');\n      }\n    } else {\n      throw new Error(`${schemaKey} not found in the given JSON`);\n    }\n  }\n}\n"],"names":["BlobWithId","Blob","canonicalize","ensureInstanceOf","AbstractBlobModule","validate","JSONSchema07","jsonFetch"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUe,MAAM,MAAM,CAAC;AAC5B;AACA;AACA;AACA;AACA;AACA,EAAE,WAAW,CAAC,EAAE,EAAE;AAClB,IAAI,IAAI,CAAC,EAAE,GAAG,EAAE;AAChB,EAAE;;AAEF,EAAE,OAAO,QAAQ,CAAC,IAAI,EAAE;AACxB,IAAI,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI;;AAE/B,IAAI,MAAM,SAAS,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC;;AAEpC,IAAI,IAAI,MAAM,EAAE;AAChB,MAAM,SAAS,CAAC,MAAM,GAAG,MAAM;AAC/B,IAAI;;AAEJ,IAAI,OAAO,SAAS;AACpB,EAAE;;AAEF;AACA;AACA;AACA;AACA,EAAE,MAAM,aAAa,CAAC,IAAI,EAAE;AAC5B,IAAI,MAAM,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC;AACrC,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI;AACtB,IAAI,OAAO,IAAI;AACf,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,IAAI,CAAC,IAAI,EAAE;AACb,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC;AACxD,IAAI,OAAO,IAAI;AACf,EAAE;;AAEF;AACA;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