Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup crypto utils #830

Merged
merged 7 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/friendly-carrots-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@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
"@web5/api": patch
---

cleanup crypto utils
4 changes: 2 additions & 2 deletions packages/agent/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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 @@ -402,7 +402,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
4 changes: 2 additions & 2 deletions packages/api/tests/utils/test-data-generator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';

export class TestDataGenerator {
/**
* Generates a random byte array of given length.
*/
static randomBytes(length: number): Uint8Array {
return cryptoUtils.randomBytes(length);
return CryptoUtils.randomBytes(length);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/credentials/src/verifiable-credential.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BearerDid } from '@web5/dids';
import type { ICredential, ICredentialSubject} from '@sphereon/ssi-types';

import { utils as cryptoUtils } from '@web5/crypto';
import { CryptoUtils } from '@web5/crypto';

import { Jwt } from './jwt.js';
import { SsiValidator } from './validators.js';
Expand Down Expand Up @@ -196,7 +196,7 @@ export class VerifiableCredential {
type : Array.isArray(type)
? [DEFAULT_VC_TYPE, ...type]
: (type ? [DEFAULT_VC_TYPE, type] : [DEFAULT_VC_TYPE]),
id : `urn:uuid:${cryptoUtils.randomUuid()}`,
id : `urn:uuid:${CryptoUtils.randomUuid()}`,
issuer : issuer,
issuanceDate : issuanceDate || getCurrentXmlSchema112Timestamp(),
credentialSubject : credentialSubject,
Expand Down
Loading