Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Dec 2, 2024
1 parent d085050 commit d41ecff
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
u8aToString,
} from '@docknetwork/credential-sdk/utils';
import { CheqdParamsId } from '@docknetwork/credential-sdk/types';
import { createInternalCheqdModule } from './builders';
import createInternalCheqdModule from './create-internal-cheqd-module';
import { CheqdCreateResource } from './payload';

const methods = {
Expand Down
47 changes: 35 additions & 12 deletions packages/credential-sdk/src/types/did/document/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,43 @@ export class CheqdDIDDocument extends TypedStruct {
versionId: option(VersionId),
};

constructor(...args) {
super(...args);
constructor(
context,
id,
alsoKnownAs,
controller,
rawVerificationMethodWithOffchainKeys,
service,
authentication,
assertionMethodWithOffchainKeys,
keyAgreement,
capabilityInvocation,
capabilityDelegation,
versionId = TypedUUID.random(),
) {
const verificationMethodWithOffchainKeys = [...rawVerificationMethodWithOffchainKeys].map((verMethod) => CheqdVerificationMethod.from(verMethod));

const offchainVerMethod = [
...this.verificationMethod.filter((verMethod) => verMethod.isOffchain()),
].map((verMethod) => verMethod.toVerificationMethod());
this.verificationMethod = this.verificationMethod.filter(
const offchainVerMethod = verificationMethodWithOffchainKeys.filter((verMethod) => verMethod.isOffchain());
const verificationMethod = verificationMethodWithOffchainKeys.filter(
(verMethod) => !verMethod.isOffchain(),
);

this.assertionMethod = [
...this.assertionMethod,
...[...offchainVerMethod].map((verMethod) => verMethod.toVerificationMethodRefWithDidKey()),
const assertionMethod = [
...AssertionMethod.from(assertionMethodWithOffchainKeys).filter((ref) => !offchainVerMethod.some((verMethod) => verMethod.id.eq(ref))),
...[...offchainVerMethod].map((verMethod) => verMethod.toVerificationMethod().toVerificationMethodRefWithDidKey()),
];

super(context,
id,
alsoKnownAs,
controller,
verificationMethod,
service,
authentication,
assertionMethod,
keyAgreement,
capabilityInvocation,
capabilityDelegation,
versionId);
}

toDIDDocument() {
Expand All @@ -345,8 +368,8 @@ export class CheqdDIDDocument extends TypedStruct {
(keyRefOrKey) => keyRefOrKey instanceof VerificationMethodRefWithDidKey,
),
].map((verMethod) => verMethod.toVerificationMethod());
const assertionMethodWithoutOffchainKeys = assertionMethod.filter(
(keyRefOrKey) => !(keyRefOrKey instanceof VerificationMethodRefWithDidKey),
const assertionMethodWithoutOffchainKeys = [...assertionMethod].map(
(keyRefOrKey) => (keyRefOrKey instanceof VerificationMethodRefWithDidKey ? keyRefOrKey.ref : keyRefOrKey),
);

return new DIDDocument(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {
import { Ed25519Verification2018Method, Ed25519Verification2020Method, VerificationMethodType } from './verification-method-type';
import VerificationMethodRef from './verification-method-ref';
import { NamespaceDid } from '../onchain/typed-did';
import { fmtIter, valueBytes } from '../../../utils';
import {
fmtIter, valueBytes,
} from '../../../utils';
import { DidKeyValue } from '../onchain/did-key';

export class PublicKeyBase58 extends withBase58(TypedBytes) {}
Expand Down
79 changes: 57 additions & 22 deletions packages/credential-sdk/src/types/generic/with-base58.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
import withFrom from './with-from';
import { decodeFromBase58, encodeAsBase58 } from '../../utils/base-x';
import {
decodeFromBase58, encodeAsBase58, isEqualToOrPrototypeOf, u8aToString, stringToU8a, maybeToJSONString,
} from '../../utils';
import TypedEnum from './typed-enum';

export default function withBase58(klass) {
const name = `withBase58(${klass.name})`;
const obj = {
[name]: class extends klass {
toString() {
return this.toBase58();
}

toJSON() {
return String(this);
}

static fromBase58(str) {
return this.from(decodeFromBase58(str));
}

toBase58() {
return encodeAsBase58(this.bytes);
}
},
};

return withFrom(obj[name], function from(value, fromFn) { return (typeof value === 'string' ? this.fromBase58(value) : fromFn(value)); });
let res;

if (!isEqualToOrPrototypeOf(TypedEnum, klass)) {
const obj = {
[name]: class extends klass {
toString() {
return this.toBase58();
}

toJSON() {
return String(this);
}

static fromBase58(str) {
return this.from(decodeFromBase58(str));
}

toBase58() {
return encodeAsBase58(this.bytes);
}
},
};

res = obj[name];
} else {
const obj = {
[name]: class extends klass {
static Class = klass.Class;

static Variants = !klass.Class ? klass.Variants?.map(withBase58) : klass.Variants;

toString() {
return this.toBase58();
}

static fromBase58(str) {
return this.fromJSON(JSON.parse(u8aToString(decodeFromBase58(str))));
}

toBase58() {
return encodeAsBase58(this.bytes);
}

get bytes() {
return stringToU8a(maybeToJSONString(this));
}
},
};

res = obj[name];
}

return withFrom(res, function from(value, fromFn) { return (typeof value === 'string' ? this.fromBase58(value) : fromFn(value)); });
}
Loading

0 comments on commit d41ecff

Please sign in to comment.