Skip to content

Commit

Permalink
Replaced SignatureInput with Signer
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Sep 18, 2023
1 parent faf1057 commit e23b2a7
Show file tree
Hide file tree
Showing 43 changed files with 493 additions and 511 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Decentralized Web Node (DWN) SDK <!-- omit in toc -->

Code Coverage
![Statements](https://img.shields.io/badge/statements-97.69%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-94.87%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-94.16%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.69%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-97.54%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-94.52%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-94.2%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.54%25-brightgreen.svg?style=flat)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down Expand Up @@ -92,7 +92,7 @@ DWN SDK includes a polyfilled distribution that can imported in a `module` scrip
dataFormat: 'application/json',
published: true,
schema: 'yeeter/post',
authorizationSignatureInput: Jws.createSignatureInput(didKey)
authorizationSigner: Jws.createSigner(didKey)
});
// get the DWN to process the RecordsWrite
Expand Down Expand Up @@ -191,7 +191,7 @@ const recordsWrite = await RecordsWrite.create({
dataFormat: 'application/json',
published: true,
schema: 'yeeter/post',
authorizationSignatureInput: Jws.createSignatureInput(didKey)
authorizationSigner: Jws.createSigner(didKey)
});

// get the DWN to process the RecordsWrite
Expand Down Expand Up @@ -249,7 +249,7 @@ const signer = new CustomSigner();

