Skip to content

Commit

Permalink
[worker] add some more documentation for parsing the node-rsa key.
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Apr 12, 2024
1 parent 2ae51fb commit a3bf38b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/worker-api/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = new BN(keyJson.n);
keyJson.e = new BN(keyJson.e);
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;
Expand Down

0 comments on commit a3bf38b

Please sign in to comment.