Skip to content

Commit

Permalink
[worker] try to encrypt the trusted call upon sending
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Apr 6, 2024
1 parent 187075e commit e13865a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/worker-api/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { KeyringPair } from '@polkadot/keyring/types';
import WebSocketAsPromised from 'websocket-as-promised';
import {Keyring} from "@polkadot/keyring";
import type {u8} from "@polkadot/types-codec";
import type {Vec} from "@polkadot/types";

export interface IEncointerWorker extends WebSocketAsPromised {
rsCount: number;
rqStack: string[];
keyring: () => Keyring | undefined;
createType: (apiType: string, obj?: any) => any;
open: () => Promise<Event>;
encrypt: (data: Uint8Array) => Vec<u8>
}

export interface JsonRpcRequest {
Expand Down
4 changes: 3 additions & 1 deletion packages/worker-api/src/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ export const sendTrustedCall = async <T>(self: IEncointerWorker, call: TrustedCa

console.log(`TrustedCall: ${JSON.stringify(call)}`);

const cyphertext = self.encrypt(call.toU8a());

const r = self.createType(
'Request', { shard, cyphertext: call.toHex() }
'Request', { shard, cyphertext: cyphertext }
);

result = sendWorkerRequest(self, createJsonRpcRequest('author_submitExtrinsic', [r.toHex()], 1), parserType, options)
Expand Down
33 changes: 30 additions & 3 deletions packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {u32, u64, Vec} from '@polkadot/types';
import {TypeRegistry} from '@polkadot/types';
import type {RegistryTypes} from '@polkadot/types/types';
import {Keyring} from '@polkadot/keyring'
import {hexToU8a} from '@polkadot/util';
import {bufferToU8a, compactAddLength, hexToU8a, u8aToBuffer} from '@polkadot/util';

import WebSocketAsPromised from 'websocket-as-promised';

Expand All @@ -21,7 +21,7 @@ import type {
CommunityIdentifier,
MeetupIndexType,
ParticipantIndexType,
SchedulerState, ShardIdentifier,
SchedulerState, ShardIdentifier, TrustedCallSigned,
Vault
} from '@encointer/types';

Expand All @@ -30,6 +30,7 @@ import {parseBalance, parseNodeRSA} from './parsers.js';
import {callGetter, sendTrustedCall} from './sendRequest.js';
import {createTrustedCall} from "@encointer/worker-api/requests.js";
import {PubKeyPinPair, toAccount} from "@encointer/util/common";
import type {u8} from "@polkadot/types-codec";

const unwrapWorkerResponse = (self: IEncointerWorker, data: string) => {
/// Defaults to return `[]`, which is fine as `createType(api.registry, <type>, [])`
Expand Down Expand Up @@ -97,6 +98,8 @@ export class EncointerWorker extends WebSocketAsPromised implements IEncointerWo

#keyring?: Keyring;

#shieldingKey?: NodeRSA

rsCount: number;

rqStack: string[];
Expand All @@ -123,6 +126,13 @@ export class EncointerWorker extends WebSocketAsPromised implements IEncointerWo
}
}

public encrypt(data: Uint8Array): Vec<u8> {
const buffer = u8aToBuffer(data);
const cypherTextBuffer = this.shieldingKey().encrypt(buffer);
const cypherArray = bufferToU8a(cypherTextBuffer);
return this.createType('Vec<u8>', compactAddLength(cypherArray))
}

public createType(apiType: string, obj?: any): any {
return this.#registry.createType(apiType as never, obj)
}
Expand All @@ -135,6 +145,14 @@ export class EncointerWorker extends WebSocketAsPromised implements IEncointerWo
this.#keyring = keyring;
}

public shieldingKey(): NodeRSA | undefined {
return this.#shieldingKey;
}

public setShieldingKey(shieldingKey: NodeRSA): void {
this.#shieldingKey = shieldingKey;
}

public cidFromStr(cidStr: String): CommunityIdentifier {
return communityIdentifierFromString(this.#registry, cidStr);
}
Expand Down Expand Up @@ -220,6 +238,15 @@ export class EncointerWorker extends WebSocketAsPromised implements IEncointerWo
public async trustedBalanceTransfer(accountOrPubKey: KeyringPair | PubKeyPinPair, shard: ShardIdentifier, mrenclave: string, params: BalanceTransferArgs, options: CallOptions = {} as CallOptions): Promise<any> {
const nonce = await this.getNonce(accountOrPubKey, mrenclave, options);
const call = createTrustedCall(this, ['balance_transfer', 'BalanceTransferArgs'], accountOrPubKey, shard, mrenclave, nonce, params);
return sendTrustedCall<u32>(this, call, shard, 'u32', options);
return this.sendTrustedCall<u32>(call, shard, 'u32', options);
}

async sendTrustedCall<T>(call: TrustedCallSigned, shard: ShardIdentifier, parser: string, options: CallOptions = {} as CallOptions): Promise<T> {
if (this.shieldingKey() == undefined) {
const key = await this.getShieldingKey(options);
this.setShieldingKey(key);
}

return sendTrustedCall(this, call, shard, parser, options);
}
}

0 comments on commit e13865a

Please sign in to comment.