# SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
# SPDX-License-Identifier: GPL-3.0-or-later
-# Save this file as `.env` and replace the following with real values
-LIBNEMO_RPC_URL="https://rpc.example.com"
-LIBNEMO_RPC_AUTHORIZATION="fedcba9876543210fedcba9876543210"
+## Save this file as `.env` and replace the examples with real values.
+
+## Specify a valid URL pointing to a live Nano node.
+##
+LIBNEMO_RPC_URL='https://rpc.example.com'
+
+## Pass a plain string to use the default "Authorization" HTTP header. Examples:
+##
+## 'Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZDEyMyE='
+## 'Bearer fedcba9876543210fedcba9876543210'
+##
+## Alternatively, pass a JSON string to use a custom header for authentication.
+## The expected JSON format is as follows:
+##
+## '{ "header": "custom-auth-header", "value": "fedcba9876543210fedcba9876543210" }'
+##
+## Replace the example values for `"header"` and `"value"` with your own.
+##
+LIBNEMO_RPC_AUTHORIZATION='Bearer fedcba9876543210fedcba9876543210'
//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: GPL-3.0-or-later
-'use strict'
-
-// Save this file as `env.mjs` and replace the following with real values
+// Save this file as `env.mjs` and replace the examples with real values
export const env = {
- LIBNEMO_RPC_URL: "https://rpc.example.com",
- LIBNEMO_RPC_AUTHORIZATION: "fedcba9876543210fedcba9876543210"
+
+ // Specify a valid URL pointing to a live Nano node.
+ //
+ LIBNEMO_RPC_URL: 'https://rpc.example.com',
+
+ // Pass a string to use the default "Authorization" HTTP header. Examples:
+ //
+ // 'Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZDEyMyE='
+ // 'Bearer fedcba9876543210fedcba9876543210'
+ //
+ // Alternatively, pass an object to use a custom header for authentication.
+ // The expected format is as follows:
+ //
+ // { header: "custom-auth-header", value: "fedcba9876543210fedcba9876543210" }
+ //
+ // Replace the example values for`header` and `value` with your own.
+ //
+ LIBNEMO_RPC_AUTHORIZATION: 'Bearer fedcba9876543210fedcba9876543210'
}
*/
export class Rpc {
#url: URL
- #auth?: string
+ #auth?: string | { header: string, value: string }
/**
* @param {(string|URL)} url Nano network node
- * @param {string} [auth] optional API key
+ * @param {string} [auth] "Authorization" token HTTP header for an API key
*/
- constructor (url: string | URL, auth?: string) {
+ constructor (url: string | URL, auth?: string)
+ /**
+ * @param {(string|URL)} url Nano network node
+ * @param {object} [auth] custom HTTP header for an API key
+ * @param {string} auth.header custom authorization HTTP header name
+ * @param {string} auth.value authorization token value
+ */
+ constructor (url: string | URL, auth?: { header: string, value: string })
+ constructor (url: string | URL, auth?: string | { header: string, value: string }) {
this.#url = new URL(url)
this.#url.protocol = 'https:'
this.#auth = auth
'Content-Type': 'application/json'
}
const auth = this.#auth ?? env?.LIBNEMO_RPC_AUTHORIZATION
- if (auth) {
- headers['Authorization'] = auth
+ if (typeof auth === 'string') {
+ const token = /^\{.*\}$/.test(auth.trim()) ? JSON.parse(auth) : auth
+ headers['Authorization'] = token
+ } else if (typeof auth?.header === 'string' && typeof auth.value === 'string') {
+ headers[auth.header] = auth.value
+ } else {
+ console.warn('Unrecognized RPC authorization format')
}
if (data !== undefined && typeof data !== 'object') {
throw new TypeError('Invalid RPC post data')