Skip to content

Commit

Permalink
refactor: improve did logic to interact with did registry obtained fr…
Browse files Browse the repository at this point in the history
…om did
  • Loading branch information
eum602 committed Jun 6, 2023
1 parent e5f1289 commit e3eccd4
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .example.env
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ SECP256K1_SIGN_ETHEREUM_TRANSACTION=/secp256k1/sign/ethereum-tx

# Did Registry
CHAIN_ID = 0x9e55c
# DID_REGISTRY_ADDRESS = 0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54 # optional address, just in case you are willing to use another did registry
# CUSTOM_DID_REG = '0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54,lac1,0001,0x9e55c' #'didRegistryAddress,didType,didVersion,chainId'
# DOMAIN_NAME = lacchain.id # optional param just in case you are willing to use another domain name


Expand Down
2 changes: 1 addition & 1 deletion .example.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ SECP256K1_SIGN_LACCHAIN_TRANSACTION=/secp256k1/sign/lacchain-tx

# Did Registry
CHAIN_ID = 0x9e55c
# DID_REGISTRY_ADDRESS = 0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54 # optional address, just in case you are willing to use another did registry
# CUSTOM_DID_REG = '0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54,lac1,0001,0x9e55c' #'didRegistryAddress,didType,didVersion,chainId'
# DOMAIN_NAME = lacchain.id # optional param just in case you are willing to use another domain name


Expand Down
6 changes: 3 additions & 3 deletions docs/API-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ api_url=http://localhost:3001 # Set LACPass API url
2. Create a did

```sh
create_did_url="$api_url"/api/v1/did-lac1
create_did_url="$api_url"/api/v1/did/lac1
r=`curl -s -X 'POST' \
${create_did_url} \
-H 'accept: application/json' \
Expand All @@ -34,7 +34,7 @@ echo "Did was created: $did"
```sh
## input variables
path_to_crt=../certs/DSC/DSC.crt # you should point to the public pem certificate that represents the signing certificate used to sign health related data
did="did:lac1:1iT4ksoK9qcxYYe8vGu2fZEDsYHCkJ7FkpxMeSJUrgLRDMmH3g5hKGry4H9bqGpZJPpB" # replace with the did previously created
did="did:lac1:1iT4XSXrcTkcUJ7mMgSW9eWubkNkShNLjfau6T3mSg6fyXAuj2DdaHW55eFQ6D9hjq7w" # replace with the did previously created


# process
Expand All @@ -49,7 +49,7 @@ curl -X 'POST' ${add_pem_certificate_url} -H 'accept: application/json' -F x509C
```sh
## input variables
path_to_crt=../certs/DSC/DSC.crt # you should point to the public pem certificate that represents the signing certificate used to sign health related data
did="did:lac1:1iT5SDUTHiVkK8ESyuH9VyKYmURs2VWk7xcCXPG2kGy8Uw79miZ5MAsjL8XWhtnZQoWh" # replace with the did previously created
did="did:lac1:1iT4XSXrcTkcUJ7mMgSW9eWubkNkShNLjfau6T3mSg6fyXAuj2DdaHW55eFQ6D9hjq7w" # replace with the did previously created
compromised=false
backwardRevocationDays=0 # backward revocation days

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lacpass-identity",
"version": "0.0.23",
"version": "0.0.24",
"description": "Rest api for laccpass identity manager",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
105 changes: 79 additions & 26 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { randomUUID } from 'crypto';
import {
DEFAULT_DID_REGISTRY,
DEFAULT_DOMAIN_NAME,
DEFAULT_REGISTRY,
DID_CODE,
DOMAIN_NAMES,
REGISTRY
SUPPORTED_DID_TYPES
} from '../constants/did-web/lac/didRegistryAddresses';
import { config } from 'dotenv';
import { ethers } from 'ethers';
import { LogLevel } from 'typescript-logging';
import { Log4TSProvider } from 'typescript-logging-log4ts-style';
import { DidRegistryParams } from 'src/interfaces/did/did.generics';

config({ path: `.env.${process.env.ENV || 'dev'}` });

Expand Down Expand Up @@ -64,35 +66,87 @@ export const TESTING_ENV = process.env.ENV === 'test';
export const CI_ENV = process.env.ENV === 'ci';
export const JWT_SECRET_DEFAULT = 'some-secret-string-default';

