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
196 changes: 166 additions & 30 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,92 @@ export const Constants = Object.freeze({
GENESIS_LENGTH: 27
});

export enum Blockchain {
Ethereum = 'eth',
Polygon = 'polygon',
ZkEVM = 'zkevm',
Unknown = 'unknown',
NoChain = '',
ReadOnly = 'readonly'
}

export enum NetworkId {
Main = 'main',
Mumbai = 'mumbai',
Goerli = 'goerli',
Sepolia = 'sepolia',
Test = 'test',
Unknown = 'unknown',
NoNetwork = ''
}

export enum DidMethod {
Iden3 = 'iden3',
PolygonId = 'polygonid',
Other = ''
}

export const DidMethodByte: { [key: string]: number } = Object.freeze({
export type BlockchainName = 'eth' | 'polygon' | 'zkevm' | 'unknown' | 'readonly' | '' | string;

export const Blockchain: { [k: BlockchainName]: string } = {
Ethereum: 'eth',
Polygon: 'polygon',
ZkEVM: 'zkevm',
Unknown: 'unknown',
NoChain: '',
ReadOnly: 'readonly'
};

export const registerBlockchain = (
name: BlockchainName,
value: BlockchainName | null = null
): void => {
if (Blockchain[name]) {
throw new Error(`blockchain ${name} already registered`);
}
Blockchain[name] = value ?? name;
};

export type NetworkName =
vmidyllic marked this conversation as resolved.
Show resolved Hide resolved
| 'main'
| 'mumbai'
| 'goerli'
| 'sepolia'
| 'test'
| 'unknown'
| ''
| string;

export const NetworkId: { [k: NetworkName]: NetworkName } = {
Main: 'main',
Mumbai: 'mumbai',
Goerli: 'goerli',
Sepolia: 'sepolia',
Test: 'test',
Unknown: 'unknown',
NoNetwork: ''
};

export const registerNetworkId = (name: NetworkName, value: NetworkName | null = null): void => {
if (NetworkId[name]) {
throw new Error(`network ${name} already registered`);
}
NetworkId[name] = value ?? name;
};

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

export const DidMethod: { [k: DidMethodName]: DidMethodName } = {
Iden3: 'iden3',
PolygonId: 'polygonid',
Other: ''
};

export const registerDidMethod = (
name: DidMethodName,
value: DidMethodName | null = null
): void => {
if (DidMethod[name]) {
throw new Error(`did method ${name} already registered`);
}
DidMethod[name] = value ?? name;
};

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

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;
};

// 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 +151,89 @@ export const DidMethodNetwork: {
[DidMethod.Other]: {
[`${Blockchain.Unknown}:${NetworkId.Unknown}`]: 0b11111111
}
});
};

export const registerDidMethodNetwork = (
method: DidMethodName,
blockchain: BlockchainName,
network: NetworkName,
networkFlag: number
): void => {
if (!DidMethod[method]) {
throw new Error(`did method ${method} not registered`);
}

if (!Blockchain[blockchain]) {
throw new Error(`blockchain ${blockchain} not registered`);
}

if (!NetworkId[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 registerDidMethodNetworkForce = (
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
method: string,
blockchain: string,
network: string
): 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;
}

if (!DidMethodNetwork[method]) {
DidMethodNetwork[method] = {};
}
const key = `${blockchain}:${network}`;
const networkFlag = DidMethodNetwork[method][key];
if (typeof networkFlag === '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;
};
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
Loading