Skip to content

Commit

Permalink
add data conversion tool for communication layer
Browse files Browse the repository at this point in the history
  • Loading branch information
rus7hex committed Jun 12, 2024
1 parent 9907e70 commit 5fcdbcc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/utility/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secux/utility",
"version": "3.0.17",
"version": "3.0.18",
"description": "SecuX Hardware Wallet internal tools for SDK",
"keywords": [
"secux",
Expand Down
30 changes: 30 additions & 0 deletions packages/utility/src/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ export function to_L1_APDU(data: communicationData): communicationData {
return toCommunicationData(apdu_L1);
}

export function communicationWrapper(instance: object): any {
const valueHandler = function (value: any) {
if (Buffer.isBuffer(value)) return value.toString("base64");
if (typeof value === "object") return JSON.stringify(value);

return value;
};

const classHandler = {
get(target: object, prop: string | symbol) {
const value = target[prop];
if (typeof value === "function") {
return function (...args: any[]) {
const execResult = value.apply(target, args);

if (execResult instanceof Promise) {
return execResult.then(a => valueHandler(a));
}

return valueHandler(execResult);
}
}

return valueHandler(value);
},
};

return new Proxy(instance, classHandler);
}

export class TransportStatusError extends Error {
#name: string = "TransportStatusError";
#message: string;
Expand Down

0 comments on commit 5fcdbcc

Please sign in to comment.