Skip to content

Commit

Permalink
Apply bitwise operators. fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Nov 27, 2023
1 parent 95264fc commit d754c96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
42 changes: 23 additions & 19 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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`);
Expand Down Expand Up @@ -155,7 +155,7 @@ export const DidMethodNetwork: {

export const registerDidMethodNetwork = (
method: DidMethodName,
blockchain: BlockChainName,
blockchain: BlockchainName,
network: NetworkName,
networkFlag: number
): void => {
Expand All @@ -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`);
Expand Down Expand Up @@ -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;
};
6 changes: 3 additions & 3 deletions src/did/did-helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
BlockChainName,
BlockchainName,
Constants,
DidMethodByte,
DidMethodName,
Expand All @@ -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}`;
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/did/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
DidMethod,
NetworkId,
DidMethodName,
BlockChainName,
BlockchainName,
NetworkName
} from '../constants';
import { BytesHelper } from '../elemBytes';
Expand Down Expand Up @@ -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]);
Expand All @@ -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);
Expand Down Expand Up @@ -196,7 +196,7 @@ export class DID {

static isUnsupported(
method: DidMethodName,
blockchain: BlockChainName,
blockchain: BlockchainName,
networkId: NetworkName
): boolean {
return (
Expand Down

0 comments on commit d754c96

Please sign in to comment.