Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom registration #30

Merged
merged 10 commits into from
Dec 12, 2023
Merged
85 changes: 58 additions & 27 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,72 @@ export const Constants = Object.freeze({
GENESIS_LENGTH: 27
});

export enum Blockchain {
Ethereum = 'eth',
Polygon = 'polygon',
ZkEVM = 'zkevm',
Unknown = 'unknown',
NoChain = '',
ReadOnly = 'readonly'
}
export type BlockchainName = 'eth' | 'polygon' | 'zkevm' | 'unknown' | 'readonly' | '' | string;

export enum NetworkId {
Main = 'main',
Mumbai = 'mumbai',
Goerli = 'goerli',
Sepolia = 'sepolia',
Test = 'test',
Unknown = 'unknown',
NoNetwork = ''
}
export const Blockchain: { [k: BlockchainName]: string } = {
Ethereum: 'eth',
Polygon: 'polygon',
ZkEVM: 'zkevm',
Unknown: 'unknown',
NoChain: '',
ReadOnly: 'readonly'
};

export enum DidMethod {
Iden3 = 'iden3',
PolygonId = 'polygonid',
Other = ''
}
export type NetworkName =
vmidyllic marked this conversation as resolved.
Show resolved Hide resolved
| 'main'
| 'mumbai'
| 'goerli'
| 'sepolia'
| 'test'
| 'unknown'
| ''
| string;

export const DidMethodByte: { [key: string]: number } = Object.freeze({
export const NetworkId: { [k: NetworkName]: NetworkName } = {
Main: 'main',
Mumbai: 'mumbai',
Goerli: 'goerli',
Sepolia: 'sepolia',
Test: 'test',
Unknown: 'unknown',
NoNetwork: ''
};

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

export const DidMethod: { [k: DidMethodName]: DidMethodName } = {
Iden3: 'iden3',
PolygonId: 'polygonid',
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,
vmidyllic marked this conversation as resolved.
Show resolved Hide resolved
'zkevm:main': 1101,
'zkevm:test': 1442
};

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

// DIDMethodNetwork is map for did methods and their blockchain networks
export const DidMethodNetwork: {
[k: string]: { [k: string]: number };
} = Object.freeze({
[k: DidMethodName]: { [k: string]: number };
} = {
[DidMethod.Iden3]: {
[`${Blockchain.ReadOnly}:${NetworkId.NoNetwork}`]: 0b00000000,
[`${Blockchain.Polygon}:${NetworkId.Main}`]: 0b00010000 | 0b00000001,
Expand All @@ -100,4 +131,4 @@ export const DidMethodNetwork: {
[DidMethod.Other]: {
[`${Blockchain.Unknown}:${NetworkId.Unknown}`]: 0b11111111
}
});
};
32 changes: 14 additions & 18 deletions src/did/did-helper.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import {
Blockchain,
BlockchainName,
Constants,
DidMethodByte,
DidMethodName,
DidMethodNetwork,
DidMethod,
NetworkId
NetworkName
} from '../constants';

// DIDNetworkFlag is a structure to represent DID blockchain and network id
export class DIDNetworkFlag {
constructor(public readonly blockchain: Blockchain, public readonly networkId: NetworkId) {}
constructor(public readonly blockchain: BlockchainName, public readonly networkId: NetworkName) {}

toString(): string {
return `${this.blockchain}:${this.networkId}`;
}

static fromString(s: string): DIDNetworkFlag {
const [blockchain, networkId] = s.split(':');
return new DIDNetworkFlag(
blockchain.replace('_', '') as Blockchain,
networkId.replace('_', '') as NetworkId
);
return new DIDNetworkFlag(blockchain.replace('_', ''), networkId.replace('_', ''));
}
}

// BuildDIDType builds bytes type from chain and network
export function buildDIDType(
method: DidMethod,
blockchain: Blockchain,
network: NetworkId
): Uint8Array {
export function buildDIDType(method: string, blockchain: string, network: string): Uint8Array {
const fb = DidMethodByte[method];
if (!fb) {
throw Constants.ERRORS.UNSUPPORTED_DID_METHOD;
Expand All @@ -53,7 +46,10 @@ export function buildDIDType(
}

// FindNetworkIDForDIDMethodByValue finds network by byte value
export function findNetworkIDForDIDMethodByValue(method: DidMethod, byteNumber: number): NetworkId {
export function findNetworkIDForDIDMethodByValue(
method: DidMethodName,
byteNumber: number
): NetworkName {
const methodMap = DidMethodNetwork[method];
if (!methodMap) {
throw Constants.ERRORS.UNSUPPORTED_DID_METHOD;
Expand All @@ -68,9 +64,9 @@ export function findNetworkIDForDIDMethodByValue(method: DidMethod, byteNumber:

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

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

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

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

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

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

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

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

static isUnsupported(method: DidMethod, blockchain: Blockchain, networkId: NetworkId): boolean {
static isUnsupported(
method: DidMethodName,
blockchain: BlockchainName,
networkId: NetworkName
): boolean {
return (
method == DidMethod.Other &&
blockchain == Blockchain.Unknown &&
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './elemBytes';
export * from './id';
export * from './schemaHash';
export * from './utils';
export * from './registration';
79 changes: 79 additions & 0 deletions src/registration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
Blockchain,
BlockchainName,
ChainIds,
DidMethod,
DidMethodByte,
DidMethodName,
DidMethodNetwork,
NetworkId,
NetworkName
} from './constants';

const registerDidMethodWithByte = (method: DidMethodName, 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`);
}

DidMethodByte[method] = byte;
};

const registerChainId = (blockchain: string, network: string, chainId?: number): void => {
if (!chainId) {
return;
}

if (network) {
blockchain += `:${network}`;
}

ChainIds[blockchain] = chainId;
};

export const getChainId = (blockchain: string, network?: string): number => {
if (network) {
blockchain += `:${network}`;
}
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`);
}
DidMethodNetwork[method][key] = networkFlag;
};
Loading