Skip to content

Commit

Permalink
fix: Date calculation && Did validation
Browse files Browse the repository at this point in the history
Signed-off-by: DaevMithran <[email protected]>
  • Loading branch information
DaevMithran committed Apr 10, 2023
1 parent 237feb5 commit c831a97
Show file tree
Hide file tree
Showing 9 changed files with 1,243 additions and 749 deletions.
2 changes: 1 addition & 1 deletion packages/cheqd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@aries-framework/anoncreds": "0.3.3",
"@aries-framework/core": "0.3.3",
"@cheqd/sdk": "cjs",
"@cheqd/ts-proto": "^2.0.1",
"@cheqd/ts-proto": "cjs",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"rxjs": "^7.2.0",
Expand Down
10 changes: 4 additions & 6 deletions packages/cheqd/src/anoncreds/services/CheqdAnonCredsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class CheqdAnonCredsRegistry implements AnonCredsRegistry {
const schema = options.schema
const schemaResource = {
id: utils.uuid(),
name: schema.name,
name: `${schema.name}-Schema`,
resourceType: 'anonCredsSchema',
data: {
name: schema.name,
Expand Down Expand Up @@ -134,10 +134,10 @@ export class CheqdAnonCredsRegistry implements AnonCredsRegistry {
try {
const cheqdDidRegistrar = agentContext.dependencyManager.resolve(CheqdDidRegistrar)
const { credentialDefinition } = options

const schema = await this.getSchema(agentContext, credentialDefinition.schemaId)
const credDefResource = {
id: utils.uuid(),
name: credentialDefinition.tag,
name: `${schema.schema?.name}-${credentialDefinition.tag}-CredDef`,
resourceType: 'anonCredsCredDef',
data: {
type: credentialDefinition.type,
Expand Down Expand Up @@ -315,9 +315,7 @@ export class CheqdAnonCredsRegistry implements AnonCredsRegistry {

return {
revocationStatusList: {
revRegId: revocationStatusList.revRegDefId,
revocationList: revocationStatusList.revocationList,
currentAccumulator: revocationStatusList.currentAccumulator,
...revocationStatusList,
issuerId: parsedDid.did,
timestamp: statusListTimestamp,
},
Expand Down
30 changes: 20 additions & 10 deletions packages/cheqd/src/anoncreds/utils/identifiers.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import type { ParsedDid } from '@aries-framework/core'

const UUID = '([a-z,0-9,-]{36,36})'
const ID_CHAR = `(?:[a-zA-Z0-9]{21,22}|${UUID})`
import { TypedArrayEncoder, utils } from '@aries-framework/core'
import { isBase58 } from 'class-validator'

const ID_CHAR = '([a-z,A-Z,0-9,-])'
const NETWORK = '(testnet|mainnet)'
const METHOD_ID = `((?:${ID_CHAR}*:)*(${ID_CHAR}+))`
const IDENTIFIER = `((?:${ID_CHAR}*:)*(${ID_CHAR}+))`
const PATH = `(/[^#?]*)?`
const QUERY = `([?][^#]*)?`
const VERSION_ID = `(.*?)`

export const cheqdSdkAnonCredsRegistryIdentifierRegex = new RegExp(
`^did:cheqd:${NETWORK}:${METHOD_ID}${PATH}${QUERY}$`
`^did:cheqd:${NETWORK}:${IDENTIFIER}${PATH}${QUERY}$`
)

export const cheqdDidRegex = new RegExp(`^did:cheqd:${NETWORK}:${METHOD_ID}${QUERY}$`)
export const cheqdDidVersionRegex = new RegExp(`^did:cheqd:${NETWORK}:${METHOD_ID}/version/${VERSION_ID}${QUERY}$`)
export const cheqdDidVersionsRegex = new RegExp(`^did:cheqd:${NETWORK}:${METHOD_ID}/versions${QUERY}$`)
export const cheqdDidMetadataRegex = new RegExp(`^did:cheqd:${NETWORK}:${METHOD_ID}/metadata${QUERY}$`)
export const cheqdResourceRegex = new RegExp(`^did:cheqd:${NETWORK}:${METHOD_ID}/resources/${UUID}${QUERY}$`)
export const cheqdDidRegex = new RegExp(`^did:cheqd:${NETWORK}:${IDENTIFIER}${QUERY}$`)
export const cheqdDidVersionRegex = new RegExp(`^did:cheqd:${NETWORK}:${IDENTIFIER}/version/${VERSION_ID}${QUERY}$`)
export const cheqdDidVersionsRegex = new RegExp(`^did:cheqd:${NETWORK}:${IDENTIFIER}/versions${QUERY}$`)
export const cheqdDidMetadataRegex = new RegExp(`^did:cheqd:${NETWORK}:${IDENTIFIER}/metadata${QUERY}$`)
export const cheqdResourceRegex = new RegExp(`^did:cheqd:${NETWORK}:${IDENTIFIER}/resources/${IDENTIFIER}${QUERY}$`)
export const cheqdResourceMetadataRegex = new RegExp(
`^did:cheqd:${NETWORK}:${METHOD_ID}/resources/${UUID}/metadata${QUERY}`
`^did:cheqd:${NETWORK}:${IDENTIFIER}/resources/${IDENTIFIER}/metadata${QUERY}`
)

export type ParsedCheqdDid = ParsedDid & { network: string }
export function parseCheqdDid(didUrl: string): ParsedCheqdDid | null {
if (didUrl === '' || !didUrl) return null
const sections = didUrl.match(cheqdSdkAnonCredsRegistryIdentifierRegex)
if (sections) {
if (
!(
utils.isValidUuid(sections[2]) ||
(isBase58(sections[2]) && TypedArrayEncoder.fromBase58(sections[2]).length == 16)
)
) {
return null
}
const parts: ParsedCheqdDid = {
did: `did:cheqd:${sections[1]}:${sections[2]}`,
method: 'cheqd',
Expand Down
2 changes: 1 addition & 1 deletion packages/cheqd/src/anoncreds/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class CheqdRevocationRegistryDefinition {
}

export class CheqdRevocationStatusList {
public constructor(options: Omit<AnonCredsRevocationStatusList, 'issuerId' | 'revRegId'> & { revRegDefId: string }) {
public constructor(options: Omit<AnonCredsRevocationStatusList, 'issuerId'>) {
if (options) {
this.revRegDefId = options.revRegDefId
this.revocationList = options.revocationList
Expand Down
2 changes: 1 addition & 1 deletion packages/cheqd/src/dids/CheqdDidResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class CheqdDidResolver implements DidResolver {
if (params.version) {
resource = resources.find((resource) => resource.version == params.version)
} else {
const date = params.resourceTime ? new Date(params.resourceTime) : new Date()
const date = params.resourceTime ? new Date(Number(params.resourceTime) * 1000) : new Date()
// find the resourceId closest to the created time
resource = getClosestResourceVersion(resources, date)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/cheqd/tests/cheqd-sdk-anoncreds-registry.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,11 @@ describe('cheqdAnonCredsRegistry', () => {
const revocationStatusListResponse = await cheqdAnonCredsRegistry.getRevocationStatusList(
agent.context,
revocationRegistryId,
new Date().getUTCSeconds()
1680789403
)

expect(revocationStatusListResponse.revocationStatusList).toMatchObject({
revRegId: `${revocationRegistryId}&resourceType=anonCredsRevocRegDef`,
revRegDefId: `${revocationRegistryId}&resourceType=anonCredsRevocRegDef`,
revocationList: [
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ export { Hasher } from './utils/Hasher'
export { MessageValidator } from './utils/MessageValidator'
export { LinkedAttachment, LinkedAttachmentOptions } from './utils/LinkedAttachment'
import { parseInvitationUrl } from './utils/parseInvitation'
import { uuid } from './utils/uuid'
import { uuid, isValidUuid } from './utils/uuid'

const utils = {
uuid,
isValidUuid,
parseInvitationUrl,
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utils/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { v4 } from 'uuid'
import { v4, validate } from 'uuid'

export function uuid() {
return v4()
}

export function isValidUuid(id: string) {
return validate(id)
}
Loading

0 comments on commit c831a97

Please sign in to comment.