-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aea3272
commit b5f912f
Showing
13 changed files
with
852 additions
and
506 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Request, Response } from 'express' | ||
|
||
import { CustomerService } from '../services/customer' | ||
import { Identity } from '../services/identity' | ||
|
||
export class CustomerController { | ||
|
||
public async create(request: Request, response: Response) { | ||
try { | ||
const kid = (await Identity.instance.createKey('Secp256k1')).kid | ||
const customer = await CustomerService.instance.create(response.locals.customerId, kid) | ||
return response.status(200).json(customer) | ||
} catch (error) { | ||
return response.status(500).json({ | ||
Error: `Error creating customer ${error}` | ||
}) | ||
} | ||
} | ||
|
||
public async update(request: Request, response: Response) { | ||
try { | ||
const result = await CustomerService.instance.update(response.locals.customerId, request.body) | ||
return response.status(200).json(result) | ||
} catch (error) { | ||
return response.status(500).json({ | ||
Error: error | ||
}) | ||
} | ||
} | ||
|
||
public async get(request: Request, response: Response) { | ||
try { | ||
const result = await CustomerService.instance.get(response.locals.customerId) | ||
if(result && !Array.isArray(result)) { | ||
const { account, ...res } = result | ||
return response.status(200).json(res) | ||
} | ||
|
||
return response.status(400).json({ | ||
error: 'Customer not found' | ||
}) | ||
} catch (error) { | ||
return response.status(500).json({ | ||
Error: error | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { DIDDocument } from 'did-resolver' | ||
import type { MethodSpecificIdAlgo, CheqdNetwork } from '@cheqd/sdk' | ||
|
||
import { VerificationMethods, createVerificationKeys, createDidVerificationMethod, createDidPayload } from '@cheqd/sdk' | ||
import { SpecValidationResult } from '../types/types' | ||
|
||
import { fromString } from 'uint8arrays' | ||
import { rawSecp256k1PubkeyToRawAddress } from '@cosmjs/amino' | ||
import { toBech32 } from '@cosmjs/encoding' | ||
import { publicKeyConvert } from 'secp256k1' | ||
|
||
export function validateSpecCompliantPayload(didDocument: DIDDocument): SpecValidationResult { | ||
// id is required, validated on both compile and runtime | ||
if (!didDocument.id && !didDocument.id.startsWith('did:cheqd:')) return { valid: false, error: 'id is required' } | ||
|
||
// verificationMethod is required | ||
if (!didDocument.verificationMethod) return { valid: false, error: 'verificationMethod is required' } | ||
|
||
// verificationMethod must be an array | ||
if (!Array.isArray(didDocument.verificationMethod)) | ||
return { valid: false, error: 'verificationMethod must be an array' } | ||
|
||
// verificationMethod must be not be empty | ||
if (!didDocument.verificationMethod.length) return { valid: false, error: 'verificationMethod must be not be empty' } | ||
|
||
// verificationMethod types must be supported | ||
const isValidVerificationMethod = didDocument.verificationMethod.every((vm) => { | ||
switch (vm.type) { | ||
case VerificationMethods.Ed255192020: | ||
return vm.publicKeyMultibase != null | ||
case VerificationMethods.JWK: | ||
return vm.publicKeyJwk != null | ||
case VerificationMethods.Ed255192018: | ||
return vm.publicKeyBase58 != null | ||
default: | ||
return false | ||
} | ||
}) | ||
|
||
if (!isValidVerificationMethod) return { valid: false, error: 'verificationMethod publicKey is Invalid' } | ||
|
||
const isValidService = didDocument.service | ||
? didDocument?.service?.every((s) => { | ||
return s?.serviceEndpoint && s?.id && s?.type | ||
}) | ||
: true | ||
|
||
if (!isValidService) return { valid: false, error: 'Service is Invalid' } | ||
return { valid: true } as SpecValidationResult | ||
} | ||
|
||
export function generateDidDoc(options: IDidDocOptions) { | ||
const { verificationMethod, methodSpecificIdAlgo, verificationMethodId, network, publicKey } = options | ||
const verificationKeys = createVerificationKeys(publicKey, methodSpecificIdAlgo, verificationMethodId, network) | ||
if (!verificationKeys) { | ||
throw new Error('Invalid DID options') | ||
} | ||
const verificationMethods = createDidVerificationMethod([verificationMethod], [verificationKeys]) | ||
|
||
return createDidPayload(verificationMethods, [verificationKeys]) | ||
} | ||
|
||
export function getCosmosAccount(kid: string) { | ||
return toBech32('cheqd', rawSecp256k1PubkeyToRawAddress(publicKeyConvert(fromString(kid, 'hex'), true))) | ||
} | ||
|
||
export interface IDidDocOptions { | ||
verificationMethod: VerificationMethods | ||
verificationMethodId: any | ||
methodSpecificIdAlgo: MethodSpecificIdAlgo | ||
network: CheqdNetwork | ||
publicKey: string | ||
} |
Oops, something went wrong.