Skip to content

Commit

Permalink
Adjust JS Core according to golang
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Dec 7, 2023
1 parent eae337f commit 748af9a
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 127 deletions.
46 changes: 36 additions & 10 deletions src/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@ import {
} from './constants';
import { DID } from './did';

export const registerDidMethod = (method: string, byte: number): void => {
DidMethod[method] = method;
export const registerBlockchain = (blockchain: string): void => {
Blockchain[blockchain] = blockchain;
};

export const registerNetwork = (network: string): void => {
NetworkId[network] = network;
};

if (typeof DidMethodByte[method] === 'number') {
throw new Error(`did method ${method} already registered`);
export const registerDidMethod = (method: string, byte: number): void => {
const existingByte = DidMethodByte[method];
if (typeof DidMethodByte[method] === 'number' && existingByte !== byte) {
throw new Error(
`DID method '${method}' already registered with byte '${existingByte.toString(2)}'`
);
}
const max = DidMethodByte[DidMethod.Other];

if (byte >= max) {
throw new Error(
`Can't register DID method byte: current '${byte.toString(2)}', maximum byte allowed: '${(
max - 1
).toString(2)}'`
);
}

DidMethod[method] = method;
DidMethodByte[method] = byte;
};

Expand All @@ -29,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}`;

if (typeof ChainIds[key] === 'number') {
throw new Error(`chainId ${blockchain}:${network} already registered`);
if (typeof ChainIds[key] === 'number' && ChainIds[key] !== chainId) {
throw new Error(
`chainId '${blockchain}:${network}' already registered with value '${ChainIds[key]}'`
);
}

ChainIds[key] = chainId;
Expand Down Expand Up @@ -109,8 +130,8 @@ export const registerDidMethodNetwork = ({
networkFlag: number;
chainId?: number;
}): void => {
Blockchain[blockchain] = blockchain;
NetworkId[network] = network;
registerBlockchain(blockchain);
registerNetwork(network);
if (typeof methodByte === 'number') {
registerDidMethod(method, methodByte);
}
Expand All @@ -124,8 +145,13 @@ export const registerDidMethodNetwork = ({
}

const key = `${blockchain}:${network}`;
if (typeof DidMethodNetwork[method][key] === 'number') {
throw new Error(`did method network ${key} already registered`);
const existedFlag = DidMethodNetwork[method][key];
if (typeof existedFlag === 'number' && existedFlag !== networkFlag) {
throw new Error(
`DID method network '${method}' with blockchain '${blockchain}' and network '${network}' already registered with another flag '${existedFlag.toString(
2
)}'`
);
}
DidMethodNetwork[method][key] = networkFlag;
};
272 changes: 155 additions & 117 deletions tests/did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,123 +46,6 @@ describe('DID tests', () => {
expect(Uint8Array.from([DidMethodByte[DidMethod.Iden3], 0b0])).toStrictEqual(id.type());
});

it('Custom DID Registration', () => {
const testCases = [
{
description: 'register new did method network',
data: {
method: 'test_method',
blockchain: 'test_chain',
network: 'test_net',
chainId: 101,
methodByte: 0b00000011,
networkFlag: 0b0001_0001
}
},
{
description: 'register one more new did method network',
data: {
method: 'method',
blockchain: 'chain',
network: 'network',
chainId: 102,
methodByte: 0b0000_0100,
networkFlag: 0b0001_0001
}
},
{
description: 'register oexistind did method byte',
data: {
method: 'method',
blockchain: 'chain',
network: 'network',
chainId: 102,
methodByte: 0b0000_0100,
networkFlag: 0b0001_0001
},
error: 'did method method already registered'
},
{
description: 'register network to existing did method',
data: {
method: DidMethod.Iden3,
blockchain: 'chain',
network: NetworkId.Test,
chainId: 103,
networkFlag: 0b01000000 | 0b00000011
}
},
{
description: 'register one more network to existing did method',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: 'network',
chainId: 104,
networkFlag: 0b01000000 | 0b00000011
}
},
{
description: 'register already registered did method network',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: 'network',
networkFlag: 0b01000000 | 0b00000011
},
error: 'did method network readonly:network already registered'
},
{
description: 'register exited chain',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: 'network',
chainId: 104,
networkFlag: 0b01000000 | 0b00000011
},
error: 'chainId readonly:network already registered'
},
{
description: 'register known chain id to new did method',
data: {
method: 'method2',
blockchain: Blockchain.Polygon,
network: NetworkId.Mumbai,
methodByte: 0b0000_0101,
networkFlag: 0b0001_0001
}
}
];

for (let i = 0; i < testCases.length; i++) {
const tc = testCases[i];
if (tc.error) {
expect(() => registerDidMethodNetwork(tc.data)).toThrowError(tc.error);
} else {
registerDidMethodNetwork(tc.data);
}
}

