Skip to content

Commit

Permalink
v0.2.10 + Added private key signer as part of Persona creation
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai authored Jan 3, 2024
1 parent 8d5cac4 commit 49c8f8d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/dwn-sdk-js",
"version": "0.2.9",
"version": "0.2.10",
"description": "A reference implementation of https://identity.foundation/decentralized-web-node/spec/",
"repository": {
"type": "git",
Expand Down
20 changes: 16 additions & 4 deletions src/did/did-key-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import varint from 'varint';
import type { Signer } from '../types/signer.js';
import type { DidDocument, DidMethodResolver, DidResolutionResult } from '../types/did-types.js';
import type { KeyMaterial, PublicJwk } from '../types/jose-types.js';

import varint from 'varint';

import { base58btc } from 'multiformats/bases/base58';
import { Did } from './did.js';
import { ed25519 } from '../../src/jose/algorithms/signing/ed25519.js';
import { Encoder } from '../utils/encoder.js';
import { PrivateKeySigner } from '../utils/private-key-signer.js';
import { Secp256k1 } from '../utils/secp256k1.js';
import type { KeyMaterial, PublicJwk } from '../types/jose-types.js';

/**
* did:key Resolver.
Expand Down Expand Up @@ -108,7 +111,7 @@ export class DidKeyResolver implements DidMethodResolver {
* Generates a new ed25519 public/private key pair. Creates a DID using the private key.
* @returns DID and its key material.
*/
public static async generate(): Promise<{ did: string } & KeyMaterial> {
public static async generate(): Promise<{ did: string, signer: Signer } & KeyMaterial> {
const { publicJwk, privateJwk } = await ed25519.generateKeyPair();

// multicodec code for Ed25519 public keys
Expand All @@ -122,7 +125,16 @@ export class DidKeyResolver implements DidMethodResolver {
const did = `did:key:${id}`;
const keyId = DidKeyResolver.getKeyId(did);

return { did, keyId, keyPair: { publicJwk, privateJwk } };
return {
did,
keyId,
keyPair : { publicJwk, privateJwk },
signer : new PrivateKeySigner({
privateJwk : privateJwk,
algorithm : privateJwk.alg,
keyId : `${did}#${keyId}`,
})
};
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/utils/test-data-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ProtocolsQueryOptions } from '../../src/interfaces/protocols-query
import type { Readable } from 'readable-stream';
import type { RecordsQueryOptions } from '../../src/interfaces/records-query.js';
import type { RecordsWriteMessage } from '../../src/types/records-types.js';
import type { Signer } from '../../src/types/signer.js';
import type { AuthorizationModel, Pagination } from '../../src/types/message-types.js';
import type { CreateFromOptions, EncryptionInput, KeyEncryptionInput, RecordsWriteOptions } from '../../src/interfaces/records-write.js';
import type { DateSort, RecordsDeleteMessage, RecordsFilter, RecordsQueryMessage } from '../../src/types/records-types.js';
Expand All @@ -32,6 +33,7 @@ import { MessagesGet } from '../../src/interfaces/messages-get.js';
import { PermissionsGrant } from '../../src/interfaces/permissions-grant.js';
import { PermissionsRequest } from '../../src/interfaces/permissions-request.js';
import { PermissionsRevoke } from '../../src/interfaces/permissions-revoke.js';
import { PrivateKeySigner } from '../../src/utils/private-key-signer.js';
import { ProtocolsConfigure } from '../../src/interfaces/protocols-configure.js';
import { ProtocolsQuery } from '../../src/interfaces/protocols-query.js';
import { Records } from '../../src/utils/records.js';
Expand All @@ -52,6 +54,7 @@ export type Persona = {
did: string;
keyId: string;
keyPair: { publicJwk: PublicJwk, privateJwk: PrivateJwk };
signer: Signer;
};

export type GenerateProtocolsConfigureInput = {
Expand Down Expand Up @@ -279,7 +282,12 @@ export class TestDataGenerator {
const persona: Persona = {
did,
keyId,
keyPair
keyPair,
signer: new PrivateKeySigner({
privateJwk : keyPair.privateJwk,
algorithm : keyPair.privateJwk.alg,
keyId : `${did}#${keyId}`,
})
};

return persona;
Expand Down

0 comments on commit 49c8f8d

Please sign in to comment.