Skip to content

Commit

Permalink
fix: Refactor to remove unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Apr 6, 2024
1 parent 496cacf commit 24bda3a
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 251 deletions.
6 changes: 6 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"style": {
"useImportType": "off",
"useNodejsImportProtocol": "off"
},
"complexity": {
"noForEach": "off"
}
}
},
Expand All @@ -23,6 +26,9 @@
"trailingComma": "all"
}
},
"organizeImports": {
"enabled": false
},
"files": {
"ignore": ["node_modules", "build", "coverage", ".vscode"]
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/instantiation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ERC725 } from '@erc725/erc725.js';
import Web3 from 'web3';
import { ERC725 } from '@erc725/erc725.js';

// this is needed because node does not support `fetch` out of the box
// isomorphic-fetch is not needed in a browser environment
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@erc725/erc725.js",
"version": "0.24.1-dev.4",
"version": "0.24.1-dev.5",
"description": "Library to interact with ERC725 smart contracts",
"main": "build/main/src/index.js",
"typings": "build/main/src/index.d.ts",
Expand Down
75 changes: 28 additions & 47 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
along with @erc725/erc725.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { arrToBufArr, bufferToHex } from 'ethereumjs-util';
/* eslint-disable @typescript-eslint/ban-types */
import { keccak256, numberToHex } from 'web3-utils';
import { numberToHex, keccak256 } from 'web3-utils';
import { arrToBufArr, bufferToHex } from 'ethereumjs-util';

import { Encoding, Method, MethodData } from '../types/Method';
import { MethodData, Encoding, Method } from '../types/Method';

