diff --git a/.gitignore b/.gitignore index 058154db..267e82ff 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ node_modules docs/html .vscode/* .nyc_output -coverage \ No newline at end of file +coverage +*.tgz \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ca013fab..c907f40e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.11.1](https://github.com/ERC725Alliance/erc725.js/compare/v0.11.0...v0.11.1) (2022-04-06) + +This version fix the npm pack error. + +### Bug Fixes + +- do not load wrong schemas ([66dc3e6](https://github.com/ERC725Alliance/erc725.js/commit/66dc3e648ad1a9aeabe66e5ae2aeb15cf3f74775)) + ## [0.11.0](https://github.com/ERC725Alliance/erc725.js/compare/v0.10.0...v0.11.0) (2022-04-05) ### ⚠ BREAKING CHANGES diff --git a/docs/schemas.md b/docs/schemas.md index 776455f8..31951e31 100644 --- a/docs/schemas.md +++ b/docs/schemas.md @@ -4,12 +4,9 @@ sidebar_position: 1.2 # Schemas -The `@erc725/erc725.js` library supports a range of ERC725 specification schemas. +The `@erc725/erc725.js` library contains a range standard [LSP ERC725 JSON schemas](https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-2-ERC725YJSONSchema.md). -The below are the schema element definitions supported by and tested -with `@erc725/erc725.js`. There are certainly more possibilities, and even several -nonsensical or redundant possibilities which will not or may not be -supported. +Schemas allow erc725.js to know how to decode and encode data written in an [ERC725Y](https://eips.ethereum.org/EIPS/eip-725) smart contract. _Quick reference for keys used in schema definitions below see_ [official @@ -21,9 +18,20 @@ documentation](https://github.com/lukso-network/LIPs/blob/master/LSPs/LSP-2-ERC7 - `valueContent`: The described content type for parsing - `valueType`: The type of the content data in store for decoding -## LSP Schemas +## Standard LSP Schemas -The most common schemas from LUKSO [LSPs](https://github.com/lukso-network/LIPs/tree/main/LSPs) are available in the [`schemas/`](https://github.com/ERC725Alliance/erc725.js/tree/develop/schemas) folder. +The most common schemas of [LUKSO Standard Proposals](https://github.com/lukso-network/LIPs/tree/main/LSPs) are available under the [`schemas/`](https://github.com/ERC725Alliance/erc725.js/tree/develop/schemas) folder. + +Current provided LSPs are: + +``` +LSP1UniversalReceiverDelegate.json +LSP3UniversalProfileMetadata.json +LSP4DigitalAsset.json +LSP4DigitalAssetLegacy.json +LSP5ReceivedAssets.json +LSP6KeyManager.json +``` You can import them from: @@ -31,4 +39,7 @@ You can import them from: import LSP3 from '@erc725/erc725.js/schemas/LSP3UniversalProfile.json'; import LSP5 from '@erc725/erc725.js/schemas/LSP5ReceivedAssets.json'; // ... + +// Later use them on instantiation +const myErc725Contract = new ERC725js(LSP3, address, web3.currentProvider); ``` diff --git a/package-lock.json b/package-lock.json index 672fd2d9..f99572f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@erc725/erc725.js", - "version": "0.11.0", + "version": "0.11.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@erc725/erc725.js", - "version": "0.11.0", + "version": "0.11.1", "license": "Apache-2.0", "dependencies": { "web3-eth-abi": "^1.5.2", diff --git a/package.json b/package.json index e958b4b6..b3df1256 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,15 @@ { "name": "@erc725/erc725.js", - "version": "0.11.0", + "version": "0.11.1", "description": "Library to interact with ERC725 smart contracts", - "main": "build/main/index.js", - "typings": "build/main/index.d.ts", - "module": "build/module/index.js", + "main": "build/main/src/index.js", + "typings": "build/main/src/index.d.ts", + "module": "build/module/src/index.js", + "files": [ + "build", + "schemas", + "docs" + ], "scripts": { "build": "run-p build:*", "build:main": "tsc -p tsconfig.json", diff --git a/src/index.ts b/src/index.ts index 4074371a..9225e93f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,7 @@ import { decodeKey, isDataAuthentic, encodeData, + encodeKeyName, } from './lib/utils'; import { getSchema } from './lib/schemaParser'; @@ -100,7 +101,7 @@ export class ERC725 { }; this.options = { - schemas, + schemas: this.validateSchemas(schemas), address, provider: this.initializeProvider(provider), ...defaultConfig, @@ -108,6 +109,28 @@ export class ERC725 { }; } + /** + * To prevent weird behovior from the lib, we must make sure all the schemas are correct before loading them. + * + * @param schemas + * @returns + */ + // eslint-disable-next-line class-methods-use-this + private validateSchemas(schemas: ERC725JSONSchema[]) { + return schemas.filter((schema) => { + const encodedKeyName = encodeKeyName(schema.name); + const isKeyValid = schema.key === encodedKeyName; + + if (!isKeyValid) { + console.log( + `The schema with keyName: ${schema.key} is skipped because its key hash does not match its key name (expected: ${encodedKeyName}, got: ${schema.key}).`, + ); + } + + return isKeyValid; + }); + } + // eslint-disable-next-line class-methods-use-this private initializeProvider(providerOrProviderWrapper) { // do not fail on no-provider diff --git a/src/lib/utils.ts b/src/lib/utils.ts index c5ac00f2..c7748324 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -123,7 +123,7 @@ export function encodeKeyValue( * @return The raw bytes key for the array element */ export function encodeArrayKey(key: string, index: number) { - return key.substr(0, 34) + padLeft(numberToHex(index), 32).replace('0x', ''); + return key.slice(0, 34) + padLeft(numberToHex(index), 32).replace('0x', ''); } /** @@ -160,9 +160,10 @@ export function guessKeyTypeFromKeyName( /** * - * @param name the schema element name - * @return the name of the key encoded as per specifications - * @return a string of the encoded schema name + * @param name the schema element name. + * @return the name of the key encoded as per specifications. + * + * @return a string of the encoded schema name. */ export function encodeKeyName(name: string) { const keyType = guessKeyTypeFromKeyName(name); @@ -208,16 +209,27 @@ export function encodeKeyName(name: string) { /** * - * @param schemas An array of ERC725JSONSchema objects - * @param {string} key A string of either the schema element name, or key - * @return The requested schema element from the full array of schemas + * @param schemas An array of ERC725JSONSchema objects. + * @param {string} keyOrKeyName A string of either the schema element name, or key. + * + * @return The requested schema element from the full array of schemas. */ -export function getSchemaElement(schemas: ERC725JSONSchema[], key: string) { - const keyHash = key.substr(0, 2) === '0x' ? key : encodeKeyName(key); +export function getSchemaElement( + schemas: ERC725JSONSchema[], + keyOrKeyName: string, +) { + const keyHash = + keyOrKeyName.slice(0, 2) === '0x' + ? keyOrKeyName + : encodeKeyName(keyOrKeyName); const schemaElement = schemas.find((e) => e.key === keyHash); if (!schemaElement) { throw new Error( - 'No matching schema found for key: "' + key + '" (' + keyHash + ').', + 'No matching schema found for key: "' + + keyOrKeyName + + '" (' + + keyHash + + ').', ); } @@ -226,9 +238,10 @@ export function getSchemaElement(schemas: ERC725JSONSchema[], key: string) { /** * - * @param schema is an object of a schema definitions - * @param value will be either key-value pairs for a key type of Array, or a single value for type Singleton - * @return the encoded value for the key as per the supplied schema + * @param schema is an object of a schema definitions. + * @param value will be either key-value pairs for a key type of Array, or a single value for type Singleton. + * + * @return the encoded value for the key as per the supplied schema. */ export function encodeKey( schema: ERC725JSONSchema, @@ -297,11 +310,12 @@ export function encodeKey( /** * - * @param {string} valueContent as per ERC725Schema definition - * @param {string} valueType as per ERC725Schema definition - * @param {string} value the encoded value as string + * @param {string} valueContent as per ERC725Schema definition. + * @param {string} valueType as per ERC725Schema definition. + * @param {string} value the encoded value as string. * @param {string} [name] - * @return the decoded value as per the schema + * + * @return the decoded value as per the schema. */ export function decodeKeyValue( valueContent: string, @@ -364,9 +378,10 @@ export function decodeKeyValue( /** * - * @param schema is an object of a schema definitions - * @param value will be either key-value pairs for a key type of Array, or a single value for type Singleton - * @return the decoded value/values as per the schema definition + * @param schema is an object of a schema definitions. + * @param value will be either key-value pairs for a key type of Array, or a single value for type Singleton. + * + * @return the decoded value/values as per the schema definition. */ export function decodeKey(schema: ERC725JSONSchema, value) { const lowerCaseKeyType = schema.keyType.toLowerCase();