From 49c8f8d8eaffc78d6f0b44f72a7e55a29fdf3172 Mon Sep 17 00:00:00 2001 From: Henry Tsai Date: Wed, 3 Jan 2024 12:31:38 -0800 Subject: [PATCH] v0.2.10 + Added private key signer as part of Persona creation --- package-lock.json | 4 ++-- package.json | 2 +- src/did/did-key-resolver.ts | 20 ++++++++++++++++---- tests/utils/test-data-generator.ts | 10 +++++++++- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index f9e3c0c38..411f96699 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tbd54566975/dwn-sdk-js", - "version": "0.2.9", + "version": "0.2.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@tbd54566975/dwn-sdk-js", - "version": "0.2.9", + "version": "0.2.10", "license": "Apache-2.0", "dependencies": { "@ipld/dag-cbor": "9.0.3", diff --git a/package.json b/package.json index 64904a8d1..a092239bf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/did/did-key-resolver.ts b/src/did/did-key-resolver.ts index b8715d42a..2b15349a7 100644 --- a/src/did/did-key-resolver.ts +++ b/src/did/did-key-resolver.ts @@ -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. @@ -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 @@ -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}`, + }) + }; } /** diff --git a/tests/utils/test-data-generator.ts b/tests/utils/test-data-generator.ts index 2bfc54d87..1654df151 100644 --- a/tests/utils/test-data-generator.ts +++ b/tests/utils/test-data-generator.ts @@ -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'; @@ -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'; @@ -52,6 +54,7 @@ export type Persona = { did: string; keyId: string; keyPair: { publicJwk: PublicJwk, privateJwk: PrivateJwk }; + signer: Signer; }; export type GenerateProtocolsConfigureInput = { @@ -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;