// https://github.com/ERC725Alliance/ERC725/blob/develop/docs/ERC-725.md#specification
export const ERC725Y_INTERFACE_IDS = {
Expand Down Expand Up @@ -156,50 +156,31 @@ export const HASH_METHODS: {
// TODO: These values can be imported from lsp-smartcontracts lib after release
// prettier-ignore
export const LSP6_DEFAULT_PERMISSIONS = {
CHANGEOWNER:
'0x0000000000000000000000000000000000000000000000000000000000000001',
ADDCONTROLLER:
'0x0000000000000000000000000000000000000000000000000000000000000002',
EDITPERMISSIONS:
'0x0000000000000000000000000000000000000000000000000000000000000004',
ADDEXTENSIONS:
'0x0000000000000000000000000000000000000000000000000000000000000008',
CHANGEEXTENSIONS:
'0x0000000000000000000000000000000000000000000000000000000000000010',
ADDUNIVERSALRECEIVERDELEGATE:
'0x0000000000000000000000000000000000000000000000000000000000000020',
CHANGEUNIVERSALRECEIVERDELEGATE:
'0x0000000000000000000000000000000000000000000000000000000000000040',
REENTRANCY:
'0x0000000000000000000000000000000000000000000000000000000000000080',
SUPER_TRANSFERVALUE:
'0x0000000000000000000000000000000000000000000000000000000000000100',
TRANSFERVALUE:
'0x0000000000000000000000000000000000000000000000000000000000000200',
SUPER_CALL:
'0x0000000000000000000000000000000000000000000000000000000000000400',
CALL: '0x0000000000000000000000000000000000000000000000000000000000000800',
SUPER_STATICCALL:
'0x0000000000000000000000000000000000000000000000000000000000001000',
STATICCALL:
'0x0000000000000000000000000000000000000000000000000000000000002000',
SUPER_DELEGATECALL:
'0x0000000000000000000000000000000000000000000000000000000000004000',
DELEGATECALL:
'0x0000000000000000000000000000000000000000000000000000000000008000',
DEPLOY: '0x0000000000000000000000000000000000000000000000000000000000010000',
SUPER_SETDATA:
'0x0000000000000000000000000000000000000000000000000000000000020000',
SETDATA: '0x0000000000000000000000000000000000000000000000000000000000040000',
ENCRYPT: '0x0000000000000000000000000000000000000000000000000000000000080000',
DECRYPT: '0x0000000000000000000000000000000000000000000000000000000000100000',
SIGN: '0x0000000000000000000000000000000000000000000000000000000000200000',
EXECUTE_RELAY_CALL:
'0x0000000000000000000000000000000000000000000000000000000000400000',
ERC4337_PERMISSION:
'0x0000000000000000000000000000000000000000000000000000000000800000',
ALL_PERMISSIONS:
'0x00000000000000000000000000000000000000000000000000000000007f3f7f', // lsp6 v0.14.0
CHANGEOWNER : "0x0000000000000000000000000000000000000000000000000000000000000001",
ADDCONTROLLER : "0x0000000000000000000000000000000000000000000000000000000000000002",
EDITPERMISSIONS : "0x0000000000000000000000000000000000000000000000000000000000000004",
ADDEXTENSIONS : "0x0000000000000000000000000000000000000000000000000000000000000008",
CHANGEEXTENSIONS : "0x0000000000000000000000000000000000000000000000000000000000000010",
ADDUNIVERSALRECEIVERDELEGATE : "0x0000000000000000000000000000000000000000000000000000000000000020",
CHANGEUNIVERSALRECEIVERDELEGATE : "0x0000000000000000000000000000000000000000000000000000000000000040",
REENTRANCY : "0x0000000000000000000000000000000000000000000000000000000000000080",
SUPER_TRANSFERVALUE : "0x0000000000000000000000000000000000000000000000000000000000000100",
TRANSFERVALUE : "0x0000000000000000000000000000000000000000000000000000000000000200",
SUPER_CALL : "0x0000000000000000000000000000000000000000000000000000000000000400",
CALL : "0x0000000000000000000000000000000000000000000000000000000000000800",
SUPER_STATICCALL : "0x0000000000000000000000000000000000000000000000000000000000001000",
STATICCALL : "0x0000000000000000000000000000000000000000000000000000000000002000",
SUPER_DELEGATECALL : "0x0000000000000000000000000000000000000000000000000000000000004000",
DELEGATECALL : "0x0000000000000000000000000000000000000000000000000000000000008000",
DEPLOY : "0x0000000000000000000000000000000000000000000000000000000000010000",
SUPER_SETDATA : "0x0000000000000000000000000000000000000000000000000000000000020000",
SETDATA : "0x0000000000000000000000000000000000000000000000000000000000040000",
ENCRYPT : "0x0000000000000000000000000000000000000000000000000000000000080000",
DECRYPT : "0x0000000000000000000000000000000000000000000000000000000000100000",
SIGN : "0x0000000000000000000000000000000000000000000000000000000000200000",
EXECUTE_RELAY_CALL : "0x0000000000000000000000000000000000000000000000000000000000400000",
ERC4337_PERMISSION : "0x0000000000000000000000000000000000000000000000000000000000800000",
ALL_PERMISSIONS : "0x00000000000000000000000000000000000000000000000000000000007f3f7f" // lsp6 v0.14.0
};

export const LSP6_ALL_PERMISSIONS =
Expand Down
65 changes: 27 additions & 38 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,35 @@
// Tests for the @erc725/erc725.js package
import { assert } from 'chai';

import * as sinon from 'sinon';
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, LSP3Schema, LSP6Schema, LSP12Schema } from './schemas';
import { LSP1Schema, LSP12Schema, LSP3Schema, LSP6Schema } from './schemas';

import ERC725, {
checkPermissions,
decodeMappingKey,
decodePermissions,
encodeKeyName,
encodePermissions,
supportsInterface,
} from '.';
import { EthereumProvider, HttpProvider } from '../test/mockProviders';
import { mockSchema } from '../test/mockSchema';
import {
generateAllData,
generateAllRawData,
generateAllResults,
} from '../test/testHelpers';
import {
decodeKeyValue,
encodeKey,
encodeKeyValue,
hashData,
} from './lib/utils';
import { ERC725JSONSchema } from './types/ERC725JSONSchema';
import { EthereumProvider, HttpProvider } from '../test/mockProviders';
import { mockSchema } from '../test/mockSchema';
import {
generateAllData,
generateAllRawData,
generateAllResults,
} from '../test/testHelpers';

import 'isomorphic-fetch';

Expand All @@ -56,8 +57,8 @@ import {
LSP6_DEFAULT_PERMISSIONS,
SUPPORTED_VERIFICATION_METHOD_STRINGS,
} from './constants/constants';
import { INTERFACE_IDS_0_12_0 } from './constants/interfaces';
import { decodeKey } from './lib/decodeData';
import { INTERFACE_IDS_0_12_0 } from './constants/interfaces';

const address = '0x0c03fba782b07bcf810deb3b7f0595024a444f4e';

Expand Down Expand Up @@ -615,13 +616,10 @@ describe('Running @erc725/erc725.js tests...', () => {
});
});

