diff --git a/package-lock.json b/package-lock.json index b1df681..3edca2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@iden3/js-iden3-core", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@iden3/js-iden3-core", - "version": "1.2.0", + "version": "1.2.1", "license": "AGPL-3.0", "devDependencies": { "@iden3/eslint-config": "https://github.com/iden3/eslint-config", diff --git a/package.json b/package.json index d58eefb..b4d9ff3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@iden3/js-iden3-core", - "version": "1.2.0", + "version": "1.2.1", "description": "Low level API to create and manipulate iden3 Claims.", "source": "./src/index.ts", "typings": "dist/types/index.d.ts", diff --git a/src/registration.ts b/src/registration.ts index 0c06f1f..71e488c 100644 --- a/src/registration.ts +++ b/src/registration.ts @@ -17,10 +17,11 @@ export const registerNetwork = (network: string): void => { }; export const registerDidMethod = (method: string, byte: number): void => { - const existingByte = DidMethodByte[method]; - if (typeof DidMethodByte[method] === 'number' && existingByte !== byte) { + if (Object.values(DidMethodByte).includes(byte) && DidMethodByte[method] !== byte) { throw new Error( - `DID method '${method}' already registered with byte '${existingByte.toString(2)}'` + `can't register method '${method}' because DID method byte '${byte.toString( + 2 + )}' already registered` ); } const max = DidMethodByte[DidMethod.Other]; @@ -47,8 +48,10 @@ export const registerDidMethod = (method: string, byte: number): void => { */ export const registerChainId = (blockchain: string, network: string, chainId: number): void => { const key = `${blockchain}:${network}`; + const isNotSameValue = typeof ChainIds[key] === 'number' && ChainIds[key] !== chainId; + const isValueAlreadyExists = Object.values(ChainIds).includes(chainId); - if (typeof ChainIds[key] === 'number' && ChainIds[key] !== chainId) { + if (isValueAlreadyExists && isNotSameValue) { throw new Error( `chainId '${blockchain}:${network}' already registered with value '${ChainIds[key]}'` ); @@ -145,6 +148,7 @@ export const registerDidMethodNetwork = ({ } const key = `${blockchain}:${network}`; + const existedFlag = DidMethodNetwork[method][key]; if (typeof existedFlag === 'number' && existedFlag !== networkFlag) { throw new Error( @@ -153,5 +157,9 @@ export const registerDidMethodNetwork = ({ )}'` ); } + + if (Object.values(DidMethodNetwork[method]).includes(networkFlag)) { + throw new Error(`DID network flag ${DidMethodNetwork[method][key]} is already registered`); + } DidMethodNetwork[method][key] = networkFlag; }; diff --git a/tests/did.test.ts b/tests/did.test.ts index c96e492..580dd45 100644 --- a/tests/did.test.ts +++ b/tests/did.test.ts @@ -288,7 +288,7 @@ describe('DID tests', () => { network: 'network', networkFlag: 0b0001_0001, chainId: 102, - methodByte: 0b00000100 + methodByte: 0b00000101 } }, { @@ -328,7 +328,7 @@ describe('DID tests', () => { blockchain: Blockchain.Polygon, network: NetworkId.Mumbai, networkFlag: 0b0001_0001, - methodByte: 0b0000001 + methodByte: 0b0000111 } } ]; @@ -340,15 +340,15 @@ describe('DID tests', () => { const d = helperBuildDIDFromType('method', 'chain', 'network'); // const did = helperBuildDIDFromType('method', 'chain', 'network'); - expect('4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT').toEqual(d.string().split(':').pop()); + expect('5UtG9EXvF25j3X5uycwr4uy7Hjhni8bMposv3Lgv8o').toEqual(d.string().split(':').pop()); // did - const didStr = 'did:method:chain:network:4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT'; + const didStr = 'did:method:chain:network:5UtG9EXvF25j3X5uycwr4uy7Hjhni8bMposv3Lgv8o'; const did3 = DID.parse(didStr); const id = DID.idFromDID(did3); - expect('4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT').toEqual(id.string()); + expect('5UtG9EXvF25j3X5uycwr4uy7Hjhni8bMposv3Lgv8o').toEqual(id.string()); const method = DID.methodFromId(id); expect(DidMethod.method).toBe(method); const blockchain = DID.blockchainFromId(id); @@ -379,7 +379,7 @@ describe('DID tests', () => { chainId: 1, methodByte: 0b00000010 }, - err: "DID method 'iden3' already registered with byte '1'" + err: "can't register method 'iden3' because DID method byte '10' already registered" }, { description: 'try to write max did method byte', @@ -389,9 +389,9 @@ describe('DID tests', () => { network: NetworkId.Main, networkFlag: 0b00100000 | 0b00000001, chainId: 1, - methodByte: 0b11111111 + methodByte: 0b111111111 }, - err: "Can't register DID method byte: current '11111111', maximum byte allowed: '11111110'" + err: "Can't register DID method byte: current '111111111', maximum byte allowed: '11111110'" }, { description: 'try to rewrite existing DID Method Network Flag', @@ -402,6 +402,41 @@ describe('DID tests', () => { networkFlag: 0b00100000 | 0b00000011 }, err: "DID method network 'iden3' with blockchain 'eth' and network 'main' already registered with another flag '100001'" + }, + { + description: 'register new did method with existing method byte', + data: { + method: 'new_method', + blockchain: 'new_chain', + network: 'new_net', + networkFlag: 0b0001_0001, + chainId: 101, + methodByte: 0b00000001 + }, + err: "DID method byte '1' already registered" + }, + { + description: 'register new did method with existing method byte', + data: { + method: 'new_method', + blockchain: Blockchain.Ethereum, + network: NetworkId.Main, + networkFlag: 0b0001_0001, + chainId: 101, + methodByte: 0b10000000 + }, + err: "chainId 'eth:main' already registered with value '1'" + }, + { + description: + 'register new network and chain with existing networkFlag for existing existing did method', + data: { + method: DidMethod.Iden3, + blockchain: 'supa_chain', + network: 'supa_net', + networkFlag: 0b00010000 | 0b00000001 + }, + err: 'DID network flag undefined is already registered' } ]; for (let i = 0; i < testCases.length; i++) {