const options: RecordsWriteOptions = {
...
authorizationSignatureInput : {
authorizationSigner : {
signer,
protectedHeader: { alg: 'EdDSA', kid: 'did:example:alice#key1' } // see https://www.iana.org/assignments/jose/jose.xhtml for valid signature `alg` values
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/dwn-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export enum DwnErrorCode {
PermissionsGrantUnauthorizedGrant = 'PermissionsGrantUnauthorizedGrant',
PermissionsRevokeMissingPermissionsGrant = 'PermissionsRevokeMissingPermissionsGrant',
PermissionsRevokeUnauthorizedRevoke = 'PermissionsRevokeUnauthorizedRevoke',
PrivateKeySignerUnableToDeduceAlgorithm = 'PrivateKeySignerUnableToDeduceAlgorithm',
PrivateKeySignerUnableToDeduceKeyId = 'PrivateKeySignerUnableToDeduceKeyId',
PrivateKeySignerUnsupportedCurve = 'PrivateKeySignerUnsupportedCurve',
ProtocolAuthorizationActionNotAllowed = 'ProtocolAuthorizationActionNotAllowed',
ProtocolAuthorizationIncorrectDataFormat = 'ProtocolAuthorizationIncorrectDataFormat',
Expand All @@ -57,7 +59,7 @@ export enum DwnErrorCode {
RecordsWriteGetEntryIdUndefinedAuthor = 'RecordsWriteGetEntryIdUndefinedAuthor',
RecordsWriteDataCidMismatch = 'RecordsWriteDataCidMismatch',
RecordsWriteDataSizeMismatch = 'RecordsWriteDataSizeMismatch',
RecordsWriteMissingAuthorizationSignatureInput = 'RecordsWriteMissingAuthorizationSignatureInput',
RecordsWriteMissingauthorizationSigner = 'RecordsWriteMissingauthorizationSigner',
RecordsWriteMissingDataInPrevious = 'RecordsWriteMissingDataInPrevious',
RecordsWriteMissingDataAssociation = 'RecordsWriteMissingDataAssociation',
RecordsWriteMissingDataStream = 'RecordsWriteMissingDataStream',
Expand Down
4 changes: 2 additions & 2 deletions src/core/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GeneralJws } from '../types/jws-types.js';
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { BaseAuthorizationPayload, Descriptor, GenericMessage } from '../types/message-types.js';

import { Cid } from '../utils/cid.js';
Expand Down Expand Up @@ -138,7 +138,7 @@ export abstract class Message<M extends GenericMessage> {
*/
public static async signAsAuthorization(
descriptor: Descriptor,
signatureInput: SignatureInput,
signatureInput: Signer,
permissionsGrantId?: string,
): Promise<GeneralJws> {
const descriptorCid = await Cid.computeCid(descriptor);
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export { RecordsDelete, RecordsDeleteOptions } from './interfaces/records-delete
export { RecordsRead, RecordsReadOptions } from './interfaces/records-read.js';
export { SnapshotsCreate, SnapshotsCreateOptions } from './interfaces/snapshots-create.js';
export { Secp256k1 } from './utils/secp256k1.js';
export { SignatureInput } from './types/jws-types.js';
export { Signer } from './types/signer.js';
export { DataStoreLevel } from './store/data-store-level.js';
export { EventLogLevel } from './event-log/event-log-level.js';
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/events-get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { EventsGetDescriptor, EventsGetMessage } from '../types/event-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -7,7 +7,7 @@ import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';

export type EventsGetOptions = {
watermark?: string;
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
messageTimestamp?: string;
};

Expand All @@ -31,7 +31,7 @@ export class EventsGet extends Message<EventsGetMessage> {
descriptor.watermark = options.watermark;
}

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/hooks-write.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { HooksWriteDescriptor, HooksWriteMessage } from '../types/hooks-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -19,7 +19,7 @@ export type HooksWriteOptions = {
filter: {
method: string,
},
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
};

/**
Expand All @@ -43,7 +43,7 @@ export class HooksWrite extends Message<HooksWriteMessage> {
// Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
removeUndefinedProperties(descriptor);

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/messages-get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { MessagesGetDescriptor, MessagesGetMessage } from '../types/messages-types.js';

import { Cid } from '../utils/cid.js';
Expand All @@ -8,7 +8,7 @@ import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';

export type MessagesGetOptions = {
messageCids: string[];
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
messageTimestamp?: string;
};

Expand All @@ -30,7 +30,7 @@ export class MessagesGet extends Message<MessagesGetMessage> {
messageTimestamp : options?.messageTimestamp ?? getCurrentTimeInHighPrecision(),
};

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
12 changes: 6 additions & 6 deletions src/interfaces/permissions-grant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PermissionsRequest } from './permissions-request.js';
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { PermissionConditions, PermissionScope, RecordsPermissionScope } from '../types/permissions-types.js';
import type { PermissionsGrantDescriptor, PermissionsGrantMessage } from '../types/permissions-types.js';

Expand All @@ -19,7 +19,7 @@ export type PermissionsGrantOptions = {
permissionsRequestId?: string;
scope: PermissionScope;
conditions?: PermissionConditions;
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
};

export type CreateFromPermissionsRequestOverrides = {
Expand Down Expand Up @@ -60,7 +60,7 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
// Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
removeUndefinedProperties(descriptor);

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message: PermissionsGrantMessage = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand All @@ -72,12 +72,12 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
/**
* generates a PermissionsGrant using the provided PermissionsRequest
* @param permissionsRequest
* @param authorizationSignatureInput - the private key and additional signature material of the grantor
* @param authorizationSigner - the private key and additional signature material of the grantor
* @param overrides - overrides that will be used instead of the properties in `permissionsRequest`
*/
public static async createFromPermissionsRequest(
permissionsRequest: PermissionsRequest,
authorizationSignatureInput: SignatureInput,
authorizationSigner: Signer,
overrides: CreateFromPermissionsRequestOverrides,
): Promise<PermissionsGrant> {
const descriptor = permissionsRequest.message.descriptor;
Expand All @@ -90,7 +90,7 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
permissionsRequestId : await Message.getCid(permissionsRequest.message),
scope : overrides.scope ?? descriptor.scope,
conditions : overrides.conditions ?? descriptor.conditions,
authorizationSignatureInput,
authorizationSigner,
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/permissions-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { PermissionConditions, PermissionScope } from '../types/permissions-types.js';
import type { PermissionsRequestDescriptor, PermissionsRequestMessage } from '../types/permissions-types.js';

Expand All @@ -15,7 +15,7 @@ export type PermissionsRequestOptions = {
grantedFor: string;
scope: PermissionScope;
conditions?: PermissionConditions;
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
};

export class PermissionsRequest extends Message<PermissionsRequestMessage> {
Expand Down Expand Up @@ -43,7 +43,7 @@ export class PermissionsRequest extends Message<PermissionsRequestMessage> {
// Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
removeUndefinedProperties(descriptor);

const auth = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const auth = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message: PermissionsRequestMessage = { descriptor, authorization: auth };

Message.validateJsonSchema(message);
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/permissions-revoke.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { PermissionsGrantMessage, PermissionsRevokeDescriptor, PermissionsRevokeMessage } from '../types/permissions-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -9,7 +9,7 @@ import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
export type PermissionsRevokeOptions = {
messageTimestamp?: string;
permissionsGrantId: string;
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
};

export class PermissionsRevoke extends Message<PermissionsRevokeMessage> {
Expand All @@ -27,7 +27,7 @@ export class PermissionsRevoke extends Message<PermissionsRevokeMessage> {
permissionsGrantId : options.permissionsGrantId,
};

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message: PermissionsRevokeMessage = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/protocols-configure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { ProtocolDefinition, ProtocolsConfigureDescriptor, ProtocolsConfigureMessage } from '../types/protocols-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -9,7 +9,7 @@ import { normalizeProtocolUrl, normalizeSchemaUrl, validateProtocolUrlNormalized
export type ProtocolsConfigureOptions = {
messageTimestamp? : string;
definition : ProtocolDefinition;
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
permissionsGrantId?: string;
};

Expand All @@ -32,7 +32,7 @@ export class ProtocolsConfigure extends Message<ProtocolsConfigureMessage> {
definition : ProtocolsConfigure.normalizeDefinition(options.definition)
};

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput, options.permissionsGrantId);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner, options.permissionsGrantId);
const message = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
9 changes: 5 additions & 4 deletions src/interfaces/protocols-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GeneralJws } from '../types/jws-types.js';
import type { MessageStore } from '../types/message-store.js';
import type { GeneralJws, SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { ProtocolsQueryDescriptor, ProtocolsQueryFilter, ProtocolsQueryMessage } from '../types/protocols-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -14,7 +15,7 @@ import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
export type ProtocolsQueryOptions = {
messageTimestamp?: string;
filter?: ProtocolsQueryFilter,
authorizationSignatureInput?: SignatureInput;
authorizationSigner?: Signer;
permissionsGrantId?: string;
};

Expand Down Expand Up @@ -46,8 +47,8 @@ export class ProtocolsQuery extends Message<ProtocolsQueryMessage> {

// only generate the `authorization` property if signature input is given
let authorization: GeneralJws | undefined;
if (options.authorizationSignatureInput !== undefined) {
authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput, options.permissionsGrantId);
if (options.authorizationSigner !== undefined) {
authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner, options.permissionsGrantId);
}

const message = { descriptor, authorization };
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/records-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import type { RecordsDeleteDescriptor, RecordsDeleteMessage } from '../types/rec

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
import { Message } from '../core/message.js';
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';

import { authorize, validateAuthorizationIntegrity } from '../core/auth.js';
import { DwnInterfaceName, DwnMethodName } from '../core/message.js';

export type RecordsDeleteOptions = {
recordId: string;
messageTimestamp?: string;
authorizationSignatureInput: SignatureInput;
authorizationSigner: Signer;
};

export class RecordsDelete extends Message<RecordsDeleteMessage> {
Expand Down Expand Up @@ -38,7 +38,7 @@ export class RecordsDelete extends Message<RecordsDeleteMessage> {
messageTimestamp : options.messageTimestamp ?? currentTime
};

const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSigner);
const message: RecordsDeleteMessage = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/records-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Pagination } from '../types/message-types.js';
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { RecordsFilter, RecordsQueryDescriptor, RecordsQueryMessage } from '../types/records-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -22,7 +22,7 @@ export type RecordsQueryOptions = {
filter: RecordsFilter;
dateSort?: DateSort;
pagination?: Pagination;
authorizationSignatureInput?: SignatureInput;
authorizationSigner?: Signer;
};

export class RecordsQuery extends Message<RecordsQueryMessage> {
Expand Down Expand Up @@ -57,8 +57,8 @@ export class RecordsQuery extends Message<RecordsQueryMessage> {
removeUndefinedProperties(descriptor);

// only generate the `authorization` property if signature input is given
const authorizationSignatureInput = options.authorizationSignatureInput;
const authorization = authorizationSignatureInput ? await Message.signAsAuthorization(descriptor, authorizationSignatureInput) : undefined;
const authorizationSigner = options.authorizationSigner;
const authorization = authorizationSigner ? await Message.signAsAuthorization(descriptor, authorizationSigner) : undefined;
const message = { descriptor, authorization };

Message.validateJsonSchema(message);
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces/records-read.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GenericMessage } from '../types/message-types.js';
import type { MessageStore } from '../types/message-store.js';
import type { RecordsWrite } from './records-write.js';
import type { SignatureInput } from '../types/jws-types.js';
import type { Signer } from '../types/signer.js';
import type { RecordsFilter , RecordsReadDescriptor, RecordsReadMessage } from '../types/records-types.js';

import { getCurrentTimeInHighPrecision } from '../utils/time.js';
Expand All @@ -16,7 +16,7 @@ import { DwnInterfaceName, DwnMethodName } from '../core/message.js';
export type RecordsReadOptions = {
filter: RecordsFilter;
date?: string;
authorizationSignatureInput?: SignatureInput;
authorizationSigner?: Signer;
permissionsGrantId?: string;
};

Expand All @@ -39,7 +39,7 @@ export class RecordsRead extends Message<RecordsReadMessage> {
* @throws {DwnError} when a combination of required RecordsReadOptions are missing
*/
public static async create(options: RecordsReadOptions): Promise<RecordsRead> {
const { filter, authorizationSignatureInput, permissionsGrantId } = options;
const { filter, authorizationSigner, permissionsGrantId } = options;
const currentTime = getCurrentTimeInHighPrecision();

const descriptor: RecordsReadDescriptor = {
Expand All @@ -53,8 +53,8 @@ export class RecordsRead extends Message<RecordsReadMessage> {

// only generate the `authorization` property if signature input is given
let authorization = undefined;
if (authorizationSignatureInput !== undefined) {
authorization = await Message.signAsAuthorization(descriptor, authorizationSignatureInput, permissionsGrantId);
if (authorizationSigner !== undefined) {
authorization = await Message.signAsAuthorization(descriptor, authorizationSigner, permissionsGrantId);
}
const message: RecordsReadMessage = { descriptor, authorization };

Expand Down
Loading

0 comments on commit e23b2a7

Please sign in to comment.