Formatting
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { subtle, getRandomValues } from "node:crypto";
|
||||
import { fromExtendedJson, toExtendedJson } from "@xo-cash/utils";
|
||||
import { sha256, binToBase64, binToHex } from "@bitauth/libauth";
|
||||
import { subtle, getRandomValues } from 'node:crypto';
|
||||
import { fromExtendedJson, toExtendedJson } from '@xo-cash/utils';
|
||||
import { sha256, binToBase64, binToHex } from '@bitauth/libauth';
|
||||
|
||||
import { BaseSyncClient, type Resource, type ResourcesRecord } from "../../source/shared/base-client.js";
|
||||
import { BaseSyncClient, type Resource, type ResourcesRecord } from '../../source/shared/base-client.js';
|
||||
|
||||
type ResourceKey = {
|
||||
resourceId: string;
|
||||
keyMaterial: Uint8Array;
|
||||
resourceId: string;
|
||||
keyMaterial: Uint8Array;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -17,160 +17,166 @@ type ResourceKey = {
|
||||
* resource ID, so the resource ID must be unguessable (high entropy) for this to be secure.
|
||||
*/
|
||||
export class EncryptedClientPerResource<T = unknown> extends BaseSyncClient<T> {
|
||||
/** Maps the hashed (server-side) resource ID to the original resource ID and its key */
|
||||
private readonly resourceKeys = new Map<string, ResourceKey>();
|
||||
/** Maps the hashed (server-side) resource ID to the original resource ID and its key */
|
||||
private readonly resourceKeys = new Map<string, ResourceKey>();
|
||||
|
||||
constructor(private readonly client: BaseSyncClient<string>) {
|
||||
super();
|
||||
constructor(private readonly client: BaseSyncClient<string>) {
|
||||
super();
|
||||
|
||||
this.client.on('resource-instance-updated', async (message) => {
|
||||
// Ignore updates for resources we don't hold the key for
|
||||
const resourceKey = this.resourceKeys.get(message.resourceId);
|
||||
if (!resourceKey) return;
|
||||
this.client.on('resource-instance-updated', async (message) => {
|
||||
// Ignore updates for resources we don't hold the key for
|
||||
const resourceKey = this.resourceKeys.get(message.resourceId);
|
||||
if (!resourceKey) return;
|
||||
|
||||
try {
|
||||
this.emit('resource-instance-updated', {
|
||||
...message,
|
||||
resourceId: resourceKey.resourceId,
|
||||
value: await EncryptedClientPerResource.decrypt(resourceKey.keyMaterial, message.value) as T,
|
||||
try {
|
||||
this.emit('resource-instance-updated', {
|
||||
...message,
|
||||
resourceId: resourceKey.resourceId,
|
||||
value: (await EncryptedClientPerResource.decrypt(resourceKey.keyMaterial, message.value)) as T,
|
||||
});
|
||||
} catch (error) {
|
||||
this.emit('error', error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.emit('error', error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives the AES key and server-side resource ID from a resource ID, and remembers the mapping.
|
||||
* @param resourceId - The resource ID (the secret)
|
||||
* @returns The hashed resource ID to send to the server
|
||||
*/
|
||||
private hashResourceId(resourceId: string): string {
|
||||
const keyMaterial = sha256.hash(new TextEncoder().encode(resourceId));
|
||||
const hashedResourceId = binToHex(sha256.hash(keyMaterial));
|
||||
|
||||
this.resourceKeys.set(hashedResourceId, { resourceId, keyMaterial });
|
||||
|
||||
return hashedResourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the original resource ID and key for a hashed resource ID.
|
||||
* @param hashedResourceId - The hashed (server-side) resource ID
|
||||
* @returns The original resource ID and its key material
|
||||
*/
|
||||
private getResourceKey(hashedResourceId: string): ResourceKey {
|
||||
const resourceKey = this.resourceKeys.get(hashedResourceId);
|
||||
|
||||
if (!resourceKey) {
|
||||
throw new Error(`No key found for resource ${hashedResourceId}`);
|
||||
}
|
||||
|
||||
return resourceKey;
|
||||
}
|
||||
/**
|
||||
* Derives the AES key and server-side resource ID from a resource ID, and remembers the mapping.
|
||||
* @param resourceId - The resource ID (the secret)
|
||||
* @returns The hashed resource ID to send to the server
|
||||
*/
|
||||
private hashResourceId(resourceId: string): string {
|
||||
const keyMaterial = sha256.hash(new TextEncoder().encode(resourceId));
|
||||
const hashedResourceId = binToHex(sha256.hash(keyMaterial));
|
||||
|
||||
/**
|
||||
* Decrypts every instance of every resource in a record, and maps the hashed resource IDs back to the original resource IDs.
|
||||
* @param resources - The encrypted resources record, keyed by hashed resource ID
|
||||
* @returns The resources record with decrypted values, keyed by original resource ID
|
||||
*/
|
||||
private async decryptResources(resources: ResourcesRecord<string>): Promise<ResourcesRecord<T>> {
|
||||
const entries = await Promise.all(Object.entries(resources).map(async ([ hashedResourceId, resource ]) => {
|
||||
const { resourceId, keyMaterial } = this.getResourceKey(hashedResourceId);
|
||||
this.resourceKeys.set(hashedResourceId, { resourceId, keyMaterial });
|
||||
|
||||
const instances: Resource<T>[] = await Promise.all(resource.instances.map(async (instance) => ({
|
||||
...instance,
|
||||
resourceId,
|
||||
value: await EncryptedClientPerResource.decrypt(keyMaterial, instance.value) as T,
|
||||
})));
|
||||
return hashedResourceId;
|
||||
}
|
||||
|
||||
return [ resourceId, { ...resource, instances } ] as const;
|
||||
}));
|
||||
/**
|
||||
* Gets the original resource ID and key for a hashed resource ID.
|
||||
* @param hashedResourceId - The hashed (server-side) resource ID
|
||||
* @returns The original resource ID and its key material
|
||||
*/
|
||||
private getResourceKey(hashedResourceId: string): ResourceKey {
|
||||
const resourceKey = this.resourceKeys.get(hashedResourceId);
|
||||
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
if (!resourceKey) {
|
||||
throw new Error(`No key found for resource ${hashedResourceId}`);
|
||||
}
|
||||
|
||||
public async connect(): Promise<void> {
|
||||
return this.client.connect();
|
||||
}
|
||||
return resourceKey;
|
||||
}
|
||||
|
||||
public async disconnect(): Promise<void> {
|
||||
return this.client.disconnect();
|
||||
}
|
||||
/**
|
||||
* Decrypts every instance of every resource in a record, and maps the hashed resource IDs back to the original resource IDs.
|
||||
* @param resources - The encrypted resources record, keyed by hashed resource ID
|
||||
* @returns The resources record with decrypted values, keyed by original resource ID
|
||||
*/
|
||||
private async decryptResources(resources: ResourcesRecord<string>): Promise<ResourcesRecord<T>> {
|
||||
const entries = await Promise.all(
|
||||
Object.entries(resources).map(async ([hashedResourceId, resource]) => {
|
||||
const { resourceId, keyMaterial } = this.getResourceKey(hashedResourceId);
|
||||
|
||||
public async readMany(resourceIds: string[]): Promise<ResourcesRecord<T>> {
|
||||
const hashedResourceIds = resourceIds.map((resourceId) => this.hashResourceId(resourceId));
|
||||
const instances: Resource<T>[] = await Promise.all(
|
||||
resource.instances.map(async (instance) => ({
|
||||
...instance,
|
||||
resourceId,
|
||||
value: (await EncryptedClientPerResource.decrypt(keyMaterial, instance.value)) as T,
|
||||
})),
|
||||
);
|
||||
|
||||
const encryptedResources = await this.client.readMany(hashedResourceIds);
|
||||
return [resourceId, { ...resource, instances }] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
return this.decryptResources(encryptedResources);
|
||||
}
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
|
||||
public async writeMany(resources: Record<string, T>): Promise<ResourcesRecord<T>> {
|
||||
const encryptedEntries = await Promise.all(Object.entries(resources).map(async ([ resourceId, value ]) => {
|
||||
const hashedResourceId = this.hashResourceId(resourceId);
|
||||
const { keyMaterial } = this.getResourceKey(hashedResourceId);
|
||||
public async connect(): Promise<void> {
|
||||
return this.client.connect();
|
||||
}
|
||||
|
||||
return [ hashedResourceId, await EncryptedClientPerResource.encrypt(keyMaterial, value) ] as const;
|
||||
}));
|
||||
public async disconnect(): Promise<void> {
|
||||
return this.client.disconnect();
|
||||
}
|
||||
|
||||
const response = await this.client.writeMany(Object.fromEntries(encryptedEntries));
|
||||
public async readMany(resourceIds: string[]): Promise<ResourcesRecord<T>> {
|
||||
const hashedResourceIds = resourceIds.map((resourceId) => this.hashResourceId(resourceId));
|
||||
|
||||
return this.decryptResources(response);
|
||||
}
|
||||
const encryptedResources = await this.client.readMany(hashedResourceIds);
|
||||
|
||||
public async subscribe(resourceId: string): Promise<void> {
|
||||
return this.client.subscribe(this.hashResourceId(resourceId));
|
||||
}
|
||||
return this.decryptResources(encryptedResources);
|
||||
}
|
||||
|
||||
public async unsubscribe(resourceId: string): Promise<void> {
|
||||
return this.client.unsubscribe(this.hashResourceId(resourceId));
|
||||
}
|
||||
public async writeMany(resources: Record<string, T>): Promise<ResourcesRecord<T>> {
|
||||
const encryptedEntries = await Promise.all(
|
||||
Object.entries(resources).map(async ([resourceId, value]) => {
|
||||
const hashedResourceId = this.hashResourceId(resourceId);
|
||||
const { keyMaterial } = this.getResourceKey(hashedResourceId);
|
||||
|
||||
/**
|
||||
* Imports raw key material as an AES-GCM key
|
||||
* @param keyMaterial - The 32 byte key material
|
||||
* @returns The AES-GCM CryptoKey
|
||||
*/
|
||||
private static importKey(keyMaterial: Uint8Array) {
|
||||
return subtle.importKey('raw', Buffer.from(keyMaterial), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
||||
}
|
||||
return [hashedResourceId, await EncryptedClientPerResource.encrypt(keyMaterial, value)] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* Encrypts a value using AES-GCM with a random 12 byte IV
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The value to encrypt
|
||||
* @returns The IV and ciphertext as a base64 string
|
||||
*/
|
||||
public static async encrypt(keyMaterial: Uint8Array, value: unknown): Promise<string> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
const iv = getRandomValues(new Uint8Array(12));
|
||||
const response = await this.client.writeMany(Object.fromEntries(encryptedEntries));
|
||||
|
||||
// Encode the value as extended JSON and then to UTF-8 bytes
|
||||
const encodedValue = new TextEncoder().encode(toExtendedJson(value));
|
||||
return this.decryptResources(response);
|
||||
}
|
||||
|
||||
const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, key, encodedValue);
|
||||
public async subscribe(resourceId: string): Promise<void> {
|
||||
return this.client.subscribe(this.hashResourceId(resourceId));
|
||||
}
|
||||
|
||||
// Prepend the IV so it can be recovered when decrypting
|
||||
return binToBase64(Buffer.concat([ iv, new Uint8Array(encrypted) ]));
|
||||
}
|
||||
public async unsubscribe(resourceId: string): Promise<void> {
|
||||
return this.client.unsubscribe(this.hashResourceId(resourceId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts a value encrypted by `encrypt`
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The IV and ciphertext as a base64 string
|
||||
* @returns The decrypted value
|
||||
*/
|
||||
public static async decrypt(keyMaterial: Uint8Array, value: string): Promise<unknown> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
/**
|
||||
* Imports raw key material as an AES-GCM key
|
||||
* @param keyMaterial - The 32 byte key material
|
||||
* @returns The AES-GCM CryptoKey
|
||||
*/
|
||||
private static importKey(keyMaterial: Uint8Array) {
|
||||
return subtle.importKey('raw', Buffer.from(keyMaterial), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
||||
}
|
||||
|
||||
// Decode the base64 first: the IV is 12 bytes, not 12 base64 characters
|
||||
const bytes = Buffer.from(value, 'base64');
|
||||
const iv = bytes.subarray(0, 12);
|
||||
const encryptedValue = bytes.subarray(12);
|
||||
/**
|
||||
* Encrypts a value using AES-GCM with a random 12 byte IV
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The value to encrypt
|
||||
* @returns The IV and ciphertext as a base64 string
|
||||
*/
|
||||
public static async encrypt(keyMaterial: Uint8Array, value: unknown): Promise<string> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
const iv = getRandomValues(new Uint8Array(12));
|
||||
|
||||
const decrypted = await subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedValue);
|
||||
// Encode the value as extended JSON and then to UTF-8 bytes
|
||||
const encodedValue = new TextEncoder().encode(toExtendedJson(value));
|
||||
|
||||
return fromExtendedJson(new TextDecoder().decode(decrypted));
|
||||
}
|
||||
const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, key, encodedValue);
|
||||
|
||||
// Prepend the IV so it can be recovered when decrypting
|
||||
return binToBase64(Buffer.concat([iv, new Uint8Array(encrypted)]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts a value encrypted by `encrypt`
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The IV and ciphertext as a base64 string
|
||||
* @returns The decrypted value
|
||||
*/
|
||||
public static async decrypt(keyMaterial: Uint8Array, value: string): Promise<unknown> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
|
||||
// Decode the base64 first: the IV is 12 bytes, not 12 base64 characters
|
||||
const bytes = Buffer.from(value, 'base64');
|
||||
const iv = bytes.subarray(0, 12);
|
||||
const encryptedValue = bytes.subarray(12);
|
||||
|
||||
const decrypted = await subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedValue);
|
||||
|
||||
return fromExtendedJson(new TextDecoder().decode(decrypted));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,127 +1,136 @@
|
||||
import { subtle, getRandomValues } from "node:crypto";
|
||||
import { fromExtendedJson, toExtendedJson } from "@xo-cash/utils";
|
||||
import { sha256, binToBase64 } from "@bitauth/libauth";
|
||||
import { subtle, getRandomValues } from 'node:crypto';
|
||||
import { fromExtendedJson, toExtendedJson } from '@xo-cash/utils';
|
||||
import { sha256, binToBase64 } from '@bitauth/libauth';
|
||||
|
||||
import { BaseSyncClient, type ResourcesRecord } from "../../source/shared/base-client.js";
|
||||
import { BaseSyncClient, type ResourcesRecord } from '../../source/shared/base-client.js';
|
||||
|
||||
/**
|
||||
* Encrypts every resource with a single shared key.
|
||||
*/
|
||||
export class EncryptedClient<T = unknown> extends BaseSyncClient<T> {
|
||||
private readonly encryptionKey: Uint8Array;
|
||||
private readonly encryptionKey: Uint8Array;
|
||||
|
||||
constructor(private readonly client: BaseSyncClient<string>, encryptionKey: Uint8Array | string) {
|
||||
super();
|
||||
constructor(
|
||||
private readonly client: BaseSyncClient<string>,
|
||||
encryptionKey: Uint8Array | string,
|
||||
) {
|
||||
super();
|
||||
|
||||
// Hash the encryption key into 32 bytes if it is a string
|
||||
this.encryptionKey = typeof encryptionKey === 'string' ? sha256.hash(new TextEncoder().encode(encryptionKey)) : encryptionKey;
|
||||
// Hash the encryption key into 32 bytes if it is a string
|
||||
this.encryptionKey = typeof encryptionKey === 'string' ? sha256.hash(new TextEncoder().encode(encryptionKey)) : encryptionKey;
|
||||
|
||||
this.client.on('resource-instance-updated', async (message) => {
|
||||
try {
|
||||
this.emit('resource-instance-updated', {
|
||||
...message,
|
||||
value: await EncryptedClient.decrypt(this.encryptionKey, message.value) as T,
|
||||
this.client.on('resource-instance-updated', async (message) => {
|
||||
try {
|
||||
this.emit('resource-instance-updated', {
|
||||
...message,
|
||||
value: (await EncryptedClient.decrypt(this.encryptionKey, message.value)) as T,
|
||||
});
|
||||
} catch (error) {
|
||||
this.emit('error', error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.emit('error', error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts every instance of every resource in a record.
|
||||
* @param resources - The encrypted resources record
|
||||
* @returns The resources record with decrypted values
|
||||
*/
|
||||
private async decryptResources(resources: ResourcesRecord<string>): Promise<ResourcesRecord<T>> {
|
||||
const entries = await Promise.all(Object.entries(resources).map(async ([ resourceId, resource ]) => {
|
||||
const instances = await Promise.all(resource.instances.map(async (instance) => ({
|
||||
...instance,
|
||||
value: await EncryptedClient.decrypt(this.encryptionKey, instance.value) as T,
|
||||
})));
|
||||
/**
|
||||
* Decrypts every instance of every resource in a record.
|
||||
* @param resources - The encrypted resources record
|
||||
* @returns The resources record with decrypted values
|
||||
*/
|
||||
private async decryptResources(resources: ResourcesRecord<string>): Promise<ResourcesRecord<T>> {
|
||||
const entries = await Promise.all(
|
||||
Object.entries(resources).map(async ([resourceId, resource]) => {
|
||||
const instances = await Promise.all(
|
||||
resource.instances.map(async (instance) => ({
|
||||
...instance,
|
||||
value: (await EncryptedClient.decrypt(this.encryptionKey, instance.value)) as T,
|
||||
})),
|
||||
);
|
||||
|
||||
return [ resourceId, { ...resource, instances } ] as const;
|
||||
}));
|
||||
return [resourceId, { ...resource, instances }] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
|
||||
public async connect(): Promise<void> {
|
||||
return this.client.connect();
|
||||
}
|
||||
public async connect(): Promise<void> {
|
||||
return this.client.connect();
|
||||
}
|
||||
|
||||
public async disconnect(): Promise<void> {
|
||||
return this.client.disconnect();
|
||||
}
|
||||
public async disconnect(): Promise<void> {
|
||||
return this.client.disconnect();
|
||||
}
|
||||
|
||||
public async readMany(resourceIds: string[]): Promise<ResourcesRecord<T>> {
|
||||
const encryptedResources = await this.client.readMany(resourceIds);
|
||||
public async readMany(resourceIds: string[]): Promise<ResourcesRecord<T>> {
|
||||
const encryptedResources = await this.client.readMany(resourceIds);
|
||||
|
||||
return this.decryptResources(encryptedResources);
|
||||
}
|
||||
return this.decryptResources(encryptedResources);
|
||||
}
|
||||
|
||||
public async writeMany(resources: Record<string, T>): Promise<ResourcesRecord<T>> {
|
||||
const encryptedEntries = await Promise.all(Object.entries(resources).map(async ([ resourceId, value ]) => {
|
||||
return [ resourceId, await EncryptedClient.encrypt(this.encryptionKey, value) ] as const;
|
||||
}));
|
||||
public async writeMany(resources: Record<string, T>): Promise<ResourcesRecord<T>> {
|
||||
const encryptedEntries = await Promise.all(
|
||||
Object.entries(resources).map(async ([resourceId, value]) => {
|
||||
return [resourceId, await EncryptedClient.encrypt(this.encryptionKey, value)] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
const response = await this.client.writeMany(Object.fromEntries(encryptedEntries));
|
||||
const response = await this.client.writeMany(Object.fromEntries(encryptedEntries));
|
||||
|
||||
return this.decryptResources(response);
|
||||
}
|
||||
return this.decryptResources(response);
|
||||
}
|
||||
|
||||
public async subscribe(resourceId: string): Promise<void> {
|
||||
return this.client.subscribe(resourceId);
|
||||
}
|
||||
public async subscribe(resourceId: string): Promise<void> {
|
||||
return this.client.subscribe(resourceId);
|
||||
}
|
||||
|
||||
public async unsubscribe(resourceId: string): Promise<void> {
|
||||
return this.client.unsubscribe(resourceId);
|
||||
}
|
||||
public async unsubscribe(resourceId: string): Promise<void> {
|
||||
return this.client.unsubscribe(resourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports raw key material as an AES-GCM key
|
||||
* @param keyMaterial - The 32 byte key material
|
||||
* @returns The AES-GCM CryptoKey
|
||||
*/
|
||||
private static importKey(keyMaterial: Uint8Array) {
|
||||
return subtle.importKey('raw', Buffer.from(keyMaterial), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
||||
}
|
||||
/**
|
||||
* Imports raw key material as an AES-GCM key
|
||||
* @param keyMaterial - The 32 byte key material
|
||||
* @returns The AES-GCM CryptoKey
|
||||
*/
|
||||
private static importKey(keyMaterial: Uint8Array) {
|
||||
return subtle.importKey('raw', Buffer.from(keyMaterial), { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypts a value using AES-GCM with a random 12 byte IV
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The value to encrypt
|
||||
* @returns The IV and ciphertext as a base64 string
|
||||
*/
|
||||
public static async encrypt(keyMaterial: Uint8Array, value: unknown): Promise<string> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
const iv = getRandomValues(new Uint8Array(12));
|
||||
/**
|
||||
* Encrypts a value using AES-GCM with a random 12 byte IV
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The value to encrypt
|
||||
* @returns The IV and ciphertext as a base64 string
|
||||
*/
|
||||
public static async encrypt(keyMaterial: Uint8Array, value: unknown): Promise<string> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
const iv = getRandomValues(new Uint8Array(12));
|
||||
|
||||
// Encode the value as extended JSON and then to UTF-8 bytes
|
||||
const encodedValue = new TextEncoder().encode(toExtendedJson(value));
|
||||
// Encode the value as extended JSON and then to UTF-8 bytes
|
||||
const encodedValue = new TextEncoder().encode(toExtendedJson(value));
|
||||
|
||||
const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, key, encodedValue);
|
||||
const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, key, encodedValue);
|
||||
|
||||
// Prepend the IV so it can be recovered when decrypting
|
||||
return binToBase64(Buffer.concat([ iv, new Uint8Array(encrypted) ]));
|
||||
}
|
||||
// Prepend the IV so it can be recovered when decrypting
|
||||
return binToBase64(Buffer.concat([iv, new Uint8Array(encrypted)]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts a value encrypted by `encrypt`
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The IV and ciphertext as a base64 string
|
||||
* @returns The decrypted value
|
||||
*/
|
||||
public static async decrypt(keyMaterial: Uint8Array, value: string): Promise<unknown> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
/**
|
||||
* Decrypts a value encrypted by `encrypt`
|
||||
* @param keyMaterial - The encryption key material
|
||||
* @param value - The IV and ciphertext as a base64 string
|
||||
* @returns The decrypted value
|
||||
*/
|
||||
public static async decrypt(keyMaterial: Uint8Array, value: string): Promise<unknown> {
|
||||
const key = await this.importKey(keyMaterial);
|
||||
|
||||
// Decode the base64 first: the IV is 12 bytes, not 12 base64 characters
|
||||
const bytes = Buffer.from(value, 'base64');
|
||||
const iv = bytes.subarray(0, 12);
|
||||
const encryptedValue = bytes.subarray(12);
|
||||
// Decode the base64 first: the IV is 12 bytes, not 12 base64 characters
|
||||
const bytes = Buffer.from(value, 'base64');
|
||||
const iv = bytes.subarray(0, 12);
|
||||
const encryptedValue = bytes.subarray(12);
|
||||
|
||||
const decrypted = await subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedValue);
|
||||
const decrypted = await subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedValue);
|
||||
|
||||
return fromExtendedJson(new TextDecoder().decode(decrypted));
|
||||
}
|
||||
return fromExtendedJson(new TextDecoder().decode(decrypted));
|
||||
}
|
||||
}
|
||||
|
||||
+21
-19
@@ -1,26 +1,28 @@
|
||||
import type { BaseSyncClient, ResourcesRecord } from "../source/shared/base-client.js";
|
||||
import type { BaseSyncClient, ResourcesRecord } from '../source/shared/base-client.js';
|
||||
|
||||
export const writeMany = async (client: BaseSyncClient<{ message: string }>): Promise<ResourcesRecord<{ message: string }>> => {
|
||||
// Create a group of resources with changes
|
||||
const resources = Object.fromEntries(Array.from({ length: 10 }, (_, index) => [
|
||||
`resource-${index}`,
|
||||
{
|
||||
message: `Hello, world! this is the minimal sync client using ${client.constructor.name} ${index}`,
|
||||
},
|
||||
]));
|
||||
// Create a group of resources with changes
|
||||
const resources = Object.fromEntries(
|
||||
Array.from({ length: 10 }, (_, index) => [
|
||||
`resource-${index}`,
|
||||
{
|
||||
message: `Hello, world! this is the minimal sync client using ${client.constructor.name} ${index}`,
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
// Write the resources to the server
|
||||
const res = await client.writeMany(resources);
|
||||
// Write the resources to the server
|
||||
const res = await client.writeMany(resources);
|
||||
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
export const readMany = async (client: BaseSyncClient<{ message: string }>,): Promise<ResourcesRecord<{ message: string }>> => {
|
||||
// Create a list of resource IDs
|
||||
const resources = Array.from({ length: 10 }, (_, index) => `resource-${index}`);
|
||||
export const readMany = async (client: BaseSyncClient<{ message: string }>): Promise<ResourcesRecord<{ message: string }>> => {
|
||||
// Create a list of resource IDs
|
||||
const resources = Array.from({ length: 10 }, (_, index) => `resource-${index}`);
|
||||
|
||||
// Read the resources from the server
|
||||
const res = await client.readMany(resources);
|
||||
// Read the resources from the server
|
||||
const res = await client.readMany(resources);
|
||||
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
+50
-46
@@ -1,74 +1,78 @@
|
||||
import { PrivateKey } from "@xo-cash/primitives";
|
||||
import { PrivateKey } from '@xo-cash/primitives';
|
||||
|
||||
// Core clients
|
||||
import type { BaseSyncClient } from "../source/shared/base-client.js";
|
||||
import type { BaseSyncClient } from '../source/shared/base-client.js';
|
||||
import { SSEClient } from '../source/sse/client.js';
|
||||
import { WsClient } from "../source/ws/client.js";
|
||||
import { WsClient } from '../source/ws/client.js';
|
||||
|
||||
// Experimental clients
|
||||
import { EncryptedClientPerResource } from "./experimental/encrypted-client-per-resource.js";
|
||||
import { EncryptedClient } from "./experimental/encrypted-client.js";
|
||||
import { EncryptedClientPerResource } from './experimental/encrypted-client-per-resource.js';
|
||||
import { EncryptedClient } from './experimental/encrypted-client.js';
|
||||
|
||||
// Shared functions
|
||||
import { writeMany, readMany } from "./shared.js";
|
||||
import { writeMany, readMany } from './shared.js';
|
||||
|
||||
const privateKey = PrivateKey.fromString('c440ce6ac5b63ae7bc4af9891b80b7a88ccdeebba5e43246885e9ec56f912ac2');
|
||||
|
||||
const clients = {
|
||||
sse: new SSEClient<{ message: string }>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
ws: new WsClient<{ message: string }>('wss://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
encrypted: new EncryptedClient<{ message: string }>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey), 'secret'),
|
||||
encryptedPerResource: new EncryptedClientPerResource<{ message: string }>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey)),
|
||||
sse: new SSEClient<{ message: string }>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
ws: new WsClient<{ message: string }>('wss://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
encrypted: new EncryptedClient<{ message: string }>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey), 'secret'),
|
||||
encryptedPerResource: new EncryptedClientPerResource<{ message: string }>(
|
||||
new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
),
|
||||
};
|
||||
|
||||
// Test the suite for a given client
|
||||
const testSuite = async (client: BaseSyncClient<{ message: string }>) => {
|
||||
client.on('resource-instance-updated', (message) => {
|
||||
console.log(message);
|
||||
});
|
||||
client.on('resource-instance-updated', (message) => {
|
||||
console.log(message);
|
||||
});
|
||||
|
||||
// Connect to the sync server
|
||||
await client.connect();
|
||||
// Connect to the sync server
|
||||
await client.connect();
|
||||
|
||||
// Subscribe to the resource
|
||||
await client.subscribe('test');
|
||||
// Subscribe to the resource
|
||||
await client.subscribe('test');
|
||||
|
||||
// Write a new value to the resource
|
||||
await client.write('test', {'message': 'Hello, world! this is the minimal sync client using ' + client.constructor.name});
|
||||
// Write a new value to the resource
|
||||
await client.write('test', {
|
||||
message: 'Hello, world! this is the minimal sync client using ' + client.constructor.name,
|
||||
});
|
||||
|
||||
// Read the current value of the resource
|
||||
const data = await client.read('test');
|
||||
console.log(data.map((resource) => resource.value.message));
|
||||
// Read the current value of the resource
|
||||
const data = await client.read('test');
|
||||
console.log(data.map((resource) => resource.value.message));
|
||||
|
||||
// Use the shared functions to write and read many resources
|
||||
const writtenResources = await writeMany(client);
|
||||
console.dir(writtenResources, { depth: null });
|
||||
// Use the shared functions to write and read many resources
|
||||
const writtenResources = await writeMany(client);
|
||||
console.dir(writtenResources, { depth: null });
|
||||
|
||||
const readResources = await readMany(client);
|
||||
console.dir(readResources, { depth: null });
|
||||
const readResources = await readMany(client);
|
||||
console.dir(readResources, { depth: null });
|
||||
|
||||
// If the user presses Ctrl+C, disconnect the client and exit the program
|
||||
process.on('SIGINT', async () => {
|
||||
await client.disconnect();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
// If the user presses Ctrl+C, disconnect the client and exit the program
|
||||
process.on('SIGINT', async () => {
|
||||
await client.disconnect();
|
||||
process.exit(0);
|
||||
});
|
||||
};
|
||||
|
||||
// Get the clients to test
|
||||
const selectClients = (params: string[]): BaseSyncClient<{ message: string }>[] => {
|
||||
// If no params are provided, test all clients
|
||||
const names = params.length > 0 ? params : Object.keys(clients);
|
||||
// If no params are provided, test all clients
|
||||
const names = params.length > 0 ? params : Object.keys(clients);
|
||||
|
||||
// Find values that are unknown to the clients object
|
||||
const unknown = names.filter((n) => !(n in clients));
|
||||
if (unknown.length > 0) {
|
||||
console.error(`Invalid client type(s): ${unknown.join(', ')}. Expected: ${Object.keys(clients).join(', ')}`);
|
||||
process.exit(1);
|
||||
}
|
||||
// Find values that are unknown to the clients object
|
||||
const unknown = names.filter((n) => !(n in clients));
|
||||
if (unknown.length > 0) {
|
||||
console.error(`Invalid client type(s): ${unknown.join(', ')}. Expected: ${Object.keys(clients).join(', ')}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Return the clients that are known to the clients object
|
||||
return names.map((n) => clients[n as keyof typeof clients]);
|
||||
}
|
||||
// Return the clients that are known to the clients object
|
||||
return names.map((n) => clients[n as keyof typeof clients]);
|
||||
};
|
||||
|
||||
// Get the params
|
||||
const params = process.argv.slice(2);
|
||||
@@ -78,7 +82,7 @@ const clientsToTest = selectClients(params);
|
||||
|
||||
// Test the clients
|
||||
for (const client of clientsToTest) {
|
||||
await testSuite(client);
|
||||
await testSuite(client);
|
||||
}
|
||||
|
||||
// Wait for the user to press Ctrl+C
|
||||
@@ -86,4 +90,4 @@ await new Promise((resolve) => process.on('SIGINT', resolve));
|
||||
|
||||
// Exit the program
|
||||
console.log('Exiting...');
|
||||
process.exit(0);
|
||||
process.exit(0);
|
||||
|
||||
+39
-35
@@ -1,56 +1,60 @@
|
||||
import { PrivateKey } from "@xo-cash/primitives";
|
||||
import { PrivateKey } from '@xo-cash/primitives';
|
||||
|
||||
import type { BaseSyncClient } from "../source/shared/base-client.js";
|
||||
import type { BaseSyncClient } from '../source/shared/base-client.js';
|
||||
|
||||
import { SSEClient } from "../source/sse/client.js";
|
||||
import { EncryptedClientPerResource } from "./experimental/encrypted-client-per-resource.js";
|
||||
import { SSEClient } from '../source/sse/client.js';
|
||||
import { EncryptedClientPerResource } from './experimental/encrypted-client-per-resource.js';
|
||||
|
||||
const privateKey = PrivateKey.fromString('c440ce6ac5b63ae7bc4af9891b80b7a88ccdeebba5e43246885e9ec56f912ac2');
|
||||
|
||||
// Define some formatting helpers
|
||||
const formatResourceId = (resourceId: string) => {
|
||||
return `\x1b[32m${resourceId}\x1b[0m`;
|
||||
}
|
||||
return `\x1b[32m${resourceId}\x1b[0m`;
|
||||
};
|
||||
|
||||
const formatMessage = (message: string) => {
|
||||
return `\x1b[34m${message}\x1b[0m`;
|
||||
}
|
||||
return `\x1b[34m${message}\x1b[0m`;
|
||||
};
|
||||
|
||||
const resetFormatting = () => {
|
||||
return `\x1b[0m`;
|
||||
}
|
||||
return '\x1b[0m';
|
||||
};
|
||||
|
||||
const startTUI = async (client: BaseSyncClient<string>) => {
|
||||
// Listen and log out the resource change
|
||||
client.on('resource-instance-updated', (message) => {
|
||||
// Print the message with colours
|
||||
console.log(`${formatResourceId(message.resourceId)}:${formatResourceId(message.publicKey)} updated: ${formatMessage(message.value)}${resetFormatting()}`);
|
||||
});
|
||||
// Listen and log out the resource change
|
||||
client.on('resource-instance-updated', (message) => {
|
||||
// Print the message with colours
|
||||
console.log(
|
||||
`${formatResourceId(message.resourceId)}:${formatResourceId(message.publicKey)} updated: ${formatMessage(message.value)}${resetFormatting()}`,
|
||||
);
|
||||
});
|
||||
|
||||
// Connect to the sync server and subscribe to the resource
|
||||
await client.connect();
|
||||
await client.subscribe('encrypted-test');
|
||||
|
||||
// Start the TUI loop
|
||||
while (true) {
|
||||
// Print the prompt
|
||||
process.stdout.write('> ');
|
||||
// Connect to the sync server and subscribe to the resource
|
||||
await client.connect();
|
||||
await client.subscribe('encrypted-test');
|
||||
|
||||
// Wait for the user to enter a command
|
||||
const data = await new Promise((resolve) => process.stdin.once('data', resolve));
|
||||
// Start the TUI loop
|
||||
while (true) {
|
||||
// Print the prompt
|
||||
process.stdout.write('> ');
|
||||
|
||||
// If the user entered a command, write it to the resource
|
||||
if (data) {
|
||||
const message = data.toString().trim();
|
||||
await client.write('encrypted-test', message);
|
||||
// Wait for the user to enter a command
|
||||
const data = await new Promise((resolve) => process.stdin.once('data', resolve));
|
||||
|
||||
// If the user entered a command, write it to the resource
|
||||
if (data) {
|
||||
const message = data.toString().trim();
|
||||
await client.write('encrypted-test', message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const clients = {
|
||||
'sse': new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
'encrypted': new EncryptedClientPerResource<string>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey)),
|
||||
}
|
||||
sse: new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
||||
encrypted: new EncryptedClientPerResource<string>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey)),
|
||||
};
|
||||
|
||||
const params = process.argv.slice(2);
|
||||
const client = params[0] === 'false' ? clients.sse : clients.encrypted;
|
||||
|
||||
startTUI(client);
|
||||
startTUI(client);
|
||||
|
||||
Reference in New Issue
Block a user