export const resolveDidRegistryAddress = (
didRegistryAddress = process.env.DID_REGISTRY_ADDRESS
): string => {
// const didRegistryAddress = process.env.DID_REGISTRY_ADDRESS;
if (didRegistryAddress) {
if (!ethers.utils.isAddress(didRegistryAddress)) {
export const resolveChainRegistry = (): DidRegistryParams => {
const supportedChainId = getChainId();
const customDidRegEnv = process.env.CUSTOM_DID_REG;
// 'didRegistryAddress,didType,didVersion,chainId'
if (!customDidRegEnv) {
// return default did registry
const defaultDidRegistry = DEFAULT_DID_REGISTRY.get(supportedChainId);
if (!defaultDidRegistry) {
log.error(
'Specified DID_REGISTRY_ADDRESS',
DID_REGISTRY_ADDRESS,
'is not a valid address ... exiting'
'There is no default did registry for chainId ' + supportedChainId
);
process.exit(1); // exiting since this is a critical error
}
if (
didRegistryAddress &&
!REGISTRY.get(CHAIN_ID)?.find(el => el === didRegistryAddress)
) {
log.info('Unknown specified did registry address ', didRegistryAddress);
process.exit(1);
}
log.info('Returning custom did registry address', didRegistryAddress);
return didRegistryAddress;
return {
...defaultDidRegistry,
chainId: supportedChainId
};
}
const customDidRegParam = customDidRegEnv.split(',');
if (customDidRegParam.length !== 4) {
log.error(
'Invalid number of param when reading CUSTOM_DID_REG environemt variable, read:',
customDidRegEnv
);
process.exit(1);
}
const [didRegistryAddress, didMethod, didVersion, chainId] =
customDidRegParam;
const didType = DID_CODE.get(didMethod);
if (!didType) {
log.error('did type not supported, read did method:', didMethod);
process.exit(1);
}
// validate type, version tuple is supported
const isSupportedDidType = SUPPORTED_DID_TYPES.get(didType);
if (
!isSupportedDidType ||
(isSupportedDidType && !isSupportedDidType.get(didVersion))
) {
log.error(
'Unsupported did type/didVersion, read didType:',
didType,
'read didVersion:',
didVersion
);
process.exit(1);
}
// validate chainId is supported
if (chainId !== supportedChainId) {
log.error(
'Unsupported chainId passed alongside custom did registry, read chainId:',
chainId,
'supported version:',
supportedChainId
);
process.exit(1);
}
const wellKnownRegistryAddress = DEFAULT_REGISTRY.get(CHAIN_ID);
if (!wellKnownRegistryAddress) {
log.error('Could not find well-known registry address for chain', CHAIN_ID);
// add registry
if (!ethers.utils.isAddress(didRegistryAddress)) {
log.error(
'Specified did registry address: ',
didRegistryAddress,
', is not a valid address ... exiting'
);
process.exit(1); // exiting since this is a critical error
}
log.info('Returning default registry address', wellKnownRegistryAddress);
return wellKnownRegistryAddress;
log.info(
'Returning custom did registry address',
didRegistryAddress,
'chainId:',
chainId,
'didMethod:',
didMethod
);
return {
didRegistryAddress,
didMethod,
didType,
didVersion,
chainId: supportedChainId
};
};

export const resolveDidDomainName = (): string => {
Expand Down Expand Up @@ -120,7 +174,6 @@ export const resolveDidDomainName = (): string => {
return defaultDomainName;
};

export const DID_REGISTRY_ADDRESS = resolveDidRegistryAddress();
export const DOMAIN_NAME = resolveDidDomainName();

export const {
Expand Down
53 changes: 49 additions & 4 deletions src/constants/did-web/lac/didRegistryAddresses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
export const REGISTRY: Map<string, string[]> = new Map<string, string[]>();
REGISTRY.set('0x9e55c', ['0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54']);
export const DEFAULT_REGISTRY: Map<string, string> = new Map<string, string>();
DEFAULT_REGISTRY.set('0x9e55c', '0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54');
import { DidSpec } from 'src/interfaces/did/did.generics';

type didversionType = Map<string, boolean>;
type didType = Map<string, didversionType>;
type didRegistryType = Map<string, didType>;
type chainregistryType = Map<string, didRegistryType>;

export const DID_LAC1_NAME = 'lac1';
export const DID_CODE = new Map<string, string>([[DID_LAC1_NAME, '0001']]);
export const OPEN_PROTESTESNET_CHAIN_ID = '0x9e55c';

export const SUPPORTED_DID_TYPES = new Map<string, didversionType>([
['0001', new Map([['0001', true]])]
]);

// setting for open protestnet
export const CHAIN_REGISTRY: chainregistryType = new Map<
string,
didRegistryType
>();
const openProtestnetSupportedRegistries: didRegistryType = new Map<
string,
didType
>();
openProtestnetSupportedRegistries.set(
'0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54',
new Map([['0001', new Map([['0001', true]])]])
);
CHAIN_REGISTRY.set(
OPEN_PROTESTESNET_CHAIN_ID,
openProtestnetSupportedRegistries
);

export const DEFAULT_DID_REGISTRY: Map<string, DidSpec> = new Map<
string,
DidSpec
>([
[
OPEN_PROTESTESNET_CHAIN_ID,
{
didType: '0001',
didVersion: '0001',
didRegistryAddress: '0x54358D929CCf45C7cCEE8Ca60FCD0C0402489F54',
didMethod: 'lac1'
}
]
]);

// WIP: for did:web
export const DOMAIN_NAMES: string[] = ['lacchain.id', 'another.lacchain.id'];
export const DEFAULT_DOMAIN_NAME = 'lacchain.id';
5 changes: 4 additions & 1 deletion src/constants/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export enum ErrorsMessages {
// eslint-disable-next-line max-len
LACCHAIN_CONTRACT_TRANSACTION_ERROR = 'There was an error, there may be an issue with the params you are sending',
// eslint-disable-next-line max-len
UNEXPECTED_RESPONSE_IN_SUCCESSFUL_TRANSACTION_ERROR = 'Transaction was successfully completed but received an unexpected response'
UNEXPECTED_RESPONSE_IN_SUCCESSFUL_TRANSACTION_ERROR = 'Transaction was successfully completed but received an unexpected response',
UNSUPPORTED_CHAIN_ID_IN_DID = 'Unsupported chainId was found in DID',
UNSUPPORTED_DID_TYPE = 'Unsupported DID type',
UNSUPPORTED_DID_VERSION = 'Unsupported DID version'
}

export const Errors = {
Expand Down
14 changes: 14 additions & 0 deletions src/interfaces/did/did.generics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface DidSpec {
didMethod: string;
didType: string;
didVersion: string;
didRegistryAddress: string;
}

export interface DidRegistryParams {
didMethod: string;
didType: string;
didVersion: string;
didRegistryAddress: string;
chainId: string;
}
Loading

0 comments on commit e3eccd4

Please sign in to comment.