From df5f8e3146666034eeffa5bab774ddfd93dafb4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= Date: Thu, 1 Jun 2023 18:00:51 +0200 Subject: [PATCH] Split APIs to have different include files for each environment (web, desktop, react-native) (#197) --- api/common.js | 34 +++++++++++++++++++++++++ api/desktop.js | 16 ++++++++++++ api/react-native.js | 16 ++++++++++++ api/web.js | 28 +++++++++++++++++++++ cli/cli.js | 2 +- cli/ws_server.js | 2 +- docs/api-pcsc.md | 4 +-- docs/api-react-native.md | 2 +- docs/api-utils.md | 11 +++++--- docs/api-web.md | 2 +- docs/desktop-pcsc.md | 2 +- docs/halo-bridge.md | 2 +- docs/mobile-react-native.md | 2 +- docs/web-reactjs.md | 2 +- halo/tests.js | 2 +- index.js | 50 +++---------------------------------- web/weblib.js | 46 ++++++---------------------------- 17 files changed, 123 insertions(+), 100 deletions(-) create mode 100644 api/common.js create mode 100644 api/desktop.js create mode 100644 api/react-native.js create mode 100644 api/web.js diff --git a/api/common.js b/api/common.js new file mode 100644 index 0000000..356e50a --- /dev/null +++ b/api/common.js @@ -0,0 +1,34 @@ +/** + * LibHaLo - Programmatically interact with HaLo tags from the web browser, mobile application or the desktop. + * Copyright by Arx Research, Inc., a Delaware corporation + * License: MIT + */ + +const { + HaloTagError, + HaloLogicError, + NFCPermissionRequestDenied, + NFCMethodNotSupported, + NFCAbortedError, + NFCOperationError +} = require("../halo/exceptions"); +const {parsePublicKeys, convertSignature, recoverPublicKey} = require("../halo/utils"); + +/** + * The LibHaLo stable API. Please don't depend on the functions imported from anywhere else + * except the lib's index.js. The library's structure is subject to change in the next versions. + */ +module.exports = { + // exported utils + haloParsePublicKeys: parsePublicKeys, + haloConvertSignature: convertSignature, + haloRecoverPublicKey: recoverPublicKey, + + // exceptions + HaloTagError, + HaloLogicError, + NFCPermissionRequestDenied, + NFCMethodNotSupported, + NFCAbortedError, + NFCOperationError +}; diff --git a/api/desktop.js b/api/desktop.js new file mode 100644 index 0000000..504e595 --- /dev/null +++ b/api/desktop.js @@ -0,0 +1,16 @@ +/** + * LibHaLo - Programmatically interact with HaLo tags from the web browser, mobile application or the desktop. + * Copyright by Arx Research, Inc., a Delaware corporation + * License: MIT + */ + +const {execHaloCmdPCSC} = require("../drivers/pcsc"); + +/** + * The LibHaLo stable API. Please don't depend on the functions imported from anywhere else + * except the lib's index.js. The library's structure is subject to change in the next versions. + */ +module.exports = { + // for desktop usage + execHaloCmdPCSC +}; diff --git a/api/react-native.js b/api/react-native.js new file mode 100644 index 0000000..a3bee3e --- /dev/null +++ b/api/react-native.js @@ -0,0 +1,16 @@ +/** + * LibHaLo - Programmatically interact with HaLo tags from the web browser, mobile application or the desktop. + * Copyright by Arx Research, Inc., a Delaware corporation + * License: MIT + */ + +const {execHaloCmdRN} = require("../drivers/nfc_manager"); + +/** + * The LibHaLo stable API. Please don't depend on the functions imported from anywhere else + * except the lib's index.js. The library's structure is subject to change in the next versions. + */ +module.exports = { + // for usage with react-native-nfc-manager + execHaloCmdRN +}; diff --git a/api/web.js b/api/web.js new file mode 100644 index 0000000..05693df --- /dev/null +++ b/api/web.js @@ -0,0 +1,28 @@ +/** + * LibHaLo - Programmatically interact with HaLo tags from the web browser, mobile application or the desktop. + * Copyright by Arx Research, Inc., a Delaware corporation + * License: MIT + */ + +const { + execHaloCmdWeb, detectMethod +} = require("../drivers/common"); +const {HaloGateway} = require("../halo/gateway/requestor"); +const {haloFindBridge} = require("../web/web_utils"); +const {haloGateExecutorCreateWs, haloGateExecutorUserConfirm} = require("../halo/gateway/executor"); + +/** + * The LibHaLo stable API. Please don't depend on the functions imported from anywhere else + * except the lib's index.js. The library's structure is subject to change in the next versions. + */ +module.exports = { + // for web usage + execHaloCmdWeb, + haloFindBridge, + haloGetDefaultMethod: detectMethod, + + // for web usage with gateway + HaloGateway, + haloGateExecutorCreateWs, + haloGateExecutorUserConfirm, +}; diff --git a/cli/cli.js b/cli/cli.js index 36c2692..26081f8 100644 --- a/cli/cli.js +++ b/cli/cli.js @@ -8,7 +8,6 @@ const Buffer = require('buffer/').Buffer; const {NFC} = require('nfc-pcsc'); const open = require('open'); -const {execHaloCmdPCSC} = require('../index.js'); const {__runTestSuite} = require("../halo/tests"); const util = require("util"); const { @@ -19,6 +18,7 @@ const { wsEventCardIncompatible, wsEventReaderDisconnected } = require("./ws_server"); +const {execHaloCmdPCSC} = require("../api/desktop"); const nfc = new NFC(); let stopPCSCTimeout = null; diff --git a/cli/ws_server.js b/cli/ws_server.js index 66ee5a1..38d0621 100644 --- a/cli/ws_server.js +++ b/cli/ws_server.js @@ -2,7 +2,6 @@ const express = require('express'); const nunjucks = require("nunjucks"); const {WebSocketServer} = require('ws'); const crypto = require('crypto').webcrypto; -const {execHaloCmdPCSC} = require('../index.js'); const {dirname, randomBuffer} = require("./util"); const jwt = require('jsonwebtoken'); const https = require("https"); @@ -10,6 +9,7 @@ const fs = require("fs"); const path = require("path"); const os = require("os"); const util = require("util"); +const {execHaloCmdPCSC} = require("../api/desktop"); let wss = null; diff --git a/docs/api-pcsc.md b/docs/api-pcsc.md index 5fede40..54e1f20 100644 --- a/docs/api-pcsc.md +++ b/docs/api-pcsc.md @@ -2,9 +2,9 @@ ## Importing the method ```javascript -const {execHaloCmdPCSC} = require('@arx-research/libhalo'); +const {execHaloCmdPCSC} = require('@arx-research/libhalo/api/desktop'); // or -import {execHaloCmdPCSC} from '@arx-research/libhalo'; +import {execHaloCmdPCSC} from '@arx-research/libhalo/api/desktop.js'; ``` ## Call specification diff --git a/docs/api-react-native.md b/docs/api-react-native.md index 031a015..63713b5 100644 --- a/docs/api-react-native.md +++ b/docs/api-react-native.md @@ -2,7 +2,7 @@ ## Importing the method ```javascript -import {execHaloCmdRN} from '@arx-research/libhalo'; +import {execHaloCmdRN} from '@arx-research/libhalo/api/react-native.js'; ``` ## Call specification diff --git a/docs/api-utils.md b/docs/api-utils.md index d7bf484..da8ca86 100644 --- a/docs/api-utils.md +++ b/docs/api-utils.md @@ -13,7 +13,7 @@ haloParsePublicKeys(queryParamStatic); **Example function call:** ```javascript -import {haloParsePublicKeys} from '@arx-research/libhalo'; +import {haloParsePublicKeys} from '@arx-research/libhalo/api/common.js'; let pkeys = haloParsePublicKeys( "4104453D40D28E0BAA4D98AC86549DDC5FFFF5F674481A47141137F8A82CB666937A67AECE33B96E" + @@ -45,7 +45,8 @@ haloConvertSignature(digest, derSignature, publicKey); **Example usage:** ```javascript -import {execHaloCmdWeb, haloConvertSignature} from '@arx-research/libhalo'; +import {execHaloCmdWeb} from '@arx-research/libhalo/api/web.js'; +import {haloConvertSignature} from '@arx-research/libhalo/api/common.js'; const KEY_NO = 1; @@ -101,7 +102,8 @@ haloRecoverPublicKey(digest, derSignature); **Example usage:** ```javascript -import {execHaloCmdWeb, haloRecoverPublicKey} from '@arx-research/libhalo'; +import {execHaloCmdWeb} from '@arx-research/libhalo/api/web.js'; +import {haloRecoverPublicKey} from '@arx-research/libhalo/api/common.js'; const KEY_NO = 1; @@ -144,7 +146,8 @@ haloGetDefaultMethod(); **Example usage:** ``` -import {execHaloCmdWeb, haloGetDefaultMethod} from '@arx-research/libhalo'; +import {execHaloCmdWeb} from '@arx-research/libhalo/api/web.js'; +import {haloGetDefaultMethod} from '@arx-research/libhalo/api/common.js'; let signRes = await execHaloCmdWeb({ "name": "sign", diff --git a/docs/api-web.md b/docs/api-web.md index ecb9c20..e6f095e 100644 --- a/docs/api-web.md +++ b/docs/api-web.md @@ -2,7 +2,7 @@ ## Importing the method ``` -import {execHaloCmdWeb} from '@arx-research/libhalo'; +import {execHaloCmdWeb} from '@arx-research/libhalo/api/web.js'; ``` **Note:** This step is only necessary for module-based applications. You don't need to add this line if you diff --git a/docs/desktop-pcsc.md b/docs/desktop-pcsc.md index 18f452e..f07de92 100644 --- a/docs/desktop-pcsc.md +++ b/docs/desktop-pcsc.md @@ -11,7 +11,7 @@ Import the libraries: ```javascript const {NFC} = require('nfc-pcsc'); -const {execHaloCmdPCSC} = require('@arx-research/libhalo'); +const {execHaloCmdPCSC} = require('@arx-research/libhalo/api/desktop'); ``` Implement basic code: diff --git a/docs/halo-bridge.md b/docs/halo-bridge.md index 542ff29..78a828b 100644 --- a/docs/halo-bridge.md +++ b/docs/halo-bridge.md @@ -32,7 +32,7 @@ Example website: [https://bulk.vrfy.ch/](https://bulk.vrfy.ch/). Use `haloFindBridge()` library function in order to obtain the HaLo Bridge address: ```javascript -import {haloFindBridge} from '@arx-research/libhalo'; +import {haloFindBridge} from '@arx-research/libhalo/api/web.js'; let wsAddress = await haloFindBridge(); ``` diff --git a/docs/mobile-react-native.md b/docs/mobile-react-native.md index 8e8a4ad..7edcfb4 100644 --- a/docs/mobile-react-native.md +++ b/docs/mobile-react-native.md @@ -64,7 +64,7 @@ Import necessary functions: ```javascript import NfcManager, {NfcTech} from 'react-native-nfc-manager'; -import {execHaloCmdRN} from '@arx-research/libhalo'; +import {execHaloCmdRN} from '@arx-research/libhalo/api/react-native.js'; ``` Add basic code to process the NFC tags: diff --git a/docs/web-reactjs.md b/docs/web-reactjs.md index 8f6cb33..1331a24 100644 --- a/docs/web-reactjs.md +++ b/docs/web-reactjs.md @@ -20,7 +20,7 @@ yarn add @arx-research/libhalo Import the library method: ```javascript -import {execHaloCmdWeb} from '@arx-research/libhalo'; +import {execHaloCmdWeb} from '@arx-research/libhalo/api/web.js'; ``` Add a state for displaying information to the user: diff --git a/halo/tests.js b/halo/tests.js index 4c0cf36..5d84fff 100644 --- a/halo/tests.js +++ b/halo/tests.js @@ -4,7 +4,7 @@ * License: MIT */ -const {HaloLogicError, HaloTagError} = require("../index"); +const {HaloLogicError, HaloTagError} = require("../api/common"); const EC = require('elliptic').ec; const ec = new EC('secp256k1'); diff --git a/index.js b/index.js index 9d6c442..7c6abc2 100644 --- a/index.js +++ b/index.js @@ -4,52 +4,8 @@ * License: MIT */ -const {execHaloCmdRN} = require("./drivers/nfc_manager"); -const {execHaloCmdPCSC} = require("./drivers/pcsc"); -const { - execHaloCmdWeb, detectMethod -} = require("./drivers/common"); -const { - HaloTagError, - HaloLogicError, - NFCPermissionRequestDenied, - NFCMethodNotSupported, - NFCAbortedError, - NFCOperationError -} = require("./halo/exceptions"); -const {parsePublicKeys, convertSignature, recoverPublicKey} = require("./halo/utils"); -const {HaloGateway} = require("./halo/gateway/requestor"); -const {haloFindBridge} = require("./web/web_utils"); - /** - * The LibHaLo stable API. Please don't depend on the functions imported from anywhere else - * except the lib's index.js. The library's structure is subject to change in the next versions. + * The LibHaLo API was moved to api/ subdirectory. + * Please include the appropriate file for your platform. */ -module.exports = { - // for desktop usage - execHaloCmdPCSC, - - // for web usage - execHaloCmdWeb, - haloFindBridge, - haloGetDefaultMethod: detectMethod, - - // for web usage with gateway - HaloGateway, - - // for usage with react-native-nfc-manager - execHaloCmdRN, - - // exported utils - haloParsePublicKeys: parsePublicKeys, - haloConvertSignature: convertSignature, - haloRecoverPublicKey: recoverPublicKey, - - // exceptions - HaloTagError, - HaloLogicError, - NFCPermissionRequestDenied, - NFCMethodNotSupported, - NFCAbortedError, - NFCOperationError -}; +module.exports = {}; diff --git a/web/weblib.js b/web/weblib.js index c45ee3c..9275cbd 100644 --- a/web/weblib.js +++ b/web/weblib.js @@ -5,53 +5,23 @@ */ const { - execHaloCmdWeb, detectMethod -} = require("../drivers/common"); -const { - HaloTagError, - HaloLogicError, - NFCPermissionRequestDenied, - NFCMethodNotSupported, - NFCAbortedError, - NFCOperationError -} = require("../halo/exceptions"); -const { - arr2hex, hex2arr, parsePublicKeys, convertSignature, recoverPublicKey + arr2hex, hex2arr } = require("../halo/utils"); const {__runTestSuite} = require("../halo/tests"); -const {HaloGateway} = require("../halo/gateway/requestor"); -const {haloGateExecutorCreateWs, haloGateExecutorUserConfirm} = require("../halo/gateway/executor"); -const {haloFindBridge, haloCreateWs} = require("./web_utils"); +const {haloCreateWs} = require("./web_utils"); module.exports = { - // utilities + // libhalo web APIs + ...require('../api/common.js'), + ...require('../api/web.js'), + + // extra utilities arr2hex, hex2arr, - haloParsePublicKeys: parsePublicKeys, - haloConvertSignature: convertSignature, - haloRecoverPublicKey: recoverPublicKey, - // for web usage - execHaloCmdWeb, - haloFindBridge, - haloGetDefaultMethod: detectMethod, - - // for bridge demo + // extra util for bridge demo haloCreateWs, - // for web usage with gateway - HaloGateway, - haloGateExecutorCreateWs, - haloGateExecutorUserConfirm, - - // exceptions - HaloTagError, - HaloLogicError, - NFCPermissionRequestDenied, - NFCMethodNotSupported, - NFCAbortedError, - NFCOperationError, - // internal, do not use __runTestSuite };