diff --git a/examples/CHANGELOG.md b/examples/CHANGELOG.md index d73301b93..1be6136e4 100644 --- a/examples/CHANGELOG.md +++ b/examples/CHANGELOG.md @@ -1,5 +1,14 @@ # @docknetwork/sdk-examples +## 0.6.7 + +### Patch Changes + +- Updated dependencies + - @docknetwork/credential-sdk@0.21.0 + - @docknetwork/dock-blockchain-api@0.8.7 + - @docknetwork/dock-blockchain-modules@0.11.2 + ## 0.6.6 ### Patch Changes diff --git a/examples/package.json b/examples/package.json index b3b9fd736..39bc30c33 100644 --- a/examples/package.json +++ b/examples/package.json @@ -2,7 +2,7 @@ "name": "@docknetwork/sdk-examples", "private": true, "type": "module", - "version": "0.6.6", + "version": "0.6.7", "scripts": { "bbs-dock-example": "babel-node ./bbs-dock.js", "claim-deduction-example": "babel-node ./claim-deduction.js", @@ -19,9 +19,9 @@ "lint": "eslint \"*.js\"" }, "dependencies": { - "@docknetwork/credential-sdk": "0.20.0", - "@docknetwork/dock-blockchain-api": "0.8.6", - "@docknetwork/dock-blockchain-modules": "0.11.1" + "@docknetwork/credential-sdk": "0.21.0", + "@docknetwork/dock-blockchain-api": "0.8.7", + "@docknetwork/dock-blockchain-modules": "0.11.2" }, "devDependencies": { "babel-eslint": "^10.1.0", diff --git a/package.json b/package.json index 2fb59dd6b..ea654059d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "root", - "version": "0.21.0", + "version": "0.22.0", "private": true, "workspaces": [ "packages/*", diff --git a/packages/cheqd-blockchain-api/CHANGELOG.md b/packages/cheqd-blockchain-api/CHANGELOG.md index 5880a5072..18eb04c6b 100644 --- a/packages/cheqd-blockchain-api/CHANGELOG.md +++ b/packages/cheqd-blockchain-api/CHANGELOG.md @@ -1,5 +1,16 @@ # @docknetwork/cheqd-blockchain-api +## 0.15.0 + +### Minor Changes + +- Serialization/deserialization tweaks for `cheqd` + +### Patch Changes + +- Updated dependencies + - @docknetwork/credential-sdk@0.21.0 + ## 0.14.5 ### Patch Changes diff --git a/packages/cheqd-blockchain-api/package.json b/packages/cheqd-blockchain-api/package.json index 8fb377e86..b87ced1a4 100644 --- a/packages/cheqd-blockchain-api/package.json +++ b/packages/cheqd-blockchain-api/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/cheqd-blockchain-api", - "version": "0.14.5", + "version": "0.15.0", "license": "MIT", "main": "./dist/esm/index.js", "type": "module", @@ -34,7 +34,7 @@ }, "dependencies": { "@cheqd/sdk": "cjs", - "@docknetwork/credential-sdk": "0.20.0" + "@docknetwork/credential-sdk": "0.21.0" }, "devDependencies": { "@babel/cli": "^7.24.1", diff --git a/packages/cheqd-blockchain-api/src/index.js b/packages/cheqd-blockchain-api/src/index.js index 82c21ab2f..07b509d6f 100644 --- a/packages/cheqd-blockchain-api/src/index.js +++ b/packages/cheqd-blockchain-api/src/index.js @@ -1,6 +1,5 @@ import { AbstractApiProvider } from '@docknetwork/credential-sdk/modules/abstract/common'; import { - maybeToJSON, maybeToJSONString, fmtIter, extendNull, @@ -22,9 +21,17 @@ import { protobufPackage as resourceProtobufPackage, } from '@cheqd/ts-proto/cheqd/resource/v2/index.js'; import { - DidRef, NamespaceDid, CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, CheqdCreateResourcePayloadWithTypeUrlAndSignatures, + DidRef, + NamespaceDid, + CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, + CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, + CheqdCreateResourcePayloadWithTypeUrlAndSignatures, + CheqdCreateResource, + CheqdDIDDocument, + CheqdDeactivateDidDocument, } from '@docknetwork/credential-sdk/types'; import { TypedEnum } from '@docknetwork/credential-sdk/types/generic'; +import { maybeToCheqdPayloadOrJSON } from '../../credential-sdk/src/utils'; export class CheqdAPI extends AbstractApiProvider { /** @@ -50,16 +57,20 @@ export class CheqdAPI extends AbstractApiProvider { }); static Payloads = extendNull({ - MsgCreateDidDoc: MsgCreateDidDocPayload, - MsgUpdateDidDoc: MsgUpdateDidDocPayload, - MsgDeactivateDidDoc: MsgDeactivateDidDocPayload, - MsgCreateResource: MsgCreateResourcePayload, + MsgCreateDidDoc: [CheqdDIDDocument, MsgCreateDidDocPayload], + MsgUpdateDidDoc: [CheqdDIDDocument, MsgUpdateDidDocPayload], + MsgDeactivateDidDoc: [ + CheqdDeactivateDidDocument, + MsgDeactivateDidDocPayload, + ], + MsgCreateResource: [CheqdCreateResource, MsgCreateResourcePayload], }); static PayloadWrappers = extendNull({ MsgCreateDidDoc: CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, MsgUpdateDidDoc: CheqdSetDidDocumentPayloadWithTypeUrlAndSignatures, - MsgDeactivateDidDoc: CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, + MsgDeactivateDidDoc: + CheqdCheqdDeactivateDidDocumentPayloadWithTypeUrlAndSignatures, MsgCreateResource: CheqdCreateResourcePayloadWithTypeUrlAndSignatures, }); @@ -123,13 +134,16 @@ export class CheqdAPI extends AbstractApiProvider { * @returns {Promise} */ async stateChangeBytes(method, payload) { - const { [method]: Payload } = this.constructor.Payloads; - if (Payload == null) { + const { [method]: Payloads } = this.constructor.Payloads; + if (Payloads == null) { throw new Error( `Can't find payload constructor for the provided method \`${method}\``, ); } - const jsonPayload = maybeToJSON(payload); + const [TypedPayload, Payload] = Payloads; + + const typedPayload = TypedPayload.from(payload); + const jsonPayload = maybeToCheqdPayloadOrJSON(typedPayload); const sdkPayload = Payload.fromPartial(jsonPayload); try { @@ -169,7 +183,9 @@ export class CheqdAPI extends AbstractApiProvider { payer: sender, }; - const txJSON = PayloadWrapper.from(tx).toJSON(); + const txJSON = maybeToCheqdPayloadOrJSON( + PayloadWrapper.from(JSON.parse(JSON.stringify(tx.toJSON()))), + ); txJSON.typeUrl = `/${prefix}.${typeUrl}`; const res = await this.sdk.signer.signAndBroadcast( diff --git a/packages/cheqd-blockchain-modules/CHANGELOG.md b/packages/cheqd-blockchain-modules/CHANGELOG.md index 5bc8267f6..27b80798a 100644 --- a/packages/cheqd-blockchain-modules/CHANGELOG.md +++ b/packages/cheqd-blockchain-modules/CHANGELOG.md @@ -1,5 +1,12 @@ # @docknetwork/cheqd-blockchain-modules +## 0.13.3 + +### Patch Changes + +- Updated dependencies + - @docknetwork/credential-sdk@0.21.0 + ## 0.13.2 ### Patch Changes diff --git a/packages/cheqd-blockchain-modules/package.json b/packages/cheqd-blockchain-modules/package.json index feb053595..e1d24580a 100644 --- a/packages/cheqd-blockchain-modules/package.json +++ b/packages/cheqd-blockchain-modules/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/cheqd-blockchain-modules", - "version": "0.13.2", + "version": "0.13.3", "type": "module", "license": "MIT", "main": "./dist/esm/index.js", @@ -33,7 +33,7 @@ "node": ">=18.0.0" }, "dependencies": { - "@docknetwork/credential-sdk": "0.20.0" + "@docknetwork/credential-sdk": "0.21.0" }, "devDependencies": { "@babel/cli": "^7.24.1", @@ -42,7 +42,7 @@ "@babel/plugin-syntax-import-attributes": "^7.25.6", "@babel/plugin-transform-modules-commonjs": "^7.24.1", "@babel/preset-env": "^7.24.3", - "@docknetwork/cheqd-blockchain-api": "0.14.5", + "@docknetwork/cheqd-blockchain-api": "0.15.0", "@rollup/plugin-alias": "^4.0.2", "@rollup/plugin-babel": "^6.0.4", "@rollup/plugin-commonjs": "^24.0.0", diff --git a/packages/credential-sdk/CHANGELOG.md b/packages/credential-sdk/CHANGELOG.md index b02fa1582..97fcc4cf3 100644 --- a/packages/credential-sdk/CHANGELOG.md +++ b/packages/credential-sdk/CHANGELOG.md @@ -1,5 +1,11 @@ # @docknetwork/credential-sdk +## 0.21.0 + +### Minor Changes + +- Serialization/deserialization tweaks for `cheqd` + ## 0.20.0 ### Minor Changes diff --git a/packages/credential-sdk/package.json b/packages/credential-sdk/package.json index d336c2ff4..cbdde0869 100644 --- a/packages/credential-sdk/package.json +++ b/packages/credential-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/credential-sdk", - "version": "0.20.0", + "version": "0.21.0", "license": "MIT", "type": "module", "files": [ diff --git a/packages/credential-sdk/src/types/accumulator/accumulator-id.js b/packages/credential-sdk/src/types/accumulator/accumulator-id.js index bbccc27a9..aceba9d4c 100644 --- a/packages/credential-sdk/src/types/accumulator/accumulator-id.js +++ b/packages/credential-sdk/src/types/accumulator/accumulator-id.js @@ -10,7 +10,12 @@ import { export class AccumulatorId extends withFrom( withQualifier(TypedEnum, true), - (value, from) => { + (valueWithUncheckedPrefix, from) => { + const value = typeof valueWithUncheckedPrefix === 'string' + && valueWithUncheckedPrefix.startsWith('dock:accumulator:') + ? `accumulator:dock:${valueWithUncheckedPrefix.slice(17)}` + : valueWithUncheckedPrefix; + try { // eslint-disable-next-line no-use-before-define return from(DockAccumulatorIdValue.from(value)); @@ -20,10 +25,6 @@ export class AccumulatorId extends withFrom( }, ) { static Qualifier = 'accumulator:'; - - toJSON() { - return String(this); - } } export class CheqdAccumulatorIdValue extends withQualifier(DidRef) { diff --git a/packages/credential-sdk/src/types/did/document/ident-ref.js b/packages/credential-sdk/src/types/did/document/ident-ref.js index f0452bdb9..446d41b60 100644 --- a/packages/credential-sdk/src/types/did/document/ident-ref.js +++ b/packages/credential-sdk/src/types/did/document/ident-ref.js @@ -40,6 +40,10 @@ export default class IdentRef extends withQualifier(TypedTuple) { return `${did}#${value}`; } + toCheqdPayload() { + return this.toString(); + } + toJSON() { return this.toString(); } diff --git a/packages/credential-sdk/src/types/did/document/verification-method-ref-or-cheqd-verification-method.js b/packages/credential-sdk/src/types/did/document/verification-method-ref-or-cheqd-verification-method.js index d0a884726..3b334b2fd 100644 --- a/packages/credential-sdk/src/types/did/document/verification-method-ref-or-cheqd-verification-method.js +++ b/packages/credential-sdk/src/types/did/document/verification-method-ref-or-cheqd-verification-method.js @@ -1,20 +1,12 @@ import VerificationMethodRef from './verification-method-ref'; -import { CheqdVerificationMethod } from './verification-method'; +import { CheqdVerificationMethodAssertion } from './verification-method'; import { withFrom } from '../../generic'; export default class VerificationMethodRefOrCheqdVerificationMethod extends withFrom( VerificationMethodRef, (value, from) => { try { - return class CheqdVerificationMethodToJSONString extends CheqdVerificationMethod { - toJSON() { - return JSON.stringify(JSON.stringify(super.toJSON())); - } - - static from(obj) { - return typeof obj === 'string' ? super.fromJSON(JSON.parse(JSON.parse(obj))) : super.from(obj); - } - }.from(value); + return CheqdVerificationMethodAssertion.from(value); } catch { return from(value); } diff --git a/packages/credential-sdk/src/types/did/document/verification-method-ref.js b/packages/credential-sdk/src/types/did/document/verification-method-ref.js index 235d6e387..35f780818 100644 --- a/packages/credential-sdk/src/types/did/document/verification-method-ref.js +++ b/packages/credential-sdk/src/types/did/document/verification-method-ref.js @@ -32,6 +32,10 @@ export default class VerificationMethodRef extends withQualifier(TypedTuple) { return `${did}#keys-${index}`; } + toCheqdPayload() { + return this.toEncodedString(); + } + toJSON() { return this.toEncodedString(); } diff --git a/packages/credential-sdk/src/types/did/document/verification-method.js b/packages/credential-sdk/src/types/did/document/verification-method.js index 9ce3b129e..728eb2faa 100644 --- a/packages/credential-sdk/src/types/did/document/verification-method.js +++ b/packages/credential-sdk/src/types/did/document/verification-method.js @@ -1,5 +1,10 @@ import { - withFrom, TypedStruct, withBase58, TypedBytes, option, TypedString, + withFrom, + TypedStruct, + withBase58, + TypedBytes, + option, + TypedString, } from '../../generic'; import { BBDT16PublicKey, @@ -26,11 +31,18 @@ import { Bls12381PSDockVerKeyName, Bls12381BBDT16DockVerKeyName, } from '../../../vc/crypto'; -import { Ed25519Verification2018Method, Ed25519Verification2020Method, VerificationMethodType } from './verification-method-type'; +import { + Ed25519Verification2018Method, + Ed25519Verification2020Method, + VerificationMethodType, +} from './verification-method-type'; import VerificationMethodRef from './verification-method-ref'; import { NamespaceDid } from '../onchain/typed-did'; import { - fmtIter, valueBytes, filterObj, + fmtIter, + valueBytes, + filterObj, + maybeToCheqdPayloadOrJSON, } from '../../../utils'; export class PublicKeyBase58 extends withBase58(TypedBytes) {} @@ -152,3 +164,21 @@ export class CheqdVerificationMethod extends withFrom( ); } } + +export class CheqdVerificationMethodAssertion extends CheqdVerificationMethod { + toCheqdPayload() { + return JSON.stringify( + JSON.stringify(this.apply(maybeToCheqdPayloadOrJSON)), + ); + } + + toJSON() { + return this.toCheqdPayload(); + } + + static from(obj) { + return typeof obj === 'string' + ? super.fromJSON(JSON.parse(JSON.parse(obj))) + : super.from(obj); + } +} diff --git a/packages/credential-sdk/src/types/did/offchain/service-endpoint.js b/packages/credential-sdk/src/types/did/offchain/service-endpoint.js index 2add085be..8829d5d72 100644 --- a/packages/credential-sdk/src/types/did/offchain/service-endpoint.js +++ b/packages/credential-sdk/src/types/did/offchain/service-endpoint.js @@ -1,4 +1,4 @@ -import { maybeToNumber } from '../../../utils'; +import { maybeToJSONString, maybeToNumber } from '../../../utils'; import { TypedStruct, TypedString, @@ -17,7 +17,7 @@ const LinkedDomainsPlaceholder = createPlaceholder((value) => { ) { return 0b0001; } else { - throw new Error(`Unknown value \`${value}\``); + throw new Error(`Unknown value \`${maybeToJSONString(value)}\``); } }); @@ -42,6 +42,10 @@ export class LinkedDomains extends ServiceEndpointType { toJSON() { return this.constructor.Type; } + + apply(fn) { + return fn(this.constructor.Type); + } } ServiceEndpointType.bindVariants(LinkedDomains); diff --git a/packages/credential-sdk/src/types/did/onchain/typed-did/cheqd-did.js b/packages/credential-sdk/src/types/did/onchain/typed-did/cheqd-did.js index fdc1f7eb9..c65dc627a 100644 --- a/packages/credential-sdk/src/types/did/onchain/typed-did/cheqd-did.js +++ b/packages/credential-sdk/src/types/did/onchain/typed-did/cheqd-did.js @@ -58,6 +58,10 @@ export class CheqdDid extends withQualifier(TypedEnum, true) { toJSON() { return String(this); } + + toCheqdPayload() { + return String(this); + } } export class CheqdTestnetDid extends CheqdDid { 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 fa23b2d4a..401830c59 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 @@ -104,19 +104,11 @@ export class DidMethodKey extends DockDidOrDidMethodKey { DockDidOrDidMethodKey.bindVariants(DockDid, DidMethodKey); -class DockDidValueToString extends DockDidValue { - toJSON() { - return String(this); - } -} - -class DidMethodKeyPublicKeyToString extends DidMethodKeyPublicKey { - toJSON() { +export class NamespaceDid extends withQualifier(TypedEnum, true) { + toCheqdPayload() { return String(this); } -} -export class NamespaceDid extends withQualifier(TypedEnum, true) { toJSON() { return String(this); } @@ -151,14 +143,14 @@ class DockNamespaceDid extends NamespaceDid { static Type = 'dock'; - static Class = DockDidValueToString; + static Class = DockDidValue; } class DidNamespaceKey extends NamespaceDid { static Qualifier = 'did:key:'; static Type = 'didMethodKey'; - static Class = DidMethodKeyPublicKeyToString; + static Class = DidMethodKeyPublicKey; } class CheqdNamespaceDid extends NamespaceDid { static Qualifier = 'did:cheqd:'; diff --git a/packages/credential-sdk/src/types/did/onchain/verification-method-signature.js b/packages/credential-sdk/src/types/did/onchain/verification-method-signature.js index b968e42f8..50e1bfb69 100644 --- a/packages/credential-sdk/src/types/did/onchain/verification-method-signature.js +++ b/packages/credential-sdk/src/types/did/onchain/verification-method-signature.js @@ -5,7 +5,7 @@ import { VerificationMethodRef } from '../document'; import { DidKeypair } from '../../../keypairs'; class BytesSignatureEd25519Value extends SignatureEd25519Value { - toJSON() { + toCheqdPayload() { return this.bytes; } } diff --git a/packages/credential-sdk/src/types/generic/typed-array.js b/packages/credential-sdk/src/types/generic/typed-array.js index 39e71902d..f7bce8685 100644 --- a/packages/credential-sdk/src/types/generic/typed-array.js +++ b/packages/credential-sdk/src/types/generic/typed-array.js @@ -1,6 +1,6 @@ import { ArrayWithoutPrototypeMethods, ensureArrayLike } from '../../utils'; import { withExtendedStaticProperties } from '../../utils/inheritance'; -import { maybeEq, maybeFrom, maybeToJSON } from '../../utils/interfaces'; +import { maybeEq, maybeFrom } from '../../utils/interfaces'; import withBase from './with-base'; import withCatchNull from './with-catch-null'; import withEq from './with-eq'; @@ -47,8 +47,8 @@ class TypedArray extends withBase(ArrayWithoutPrototypeMethods) { ); } - toJSON() { - return [...Array.prototype.values.call(this)].map(maybeToJSON); + apply(fn) { + return [...this].map(fn); } static fromApi(arr) { diff --git a/packages/credential-sdk/src/types/generic/typed-bytes-array.js b/packages/credential-sdk/src/types/generic/typed-bytes-array.js index 1b64cd096..6dc118dc7 100644 --- a/packages/credential-sdk/src/types/generic/typed-bytes-array.js +++ b/packages/credential-sdk/src/types/generic/typed-bytes-array.js @@ -6,6 +6,10 @@ export default class TypedBytesArray extends TypedBytes { } toJSON() { + return Array.from(this); + } + + toCheqdPayload() { return this.value; } } diff --git a/packages/credential-sdk/src/types/generic/typed-bytes.js b/packages/credential-sdk/src/types/generic/typed-bytes.js index bc75b44c0..1f582232b 100644 --- a/packages/credential-sdk/src/types/generic/typed-bytes.js +++ b/packages/credential-sdk/src/types/generic/typed-bytes.js @@ -53,7 +53,11 @@ class TypedBytes extends withBase(ArrayWithoutPrototypeMethods) { } toJSON() { - return this.value; + return this.toHex(); + } + + apply(fn) { + return fn(this.value); } static fromJSON(json) { diff --git a/packages/credential-sdk/src/types/generic/typed-enum.js b/packages/credential-sdk/src/types/generic/typed-enum.js index f67858989..364461471 100644 --- a/packages/credential-sdk/src/types/generic/typed-enum.js +++ b/packages/credential-sdk/src/types/generic/typed-enum.js @@ -176,18 +176,14 @@ class TypedEnum extends withBase(class EnumBase {}) { return this[this.type]; } - /** - * Convert the instance to a JSON representation. - * @returns {Object} The JSON representation of the instance. - */ - toJSON() { + apply(fn) { if (this.constructor.isNullish) { - return this.constructor.JsonType; + return fn(this.constructor.JsonType); } else { const { type, value } = this; return { - [type]: maybeToJSON(value), + [type]: fn(value), }; } } diff --git a/packages/credential-sdk/src/types/generic/typed-map.js b/packages/credential-sdk/src/types/generic/typed-map.js index d11ec4110..7939622c1 100644 --- a/packages/credential-sdk/src/types/generic/typed-map.js +++ b/packages/credential-sdk/src/types/generic/typed-map.js @@ -65,9 +65,11 @@ class TypedMap extends withBase(Map) { } toJSON() { - return [...this.entries()] - .map(([key, value]) => [maybeToJSON(key), maybeToJSON(value)]) - .sort(([k1], [k2]) => String(k1).localeCompare(String(k2))); + return this.apply(maybeToJSON).sort(([k1], [k2]) => String(k1).localeCompare(String(k2))); + } + + apply(fn) { + return [...this.entries()].map(([key, value]) => [fn(key), fn(value)]); } static fromJSON(entries) { diff --git a/packages/credential-sdk/src/types/generic/typed-number.js b/packages/credential-sdk/src/types/generic/typed-number.js index d9a89cb26..97474aaed 100644 --- a/packages/credential-sdk/src/types/generic/typed-number.js +++ b/packages/credential-sdk/src/types/generic/typed-number.js @@ -33,6 +33,10 @@ class TypedNumber extends withBase(class NumberBase {}) { return this.value; } + apply(fn) { + return fn(this.value); + } + static from(value) { if (value instanceof this) { return value; diff --git a/packages/credential-sdk/src/types/generic/typed-set.js b/packages/credential-sdk/src/types/generic/typed-set.js index 8e6141425..b8f24aee7 100644 --- a/packages/credential-sdk/src/types/generic/typed-set.js +++ b/packages/credential-sdk/src/types/generic/typed-set.js @@ -50,7 +50,11 @@ class TypedSet extends withBase(Set) { } toJSON() { - return [...this.values()].map(maybeToJSON).sort(); + return this.apply(maybeToJSON).sort(); + } + + apply(fn) { + return [...this.values()].map(fn); } clear() { diff --git a/packages/credential-sdk/src/types/generic/typed-string.js b/packages/credential-sdk/src/types/generic/typed-string.js index 19875ce8d..a52cafcdd 100644 --- a/packages/credential-sdk/src/types/generic/typed-string.js +++ b/packages/credential-sdk/src/types/generic/typed-string.js @@ -11,6 +11,10 @@ class TypedString extends TypedBytes { return u8aToString(this.bytes); } + toJSON() { + return String(this); + } + toString() { return this.value; } diff --git a/packages/credential-sdk/src/types/generic/typed-struct.js b/packages/credential-sdk/src/types/generic/typed-struct.js index 01c3dc41a..8a20c8290 100644 --- a/packages/credential-sdk/src/types/generic/typed-struct.js +++ b/packages/credential-sdk/src/types/generic/typed-struct.js @@ -2,7 +2,7 @@ import { validateProperties, withExtendedStaticProperties, } from '../../utils/inheritance'; -import { maybeToJSON, maybeEq, maybeFrom } from '../../utils/interfaces'; +import { maybeEq, maybeFrom } from '../../utils/interfaces'; import withBase from './with-base'; import withCatchNull from './with-catch-null'; import withEq from './with-eq'; @@ -40,9 +40,9 @@ class TypedStruct extends withBase(class StructBase {}) { Object.seal(this); } - toJSON() { + apply(fn) { return Object.fromEntries( - Object.entries(this).map(([key, value]) => [key, maybeToJSON(value)]), + Object.entries(this).map(([key, value]) => [key, fn(value)]), ); } diff --git a/packages/credential-sdk/src/types/generic/typed-tuple.js b/packages/credential-sdk/src/types/generic/typed-tuple.js index 099f9a0f4..8331fd774 100644 --- a/packages/credential-sdk/src/types/generic/typed-tuple.js +++ b/packages/credential-sdk/src/types/generic/typed-tuple.js @@ -1,5 +1,5 @@ import { withExtendedStaticProperties } from '../../utils/inheritance'; -import { maybeEq, maybeFrom, maybeToJSON } from '../../utils/interfaces'; +import { maybeEq, maybeFrom } from '../../utils/interfaces'; import { ArrayWithoutPrototypeMethods } from '../../utils/generic'; import withBase from './with-base'; import withCatchNull from './with-catch-null'; @@ -53,8 +53,8 @@ class TypedTuple extends withBase(ArrayWithoutPrototypeMethods) { Object.seal(this); } - toJSON() { - return [...Array.prototype.values.call(this)].map(maybeToJSON); + apply(fn) { + return [...this].map(fn); } static from(value) { diff --git a/packages/credential-sdk/src/types/generic/typed-uuid.js b/packages/credential-sdk/src/types/generic/typed-uuid.js index 19531e03f..4d190a393 100644 --- a/packages/credential-sdk/src/types/generic/typed-uuid.js +++ b/packages/credential-sdk/src/types/generic/typed-uuid.js @@ -21,6 +21,10 @@ export default class TypedUUID extends TypedBytes { return this.value; } + toJSON() { + return String(this); + } + static fromBytesAdapt(bytesOrString) { let bytes = normalizeOrConvertStringToU8a(bytesOrString); if (bytes.length < 16) { diff --git a/packages/credential-sdk/src/types/generic/with-base.js b/packages/credential-sdk/src/types/generic/with-base.js index 1b53c165e..66e2b5660 100644 --- a/packages/credential-sdk/src/types/generic/with-base.js +++ b/packages/credential-sdk/src/types/generic/with-base.js @@ -3,7 +3,7 @@ import { withExtendedStaticProperties, withExtendedPrototypeProperties, } from '../../utils/inheritance'; -import { applyToValue, maybeEq } from '../../utils/interfaces'; +import { maybeEq, maybeToJSON } from '../../utils/interfaces'; import withCatchNull from './with-catch-null'; import withEq from './with-eq'; @@ -57,6 +57,16 @@ export default function withBase(klass) { * @returns {object} The JSON representation of the instance. */ toJSON() { + return this.apply(maybeToJSON); + } + + /** + * Recursively applies supplied function to the underlying values. + * @template T + * @param {function(this): T} _fn + * @returns {T} + */ + apply(_fn) { throw new Error('Unimplemented'); } @@ -84,24 +94,6 @@ export default function withBase(klass) { return JSON.stringify(this.toJSON()); } - /** - * Applies supplied function to the underlying value if `check` returns `true`. - * Otherwise, attempts to do the same on the inner value. - * - * @template T - * @param {function(this): T} fn - * @returns {T} - */ - applyToValue(check, fn) { - if (check(this)) { - return fn(this); - } - const { value } = this; - const hasValue = typeof value !== 'undefined'; - - return applyToValue(check, fn, value ?? this, hasValue); - } - /** * Performs an equality check against other value. * @@ -117,7 +109,7 @@ export default function withBase(klass) { return withExtendedStaticProperties( ['fromJSON', 'fromApi'], withExtendedPrototypeProperties( - ['toJSON'], + ['apply'], withEq(withCatchNull(classes[name])), ), ); diff --git a/packages/credential-sdk/src/types/generic/with-base58.js b/packages/credential-sdk/src/types/generic/with-base58.js index 54c52fb4f..7c44ec914 100644 --- a/packages/credential-sdk/src/types/generic/with-base58.js +++ b/packages/credential-sdk/src/types/generic/with-base58.js @@ -1,6 +1,11 @@ import withFrom from './with-from'; import { - decodeFromBase58, encodeAsBase58, isEqualToOrPrototypeOf, u8aToString, stringToU8a, maybeToJSONString, + decodeFromBase58, + encodeAsBase58, + isEqualToOrPrototypeOf, + u8aToString, + stringToU8a, + maybeToJSONString, } from '../../utils'; import TypedEnum from './typed-enum'; @@ -19,6 +24,10 @@ export default function withBase58(klass) { return String(this); } + get value() { + return this.toBase58(); + } + static fromBase58(str) { return this.from(decodeFromBase58(str)); } @@ -35,7 +44,9 @@ export default function withBase58(klass) { [name]: class extends klass { static Class = klass.Class; - static Variants = !klass.Class ? klass.Variants?.map(withBase58) : klass.Variants; + static Variants = !klass.Class + ? klass.Variants?.map(withBase58) + : klass.Variants; toString() { return this.toBase58(); @@ -58,5 +69,7 @@ export default function withBase58(klass) { res = obj[name]; } - return withFrom(res, function from(value, fromFn) { return (typeof value === 'string' ? this.fromBase58(value) : fromFn(value)); }); + return withFrom(res, function from(value, fromFn) { + return typeof value === 'string' ? this.fromBase58(value) : fromFn(value); + }); } diff --git a/packages/credential-sdk/src/types/generic/with-base58btc.js b/packages/credential-sdk/src/types/generic/with-base58btc.js index 432823c19..f99c549df 100644 --- a/packages/credential-sdk/src/types/generic/with-base58btc.js +++ b/packages/credential-sdk/src/types/generic/with-base58btc.js @@ -17,6 +17,10 @@ export default function withBase58btc(klass) { return String(this); } + get value() { + return this.toBase58btc(); + } + static fromBase58btc(str) { return this.from(decodeFromBase58btc(str)); } @@ -31,5 +35,8 @@ export default function withBase58btc(klass) { }, }; - return withExtendedStaticProperties(['Prefix'], withFrom(obj[name], (value, from) => (typeof value === 'string' ? this.fromBase58btc(value) : from(value)))); + return withExtendedStaticProperties( + ['Prefix'], + withFrom(obj[name], (value, from) => (typeof value === 'string' ? this.fromBase58btc(value) : from(value))), + ); } diff --git a/packages/credential-sdk/src/types/generic/with-base64.js b/packages/credential-sdk/src/types/generic/with-base64.js index c0f499364..938754ca3 100644 --- a/packages/credential-sdk/src/types/generic/with-base64.js +++ b/packages/credential-sdk/src/types/generic/with-base64.js @@ -1,6 +1,11 @@ import withFrom from './with-from'; import { - decodeFromBase64, encodeAsBase64, isEqualToOrPrototypeOf, u8aToString, stringToU8a, maybeToJSONString, + decodeFromBase64, + encodeAsBase64, + isEqualToOrPrototypeOf, + u8aToString, + stringToU8a, + maybeToJSONString, } from '../../utils'; import TypedEnum from './typed-enum'; @@ -19,6 +24,10 @@ export default function withBase64(klass) { return String(this); } + get value() { + return this.toBase64(); + } + static fromBase64(str) { return this.from(decodeFromBase64(str)); } @@ -35,7 +44,9 @@ export default function withBase64(klass) { [name]: class extends klass { static Class = klass.Class; - static Variants = !klass.Class ? klass.Variants?.map(withBase64) : klass.Variants; + static Variants = !klass.Class + ? klass.Variants?.map(withBase64) + : klass.Variants; toString() { return this.toBase64(); @@ -58,5 +69,7 @@ export default function withBase64(klass) { res = obj[name]; } - return withFrom(res, function from(value, fromFn) { return (typeof value === 'string' ? this.fromBase64(value) : fromFn(value)); }); + return withFrom(res, function from(value, fromFn) { + return typeof value === 'string' ? this.fromBase64(value) : fromFn(value); + }); } diff --git a/packages/credential-sdk/src/types/generic/with-qualifier.js b/packages/credential-sdk/src/types/generic/with-qualifier.js index 0d2bdf050..64e6301a2 100644 --- a/packages/credential-sdk/src/types/generic/with-qualifier.js +++ b/packages/credential-sdk/src/types/generic/with-qualifier.js @@ -28,7 +28,8 @@ export default function withQualifier(klass, wrapper = false) { static get Qualifiers() { return [].concat( - this.Qualifier ?? this.Class?.Qualifiers ?? + this.Qualifier ?? + this.Class?.Qualifiers ?? this.Variants.flatMap((variant) => [].concat( variant.Qualifier ?? variant !== this @@ -98,6 +99,10 @@ export default function withQualifier(klass, wrapper = false) { toString() { return this.value.toString(); } + + toCheqdPayload() { + return String(this); + } }, }; @@ -110,8 +115,10 @@ export default function withQualifier(klass, wrapper = false) { } } else if ( value?.constructor?.Qualifier != null && - !this.Qualifiers.find((qualifier) => - value.constructor.Qualifier.startsWith(qualifier) || qualifier.startsWith(value.constructor.Qualifier) + !this.Qualifiers.find( + (qualifier) => + value.constructor.Qualifier.startsWith(qualifier) || + qualifier.startsWith(value.constructor.Qualifier) ) ) { throw new Error( @@ -181,6 +188,10 @@ export default function withQualifier(klass, wrapper = false) { toString() { return this.toQualifiedEncodedString(); } + + toCheqdPayload() { + return String(this); + } }, }; @@ -201,7 +212,8 @@ export default function withQualifier(klass, wrapper = false) { } } else if ( value?.constructor?.Qualifier != null && - !value.constructor.Qualifier.startsWith(this.Qualifier) && !this.Qualifier.startsWith(value.constructor.Qualifier) + !value.constructor.Qualifier.startsWith(this.Qualifier) && + !this.Qualifier.startsWith(value.constructor.Qualifier) ) { throw new Error( `Value has a different qualifier: \`${value.constructor.Qualifier}\` while expected \`${this.Qualifier}\` by \`${this.name}\`` diff --git a/packages/credential-sdk/src/utils/interfaces.js b/packages/credential-sdk/src/utils/interfaces.js index fd7e86bc4..9c2775561 100644 --- a/packages/credential-sdk/src/utils/interfaces.js +++ b/packages/credential-sdk/src/utils/interfaces.js @@ -16,6 +16,19 @@ export const maybeToJSON = (value) => (typeof value?.toJSON === 'function' */ export const maybeToJSONString = (value) => JSON.stringify(maybeToJSON(value)); +/** + * Attempts to convert provided value to the Cheqd Payload or JSON. + * @param {*} value + * @returns {object} + */ +export const maybeToCheqdPayloadOrJSON = (obj) => (typeof obj?.toCheqdPayload === 'function' // eslint-disable-line no-nested-ternary + ? obj.toCheqdPayload() + : typeof obj?.apply === 'function' // eslint-disable-line no-nested-ternary + ? obj.apply(maybeToCheqdPayloadOrJSON) + : typeof value !== 'object' && typeof value !== 'function' + ? obj + : maybeToJSON(obj)); + /** * Returns bytes of the value converted to a stringified JSON. * @template T @@ -73,7 +86,46 @@ export const maybeNew = (Class, args) => (!Class[NotAConstructor] ? new Class(.. export const maybeFrom = (klass, obj) => (typeof klass.from === 'function' ? klass.from(obj) : maybeNew(klass, [obj])); /** - * Applies function to the inner value of the provided object. + * Error thrown when the provided function was executed more than once or wasn't executed at all. + */ +export class MustBeExecutedOnce extends Error { + constructor(fn) { + super(`Function must be executed exactly once: \`${fn}\``); + } + + static ensure(fn, call) { + let executed = false; + const willExecute = () => { + if (executed) { + throw new this(fn); + } + + executed = true; + }; + const wasExecuted = () => { + if (!executed) { + throw new this(fn); + } + }; + + const name = `mustBeExecutedOnce(${fn.name})`; + const obj = { + [name](...args) { + willExecute(); + + return fn.apply(this, args); + }, + }; + + const res = call(obj[name]); + wasExecuted(); + + return res; + } +} + +/** + * Applies function to the first value of the provided object that passes the check. * @template T * @template I * @template O @@ -82,11 +134,18 @@ export const maybeFrom = (klass, obj) => (typeof klass.from === 'function' ? kla * @param {T} value * @returns {O} */ -export const applyToValue = (check, fn, value, rec = true) => { +export const applyToValue = (check, fn, value) => { if (check(value)) { return fn(value); - } else if (rec && typeof value?.applyToValue === 'function') { - return value.applyToValue(check, fn); + } else if (typeof value?.apply === 'function') { + let res; + MustBeExecutedOnce.ensure( + (obj) => { + res = applyToValue(check, fn, obj); + }, + (wrapped) => value.apply(wrapped), + ); + return res; } throw new Error( diff --git a/packages/dock-blockchain-api/CHANGELOG.md b/packages/dock-blockchain-api/CHANGELOG.md index 0c98f2569..310a5b706 100644 --- a/packages/dock-blockchain-api/CHANGELOG.md +++ b/packages/dock-blockchain-api/CHANGELOG.md @@ -1,5 +1,12 @@ # @docknetwork/dock-blockchain-api +## 0.8.7 + +### Patch Changes + +- Updated dependencies + - @docknetwork/credential-sdk@0.21.0 + ## 0.8.6 ### Patch Changes diff --git a/packages/dock-blockchain-api/package.json b/packages/dock-blockchain-api/package.json index 56edbeb90..621eb9660 100644 --- a/packages/dock-blockchain-api/package.json +++ b/packages/dock-blockchain-api/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/dock-blockchain-api", - "version": "0.8.6", + "version": "0.8.7", "license": "MIT", "main": "./dist/esm/index.js", "type": "module", @@ -89,7 +89,7 @@ "@polkadot/api": "10.12.4" }, "dependencies": { - "@docknetwork/credential-sdk": "0.20.0", + "@docknetwork/credential-sdk": "0.21.0", "@docknetwork/node-types": "^0.17.0", "@juanelas/base64": "^1.0.5", "@polkadot/api": "10.12.4", diff --git a/packages/dock-blockchain-modules/CHANGELOG.md b/packages/dock-blockchain-modules/CHANGELOG.md index 7e5f7160a..64e844dd9 100644 --- a/packages/dock-blockchain-modules/CHANGELOG.md +++ b/packages/dock-blockchain-modules/CHANGELOG.md @@ -1,5 +1,12 @@ # @docknetwork/dock-blockchain-modules +## 0.11.2 + +### Patch Changes + +- Updated dependencies + - @docknetwork/credential-sdk@0.21.0 + ## 0.11.1 ### Patch Changes diff --git a/packages/dock-blockchain-modules/package.json b/packages/dock-blockchain-modules/package.json index 1fc09eacb..25ac6f60c 100644 --- a/packages/dock-blockchain-modules/package.json +++ b/packages/dock-blockchain-modules/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/dock-blockchain-modules", - "version": "0.11.1", + "version": "0.11.2", "license": "MIT", "type": "module", "main": "./dist/esm/index.js", @@ -33,7 +33,7 @@ "node": ">=18.0.0" }, "dependencies": { - "@docknetwork/credential-sdk": "0.20.0" + "@docknetwork/credential-sdk": "0.21.0" }, "devDependencies": { "@babel/cli": "^7.24.1", @@ -42,7 +42,7 @@ "@babel/plugin-syntax-import-attributes": "^7.25.6", "@babel/plugin-transform-modules-commonjs": "^7.24.1", "@babel/preset-env": "^7.24.3", - "@docknetwork/dock-blockchain-api": "0.8.6", + "@docknetwork/dock-blockchain-api": "0.8.7", "@rollup/plugin-alias": "^4.0.2", "@rollup/plugin-babel": "^6.0.4", "@rollup/plugin-commonjs": "^24.0.0",