Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Oct 10, 2024
1 parent b3ff16a commit 2fed964
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
50 changes: 49 additions & 1 deletion packages/credential-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
12 changes: 6 additions & 6 deletions packages/credential-sdk/src/types/did/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
9 changes: 5 additions & 4 deletions tutorials/src/tutorial_did.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2fed964

Please sign in to comment.