const contractVersions = [
[
{ name: 'legacy', interface: ERC725Y_INTERFACE_IDS.legacy },
{ name: 'latest', interface: ERC725Y_INTERFACE_IDS['3.0'] },
];
for (let i = 0; i < contractVersions.length; i++) {
const contractVersion = contractVersions[i];

].forEach((contractVersion) => {
describe(`Getting all data in schema by provider [ERC725Y ${contractVersion.name}][mock]`, () => {
// Construct the full data and results
const fullResults = generateAllResults(mockSchema);
Expand Down Expand Up @@ -843,12 +841,10 @@ describe('Running @erc725/erc725.js tests...', () => {
});
}
});
}
});

describe('Getting data by schema element by provider', () => {
for (let index = 0; index < mockSchema.length; index++) {
const schemaElement = mockSchema[index];

mockSchema.forEach((schemaElement) => {
it(`${schemaElement.name} with web3.currentProvider`, async () => {
const returnRawData = generateAllRawData([schemaElement], false);
const provider = new HttpProvider({ returnData: returnRawData }, [
Expand Down Expand Up @@ -890,7 +886,7 @@ describe('Running @erc725/erc725.js tests...', () => {
value: schemaElement.expectedResult,
});
});
}
});
});

describe('Testing utility encoding & decoding functions', () => {
Expand Down Expand Up @@ -999,11 +995,10 @@ describe('Running @erc725/erc725.js tests...', () => {
values: [],
};

for (let i = 0; i < keyValuePairs.length; i++) {
const { key, value } = keyValuePairs[i];
keyValuePairs.forEach(({ key, value }) => {
intendedResult.keys.push(key);
intendedResult.values.push(value);
}
});

const erc725 = new ERC725([schemaElement]);

Expand Down Expand Up @@ -1403,8 +1398,7 @@ describe('Running @erc725/erc725.js tests...', () => {
const erc725Instance = new ERC725([]);

describe('encodePermissions', () => {
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
testCases.forEach((testCase) => {
it(`Encodes ${testCase.hex} permission correctly`, () => {
assert.deepStrictEqual(
encodePermissions(testCase.permissions),
Expand All @@ -1415,7 +1409,7 @@ describe('Running @erc725/erc725.js tests...', () => {
testCase.hex,
);
});
}
});

it('Defaults permissions to false if not passed', () => {
assert.deepStrictEqual(
Expand All @@ -1438,13 +1432,11 @@ describe('Running @erc725/erc725.js tests...', () => {
describe('Randomized Permissions Encoding', () => {
function convertToPermissionBits(permissions: { [key: string]: string }) {
const permissionBits = {};
const entries = Object.entries(permissions);
for (let i = 0; i < entries.length; i++) {
const [key, hexValue] = entries[i];
Object.entries(permissions).forEach(([key, hexValue]) => {
// Convert hex to binary, then find the position of the '1' bit
const bitPosition = BigInt(hexValue).toString(2).length - 1;
permissionBits[key] = bitPosition;
}
});
return permissionBits;
}

Expand Down Expand Up @@ -1475,14 +1467,12 @@ describe('Running @erc725/erc725.js tests...', () => {
// Function to calculate expected hex based on permissions
const calculateExpectedHex = (permissions) => {
let basePermissions = BigInt(0);
const entries = Object.entries(permissions);
for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];
Object.entries(permissions).forEach(([key, value]) => {
if (value) {
const bitPosition = permissionBits[key];
basePermissions |= BigInt(1) << BigInt(bitPosition);
}
}
});
// Convert to hex string, properly padded
return `0x${basePermissions.toString(16).padStart(64, '0')}`;
};
Expand Down Expand Up @@ -1593,8 +1583,7 @@ describe('Running @erc725/erc725.js tests...', () => {
});

describe('decodePermissions', () => {
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
testCases.forEach((testCase) => {
it(`Decodes ${testCase.hex} permission correctly`, () => {
assert.deepStrictEqual(
decodePermissions(testCase.hex),
Expand All @@ -1606,7 +1595,7 @@ describe('Running @erc725/erc725.js tests...', () => {
testCase.permissions,
);
});
}
});
it('Decodes 0xfff...fff admin permission correctly', () => {
assert.deepStrictEqual(
decodePermissions(
Expand Down Expand Up @@ -1821,7 +1810,7 @@ describe('decodeMappingKey', () => {

it('is available on instance and class', () => {
assert.deepStrictEqual(
ERC725.decodeMappingKey(
decodeMappingKey(
'0x35e6950bc8d21a1699e50000cafecafecafecafecafecafecafecafecafecafe',
'MyKeyName:<address>',
),
Expand Down
30 changes: 15 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,48 @@
* @date 2020
*/

import HttpProvider from 'web3-providers-http';
import { isAddress } from 'web3-utils';
import HttpProvider from 'web3-providers-http';

import { ProviderWrapper } from './provider/providerWrapper';

import {
convertIPFSGatewayUrl,
duplicateMultiTypeERC725SchemaEntry,
encodeData,
convertIPFSGatewayUrl,
generateSchemasFromDynamicKeys,
duplicateMultiTypeERC725SchemaEntry,
} from './lib/utils';

import { isValidSignature } from './lib/isValidSignature';
import { getSchema } from './lib/schemaParser';
import { isValidSignature } from './lib/isValidSignature';

import { DEFAULT_GAS_VALUE } from './constants/constants';
import { encodeKeyName, isDynamicKeyName } from './lib/encodeKeyName';

import { decodeData } from './lib/decodeData';
import { decodeMappingKey } from './lib/decodeMappingKey';
import { _supportsInterface, checkPermissions } from './lib/detector';
import { decodeValueType, encodeValueType } from './lib/encoder';
import { getData } from './lib/getData';
import { getDataFromExternalSources } from './lib/getDataFromExternalSources';
import { decodePermissions, encodePermissions } from './lib/permissions';
// Types
import { ERC725Config, ERC725Options } from './types/Config';
import { Permissions } from './types/Method';
import {
ERC725JSONSchema,
ERC725JSONSchemaKeyType,
ERC725JSONSchemaValueContent,
ERC725JSONSchemaValueType,
} from './types/ERC725JSONSchema';
import { GetDataDynamicKey, GetDataInput } from './types/GetData';
import { Permissions } from './types/Method';
import {
DecodeDataInput,
DecodeDataOutput,
EncodeDataInput,
FetchDataOutput,
} from './types/decodeData';
import { GetDataDynamicKey, GetDataInput } from './types/GetData';
import { decodeData } from './lib/decodeData';
import { getDataFromExternalSources } from './lib/getDataFromExternalSources';
import { DynamicKeyPart, DynamicKeyParts } from './types/dynamicKeys';
import { getData } from './lib/getData';
import { decodeValueType, encodeValueType } from './lib/encoder';
import { checkPermissions, internalSupportsInterface } from './lib/detector';
import { decodeMappingKey } from './lib/decodeMappingKey';
import { encodePermissions, decodePermissions } from './lib/permissions';

export {
ERC725JSONSchema,
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function supportsInterface(
throw new Error('Missing RPC URL');
}

return _supportsInterface(interfaceIdOrName, {
return internalSupportsInterface(interfaceIdOrName, {
address: options.address,
provider:
options.provider ||
Expand Down Expand Up @@ -595,7 +595,7 @@ export class ERC725 {
async supportsInterface(interfaceIdOrName: string): Promise<boolean> {
const { address, provider } = this.getAddressAndProvider();

return _supportsInterface(interfaceIdOrName, {
return internalSupportsInterface(interfaceIdOrName, {
address,
provider,
});
Expand Down
Loading

0 comments on commit 24bda3a

Please sign in to comment.