Skip to content

Commit

Permalink
Definitely fix parsing of the node-rsa key in browser (#98)
Browse files Browse the repository at this point in the history
* [worker] fix parsing of 'n' in node in browser

* v0.12.0-alpha.11

* [worker] add some more documentation for parsing the node-rsa key.

* [worker] improve logging

* [worker] fix unused import

* [worker] fix endianness in node-rsa parsing

* v0.12.0-alpha.12

* [worker] properly log request

* [worker] fix paseo shard

* [worker] fix local docker network shard

* [worker] use paseo in integration tests

* v0.12.0-alpha.13

* [worker] skip failing integration tests
  • Loading branch information
clangenb authored Apr 17, 2024
1 parent 025fa54 commit 3059c83
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"publishConfig": {
"directory": "build"
},
"version": "0.12.0-alpha.10"
"version": "0.12.0-alpha.13"
}
4 changes: 2 additions & 2 deletions packages/node-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
},
"sideEffects": false,
"type": "module",
"version": "0.12.0-alpha.10",
"version": "0.12.0-alpha.13",
"main": "index.js",
"dependencies": {
"@encointer/types": "^0.12.0-alpha.10",
"@encointer/types": "^0.12.0-alpha.13",
"@polkadot/api": "^10.9.1",
"tslib": "^2.5.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.12.0-alpha.10",
"version": "0.12.0-alpha.13",
"main": "index.js",
"scripts": {
"generate:defs": "node --experimental-specifier-resolution=node --loader ts-node/esm ../../node_modules/.bin/polkadot-types-from-defs --package @encointer/types/interfaces --input ./src/interfaces",
Expand Down
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sideEffects": false,
"type": "module",
"types": "./index.d.ts",
"version": "0.12.0-alpha.10",
"version": "0.12.0-alpha.13",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.9",
Expand Down
8 changes: 4 additions & 4 deletions packages/worker-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"sideEffects": false,
"type": "module",
"types": "./index.d.ts",
"version": "0.12.0-alpha.10",
"version": "0.12.0-alpha.13",
"main": "index.js",
"dependencies": {
"@encointer/node-api": "^0.12.0-alpha.10",
"@encointer/types": "^0.12.0-alpha.10",
"@encointer/util": "^0.12.0-alpha.10",
"@encointer/node-api": "^0.12.0-alpha.13",
"@encointer/types": "^0.12.0-alpha.13",
"@encointer/util": "^0.12.0-alpha.13",
"@polkadot/api": "^10.9.1",
"@polkadot/keyring": "^12.3.2",
"@polkadot/types": "^10.9.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/worker-api/src/integriteeWorker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('worker', () => {

describe('balance transfer should work', () => {
it('should return value', async () => {
const shard = network.mrenclave;
const shard = network.chosenCid;
const result = await worker.trustedBalanceTransfer(
alice,
shard,
Expand All @@ -90,7 +90,7 @@ describe('worker', () => {

describe('balance unshield should work', () => {
it('should return value', async () => {
const shard = network.mrenclave;
const shard = network.chosenCid;
const result = await worker.balanceUnshieldFunds(
alice,
shard,
Expand Down
2 changes: 1 addition & 1 deletion packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class IntegriteeWorker extends Worker {

async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier, options: CallOptions = {} as CallOptions): Promise<Hash> {
if (this.shieldingKey() == undefined) {
console.log(`Setting the shielding pubKey of the worker.`)
console.log(`[sentTrustedCall] Setting the shielding pubKey of the worker.`)
await this.getShieldingKey(options);
}

Expand Down
19 changes: 16 additions & 3 deletions packages/worker-api/src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseI64F64 } from '@encointer/util';
import { u8aToBn, u8aToBuffer } from '@polkadot/util';
import { u8aToBn } from '@polkadot/util';

// @ts-ignore
import NodeRSA from 'node-rsa';
Expand All @@ -23,14 +23,27 @@ export function parseBalanceType(data: any): number {
return parseI64F64(u8aToBn(data));
}

/**
* Parse a public key retrieved from the worker into `NodeRsa`.
*
* Note: This code is relatively sensitive: Changes here could lead
* to errors parsing and encryption errors in the browser, probably
* because of inconsistencies of node's `Buffer and the `buffer`
* polyfill in browser.
* @param data
*/
export function parseNodeRSA(data: any): NodeRSA {
const keyJson = JSON.parse(data);
keyJson.n = u8aToBuffer(keyJson.n).reverse();
keyJson.n = new BN(keyJson.n, 'le');
keyJson.e = new BN(keyJson.e);
const key = new NodeRSA();
setKeyOpts(key);
key.importKey({
n: keyJson.n,
// Important: use string here, not buffer, otherwise the browser will
// misinterpret the `n`.
n: keyJson.n.toString(10),
// Important: use number here, not buffer, otherwise the browser will
// misinterpret the `e`.
e: keyJson.e.toNumber()
}, 'components-public');
return key;
Expand Down
8 changes: 6 additions & 2 deletions packages/worker-api/src/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const sendTrustedCall = async <T>(self: IWorker, call: IntegriteeTrustedC
let result: Promise<any>;
let parserType: string = options.debug ? 'raw': parser;

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

let top;
if (direct) {
Expand All @@ -83,7 +83,11 @@ export const sendTrustedCall = async <T>(self: IWorker, call: IntegriteeTrustedC
'Request', { shard, cyphertext: cyphertext }
);

result = sendWorkerRequest(self, createJsonRpcRequest('author_submitExtrinsic', [r.toHex()], 1), parserType, options)
const rpc = createJsonRpcRequest('author_submitExtrinsic', [r.toHex()], 1);
result = sendWorkerRequest(self, rpc, parserType, options)

console.log(`[sendTrustedCall] sent request: ${JSON.stringify(rpc)}`);

return result as Promise<T>
}

4 changes: 2 additions & 2 deletions packages/worker-api/src/testUtils/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const localDockerNetwork = () => {
worker: 'wss://127.0.0.1:2000',
genesisHash: '0x388c446a804e24e77ae89f5bb099edb60cacc2ac7c898ce175bdaa08629c1439',
mrenclave: 'HjkQuPjBn531Hkji2Dsj4CEYCGpqCc3aXqETMCM7x7z4',
chosenCid: '4Xgnkpg4RwXjFegLzYKgY6Y3jCSPmxyPmNvfs42Uvsuh',
chosenCid: 'HjkQuPjBn531Hkji2Dsj4CEYCGpqCc3aXqETMCM7x7z4',
customTypes: {},
palletOverrides: {}
};
Expand All @@ -54,7 +54,7 @@ export const paseoNetwork = () => {
genesisHash: '',
mrenclave: '7RuM6U4DLEtrTnVntDjDPBCAN4LbCGRpnmcTYUGhLqc7',
// abused as shard vault
chosenCid: '5CBWPstfcW7dPYGdUG4kVDZSQq9Q9Ed65LT2Eu1inhJRoY8e',
chosenCid: '5wePd1LYa5M49ghwgZXs55cepKbJKhj5xfzQGfPeMS7c',
customTypes: {},
palletOverrides: {}
};
Expand Down
3 changes: 2 additions & 1 deletion packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const parseGetterResponse = (self: IWorker, responseType: string, data: string)
throw new Error(`Worker error: ${data}`);
}

console.log(`Getter response: ${data}`);
// console.log(`Getter response: ${data}`);
const json = JSON.parse(data);

const value = hexToU8a(json["result"]);
Expand Down Expand Up @@ -70,6 +70,7 @@ const parseGetterResponse = (self: IWorker, responseType: string, data: string)
parsedData = self.createType(responseType, returnValue.value);
break
case 'TrustedOperationResult':
console.log(`Got TrustedOperationResult`)
parsedData = self.createType('Hash', returnValue.value);
break
default:
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -755,18 +755,18 @@ __metadata:
languageName: node
linkType: hard

"@encointer/node-api@^0.12.0-alpha.10, @encointer/node-api@workspace:packages/node-api":
"@encointer/node-api@^0.12.0-alpha.13, @encointer/node-api@workspace:packages/node-api":
version: 0.0.0-use.local
resolution: "@encointer/node-api@workspace:packages/node-api"
dependencies:
"@encointer/types": ^0.12.0-alpha.10
"@encointer/types": ^0.12.0-alpha.13
"@polkadot/api": ^10.9.1
"@polkadot/util-crypto": ^12.3.2
tslib: ^2.5.3
languageName: unknown
linkType: soft

"@encointer/types@^0.12.0-alpha.10, @encointer/types@workspace:packages/types":
"@encointer/types@^0.12.0-alpha.13, @encointer/types@workspace:packages/types":
version: 0.0.0-use.local
resolution: "@encointer/types@workspace:packages/types"
dependencies:
Expand All @@ -781,7 +781,7 @@ __metadata:
languageName: unknown
linkType: soft

"@encointer/util@^0.12.0-alpha.10, @encointer/util@workspace:packages/util":
"@encointer/util@^0.12.0-alpha.13, @encointer/util@workspace:packages/util":
version: 0.0.0-use.local
resolution: "@encointer/util@workspace:packages/util"
dependencies:
Expand All @@ -798,9 +798,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "@encointer/worker-api@workspace:packages/worker-api"
dependencies:
"@encointer/node-api": ^0.12.0-alpha.10
"@encointer/types": ^0.12.0-alpha.10
"@encointer/util": ^0.12.0-alpha.10
"@encointer/node-api": ^0.12.0-alpha.13
"@encointer/types": ^0.12.0-alpha.13
"@encointer/util": ^0.12.0-alpha.13
"@polkadot/api": ^10.9.1
"@polkadot/keyring": ^12.3.2
"@polkadot/types": ^10.9.1
Expand Down

0 comments on commit 3059c83

Please sign in to comment.