Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Dec 1, 2023
1 parent 43ce078 commit 0cc0e92
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 142 deletions.
41 changes: 12 additions & 29 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export const Constants = Object.freeze({
GENESIS_LENGTH: 27
});

export type BlockchainName = 'eth' | 'polygon' | 'zkevm' | 'unknown' | 'readonly' | '' | string;

export const Blockchain: { [k: BlockchainName]: string } = {
export const Blockchain: { [k: string]: string } = {
Ethereum: 'eth',
Polygon: 'polygon',
ZkEVM: 'zkevm',
Expand All @@ -53,17 +51,7 @@ export const Blockchain: { [k: BlockchainName]: string } = {
ReadOnly: 'readonly'
};

export type NetworkName =
| 'main'
| 'mumbai'
| 'goerli'
| 'sepolia'
| 'test'
| 'unknown'
| ''
| string;

export const NetworkId: { [k: NetworkName]: NetworkName } = {
export const NetworkId: { [k: string]: string } = {
Main: 'main',
Mumbai: 'mumbai',
Goerli: 'goerli',
Expand All @@ -73,9 +61,7 @@ export const NetworkId: { [k: NetworkName]: NetworkName } = {
NoNetwork: ''
};

export type DidMethodName = 'iden3' | 'polygonid' | '' | string;

export const DidMethod: { [k: DidMethodName]: DidMethodName } = {
export const DidMethod: { [k: string]: string } = {
Iden3: 'iden3',
PolygonId: 'polygonid',
Other: ''
Expand All @@ -86,27 +72,24 @@ export const DidMethod: { [k: DidMethodName]: DidMethodName } = {
* @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
[`${Blockchain.Ethereum}:${NetworkId.Main}`]: 1,
[`${Blockchain.Ethereum}:${NetworkId.Goerli}`]: 5,
[`${Blockchain.Ethereum}:${NetworkId.Sepolia}`]: 11155111,
[`${Blockchain.Polygon}:${NetworkId.Main}`]: 137,
[`${Blockchain.Polygon}:${NetworkId.Mumbai}`]: 80001,
[`${Blockchain.ZkEVM}:${NetworkId.Main}`]: 1101,
[`${Blockchain.ZkEVM}:${NetworkId.Test}`]: 1442
};

export const DidMethodByte: { [key: DidMethodName]: number } = {
export const DidMethodByte: { [key: string]: number } = {
[DidMethod.Iden3]: 0b00000001,
[DidMethod.PolygonId]: 0b00000010,
[DidMethod.Other]: 0b11111111
};

// DIDMethodNetwork is map for did methods and their blockchain networks
export const DidMethodNetwork: {
[k: DidMethodName]: { [k: string]: number };
[k: string]: { [k: string]: number };
} = {
[DidMethod.Iden3]: {
[`${Blockchain.ReadOnly}:${NetworkId.NoNetwork}`]: 0b00000000,
Expand Down
23 changes: 5 additions & 18 deletions src/did/did-helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import {
BlockchainName,
Constants,
DidMethodByte,
DidMethodName,
DidMethodNetwork,
NetworkName
} from '../constants';
import { Constants, DidMethodByte, DidMethodNetwork } from '../constants';

// 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: string, public readonly networkId: string) {}

toString(): string {
return `${this.blockchain}:${this.networkId}`;
Expand Down Expand Up @@ -46,10 +39,7 @@ export function buildDIDType(method: string, blockchain: string, network: string
}

// FindNetworkIDForDIDMethodByValue finds network by byte value
export function findNetworkIDForDIDMethodByValue(
method: DidMethodName,
byteNumber: number
): NetworkName {
export function findNetworkIDForDIDMethodByValue(method: string, byteNumber: number): string {
const methodMap = DidMethodNetwork[method];
if (!methodMap) {
throw Constants.ERRORS.UNSUPPORTED_DID_METHOD;
Expand All @@ -63,10 +53,7 @@ export function findNetworkIDForDIDMethodByValue(
}

// findBlockchainForDIDMethodByValue finds blockchain type by byte value
export function findBlockchainForDIDMethodByValue(
method: DidMethodName,
byteNumber: number
): BlockchainName {
export function findBlockchainForDIDMethodByValue(method: string, byteNumber: number): string {
const methodMap = DidMethodNetwork[method];
if (!methodMap) {
throw new Error(
Expand All @@ -82,7 +69,7 @@ export function findBlockchainForDIDMethodByValue(
}

// findDIDMethodByValue finds did method by its byte value
export function findDIDMethodByValue(byteNumber: number): DidMethodName {
export function findDIDMethodByValue(byteNumber: number): string {
for (const [key, value] of Object.entries(DidMethodByte)) {
if (value === byteNumber) {
return key;
Expand Down
29 changes: 11 additions & 18 deletions src/did/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {
DidMethodByte,
DidMethodNetwork,
DidMethod,
NetworkId,
DidMethodName,
BlockchainName,
NetworkName
NetworkId
} from '../constants';
import { BytesHelper } from '../elemBytes';
import {
Expand Down Expand Up @@ -110,9 +107,9 @@ export class DID {
}

static decodePartsFromId(id: Id): {
method: DidMethodName;
blockchain: BlockchainName;
networkId: NetworkName;
method: string;
blockchain: string;
networkId: string;
} {
const method = findDIDMethodByValue(id.bytes[0]);
const blockchain = findBlockchainForDIDMethodByValue(method, id.bytes[1]);
Expand All @@ -122,22 +119,22 @@ export class DID {
return { method, blockchain, networkId };
}

static networkIdFromId(id: Id): NetworkName {
static networkIdFromId(id: Id): string {
return DID.throwIfDIDUnsupported(id).networkId;
}

static methodFromId(id: Id): DidMethodName {
static methodFromId(id: Id): string {
return DID.throwIfDIDUnsupported(id).method;
}

static blockchainFromId(id: Id): BlockchainName {
static blockchainFromId(id: Id): string {
return DID.throwIfDIDUnsupported(id).blockchain;
}

private static throwIfDIDUnsupported(id: Id): {
method: DidMethodName;
blockchain: BlockchainName;
networkId: NetworkName;
method: string;
blockchain: string;
networkId: string;
} {
const { method, blockchain, networkId } = DID.decodePartsFromId(id);

Expand Down Expand Up @@ -194,11 +191,7 @@ export class DID {
return id;
}

static isUnsupported(
method: DidMethodName,
blockchain: BlockchainName,
networkId: NetworkName
): boolean {
static isUnsupported(method: string, blockchain: string, networkId: string): boolean {
return (
method == DidMethod.Other &&
blockchain == Blockchain.Unknown &&
Expand Down
94 changes: 73 additions & 21 deletions src/registration.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import {
Blockchain,
BlockchainName,
ChainIds,
DidMethod,
DidMethodByte,
DidMethodName,
DidMethodNetwork,
NetworkId,
NetworkName
NetworkId
} from './constants';
import { DID } from './did';

const registerDidMethodWithByte = (method: DidMethodName, byte?: number): void => {
const registerDidMethodWithByte = (method: string, byte: number): void => {
DidMethod[method] = method;
if (typeof byte !== 'number') {
return;
}

if (typeof DidMethodByte[method] === 'number') {
throw new Error(`did method byte ${method} already registered`);
throw new Error(`did method ${method} already registered`);
}

DidMethodByte[method] = byte;
};

const registerChainId = (blockchain: string, network: string, chainId?: number): void => {
if (!chainId) {
return;
}
/**
* Register chain ID for a blockchain and network.
*
* @param {string} blockchain
* @param {string} network
* @param {number} [chainId]
* @returns {void}
*/
export const registerChainId = (blockchain: string, network: string, chainId: number): void => {
const key = `${blockchain}:${network}`;

if (network) {
blockchain += `:${network}`;
if (typeof ChainIds[key] === 'number') {
throw new Error(`chainId ${blockchain}:${network} already registered`);
}

ChainIds[blockchain] = chainId;
ChainIds[key] = chainId;
};

/**
* Get chain ID by a blockchain and network.
*
* @param {string} blockchain
* @param {string} [network]
* @returns {number}
*/
export const getChainId = (blockchain: string, network?: string): number => {
if (network) {
blockchain += `:${network}`;
Expand All @@ -46,6 +54,46 @@ export const getChainId = (blockchain: string, network?: string): number => {
return chainId;
};

/**
* ChainIDfromDID returns chain name from w3c.DID
*
* @param {DID} did
* @returns {number}
*/
export const chainIDfromDID = (did: DID): number => {
const id = DID.idFromDID(did);

const blockchain = DID.blockchainFromId(id);

const networkId = DID.networkIdFromId(id);

const chainId = ChainIds[`${blockchain}:${networkId}`];
if (typeof chainId !== 'number') {
throw new Error(`chainId not found for ${blockchain}:${networkId}`);
}

return chainId;
};

/**
* Register a DID method with a byte value.
* https://docs.iden3.io/getting-started/identity/identity-types/#regular-identity
* @param {{
* method: DidMethodName; DID method name
* methodByte?: number; put DID method byte value in case you want to register new DID method
* blockchain: BlockchainName; blockchain name
* network: NetworkName; network name
* networkFlag: number; network flag
* chainId?: number; put chain ID in case you need to use it
* }} {
* method,
* methodByte,
* blockchain,
* network,
* chainId,
* networkFlag
* }
*/
export const registerDidMethodNetwork = ({
method,
methodByte,
Expand All @@ -54,22 +102,26 @@ export const registerDidMethodNetwork = ({
chainId,
networkFlag
}: {
method: DidMethodName;
method: string;
methodByte?: number;
blockchain: BlockchainName;
network: NetworkName;
blockchain: string;
network: string;
networkFlag: number;
chainId?: number;
}): void => {
Blockchain[blockchain] = blockchain;
NetworkId[network] = network;
registerDidMethodWithByte(method, methodByte);
if (typeof methodByte === 'number') {
registerDidMethodWithByte(method, methodByte);
}

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

registerChainId(blockchain, network, chainId);
if (typeof chainId === 'number') {
registerChainId(blockchain, network, chainId);
}

const key = `${blockchain}:${network}`;
if (typeof DidMethodNetwork[method][key] === 'number') {
Expand Down
Loading

0 comments on commit 0cc0e92

Please sign in to comment.