Skip to content

Commit

Permalink
Merge pull request #130 from ERC725Alliance/fix-build
Browse files Browse the repository at this point in the history
Fix npm pack and publish + bump to 0.11.1
  • Loading branch information
Hugoo authored Apr 6, 2022
2 parents 139d0ac + 2f7f0f5 commit 73d0dce
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 28 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
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 73d0dce

Please sign in to comment.