Skip to content

Commit

Permalink
Forbid creates flags with existing values
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Dec 18, 2023
1 parent 51f411b commit b577b5e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 12 additions & 4 deletions src/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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]}'`
);
Expand Down Expand Up @@ -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(
Expand All @@ -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;
};
51 changes: 43 additions & 8 deletions tests/did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ describe('DID tests', () => {
network: 'network',
networkFlag: 0b0001_0001,
chainId: 102,
methodByte: 0b00000100
methodByte: 0b00000101
}
},
{
Expand Down Expand Up @@ -328,7 +328,7 @@ describe('DID tests', () => {
blockchain: Blockchain.Polygon,
network: NetworkId.Mumbai,
networkFlag: 0b0001_0001,
methodByte: 0b0000001
methodByte: 0b0000111
}
}
];
Expand All @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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++) {
Expand Down

0 comments on commit b577b5e

Please sign in to comment.