From b958c91779692e9228f3d180b5933eeee25c62f3 Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 4 Feb 2025 11:48:42 +0400 Subject: [PATCH] Deduplicate --- .../accumulator/{accumulator-id.js => id.js} | 48 +------------------ .../src/types/accumulator/index.js | 2 +- .../src/types/blob/{blob-id.js => id.js} | 44 +---------------- .../credential-sdk/src/types/blob/index.js | 14 ++++-- .../src/types/did/onchain/typed-did/index.js | 33 +++++++++++++ .../src/types/status-list-credential/id.js | 44 +---------------- 6 files changed, 51 insertions(+), 134 deletions(-) rename packages/credential-sdk/src/types/accumulator/{accumulator-id.js => id.js} (70%) rename packages/credential-sdk/src/types/blob/{blob-id.js => id.js} (67%) diff --git a/packages/credential-sdk/src/types/accumulator/accumulator-id.js b/packages/credential-sdk/src/types/accumulator/id.js similarity index 70% rename from packages/credential-sdk/src/types/accumulator/accumulator-id.js rename to packages/credential-sdk/src/types/accumulator/id.js index 0de91dbb0..f42b11a05 100644 --- a/packages/credential-sdk/src/types/accumulator/accumulator-id.js +++ b/packages/credential-sdk/src/types/accumulator/id.js @@ -1,5 +1,5 @@ import { valueBytes } from '../../utils'; -import { CheqdMainnetDid, CheqdTestnetDid, DidRef } from '../did'; +import { CheqdDidRef, CheqdMainnetDid, CheqdTestnetDid } from '../did'; import { TypedBytes, TypedEnum, @@ -66,7 +66,7 @@ export class DockAccumulatorId extends AccumulatorId { } } -export class CheqdAccumulatorIdValue extends withQualifier(DidRef) { +export class CheqdAccumulatorIdValue extends CheqdDidRef { static Qualifier = 'accumulator:cheqd:'; static Ident = withFromDockId( @@ -74,58 +74,14 @@ export class CheqdAccumulatorIdValue extends withQualifier(DidRef) { DockAccumulatorId, 'accumulator:cheqd:', ); - - static cheqdDid(did) { - return did.value; - } - - static fromUnqualifiedString(str) { - const lastColon = str.lastIndexOf(':'); - const did = `did:cheqd:${str.slice(0, lastColon)}`; - const id = str.slice(lastColon + 1); - - return new this(did, id); - } - - toJSON() { - return String(this); - } - - toEncodedString() { - const { did, value, constructor } = this; - const { cheqdDid } = constructor; - - let prefix = ''; - if (cheqdDid(did) instanceof CheqdTestnetDid) { - prefix = 'testnet'; - } else if (cheqdDid(did) instanceof CheqdMainnetDid) { - prefix = 'mainnet'; - } else { - throw new Error( - `Can't determine DID type: \`${cheqdDid(did)}\`, instance of \`${ - cheqdDid(did).constructor.name - }\``, - ); - } - - return `${prefix}:${did.toEncodedString()}:${value}`; - } } export class CheqdTestnetAccumulatorIdValue extends CheqdAccumulatorIdValue { static Did = CheqdTestnetDid; - - static cheqdDid(did) { - return did; - } } export class CheqdMainnetAccumulatorIdValue extends CheqdAccumulatorIdValue { static Did = CheqdMainnetDid; - - static cheqdDid(did) { - return did; - } } export class CheqdAccumulatorId extends AccumulatorId { diff --git a/packages/credential-sdk/src/types/accumulator/index.js b/packages/credential-sdk/src/types/accumulator/index.js index 628f8868c..d5e97b6ef 100644 --- a/packages/credential-sdk/src/types/accumulator/index.js +++ b/packages/credential-sdk/src/types/accumulator/index.js @@ -3,4 +3,4 @@ export * from './params'; export * from './public-key'; export * from './accumulator'; export * from './counters'; -export * from './accumulator-id'; +export * from './id'; diff --git a/packages/credential-sdk/src/types/blob/blob-id.js b/packages/credential-sdk/src/types/blob/id.js similarity index 67% rename from packages/credential-sdk/src/types/blob/blob-id.js rename to packages/credential-sdk/src/types/blob/id.js index 376f03cc1..5688b13ed 100644 --- a/packages/credential-sdk/src/types/blob/blob-id.js +++ b/packages/credential-sdk/src/types/blob/id.js @@ -12,7 +12,7 @@ import withFromDockId, { patchWithFromDock, } from '../generic/with-from-dock-id'; import { CheqdBlobQualifier, DockBlobQualifier } from './const'; -import { CheqdMainnetDid, CheqdTestnetDid, DidRef } from '../did'; +import { CheqdDidRef, CheqdMainnetDid, CheqdTestnetDid } from '../did'; import dockDidById from '../../utils/dock-did-by-id'; export class BlobId extends withFrom( @@ -59,58 +59,18 @@ export class DockBlobId extends BlobId { } } -export class CheqdBlobIdValue extends withQualifier(DidRef) { +export class CheqdBlobIdValue extends CheqdDidRef { static Qualifier = CheqdBlobQualifier; static Ident = withFromDockId(TypedUUID, DockBlobId, 'blob:cheqd:'); - - static fromUnqualifiedString(str) { - const lastColon = str.lastIndexOf(':'); - const did = `did:cheqd:${str.slice(0, lastColon)}`; - const id = str.slice(lastColon + 1); - - return new this(did, id); - } - - static cheqdDid(did) { - return did.value; - } - - toEncodedString() { - const { did, value, constructor } = this; - const cheqdDid = constructor.cheqdDid(did); - - let prefix = ''; - if (cheqdDid instanceof CheqdTestnetDid) { - prefix = 'testnet'; - } else if (cheqdDid instanceof CheqdMainnetDid) { - prefix = 'mainnet'; - } else { - throw new Error( - `Can't determine DID type: \`${cheqdDid(did)}\`, instance of \`${ - cheqdDid(did).constructor.name - }\``, - ); - } - - return `${prefix}:${did.toEncodedString()}:${value}`; - } } export class CheqdTestnetBlobIdValue extends CheqdBlobIdValue { static Did = CheqdTestnetDid; - - static cheqdDid(did) { - return did; - } } export class CheqdMainnetBlobIdValue extends CheqdBlobIdValue { static Did = CheqdMainnetDid; - - static cheqdDid(did) { - return did; - } } export class CheqdBlobId extends BlobId { diff --git a/packages/credential-sdk/src/types/blob/index.js b/packages/credential-sdk/src/types/blob/index.js index 9348618d3..7b98e12fc 100644 --- a/packages/credential-sdk/src/types/blob/index.js +++ b/packages/credential-sdk/src/types/blob/index.js @@ -2,7 +2,7 @@ import { TypedStruct, withProp } from '../generic'; import { BlobId, CheqdBlobIdValue, DockBlobIdValue } from './blob-id'; import Blob from './blob'; -export * from './blob-id'; +export * from './id'; export { default as Blob } from './blob'; export class BlobWithId extends TypedStruct { @@ -12,5 +12,13 @@ export class BlobWithId extends TypedStruct { }; } -export class CheqdBlobWithId extends withProp(BlobWithId, 'id', CheqdBlobIdValue) {} -export class DockBlobWithId extends withProp(BlobWithId, 'id', DockBlobIdValue) {} +export class CheqdBlobWithId extends withProp( + BlobWithId, + 'id', + CheqdBlobIdValue, +) {} +export class DockBlobWithId extends withProp( + BlobWithId, + 'id', + DockBlobIdValue, +) {} diff --git a/packages/credential-sdk/src/types/did/onchain/typed-did/index.js b/packages/credential-sdk/src/types/did/onchain/typed-did/index.js index 51c1dc369..3e30d0027 100644 --- a/packages/credential-sdk/src/types/did/onchain/typed-did/index.js +++ b/packages/credential-sdk/src/types/did/onchain/typed-did/index.js @@ -235,6 +235,39 @@ export class DidRef extends withExtendedStaticProperties( } } +export class CheqdDidRef extends withQualifier(DidRef) { + static Did = CheqdDid; + + static fromUnqualifiedString(str) { + const lastColon = str.lastIndexOf(':'); + const did = `did:cheqd:${str.slice(0, lastColon)}`; + const id = str.slice(lastColon + 1); + + return new this(did, id); + } + + toJSON() { + return String(this); + } + + toEncodedString() { + const { did, value } = this; + + let prefix = ''; + if (did.isTestnet) { + prefix = 'testnet'; + } else if (did.isMainnet) { + prefix = 'mainnet'; + } else { + throw new Error( + `Can't determine DID type: \`${did}\`, instance of \`${did.constructor.name}\``, + ); + } + + return `${prefix}:${did.toEncodedString()}:${value}`; + } +} + DidOrDidMethodKeySignature.bindVariants( DockDidSignature, DidMethodKeySignature, diff --git a/packages/credential-sdk/src/types/status-list-credential/id.js b/packages/credential-sdk/src/types/status-list-credential/id.js index 6aa211d2d..992c5aab3 100644 --- a/packages/credential-sdk/src/types/status-list-credential/id.js +++ b/packages/credential-sdk/src/types/status-list-credential/id.js @@ -9,7 +9,7 @@ import { import withFromDockId, { patchWithFromDock, } from '../generic/with-from-dock-id'; -import { CheqdMainnetDid, CheqdTestnetDid, DidRef } from '../did'; +import { CheqdDidRef, CheqdMainnetDid, CheqdTestnetDid } from '../did'; import { DockStatusList2021Qualifier, CheqdStatusList2021Qualifier, @@ -64,7 +64,7 @@ export class DockStatusListCredentialId extends StatusListCredentialId { } } -export class CheqdStatusListCredentialIdValue extends withQualifier(DidRef) { +export class CheqdStatusListCredentialIdValue extends CheqdDidRef { static Qualifier = CheqdStatusList2021Qualifier; static Ident = withFromDockId( @@ -72,54 +72,14 @@ export class CheqdStatusListCredentialIdValue extends withQualifier(DidRef) { DockStatusListCredentialId, 'status-list2021:cheqd:', ); - - static fromUnqualifiedString(str) { - const lastColon = str.lastIndexOf(':'); - const did = `did:cheqd:${str.slice(0, lastColon)}`; - const id = str.slice(lastColon + 1); - - return new this(did, id); - } - - static cheqdDid(did) { - return did.value; - } - - toEncodedString() { - const { did, value, constructor } = this; - const { cheqdDid } = constructor; - - let prefix = ''; - if (cheqdDid(did) instanceof CheqdTestnetDid) { - prefix = 'testnet'; - } else if (cheqdDid(did) instanceof CheqdMainnetDid) { - prefix = 'mainnet'; - } else { - throw new Error( - `Can't determine DID type: \`${cheqdDid(did)}\`, instance of \`${ - cheqdDid(did).constructor.name - }\``, - ); - } - - return `${prefix}:${cheqdDid(did).toEncodedString()}:${value}`; - } } class CheqdTestnetStatusListCredentialIdValue extends CheqdStatusListCredentialIdValue { static Did = CheqdTestnetDid; - - static cheqdDid(did) { - return did; - } } class CheqdMainnetStatusListCredentialIdValue extends CheqdStatusListCredentialIdValue { static Did = CheqdMainnetDid; - - static cheqdDid(did) { - return did; - } } export class CheqdStatusListCredentialId extends StatusListCredentialId {