From a3bf38b323c17bbfb97ac8663feac2b5ead3b3f5 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Fri, 12 Apr 2024 11:16:38 +0800 Subject: [PATCH] [worker] add some more documentation for parsing the node-rsa key. --- packages/worker-api/src/parsers.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/worker-api/src/parsers.ts b/packages/worker-api/src/parsers.ts index 4ee2e741..0dab8818 100644 --- a/packages/worker-api/src/parsers.ts +++ b/packages/worker-api/src/parsers.ts @@ -23,6 +23,15 @@ 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 = new BN(keyJson.n); @@ -30,7 +39,11 @@ export function parseNodeRSA(data: any): NodeRSA { const key = new NodeRSA(); setKeyOpts(key); key.importKey({ + // 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;