From 39262adfe9ae15edd021557e6368880286728f56 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Thu, 2 May 2024 11:41:41 +0200 Subject: [PATCH] [worker] cleanup --- packages/worker-api/src/webCryptoRSA.ts | 5 +-- packages/worker-api/src/worker.ts | 41 +++---------------------- 2 files changed, 6 insertions(+), 40 deletions(-) diff --git a/packages/worker-api/src/webCryptoRSA.ts b/packages/worker-api/src/webCryptoRSA.ts index faee6757..e7435e2a 100644 --- a/packages/worker-api/src/webCryptoRSA.ts +++ b/packages/worker-api/src/webCryptoRSA.ts @@ -45,9 +45,6 @@ export async function parseWebCryptoRSA(data: any): Promise { console.log(`PublicKey: ${JSON.stringify(publicKey)}`); - const exported = await cryptoProvider.subtle.exportKey("jwk", publicKey); - console.log(`PublicKey: ${JSON.stringify(exported)}`); - return publicKey; } @@ -60,7 +57,7 @@ export async function encryptWithPublicKey(data: Uint8Array, publicKey: CryptoKe data ); - console.log(`EncryptedData: ${JSON.stringify({encrypted: buf2hex(encryptedData)})}`); + // console.log(`EncryptedData: ${JSON.stringify({encrypted: buf2hex(encryptedData)})}`); return encryptedData; } diff --git a/packages/worker-api/src/worker.ts b/packages/worker-api/src/worker.ts index 2c0f3304..9bcf3100 100644 --- a/packages/worker-api/src/worker.ts +++ b/packages/worker-api/src/worker.ts @@ -9,14 +9,12 @@ import WebSocketAsPromised from 'websocket-as-promised'; import {options as encointerOptions} from '@encointer/node-api'; import {parseI64F64} from '@encointer/util'; -import type { - Vault -} from '@encointer/types'; +import type {Vault} from '@encointer/types'; import {type CallOptions, type IWorker, Request, type WorkerOptions} from './interface.js'; import {parseBalance} from './parsers.js'; import {callGetter} from './sendRequest.js'; -import {parseWebCryptoRSA, encryptWithPublicKey} from "./webCryptoRSA.js"; +import {encryptWithPublicKey, parseWebCryptoRSA} from "./webCryptoRSA.js"; import type {u8} from "@polkadot/types-codec"; import BN from "bn.js"; @@ -117,18 +115,16 @@ export class Worker extends WebSocketAsPromised implements IWorker { } public async encrypt(data: Uint8Array): Promise> { - const inputData = data; - const dataBE = new BN(inputData); + const dataBE = new BN(data); const dataArrayBE = new Uint8Array(dataBE.toArray()); const cypherTextBuffer = await encryptWithPublicKey(dataArrayBE, this.shieldingKey() as CryptoKey); - const cypherArray = new Uint8Array(cypherTextBuffer); - const outputData = cypherArray; + const outputData = new Uint8Array(cypherTextBuffer); const be = new BN(outputData) const beArray = new Uint8Array(be.toArray()); - console.log(`${JSON.stringify({encrypted_array: beArray})}`) + // console.log(`${JSON.stringify({encrypted_array: beArray})}`) return this.createType('Vec', compactAddLength(beArray)) } @@ -167,30 +163,3 @@ export class Worker extends WebSocketAsPromised implements IWorker { return await callGetter(this, [Request.Worker, 'author_getShardVault', 'Vault'], {}, options) } } - -// function changeByteAndBitEndianness(bytes: Uint8Array): Uint8Array { -// const reversedBytes = changeEndianness(bytes); -// const changedBytes = new Uint8Array(reversedBytes.length); -// for (let i = 0; i < reversedBytes.length; i++) { -// changedBytes[i] = changeBitEndianness(reversedBytes[i]); -// } -// return changedBytes; -// } -// -// function changeEndianness(bytes: Uint8Array): Uint8Array { -// const reversedBytes = new Uint8Array(bytes.length); -// for (let i = 0; i < bytes.length; i++) { -// reversedBytes[i] = bytes[bytes.length - i - 1]; -// } -// return reversedBytes; -// } -// -// function changeBitEndianness(value: number): number { -// // Bit reversal algorithm: https://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits -// value = ((value >> 1) & 0x55555555) | ((value & 0x55555555) << 1); -// value = ((value >> 2) & 0x33333333) | ((value & 0x33333333) << 2); -// value = ((value >> 4) & 0x0F0F0F0F) | ((value & 0x0F0F0F0F) << 4); -// value = ((value >> 8) & 0x00FF00FF) | ((value & 0x00FF00FF) << 8); -// value = (value >> 16) | (value << 16); -// return value >>> 24; // Unsigned right shift to discard excess bits -// }