const d = helperBuildDIDFromType('method', 'chain', 'network');
// const did = helperBuildDIDFromType('method', 'chain', 'network');
expect('4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT').toEqual(d.string().split(':').pop());

// did
const didStr = 'did:method:chain:network:4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT';

const did3 = DID.parse(didStr);
const id = DID.idFromDID(did3);

expect('4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT').toEqual(id.string());
const method = DID.methodFromId(id);
expect(DidMethod.method).toBe(method);
const blockchain = DID.blockchainFromId(id);
expect(Blockchain.chain).toBe(blockchain);
const networkId = DID.networkIdFromId(id);
expect(NetworkId.network).toBe(networkId);
});

it('TestDID_UnmarshalJSON', () => {
const parseRes = JSON.parse(
`{"obj": "did:iden3:polygon:mumbai:wyFiV4w71QgWPn6bYLsZoysFay66gKtVa9kfu6yMZ"}`
Expand Down Expand Up @@ -372,4 +255,159 @@ describe('DID tests', () => {
"can't get Ethereum address: high bytes of genesis are not zero"
);
});

it('TestCustomDIDRegistration', () => {
const testCases = [
{
description: 'register new did method network',
data: {
method: 'test_method',
blockchain: 'test_chain',
network: 'test_net',
networkFlag: 0b0001_0001,
chainId: 101,
methodByte: 0b00000011
}
},
{
description: 'register one more new did method network',
data: {
method: 'method',
blockchain: 'chain',
network: 'network',
networkFlag: 0b0001_0001,
chainId: 102,
methodByte: 0b00000100
}
},
{
description: 'register the same new did method network',
data: {
method: 'method',
blockchain: 'chain',
network: 'network',
networkFlag: 0b0001_0001,
chainId: 102,
methodByte: 0b00000100
}
},
{
description: 'register network to existing did method',
data: {
method: DidMethod.Iden3,
blockchain: 'chain',
network: NetworkId.Test,
networkFlag: 0b01000000 | 0b00000011,
chainId: 103
}
},
{
description: 'register network to existing did method and chainId',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: NetworkId.NoNetwork,
networkFlag: 0b00000000,
chainId: 103
}
},
{
description: 'register one more network to existing did method',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: 'network',
networkFlag: 0b01000000 | 0b00000011,
chainId: 104
}
},
{
description: 'register known chain id to new did method',
data: {
method: 'method2',
blockchain: Blockchain.Polygon,
network: NetworkId.Mumbai,
networkFlag: 0b0001_0001,
methodByte: 0b0000001
}
}
];

for (let i = 0; i < testCases.length; i++) {
const tc = testCases[i];
expect(() => registerDidMethodNetwork(tc.data)).not.toThrow();
}

const d = helperBuildDIDFromType('method', 'chain', 'network');
// const did = helperBuildDIDFromType('method', 'chain', 'network');
expect('4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT').toEqual(d.string().split(':').pop());

// did
const didStr = 'did:method:chain:network:4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT';

const did3 = DID.parse(didStr);
const id = DID.idFromDID(did3);

expect('4bb86obLkMrifHixMY62WM4iQQVr7u29cxWjMAinrT').toEqual(id.string());
const method = DID.methodFromId(id);
expect(DidMethod.method).toBe(method);
const blockchain = DID.blockchainFromId(id);
expect(Blockchain.chain).toBe(blockchain);
const networkId = DID.networkIdFromId(id);
expect(NetworkId.network).toBe(networkId);
});

const testCases = [
{
description: 'try to overwrite existing chain id',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.Polygon,
network: NetworkId.Mumbai,
networkFlag: 0b0001_0001,
chainId: 1
},
err: "chainId 'polygon:mumbai' already registered with value '80001'"
},
{
description: 'try to overwrite existing DID method byte',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.Ethereum,
network: NetworkId.Main,
networkFlag: 0b00100000 | 0b00000001,
chainId: 1,
methodByte: 0b00000010
},
err: "DID method 'iden3' already registered with byte '1'"
},
{
description: 'try to write max did method byte',
data: {
method: 'method33',
blockchain: Blockchain.Ethereum,
network: NetworkId.Main,
networkFlag: 0b00100000 | 0b00000001,
chainId: 1,
methodByte: 0b11111111
},
err: "Can't register DID method byte: current '11111111', maximum byte allowed: '11111110'"
},
{
description: 'try to rewrite existing DID Method Network Flag',
data: {
method: DidMethod.Iden3,
blockchain: Blockchain.Ethereum,
network: NetworkId.Main,
networkFlag: 0b00100000 | 0b00000011
},
err: "DID method network 'iden3' with blockchain 'eth' and network 'main' already registered with another flag '100001'"
}
];
for (let i = 0; i < testCases.length; i++) {
const tc = testCases[i];
it(tc.description, () => {
expect(() => registerDidMethodNetwork(tc.data)).toThrowError(tc.err);
});
}
});

0 comments on commit 748af9a

Please sign in to comment.