From 2fed964e427b77c2683f74ff1ae9fde6ff8046f1 Mon Sep 17 00:00:00 2001 From: olegnn Date: Thu, 10 Oct 2024 17:06:08 +0200 Subject: [PATCH] Tweaks --- packages/credential-sdk/README.md | 50 ++++++++++++++++++- .../credential-sdk/src/types/did/document.js | 12 ++--- tutorials/src/tutorial_did.md | 9 ++-- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/packages/credential-sdk/README.md b/packages/credential-sdk/README.md index 53ffd6f22..c2f610f9d 100644 --- a/packages/credential-sdk/README.md +++ b/packages/credential-sdk/README.md @@ -36,8 +36,56 @@ Detailed documentation and API reference are provided at the [Dock Network Docum Contributions to the @docknetwork/credential-sdk are welcome. Please feel free to open issues or submit pull requests to improve the SDK or add new features. +## Types Overview + +The Credential SDK provides a flexible and extensible set of typed structures that facilitate working with complex data types. These types are designed to provide enhanced type safety, ensure consistent data handling, and simplify JSON and API integration. This includes support for versatile data representations such as Enums, Tuples, Strings, and Arrays, all of which come with utility methods for manipulation and comparison. + +### Available Types + +- **TypedString**: Represents string data. Internally managed as `Array` to handle binary data conversion seamlessly. Provides various methods for equality check and conversion between hexadecimal and string representations. + +- **TypedNumber**: Ensures reliable number handling by enforcing numerical checks during instantiation. Supports conversion from different inputs and API structures, offering consistent JSON integration. + +- **TypedEnum**: Facilitates the representation of enumeration types with extensible variants. Ensures strict type conformity when dealing with potential multiple representations of a single conceptual state. + +- **TypedArray**: Provides abstraction over JavaScript arrays, allowing for uniform handling of a single item type. Equipped with methods that maintain type integrity while offering array operations like `push`, `unshift`, and `equality check`. + +- **TypedMap**: Extends the Map structure, offering consistent key/value type management and conversion capabilities. Includes support for serialization to JSON and deserialization from API responses. + +- **TypedUUID**: Specially tailored for handling UUIDs, including validation, parsing, and generation of random UUIDs. + +- **TypedStruct**: Represents structured, dictionary-like data with predefined keys and types, facilitating robust data manipulation and JSON compatibility. + +- **TypedTuple**: Enforces a fixed-size collection of elements, each having a specified type. Essential for maintaining order and type checks in tuple-based data structures. + +- **Any**: Acts as a catch-all, capable of holding any value or object. It is useful in scenarios where type flexibility is essential, such as interfacing with dynamic data sources or when type restrictions are not suitable. + +- **Null**: Acts as a `null` allowing to have some field initialized to `null`. + +### Utility Mixins + +- **withBase**: A foundational mixin that adds basic methods like `from`, `toJSON`, and equality checks to a class. + +- **withCatchNull**: Ensures that `from` and `to` methods gracefully handle `null` or undefined values, safeguarding against unexpected errors. + +- **withEq**: Provides enhanced equality checking, allowing deep comparison among complex objects. + +- **withFrom**: Facilitates custom instantiation logic for classes, particularly useful when dealing with various input forms that require specialized initialization. + +- **withQualifier**: Extends classes to handle prefixed string identifiers and encoding, suitable for scenarios involving qualified identifiers (e.g., DID systems). + +- **option**: Allows to pass `null` to `from` method returning `null` in this case. + +- **withNullIfNotAVariant**: A class decorator for types extending `TypedEnum`. It ensures that instantiation from JSON or API data returns `null` if the provided value doesn't match any defined variant type. This mixin helps enforce strict type conformity. + +- **withProp**: Extends classes derived from `TypedStruct` or `TypedEnum` by adding or overriding properties. It facilitates dynamic property management, allowing for seamless integration of new fields or modifications without altering base classes. + +- **withQualifier**: Adds functionality for dealing with qualified strings, such as those used in decentralized identifiers (DIDs). This mixin supports operations involving strings with specific prefixes or encoding requirements. + +- **withoutProp**: Complements `withProp` by removing properties from classes that extend `TypedStruct` or `TypedEnum`. It is useful for deprecating or cleaning up obsolete properties. + ## License This SDK is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more details. -For any questions or issues, please refer to our [GitHub repository](https://github.com/docknetwork/credential-sdk) or [contact us](https://dock.io/contact). +For any questions or issues, please refer to our [GitHub repository](https://github.com/docknetwork/credential-sdk). diff --git a/packages/credential-sdk/src/types/did/document.js b/packages/credential-sdk/src/types/did/document.js index d7cb83cdf..bf2a24406 100644 --- a/packages/credential-sdk/src/types/did/document.js +++ b/packages/credential-sdk/src/types/did/document.js @@ -641,14 +641,14 @@ export class DIDDocument extends TypedStruct { capabilityInvocation, } = this; - class VerificationMethodRefSet extends TypedSet { - static Class = VerificationMethodRef; + class VerificationMethodRefOrKeySet extends TypedSet { + static Class = VerificationMethodRefOrKey; } - const auth = new VerificationMethodRefSet(authentication); - const assertion = new VerificationMethodRefSet(assertionMethod); - const keyAgr = new VerificationMethodRefSet(keyAgreement); - const capInv = new VerificationMethodRefSet(capabilityInvocation); + const auth = new VerificationMethodRefOrKeySet(authentication); + const assertion = new VerificationMethodRefOrKeySet(assertionMethod); + const keyAgr = new VerificationMethodRefOrKeySet(keyAgreement); + const capInv = new VerificationMethodRefOrKeySet(capabilityInvocation); const keys = [...verificationMethod] .map((method) => { diff --git a/tutorials/src/tutorial_did.md b/tutorials/src/tutorial_did.md index f3d12130c..b572a33c6 100644 --- a/tutorials/src/tutorial_did.md +++ b/tutorials/src/tutorial_did.md @@ -46,7 +46,8 @@ const privateKey = kp.privateKey(); const message = Uint8Array.from([1, 2, 3]); const signature = kp.sign(message); -assert(Ed25519Keypair.verify(message, signature, publicKey)); + +const verified = Ed25519Keypair.verify(message, signature, publicKey); ``` ## Registering a new DID on chain @@ -69,9 +70,9 @@ In most cases, a DID will have its own keys and will control itself, i.e. a self 2. Second, let's get a did key with verication relationship from the did's keypair. The only argument is the verification relationship. A verification relationship can be 1 or more of these `authentication`, `assertion`, `capabilityInvocation` or `keyAgreement` - ```js - const didKey = didKeypair.didKey(); - ``` + ```js + const didKey = didKeypair.didKey(); + ``` 3. Now submit the transaction using a `DockAPI` object and the newly created DID `did` and `didKey`. ```js