Skip to content

Commit

Permalink
cleanup crypto utils (#838)
Browse files Browse the repository at this point in the history
* cleanup crypto utils (#830)

* cleanup crypto utils

* changeset

* Update index.ts

* finish: delete package.json utils export

* bump dwn server

* fix api bump

* Update utils.ts

* bump dwn server

* maybe fix codecov
  • Loading branch information
shamilovtim authored Aug 20, 2024
1 parent 0862ffc commit 2d0b423
Show file tree
Hide file tree
Showing 28 changed files with 157 additions and 312 deletions.
12 changes: 12 additions & 0 deletions .changeset/friendly-carrots-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@web5/crypto-aws-kms": patch
"@web5/identity-agent": patch
"@web5/credentials": patch
"@web5/proxy-agent": patch
"@web5/user-agent": patch
"@web5/crypto": patch
"@web5/agent": patch
"@web5/dids": patch
---

cleanup crypto utils
2 changes: 1 addition & 1 deletion .github/workflows/tests-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
run: kill $DWN_SERVER_BACKGROUND_PROCESS || true

- name: Upload test coverage to Codecov
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 #v4.5.0
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

Expand Down
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ component_management:
- type: project
target: auto # auto compares coverage to the previous base commit
threshold: 5% # allows a 5% drop from the previous base commit coverage
informational: true
- type: patch
target: 90
informational: true
if_ci_failed: success

individual_components:
- component_id: package-agent
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@changesets/cli": "^2.27.5",
"@npmcli/package-json": "5.0.0",
"@typescript-eslint/eslint-plugin": "7.9.0",
"@web5/dwn-server": "0.4.6",
"@web5/dwn-server": "0.4.7",
"audit-ci": "^7.0.1",
"eslint-plugin-mocha": "10.4.3",
"globals": "^13.24.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@tbd54566975/dwn-sdk-js';

import { NodeStream } from '@web5/common';
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';
import { DidDht, DidJwk, DidResolverCacheLevel, UniversalResolver } from '@web5/dids';

import type { Web5PlatformAgent } from './types/agent.js';
Expand Down Expand Up @@ -396,7 +396,7 @@ export class AgentDwnApi {
const keyManager = this.agent.keyManager;

return {
algorithm : cryptoUtils.getJoseSignatureAlgorithmFromPublicKey(publicKey),
algorithm : CryptoUtils.getJoseSignatureAlgorithmFromPublicKey(publicKey),
keyId : signingMethod.id,
sign : async (data: Uint8Array) => {
return await keyManager.sign({ data, keyUri: keyUri! });
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/src/dwn-registrar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sha256, utils } from '@web5/crypto';
import { Sha256, CryptoUtils } from '@web5/crypto';
import { concatenateUrl } from './utils.js';
import { Convert } from '@web5/common';

Expand Down Expand Up @@ -120,8 +120,8 @@ export class DwnRegistrar {
* Generates 32 random bytes expressed as a HEX string.
*/
public static async generateNonce(): Promise<string> {
const randomBytes = utils.randomBytes(32);
const hexString = await Convert.uint8Array(randomBytes).toHex().toUpperCase();
const randomBytes = CryptoUtils.randomBytes(32);
const hexString = Convert.uint8Array(randomBytes).toHex().toUpperCase();
return hexString;
}
}
4 changes: 2 additions & 2 deletions packages/agent/src/prototyping/clients/http-dwn-rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { JsonRpcResponse } from './json-rpc.js';
import type { DwnRpc, DwnRpcRequest, DwnRpcResponse } from './dwn-rpc-types.js';

import { createJsonRpcRequest, parseJson } from './json-rpc.js';
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';
import { DwnServerInfoCache, ServerInfo } from './server-info-types.js';
import { DwnServerInfoCacheMemory } from './dwn-server-info-cache-memory.js';

Expand All @@ -18,7 +18,7 @@ export class HttpDwnRpcClient implements DwnRpc {
get transportProtocols() { return ['http:', 'https:']; }

async sendDwnRequest(request: DwnRpcRequest): Promise<DwnRpcResponse> {
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const jsonRpcRequest = createJsonRpcRequest(requestId, 'dwn.processMessage', {
target : request.targetDid,
message : request.message
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/src/prototyping/clients/json-rpc-socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';
import IsomorphicWebSocket from 'isomorphic-ws';
import { JsonRpcId, JsonRpcRequest, JsonRpcResponse, createJsonRpcSubscriptionRequest, parseJson } from './json-rpc.js';

Expand Down Expand Up @@ -81,7 +81,7 @@ export class JsonRpcSocket {
*/
async request(request: JsonRpcRequest): Promise<JsonRpcResponse> {
return new Promise((resolve, reject) => {
request.id ??= cryptoUtils.randomUuid();
request.id ??= CryptoUtils.randomUuid();

const handleResponse = (event: { data: any }):void => {
const jsonRpsResponse = parseJson(event.data) as JsonRpcResponse;
Expand Down Expand Up @@ -155,7 +155,7 @@ export class JsonRpcSocket {
}

private closeSubscription(id: JsonRpcId): Promise<JsonRpcResponse> {
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcSubscriptionRequest(requestId, 'close', id, {});
return this.request(request);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/agent/src/prototyping/clients/web-socket-clients.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DwnRpc, DwnRpcRequest, DwnRpcResponse, DwnSubscriptionHandler } from './dwn-rpc-types.js';
import type { GenericMessage, MessageSubscription, UnionMessageReply } from '@tbd54566975/dwn-sdk-js';

import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';
import { createJsonRpcRequest, createJsonRpcSubscriptionRequest } from './json-rpc.js';
import { JsonRpcSocket, JsonRpcSocketOptions } from './json-rpc-socket.js';

Expand Down Expand Up @@ -46,7 +46,7 @@ export class WebSocketDwnRpcClient implements DwnRpc {
}

private static async processMessage(connection: SocketConnection, target: string, message: GenericMessage): Promise<DwnRpcResponse> {
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'dwn.processMessage', { target, message });

const { socket } = connection;
Expand All @@ -61,8 +61,8 @@ export class WebSocketDwnRpcClient implements DwnRpc {
}

private static async subscriptionRequest(connection: SocketConnection, target:string, message: GenericMessage, messageHandler: DwnSubscriptionHandler): Promise<DwnRpcResponse> {
const requestId = cryptoUtils.randomUuid();
const subscriptionId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const subscriptionId = CryptoUtils.randomUuid();
const request = createJsonRpcSubscriptionRequest(requestId, 'dwn.processMessage', subscriptionId, { target, message });

const { socket, subscriptions } = connection;
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/prototyping/crypto/jose/jwe-flattened.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Jwk, KeyIdentifier } from '@web5/crypto';

import { Convert } from '@web5/common';
import { LocalKeyManager, utils as cryptoUtils } from '@web5/crypto';
import { LocalKeyManager, CryptoUtils } from '@web5/crypto';

import type { CryptoApi } from '../types/crypto-api.js';
import type { KeyManager } from '../types/key-manager.js';
Expand Down Expand Up @@ -404,7 +404,7 @@ export class FlattenedJwe {
case 'A128GCM':
case 'A192GCM':
case 'A256GCM':
iv = cryptoUtils.randomBytes(12);
iv = CryptoUtils.randomBytes(12);
break;
default:
iv = new Uint8Array(0);
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/rpc-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';


import type { DwnRpc, DwnRpcRequest, DwnRpcResponse } from './prototyping/clients/dwn-rpc-types.js';
Expand Down Expand Up @@ -114,7 +114,7 @@ export class Web5RpcClient implements Web5Rpc {

export class HttpWeb5RpcClient extends HttpDwnRpcClient implements Web5Rpc {
async sendDidRequest(request: DidRpcRequest): Promise<DidRpcResponse> {
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const jsonRpcRequest = createJsonRpcRequest(requestId, request.method, {
data: request.data
});
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/tests/crypto-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Jwk } from '@web5/crypto';

import { expect } from 'chai';
import { Convert } from '@web5/common';
import { utils as cryptoUtils, isOctPrivateJwk } from '@web5/crypto';
import { CryptoUtils, isOctPrivateJwk } from '@web5/crypto';

import { isChrome } from './utils/runtimes.js';
import { AgentCryptoApi } from '../src/crypto-api.js';
Expand Down Expand Up @@ -379,7 +379,7 @@ describe('AgentCryptoApi', () => {
kid : 'kpI8W6JS7O5ncakbn5dUOgP7uCuHGtZnkNOX2ZnRiss',
};
const plaintext = new Uint8Array([1, 2, 3, 4]);
const iv = cryptoUtils.randomBytes(12); // Initialization vector.
const iv = CryptoUtils.randomBytes(12); // Initialization vector.
const tagLength = 128; // Size in bits of the authentication tag.

// Test the method.
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/tests/local-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BearerDid } from '@web5/dids';

import { expect } from 'chai';
import { Convert } from '@web5/common';
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';

import type { Web5PlatformAgent } from '../src/types/agent.js';

Expand Down Expand Up @@ -89,7 +89,7 @@ describe('LocalKeyManager', () => {
// Setup.
const encryptionKeyUri = await testHarness.agent.keyManager.generateKey({ algorithm: 'A128GCM' });
const plaintext = new Uint8Array([1, 2, 3, 4]);
const iv = cryptoUtils.randomBytes(12); // Initialization vector.
const iv = CryptoUtils.randomBytes(12); // Initialization vector.
const tagLength = 128; // Size in bits of the authentication tag.

// Test the method.
Expand Down
30 changes: 15 additions & 15 deletions packages/agent/tests/prototyping/clients/json-rpc-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import sinon from 'sinon';

import { JsonRpcSocket } from '../../../src/prototyping/clients/json-rpc-socket.js';
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';
import { JsonRpcErrorCodes, JsonRpcResponse, createJsonRpcErrorResponse, createJsonRpcRequest, createJsonRpcSubscriptionRequest, createJsonRpcSuccessResponse } from '../../../src/prototyping/clients/json-rpc.js';
import { testDwnUrl } from '../../utils/test-config.js';
import { Persona, TestDataGenerator } from '@tbd54566975/dwn-sdk-js';
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('JsonRpcSocket', () => {

it('generates a request id if one is not provided', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'dwn.processMessage', { param1: 'test-param1', param2: 'test-param2' });
delete request.id;

Expand All @@ -47,7 +47,7 @@ describe('JsonRpcSocket', () => {

it('resolves a request with given params', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'dwn.processMessage', { param1: 'test-param1', param2: 'test-param2' });
const response = await client.request(request);
expect(response.id).to.equal(request.id);
Expand All @@ -56,7 +56,7 @@ describe('JsonRpcSocket', () => {
it('request times out', async () => {
// time out after 1 ms
const client = await JsonRpcSocket.connect(socketDwnUrl, { responseTimeout: 1 });
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'down.processMessage', { param1: 'test-param1', param2: 'test-param2' });
try {
await client.request(request);
Expand All @@ -69,7 +69,7 @@ describe('JsonRpcSocket', () => {
it('adds a handler to the messageHandlers map when listening for a response to a request', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const { message } = await TestDataGenerator.generateRecordsSubscribe({ author: alice });
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'dwn.processMessage', { target: alice.did, message });
const response = client.request(request);
expect(client['messageHandlers'].has(requestId)).to.be.true;
Expand All @@ -84,8 +84,8 @@ describe('JsonRpcSocket', () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const { message } = await TestDataGenerator.generateRecordsSubscribe({ author: alice });

const requestId = cryptoUtils.randomUuid();
const subscriptionId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const subscriptionId = CryptoUtils.randomUuid();
const request = createJsonRpcSubscriptionRequest(
requestId,
'dwn.processMessage',
Expand All @@ -104,8 +104,8 @@ describe('JsonRpcSocket', () => {

it('removes listener if subscription json rpc is rejected ', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const requestId = cryptoUtils.randomUuid();
const subscribeId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const subscribeId = CryptoUtils.randomUuid();

const request = createJsonRpcSubscriptionRequest(
requestId,
Expand All @@ -126,8 +126,8 @@ describe('JsonRpcSocket', () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const { message } = await TestDataGenerator.generateRecordsSubscribe({ author: alice });

const requestId = cryptoUtils.randomUuid();
const subscriptionId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const subscriptionId = CryptoUtils.randomUuid();
const request = createJsonRpcSubscriptionRequest(
requestId,
'dwn.processMessage',
Expand All @@ -149,7 +149,7 @@ describe('JsonRpcSocket', () => {

it('only JSON RPC Methods prefixed with `rpc.subscribe.` are accepted for a subscription', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'test.method', { param1: 'test-param1', param2: 'test-param2' });
try {
await client.subscribe(request, () => {});
Expand All @@ -161,7 +161,7 @@ describe('JsonRpcSocket', () => {

it('subscribe methods must contain a subscribe object within the request which contains the subscription JsonRpcId', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'rpc.subscribe.test.method', { param1: 'test-param1', param2: 'test-param2' });
try {
await client.subscribe(request, () => {});
Expand Down Expand Up @@ -235,8 +235,8 @@ describe('JsonRpcSocket', () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const { message } = await TestDataGenerator.generateRecordsSubscribe({ author: alice });

const requestId = cryptoUtils.randomUuid();
const subscriptionId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const subscriptionId = CryptoUtils.randomUuid();
const request = createJsonRpcSubscriptionRequest(
requestId,
'dwn.processMessage',
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/tests/rpc-client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { testDwnUrl } from './utils/test-config.js';
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';

import { DidRpcMethod, HttpWeb5RpcClient, Web5RpcClient, WebSocketWeb5RpcClient } from '../src/rpc-client.js';
import { DwnServerInfoCacheMemory } from '../src/prototyping/clients/dwn-server-info-cache-memory.js';
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('RPC Clients', () => {
it('should throw if json rpc server responds with an error', async () => {
const request = { method: DidRpcMethod.Resolve, url: testDwnUrl, data: 'some-data' };

const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const jsonRpcResponse = createJsonRpcErrorResponse(
requestId,
JsonRpcErrorCodes.InternalError,
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('RPC Clients', () => {
it('should return json rpc result', async () => {
const request = { method: DidRpcMethod.Resolve, url: testDwnUrl, data: 'some-data' };

const requestId = cryptoUtils.randomUuid();
const requestId = CryptoUtils.randomUuid();
const jsonRpcResponse = createJsonRpcSuccessResponse(
requestId,
{ status: { code: 200 }, data: 'data' }
Expand Down
8 changes: 4 additions & 4 deletions packages/agent/tests/sync-engine-level.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';
import { DwnConstant, ProtocolDefinition } from '@tbd54566975/dwn-sdk-js';

import type { BearerIdentity } from '../src/bearer-identity.js';
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('SyncEngineLevel', () => {
});

beforeEach(async () => {
randomSchema = cryptoUtils.randomUuid();
randomSchema = CryptoUtils.randomUuid();

sinon.restore();

Expand Down Expand Up @@ -821,7 +821,7 @@ describe('SyncEngineLevel', () => {
it('silently ignores a messageCid from the eventLog that does not exist on the local DWN', async () => {
// It's important to create a new DID here to avoid conflicts with the previous test on the remote DWN,
// since we are not clearing the remote DWN's storage before each test.
const name = cryptoUtils.randomUuid();
const name = CryptoUtils.randomUuid();
const alice = await testHarness.createIdentity({ name, testDwnUrls });

// scenario: The messageCids returned from the local eventLog contains a Cid that is not found when attempting to push it to the remote DWN
Expand Down Expand Up @@ -926,7 +926,7 @@ describe('SyncEngineLevel', () => {
it('silently ignores a messageCid that already exists on the remote DWN', async () => {
// It's important to create a new DID here to avoid conflicts with the previous test on the remote DWN,
// since we are not clearing the remote DWN's storage before each test.
const name = cryptoUtils.randomUuid();
const name = CryptoUtils.randomUuid();
const alice = await testHarness.createIdentity({ name, testDwnUrls });

// Register Alice's DID to be synchronized.
Expand Down
Loading

0 comments on commit 2d0b423

Please sign in to comment.