From 43ce078430301a3b0a4896e9226eaa13fad3de46 Mon Sep 17 00:00:00 2001 From: Dimasik Kolezhniuk Date: Thu, 30 Nov 2023 11:32:32 +0100 Subject: [PATCH] Add one method --- src/constants.ts | 17 +++++++++ src/index.ts | 1 + src/registration.ts | 75 ++++++++++++++++++++++---------------- tests/did.test.ts | 89 +++++++++++++++++++++++++-------------------- 4 files changed, 111 insertions(+), 71 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 69d791a..c097a12 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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, diff --git a/src/index.ts b/src/index.ts index 042fe84..489d451 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,3 +5,4 @@ export * from './elemBytes'; export * from './id'; export * from './schemaHash'; export * from './utils'; +export * from './registration'; diff --git a/src/registration.ts b/src/registration.ts index 18e046b..c51ba2a 100644 --- a/src/registration.ts +++ b/src/registration.ts @@ -1,6 +1,7 @@ import { Blockchain, BlockchainName, + ChainIds, DidMethod, DidMethodByte, DidMethodName, @@ -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`); diff --git a/tests/did.test.ts b/tests/did.test.ts index 8dc7c22..c427928 100644 --- a/tests/did.test.ts +++ b/tests/did.test.ts @@ -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, @@ -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');