diff options
author | Florian Dold <florian@dold.me> | 2023-02-15 23:32:42 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-02-16 02:50:29 +0100 |
commit | 825d2c4352022e7397854b2bd9ba7d3589873c07 (patch) | |
tree | d23530bf8408367439e6b3820ea0c4269bfeb39a /packages/taler-wallet-cli | |
parent | cb2f4c21d85707abb0221cbf2a859a98836b2d44 (diff) |
make wallet-cli runnable under qtart
Diffstat (limited to 'packages/taler-wallet-cli')
-rwxr-xr-x | packages/taler-wallet-cli/build.mjs | 6 | ||||
-rw-r--r-- | packages/taler-wallet-cli/src/index.ts | 131 |
2 files changed, 67 insertions, 70 deletions
diff --git a/packages/taler-wallet-cli/build.mjs b/packages/taler-wallet-cli/build.mjs index 14b626815..b2ed2c937 100755 --- a/packages/taler-wallet-cli/build.mjs +++ b/packages/taler-wallet-cli/build.mjs @@ -53,7 +53,7 @@ function git_hash() { export const buildConfig = { entryPoints: ["src/index.ts"], - outfile: "dist/taler-wallet-cli.mjs", + outfile: "dist/taler-wallet-cli.qtart.mjs", bundle: true, minify: false, target: [ @@ -61,7 +61,11 @@ export const buildConfig = { ], format: 'esm', platform: 'neutral', + mainFields: ["module", "main"], + conditions: ["qtart"], sourcemap: true, + // quickjs standard library + external: ["std", "os"], define: { '__VERSION__': `"${_package.version}"`, '__GIT_HASH__': `"${GIT_HASH}"`, diff --git a/packages/taler-wallet-cli/src/index.ts b/packages/taler-wallet-cli/src/index.ts index 228395991..aed9a24c0 100644 --- a/packages/taler-wallet-cli/src/index.ts +++ b/packages/taler-wallet-cli/src/index.ts @@ -21,12 +21,11 @@ import { addPaytoQueryParams, AgeRestriction, classifyTalerUri, - clk, codecForList, codecForString, CoreApiResponse, - decodeCrock, encodeCrock, + getErrorDetailFromException, getRandomBytes, j2s, Logger, @@ -35,20 +34,26 @@ import { RecoveryMergeStrategy, setDangerousTimetravel, setGlobalLogLevelFromString, + summarizeTalerErrorDetail, TalerUriType, WalletNotification, } from "@gnu-taler/taler-util"; +import { clk } from "@gnu-taler/taler-util/clk"; +import { + getenv, + pathHomedir, + processExit, + setUnhandledRejectionHandler, +} from "@gnu-taler/taler-util/compat"; +import { createPlatformHttpLib } from "@gnu-taler/taler-util/http"; import { JsonMessage, runRpcServer } from "@gnu-taler/taler-util/twrpc"; import { + createNativeWalletHost, + createNativeWalletHost2, CryptoDispatcher, - getDefaultNodeWallet, - getDefaultNodeWallet2, - getErrorDetailFromException, nativeCrypto, - NodeHttpLib, - NodeThreadCryptoWorkerFactory, - summarizeTalerErrorDetail, - SynchronousCryptoWorkerFactoryNode, + //NodeThreadCryptoWorkerFactory, + //SynchronousCryptoWorkerFactoryPlain, TalerCryptoInterface, Wallet, WalletApiOperation, @@ -60,8 +65,6 @@ import { getClientFromRemoteWallet, makeNotificationWaiter, } from "@gnu-taler/taler-wallet-core/remote"; -import fs from "fs"; -import os from "os"; // This module also serves as the entry point for the crypto // thread worker, and thus must expose these two handlers. @@ -76,13 +79,13 @@ const EXIT_EXCEPTION = 4; const EXIT_API_ERROR = 5; const EXIT_RETRIES_EXCEEDED = 6; -process.on("unhandledRejection", (error: any) => { +setUnhandledRejectionHandler((error: any) => { logger.error("unhandledRejection", error.message); logger.error("stack", error.stack); - process.exit(2); + processExit(1); }); -const defaultWalletDbPath = os.homedir + "/" + ".talerwalletdb.json"; +const defaultWalletDbPath = pathHomedir() + "/" + ".talerwalletdb.json"; function assertUnreachable(x: never): never { throw new Error("Didn't expect to get here"); @@ -99,7 +102,7 @@ async function doPay( if (result.status === PreparePayResultType.InsufficientBalance) { console.log("contract", result.contractTerms); console.error("insufficient balance"); - process.exit(1); + processExit(1); return; } if (result.status === PreparePayResultType.AlreadyConfirmed) { @@ -108,8 +111,7 @@ async function doPay( } else { console.log("payment already in progress"); } - - process.exit(0); + processExit(0); return; } if (result.status === "payment-possible") { @@ -154,7 +156,7 @@ function applyVerbose(verbose: boolean): void { declare const __VERSION__: string; function printVersion(): void { console.log(__VERSION__); - process.exit(0); + processExit(0); } export const walletCli = clk @@ -203,7 +205,7 @@ export const walletCli = clk type WalletCliArgsType = clk.GetArgType<typeof walletCli>; function checkEnvFlag(name: string): boolean { - const val = process.env[name]; + const val = getenv(name); if (val == "1") { return true; } @@ -238,11 +240,10 @@ async function createLocalWallet( notificationHandler?: (n: WalletNotification) => void, ): Promise<Wallet> { const dbPath = walletCliArgs.wallet.walletDbFile ?? defaultWalletDbPath; - const myHttpLib = new NodeHttpLib(); - if (walletCliArgs.wallet.noThrottle) { - myHttpLib.setThrottling(false); - } - const wallet = await getDefaultNodeWallet({ + const myHttpLib = createPlatformHttpLib({ + enableThrottling: walletCliArgs.wallet.noThrottle ? false : true, + }); + const wallet = await createNativeWalletHost({ persistentStoragePath: dbPath !== ":memory:" ? dbPath : undefined, httpLib: myHttpLib, notifyHandler: (n) => { @@ -268,7 +269,7 @@ async function createLocalWallet( const ed = getErrorDetailFromException(e); console.error("Operation failed: " + summarizeTalerErrorDetail(ed)); console.error("Error details:", JSON.stringify(ed, undefined, 2)); - process.exit(1); + processExit(1); } finally { logger.trace("operation with wallet finished, stopping"); logger.trace("stopped wallet"); @@ -357,7 +358,7 @@ walletCli requestJson = JSON.parse(args.api.request); } catch (e) { console.error("Invalid JSON"); - process.exit(1); + processExit(1); } try { const resp = await wallet.makeCoreApiRequest( @@ -367,12 +368,12 @@ walletCli console.log(JSON.stringify(resp, undefined, 2)); if (resp.type === "error") { if (args.api.expectSuccess) { - process.exit(EXIT_API_ERROR); + processExit(EXIT_API_ERROR); } } } catch (e) { logger.error(`Got exception while handling API request ${e}`); - process.exit(EXIT_EXCEPTION); + processExit(EXIT_EXCEPTION); } }); logger.info("finished handling API request"); @@ -475,7 +476,7 @@ walletCli }); wallet.ws.stop(); if (resp.retriesExceeded && args.finishPendingOpt.failOnMaxRetries) { - process.exit(EXIT_RETRIES_EXCEEDED); + processExit(EXIT_RETRIES_EXCEEDED); } }); }); @@ -594,7 +595,7 @@ walletCli const selectedExchange = withdrawInfo.defaultExchangeBaseUrl; if (!selectedExchange) { console.error("no suggested exchange!"); - process.exit(1); + processExit(1); return; } const res = await wallet.client.call( @@ -1014,9 +1015,10 @@ advancedCli help: "Run the 'bench-internal' benchmark", }) .action(async (args) => { - const myHttpLib = new NodeHttpLib(); - myHttpLib.setThrottling(false); - const res = await getDefaultNodeWallet2({ + const myHttpLib = createPlatformHttpLib({ + enableThrottling: false, + }); + const res = await createNativeWalletHost2({ // No persistent DB storage. persistentStoragePath: undefined, httpLib: myHttpLib, @@ -1061,15 +1063,6 @@ advancedCli }); advancedCli - .subcommand("decode", "decode", { - help: "Decode base32-crockford.", - }) - .action((args) => { - const enc = fs.readFileSync(0, "utf8"); - console.log(decodeCrock(enc.trim())); - }); - -advancedCli .subcommand("genSegwit", "gen-segwit") .requiredArgument("paytoUri", clk.STRING) .requiredArgument("reservePub", clk.STRING) @@ -1229,7 +1222,7 @@ advancedCli ); } catch (e: any) { console.log("could not parse coin list:", e.message); - process.exit(1); + processExit(1); } for (const c of coinPubList) { await wallet.client.call(WalletApiOperation.SetCoinSuspended, { @@ -1254,7 +1247,7 @@ advancedCli ); } catch (e: any) { console.log("could not parse coin list:", e.message); - process.exit(1); + processExit(1); } for (const c of coinPubList) { await wallet.client.call(WalletApiOperation.SetCoinSuspended, { @@ -1420,33 +1413,33 @@ async function read(stream: NodeJS.ReadStream) { return Buffer.concat(chunks).toString("utf8"); } -testCli - .subcommand("cryptoworker", "cryptoworker") - .maybeOption("impl", ["--impl"], clk.STRING) - .action(async (args) => { - let cryptoApi: TalerCryptoInterface; - if (!args.cryptoworker.impl || args.cryptoworker.impl === "node") { - const workerFactory = new NodeThreadCryptoWorkerFactory(); - const cryptoDisp = new CryptoDispatcher(workerFactory); - cryptoApi = cryptoDisp.cryptoApi; - } else if (args.cryptoworker.impl === "sync") { - const workerFactory = new SynchronousCryptoWorkerFactoryNode(); - const cryptoDisp = new CryptoDispatcher(workerFactory); - cryptoApi = cryptoDisp.cryptoApi; - } else if (args.cryptoworker.impl === "none") { - cryptoApi = nativeCrypto; - } else { - throw Error(`invalid crypto worker type ${args.cryptoworker.impl}`); - } - - const input = "foo"; - console.log(`testing crypto worker by hashing string '${input}'`); - const res = await cryptoApi.hashString({ str: input }); - console.log(res); - }); +// testCli +// .subcommand("cryptoworker", "cryptoworker") +// .maybeOption("impl", ["--impl"], clk.STRING) +// .action(async (args) => { +// let cryptoApi: TalerCryptoInterface; +// if (!args.cryptoworker.impl || args.cryptoworker.impl === "node") { +// const workerFactory = new NodeThreadCryptoWorkerFactory(); +// const cryptoDisp = new CryptoDispatcher(workerFactory); +// cryptoApi = cryptoDisp.cryptoApi; +// } else if (args.cryptoworker.impl === "sync") { +// const workerFactory = new SynchronousCryptoWorkerFactoryPlain(); +// const cryptoDisp = new CryptoDispatcher(workerFactory); +// cryptoApi = cryptoDisp.cryptoApi; +// } else if (args.cryptoworker.impl === "none") { +// cryptoApi = nativeCrypto; +// } else { +// throw Error(`invalid crypto worker type ${args.cryptoworker.impl}`); +// } + +// const input = "foo"; +// console.log(`testing crypto worker by hashing string '${input}'`); +// const res = await cryptoApi.hashString({ str: input }); +// console.log(res); +// }); export function main() { - if (process.env["TALER_WALLET_DEBUG_DENOMSEL_ALLOW_LATE"]) { + if (getenv("TALER_WALLET_DEBUG_DENOMSEL_ALLOW_LATE")) { logger.warn("Allowing withdrawal of late denominations for debugging"); walletCoreDebugFlags.denomselAllowLate = true; } |