Skip to content

Commit

Permalink
Merge pull request #131 from ERC725Alliance/develop
Browse files Browse the repository at this point in the history
Release v0.11.1
  • Loading branch information
Hugoo authored Apr 6, 2022
2 parents b050aeb + 73d0dce commit 269a540
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
docs/html
.vscode/*
.nyc_output
coverage
coverage
*.tgz
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions docs/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,14 +18,28 @@ 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:

```js
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);
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
decodeKey,
isDataAuthentic,
encodeData,
encodeKeyName,
} from './lib/utils';

import { getSchema } from './lib/schemaParser';
Expand Down Expand Up @@ -100,14 +101,36 @@ export class ERC725<Schema extends GenericSchema> {
};

this.options = {
schemas,
schemas: this.validateSchemas(schemas),
address,
provider: this.initializeProvider(provider),
...defaultConfig,
...config,
};
}

/**
* 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
Expand Down
55 changes: 35 additions & 20 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', '');
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 +
').',
);
}

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 269a540

Please sign in to comment.