Skip to content

Commit

Permalink
Add one method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Nov 30, 2023
1 parent 8180c69 commit 43ce078
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 71 deletions.
17 changes: 17 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ export const DidMethod: { [k: DidMethodName]: DidMethodName } = {
Other: ''
};

/**
* Object containing chain IDs for various blockchains and networks.
* @type { [key: string]: number }
*/
export const ChainIds: { [key: string]: number } = {
eth: 1,
'eth:main': 1,
'eth:goerli': 5,
'eth:sepolia': 11155111,
polygon: 137,
'polygon:main': 137,
'polygon:mumbai': 80001,
zkevm: 1101,
'zkevm:main': 1101,
'zkevm:test': 1442
};

export const DidMethodByte: { [key: DidMethodName]: number } = {
[DidMethod.Iden3]: 0b00000001,
[DidMethod.PolygonId]: 0b00000010,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './elemBytes';
export * from './id';
export * from './schemaHash';
export * from './utils';
export * from './registration';
75 changes: 43 additions & 32 deletions src/registration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Blockchain,
BlockchainName,
ChainIds,
DidMethod,
DidMethodByte,
DidMethodName,
Expand All @@ -9,57 +10,67 @@ import {
NetworkName
} from './constants';

export const registerBlockchain = (chain: BlockchainName): void => {
if (Object.values(Blockchain).includes(chain)) {
throw new Error(`blockchain ${chain} already registered`);
const registerDidMethodWithByte = (method: DidMethodName, byte?: number): void => {
DidMethod[method] = method;
if (typeof byte !== 'number') {
return;
}
Blockchain[chain] = chain;
};

export const registerDidMethodWithByte = (method: DidMethodName, byte: number): void => {
if (Object.values(DidMethod).includes(method)) {
throw new Error(`did method ${method} already registered`);
}
DidMethod[method] = method;
if (DidMethodByte[method]) {
if (typeof DidMethodByte[method] === 'number') {
throw new Error(`did method byte ${method} already registered`);
}

DidMethodByte[method] = byte;
};

export const registerNetworkId = (network: NetworkName): void => {
if (Object.values(NetworkId).includes(network)) {
throw new Error(`network ${network} already registered`);
const registerChainId = (blockchain: string, network: string, chainId?: number): void => {
if (!chainId) {
return;
}
NetworkId[network] = network;
};

export const registerDidMethodNetwork = (
method: DidMethodName,
blockchain: BlockchainName,
network: NetworkName,
networkFlag: number
): void => {
if (!Object.values(DidMethod).includes(method)) {
throw new Error(`did method ${method} not registered`);
if (network) {
blockchain += `:${network}`;
}

if (!Object.values(Blockchain).includes(blockchain)) {
throw new Error(`blockchain ${blockchain} not registered`);
}
ChainIds[blockchain] = chainId;
};

if (!Object.values(NetworkId).includes(network)) {
throw new Error(`network ${network} not registered`);
export const getChainId = (blockchain: string, network?: string): number => {
if (network) {
blockchain += `:${network}`;
}

if (typeof DidMethodByte[method] !== 'number') {
throw new Error(`did method byte for ${method} is not registered`);
const chainId = ChainIds[blockchain];
if (!chainId) {
throw new Error(`chainId not found for ${blockchain}`);
}
return chainId;
};

export const registerDidMethodNetwork = ({
method,
methodByte,
blockchain,
network,
chainId,
networkFlag
}: {
method: DidMethodName;
methodByte?: number;
blockchain: BlockchainName;
network: NetworkName;
networkFlag: number;
chainId?: number;
}): void => {
Blockchain[blockchain] = blockchain;
NetworkId[network] = network;
registerDidMethodWithByte(method, methodByte);

if (!DidMethodNetwork[method]) {
DidMethodNetwork[method] = {};
}

registerChainId(blockchain, network, chainId);

const key = `${blockchain}:${network}`;
if (typeof DidMethodNetwork[method][key] === 'number') {
throw new Error(`did method network ${key} already registered`);
Expand Down
89 changes: 50 additions & 39 deletions tests/did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { DID, buildDIDType } from '../src/did';
import { Id } from '../src/id';
import { Blockchain, DidMethodByte, DidMethod, NetworkId, Constants } from './../src/constants';
import { genesisFromEthAddress } from '../src/utils';
import {
registerBlockchain,
registerNetworkId,
registerDidMethodWithByte,
registerDidMethodNetwork
} from '../src/registration';
import { registerDidMethodNetwork } from '../src/registration';

export const helperBuildDIDFromType = (
method: string,
Expand Down Expand Up @@ -52,42 +47,58 @@ describe('DID tests', () => {
});

it('Custom ParseDID', () => {
// explicitly register all the things
registerBlockchain('test_chain');
registerNetworkId('test_net');
registerDidMethodWithByte('test_method', 0b00000011);
registerDidMethodNetwork('test_method', 'test_chain', 'test_net', 0b0001_0001);
// implicitly register all the things
registerDidMethodWithByte('method', 0b0000_0100);
registerBlockchain('chain');
registerNetworkId('network');

registerDidMethodNetwork(DidMethod.method, Blockchain.chain, NetworkId.network, 0b0001_0001);
registerDidMethodNetwork(
DidMethod.Iden3,
Blockchain.chain,
NetworkId.Test,
0b01000000 | 0b00000011
);
registerDidMethodNetwork(
DidMethod.Iden3,
Blockchain.ReadOnly,
NetworkId.network,
0b01000000 | 0b00000011
);
registerDidMethodNetwork({
method: 'test_method',
blockchain: 'test_chain',
network: 'test_net',
chainId: 101,
methodByte: 0b00000011,
networkFlag: 0b0001_0001
});

registerDidMethodNetwork({
method: 'method',
blockchain: 'chain',
network: 'network',
chainId: 102,
methodByte: 0b0000_0100,
networkFlag: 0b0001_0001
});

registerDidMethodNetwork({
method: DidMethod.Iden3,
blockchain: Blockchain.chain,
network: NetworkId.Test,
chainId: 103,
networkFlag: 0b01000000 | 0b00000011
});

registerDidMethodNetwork({
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: NetworkId.network,
chainId: 104,
networkFlag: 0b01000000 | 0b00000011
});

expect(() =>
registerDidMethodNetwork(
DidMethod.Iden3,
Blockchain.ReadOnly,
NetworkId.network,
0b01010000 | 0b00000100
)
registerDidMethodNetwork({
method: DidMethod.Iden3,
blockchain: Blockchain.ReadOnly,
network: NetworkId.network,
chainId: 104,
networkFlag: 0b01000000 | 0b00000011
})
).toThrowError('did method network readonly:network already registered');

registerDidMethodWithByte('method2', 0b0000_0101);
registerBlockchain('chain2');
registerNetworkId('network2');
registerDidMethodNetwork('method2', 'chain2', 'network2', 0b0001_0001);
registerDidMethodNetwork({
method: 'method2',
blockchain: 'chain2',
network: 'network2',
chainId: 105,
methodByte: 0b0000_0101,
networkFlag: 0b0001_0001
});

const d = helperBuildDIDFromType('method2', 'chain2', 'network2');
// const did = helperBuildDIDFromType('method', 'chain', 'network');
Expand Down

0 comments on commit 43ce078

Please sign in to comment.