Skip to content

Commit

Permalink
[worker] cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed May 2, 2024
1 parent c495bd8 commit 39262ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 40 deletions.
5 changes: 1 addition & 4 deletions packages/worker-api/src/webCryptoRSA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export async function parseWebCryptoRSA(data: any): Promise<CryptoKey> {

console.log(`PublicKey: ${JSON.stringify(publicKey)}`);

const exported = await cryptoProvider.subtle.exportKey("jwk", publicKey);
console.log(`PublicKey: ${JSON.stringify(exported)}`);

return publicKey;
}

Expand All @@ -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;
}
Expand Down
41 changes: 5 additions & 36 deletions packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -117,18 +115,16 @@ export class Worker extends WebSocketAsPromised implements IWorker {
}

public async encrypt(data: Uint8Array): Promise<Vec<u8>> {
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<u8>', compactAddLength(beArray))
}
Expand Down Expand Up @@ -167,30 +163,3 @@ export class Worker extends WebSocketAsPromised implements IWorker {
return await callGetter<Vault>(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
// }

0 comments on commit 39262ad

Please sign in to comment.