diff --git a/src/registration.ts b/src/registration.ts index ae71e1c..18e046b 100644 --- a/src/registration.ts +++ b/src/registration.ts @@ -10,34 +10,28 @@ import { } from './constants'; export const registerBlockchain = (chain: BlockchainName): void => { - if (Blockchain[chain]) { + if (Object.values(Blockchain).includes(chain)) { throw new Error(`blockchain ${chain} already registered`); } Blockchain[chain] = chain; }; -export const registerDidMethodByte = (name: DidMethodName, value: number): void => { - if (!DidMethod[name]) { - throw new Error(`did method ${name} not registered`); - } - if (DidMethodByte[name]) { - throw new Error(`did method byte ${name} already registered`); - } - DidMethodByte[name] = value; -}; - -export const registerDidMethod = (method: DidMethodName): void => { - if (DidMethod[method]) { +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]) { + throw new Error(`did method byte ${method} already registered`); + } + DidMethodByte[method] = byte; }; -export const registerNetworkId = (name: NetworkName): void => { - if (NetworkId[name]) { - throw new Error(`network ${name} already registered`); +export const registerNetworkId = (network: NetworkName): void => { + if (Object.values(NetworkId).includes(network)) { + throw new Error(`network ${network} already registered`); } - NetworkId[name] = name; + NetworkId[network] = network; }; export const registerDidMethodNetwork = ( @@ -46,81 +40,29 @@ export const registerDidMethodNetwork = ( network: NetworkName, networkFlag: number ): void => { - if (!DidMethod[method]) { + if (!Object.values(DidMethod).includes(method)) { throw new Error(`did method ${method} not registered`); } - if (!Blockchain[blockchain]) { + if (!Object.values(Blockchain).includes(blockchain)) { throw new Error(`blockchain ${blockchain} not registered`); } - if (!NetworkId[network]) { + if (!Object.values(NetworkId).includes(network)) { throw new Error(`network ${network} not registered`); } - if (!DidMethodNetwork[method]) { - DidMethodNetwork[method] = {}; - } - - const key = `${blockchain}:${network}`; - if (DidMethodNetwork[method][key]) { - throw new Error(`did method network ${key} already registered`); - } - DidMethodNetwork[method][key] = networkFlag; -}; - -export const registerDidMethodNetworkImplicit = ( - method: DidMethodName, - blockchain: BlockchainName, - network: NetworkName -): void => { - if (!DidMethod[method]) { - DidMethod[method] = method; - } - if (typeof DidMethodByte[method] !== 'number') { - const methodBytes = Object.values(DidMethodByte).sort((sm, big) => big - sm); - // take second of methodBytes because max byte is occupied by [DidMethod.Other]: 0b11111111 - DidMethodByte[method] = methodBytes[1] + 0b1; - if (DidMethodByte[method] > 0b11111111) { - throw new Error(`did method byte ${method} already registered`); - } - } - - if (!Blockchain[blockchain]) { - Blockchain[blockchain] = blockchain; - } - - if (!NetworkId[network]) { - NetworkId[network] = network; + throw new Error(`did method byte for ${method} is not registered`); } if (!DidMethodNetwork[method]) { DidMethodNetwork[method] = {}; } + const key = `${blockchain}:${network}`; - const networkFlag = DidMethodNetwork[method][key]; - if (typeof networkFlag === 'number') { + if (typeof DidMethodNetwork[method][key] === 'number') { throw new Error(`did method network ${key} already registered`); } - // get the biggest network flag - const flags = Object.values(DidMethodNetwork[method]); - if (!flags.length) { - DidMethodNetwork[method][key] = 0b00010000 | 0b00000001; - return; - } - //get binary representation of biggest flag - const biggestFlag = flags.sort((sm, big) => big - sm)[0]; - const chainPart = (biggestFlag >> 4) + 1; - const networkPart = (biggestFlag & 0b0000_1111) + 1; - - if (chainPart >= 0b1111) { - throw new Error(`Reached max number of blockchains for did method ${method}`); - } - - if (networkPart >= 0b1111) { - throw new Error(`Reached max number of networks for did method ${method}`); - } - - DidMethodNetwork[method][key] = (chainPart << 4) | networkPart; + DidMethodNetwork[method][key] = networkFlag; }; diff --git a/tests/did.test.ts b/tests/did.test.ts index d5fe9de..8dc7c22 100644 --- a/tests/did.test.ts +++ b/tests/did.test.ts @@ -6,10 +6,8 @@ import { genesisFromEthAddress } from '../src/utils'; import { registerBlockchain, registerNetworkId, - registerDidMethod, - registerDidMethodByte, - registerDidMethodNetwork, - registerDidMethodNetworkImplicit + registerDidMethodWithByte, + registerDidMethodNetwork } from '../src/registration'; export const helperBuildDIDFromType = ( @@ -57,15 +55,39 @@ describe('DID tests', () => { // explicitly register all the things registerBlockchain('test_chain'); registerNetworkId('test_net'); - registerDidMethod('test_method'); - registerDidMethodByte('test_method', 0b00000011); + registerDidMethodWithByte('test_method', 0b00000011); registerDidMethodNetwork('test_method', 'test_chain', 'test_net', 0b0001_0001); // implicitly register all the things - registerDidMethodNetworkImplicit('method', 'chain', 'network'); - registerDidMethodNetworkImplicit('iden3', 'chain', NetworkId.Test); - registerDidMethodNetworkImplicit('iden3', Blockchain.ReadOnly, 'network'); - registerDidMethodNetworkImplicit('iden3', Blockchain.ReadOnly, NetworkId.Test); - registerDidMethodNetworkImplicit('method2', 'chain2', 'network2'); + 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 + ); + expect(() => + registerDidMethodNetwork( + DidMethod.Iden3, + Blockchain.ReadOnly, + NetworkId.network, + 0b01010000 | 0b00000100 + ) + ).toThrowError('did method network readonly:network already registered'); + + registerDidMethodWithByte('method2', 0b0000_0101); + registerBlockchain('chain2'); + registerNetworkId('network2'); + registerDidMethodNetwork('method2', 'chain2', 'network2', 0b0001_0001); const d = helperBuildDIDFromType('method2', 'chain2', 'network2'); // const did = helperBuildDIDFromType('method', 'chain', 'network');