Skip to content

Commit

Permalink
feat: allow to load typed schemas from schemas/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Jan 22, 2024
1 parent 4c1eb57 commit c54a370
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
15 changes: 15 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import Web3 from 'web3';
import * as sinon from 'sinon';
import { hexToNumber, leftPad, numberToHex } from 'web3-utils';

// examples of schemas to load (for testing)
import { LSP1Schema, LSP12Schema, LSP3Schema, LSP6Schema } from './schemas';

import ERC725 from '.';
import {
decodeKeyValue,
Expand Down Expand Up @@ -68,6 +71,18 @@ describe('Running @erc725/erc725.js tests...', () => {
}, /Incorrect or unsupported provider/);
});

it('should allow importing the schemas and instantiating with them', async () => {
const schemasToLoad = [
...LSP1Schema,
...LSP12Schema,
...LSP3Schema,
...LSP6Schema,
];
const erc725 = new ERC725(schemasToLoad);

assert.deepStrictEqual(erc725.options.schemas, schemasToLoad);
});

it('should throw when calling getData without address & provider options set', async () => {
const erc725 = new ERC725(mockSchema);
try {
Expand Down
47 changes: 24 additions & 23 deletions src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { ERC725JSONSchema } from '../types/ERC725JSONSchema';

import LSP1UniversalReceiverDelegate from '../../schemas/LSP1UniversalReceiverDelegate.json';
import LSP3Profile from '../../schemas/LSP3ProfileMetadata.json';
import LSP4DigitalAssetLegacy from '../../schemas/LSP4DigitalAssetLegacy.json';
import LSP4DigitalAsset from '../../schemas/LSP4DigitalAsset.json';
import LSP5ReceivedAssets from '../../schemas/LSP5ReceivedAssets.json';
import LSP6KeyManager from '../../schemas/LSP6KeyManager.json';
import LSP8IdentifiableDigitalAsset from '../../schemas/LSP8IdentifiableDigitalAsset.json';
import LSP9Vault from '../../schemas/LSP9Vault.json';
import LSP10ReceivedVaults from '../../schemas/LSP10ReceivedVaults.json';
import LSP12IssuedAssets from '../../schemas/LSP12IssuedAssets.json';
import LSP17ContractExtension from '../../schemas/LSP17ContractExtension.json';
import LSP1JSONSchema from '../../schemas/LSP1UniversalReceiverDelegate.json';
import LSP3JSONSchema from '../../schemas/LSP3ProfileMetadata.json';
import LSP4JSONSchema from '../../schemas/LSP4DigitalAsset.json';
import LSP4LegacyJSONSchema from '../../schemas/LSP4DigitalAssetLegacy.json';
import LSP5JSONSchema from '../../schemas/LSP5ReceivedAssets.json';
import LSP6JSONSchema from '../../schemas/LSP6KeyManager.json';
import LSP8JSONSchema from '../../schemas/LSP8IdentifiableDigitalAsset.json';
import LSP9JSONSchema from '../../schemas/LSP9Vault.json';
import LSP10JSONSchema from '../../schemas/LSP10ReceivedVaults.json';
import LSP12JSONSchema from '../../schemas/LSP12IssuedAssets.json';
import LSP17JSONSchema from '../../schemas/LSP17ContractExtension.json';

export default LSP1UniversalReceiverDelegate.concat(
LSP3Profile,
LSP4DigitalAssetLegacy,
LSP4DigitalAsset,
LSP5ReceivedAssets,
LSP6KeyManager,
LSP8IdentifiableDigitalAsset,
LSP9Vault,
LSP10ReceivedVaults,
LSP12IssuedAssets,
LSP17ContractExtension,
) as ERC725JSONSchema[];
type schemaType = ERC725JSONSchema[];

export const LSP1Schema: schemaType = LSP1JSONSchema as schemaType;
export const LSP3Schema: schemaType = LSP3JSONSchema as schemaType;
export const LSP4Schema: schemaType = LSP4JSONSchema as schemaType;
export const LSP4LegacySchema: schemaType = LSP4LegacyJSONSchema as schemaType;
export const LSP5Schema: schemaType = LSP5JSONSchema as schemaType;
export const LSP6Schema: schemaType = LSP6JSONSchema as schemaType;
export const LSP8Schema: schemaType = LSP8JSONSchema as schemaType;
export const LSP9Schema: schemaType = LSP9JSONSchema as schemaType;
export const LSP10Schema: schemaType = LSP10JSONSchema as schemaType;
export const LSP12Schema: schemaType = LSP12JSONSchema as schemaType;
export const LSP17Schema: schemaType = LSP17JSONSchema as schemaType;
2 changes: 1 addition & 1 deletion src/types/ERC725JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export function isValidValueType(
export interface ERC725JSONSchema {
name: string; // Describes the name of the key, SHOULD compromise of the Standards name + sub type. e.g: LSP2Name
key: string; // The keccak256 hash of the name. This is the actual key that MUST be retrievable via ERC725Y.getData(bytes32 key)
keyType: ERC725JSONSchemaKeyType; // Types that determine how the values should be interpreted.
keyType: ERC725JSONSchemaKeyType | string; // Types that determine how the values should be interpreted.
valueContent: ERC725JSONSchemaValueContent | string; // string holds '0x1345ABCD...' If the value content are specific bytes, than the returned value is expected to equal those bytes.
valueType: ERC725JSONSchemaValueType | string; // The type of the value. This is used to determine how the value should be encoded / decode (`string` for tuples and CompactBytesArray).
}

0 comments on commit c54a370

Please sign in to comment.