From d754c963228e01258dd0f116041cad073bcaa8f2 Mon Sep 17 00:00:00 2001 From: Dimasik Kolezhniuk Date: Mon, 27 Nov 2023 21:36:10 +0100 Subject: [PATCH] Apply bitwise operators. fix comment --- src/constants.ts | 42 +++++++++++++++++++++++------------------- src/did/did-helper.ts | 6 +++--- src/did/did.ts | 10 +++++----- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index ab3e2ee..5fac4e0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -42,9 +42,9 @@ export const Constants = Object.freeze({ GENESIS_LENGTH: 27 }); -export type BlockChainName = 'eth' | 'polygon' | 'zkevm' | 'unknown' | 'readonly' | '' | string; +export type BlockchainName = 'eth' | 'polygon' | 'zkevm' | 'unknown' | 'readonly' | '' | string; -export const Blockchain: { [k: BlockChainName]: string } = { +export const Blockchain: { [k: BlockchainName]: string } = { Ethereum: 'eth', Polygon: 'polygon', ZkEVM: 'zkevm', @@ -54,8 +54,8 @@ export const Blockchain: { [k: BlockChainName]: string } = { }; export const registerBlockchain = ( - name: BlockChainName, - value: BlockChainName | null = null + name: BlockchainName, + value: BlockchainName | null = null ): void => { if (Blockchain[name]) { throw new Error(`blockchain ${name} already registered`); @@ -155,7 +155,7 @@ export const DidMethodNetwork: { export const registerDidMethodNetwork = ( method: DidMethodName, - blockchain: BlockChainName, + blockchain: BlockchainName, network: NetworkName, networkFlag: number ): void => { @@ -174,6 +174,7 @@ export const registerDidMethodNetwork = ( if (!DidMethodNetwork[method]) { DidMethodNetwork[method] = {}; } + const key = `${blockchain}:${network}`; if (DidMethodNetwork[method][key]) { throw new Error(`did method network ${key} already registered`); @@ -216,20 +217,23 @@ export const registerDidMethodNetworkForce = ( throw new Error(`did method network ${key} already registered`); } // get the biggest network flag - const biggestFlag = Object.values(DidMethodNetwork[method]).sort((sm, big) => big - sm)[0]; - if (typeof biggestFlag !== 'number') { + const flags = Object.values(DidMethodNetwork[method]); + if (!flags.length) { DidMethodNetwork[method][key] = 0b00010000 | 0b00000001; - } else { - //get binary representation of biggest flag - const biggestFlagBinary = biggestFlag.toString(2).padStart(8, '0'); - const chainPart = parseInt(biggestFlagBinary.slice(0, 4), 2) + 1; - const networkPart = parseInt(biggestFlagBinary.slice(4), 2) + 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; + 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; }; diff --git a/src/did/did-helper.ts b/src/did/did-helper.ts index 081f428..26779be 100644 --- a/src/did/did-helper.ts +++ b/src/did/did-helper.ts @@ -1,5 +1,5 @@ import { - BlockChainName, + BlockchainName, Constants, DidMethodByte, DidMethodName, @@ -9,7 +9,7 @@ import { // DIDNetworkFlag is a structure to represent DID blockchain and network id export class DIDNetworkFlag { - constructor(public readonly blockchain: BlockChainName, public readonly networkId: NetworkName) {} + constructor(public readonly blockchain: BlockchainName, public readonly networkId: NetworkName) {} toString(): string { return `${this.blockchain}:${this.networkId}`; @@ -66,7 +66,7 @@ export function findNetworkIDForDIDMethodByValue( export function findBlockchainForDIDMethodByValue( method: DidMethodName, byteNumber: number -): BlockChainName { +): BlockchainName { const methodMap = DidMethodNetwork[method]; if (!methodMap) { throw new Error( diff --git a/src/did/did.ts b/src/did/did.ts index b687160..5afb39b 100644 --- a/src/did/did.ts +++ b/src/did/did.ts @@ -7,7 +7,7 @@ import { DidMethod, NetworkId, DidMethodName, - BlockChainName, + BlockchainName, NetworkName } from '../constants'; import { BytesHelper } from '../elemBytes'; @@ -111,7 +111,7 @@ export class DID { static decodePartsFromId(id: Id): { method: DidMethodName; - blockchain: BlockChainName; + blockchain: BlockchainName; networkId: NetworkName; } { const method = findDIDMethodByValue(id.bytes[0]); @@ -130,13 +130,13 @@ export class DID { return DID.throwIfDIDUnsupported(id).method; } - static blockchainFromId(id: Id): BlockChainName { + static blockchainFromId(id: Id): BlockchainName { return DID.throwIfDIDUnsupported(id).blockchain; } private static throwIfDIDUnsupported(id: Id): { method: DidMethodName; - blockchain: BlockChainName; + blockchain: BlockchainName; networkId: NetworkName; } { const { method, blockchain, networkId } = DID.decodePartsFromId(id); @@ -196,7 +196,7 @@ export class DID { static isUnsupported( method: DidMethodName, - blockchain: BlockChainName, + blockchain: BlockchainName, networkId: NetworkName ): boolean { return (