Skip to content

Commit

Permalink
Deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Feb 4, 2025
1 parent 13e151a commit b958c91
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { valueBytes } from '../../utils';
import { CheqdMainnetDid, CheqdTestnetDid, DidRef } from '../did';
import { CheqdDidRef, CheqdMainnetDid, CheqdTestnetDid } from '../did';
import {
TypedBytes,
TypedEnum,
Expand Down Expand Up @@ -66,66 +66,22 @@ export class DockAccumulatorId extends AccumulatorId {
}
}

export class CheqdAccumulatorIdValue extends withQualifier(DidRef) {
export class CheqdAccumulatorIdValue extends CheqdDidRef {
static Qualifier = 'accumulator:cheqd:';

static Ident = withFromDockId(
TypedUUID,
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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/src/types/accumulator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export * from './params';
export * from './public-key';
export * from './accumulator';
export * from './counters';
export * from './accumulator-id';
export * from './id';
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 11 additions & 3 deletions packages/credential-sdk/src/types/blob/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
) {}
33 changes: 33 additions & 0 deletions packages/credential-sdk/src/types/did/onchain/typed-did/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 2 additions & 42 deletions packages/credential-sdk/src/types/status-list-credential/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -64,62 +64,22 @@ export class DockStatusListCredentialId extends StatusListCredentialId {
}
}

export class CheqdStatusListCredentialIdValue extends withQualifier(DidRef) {
export class CheqdStatusListCredentialIdValue extends CheqdDidRef {
static Qualifier = CheqdStatusList2021Qualifier;

static Ident = withFromDockId(
TypedUUID,
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 {
Expand Down

0 comments on commit b958c91

Please sign in to comment.