@paybox-sh/sdk 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  PayboxClient,
4
+ PayboxError,
4
5
  appBaseFrom,
5
6
  configDir,
6
7
  configFile,
@@ -13,11 +14,10 @@ import {
13
14
  pendingFundingToolResponse,
14
15
  refreshTokens,
15
16
  saveConfig
16
- } from "./chunk-DWDC6W5P.js";
17
+ } from "./chunk-ACNRTJGL.js";
17
18
 
18
19
  // src/cli.ts
19
- import { spawnSync } from "node:child_process";
20
- import { readFileSync, rmSync } from "node:fs";
20
+ import { readFileSync as readFileSync2, rmSync as rmSync2 } from "node:fs";
21
21
  import { createRequire } from "node:module";
22
22
  import { Command } from "commander";
23
23
 
@@ -344,284 +344,20 @@ var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
344
344
  var sha2562 = sha256;
345
345
 
346
346
  // node_modules/.pnpm/@paybox+mcp-app@file+..+mcp-app_@modelcontextprotocol+sdk@1.29.0_zod@4.4.3__bn.js@5.2.3_ac1e09a20c76a1143611be5dd3d4f9db/node_modules/@paybox/mcp-app/src/signing/agent-code.ts
347
- var ALPHABET = [
348
- "\u{1F436}",
349
- "\u{1F431}",
350
- "\u{1F42D}",
351
- "\u{1F430}",
352
- "\u{1F98A}",
353
- "\u{1F43B}",
354
- "\u{1F43C}",
355
- "\u{1F981}",
356
- "\u{1F42E}",
357
- "\u{1F437}",
358
- "\u{1F438}",
359
- "\u{1F435}",
360
- "\u{1F414}",
361
- "\u{1F427}",
362
- "\u{1F426}",
363
- "\u{1F424}",
364
- "\u{1F986}",
365
- "\u{1F985}",
366
- "\u{1F989}",
367
- "\u{1F987}",
368
- "\u{1F417}",
369
- "\u{1F434}",
370
- "\u{1F984}",
371
- "\u{1F41D}",
372
- "\u{1F41B}",
373
- "\u{1F98B}",
374
- "\u{1F40C}",
375
- "\u{1F41E}",
376
- "\u{1F41C}",
377
- "\u{1F422}",
378
- "\u{1F40D}",
379
- "\u{1F98E}",
380
- "\u{1F419}",
381
- "\u{1F990}",
382
- "\u{1F980}",
383
- "\u{1F421}",
384
- "\u{1F420}",
385
- "\u{1F42C}",
386
- "\u{1F433}",
387
- "\u{1F988}",
388
- "\u{1F406}",
389
- "\u{1F993}",
390
- "\u{1F98D}",
391
- "\u{1F418}",
392
- "\u{1F98F}",
393
- "\u{1F42A}",
394
- "\u{1F992}",
395
- "\u{1F411}",
396
- "\u{1F98C}",
397
- "\u{1F983}",
398
- "\u{1F99A}",
399
- "\u{1F335}",
400
- "\u{1F384}",
401
- "\u{1F333}",
402
- "\u{1F334}",
403
- "\u{1F331}",
404
- "\u{1F340}",
405
- "\u{1F38D}",
406
- "\u{1F341}",
407
- "\u{1F342}",
408
- "\u{1F343}",
409
- "\u{1F33B}",
410
- "\u{1F339}",
411
- "\u{1F337}",
412
- "\u{1F338}",
413
- "\u{1F33C}",
414
- "\u{1F33E}",
415
- "\u{1F490}",
416
- "\u{1F344}",
417
- "\u{1F330}",
418
- "\u{1F30D}",
419
- "\u{1F319}",
420
- "\u2B50",
421
- "\u{1F308}",
422
- "\u{1F525}",
423
- "\u{1F4A7}",
424
- "\u{1F30A}",
425
- "\u{1F32A}",
426
- "\u{1F329}",
427
- "\u{1F347}",
428
- "\u{1F349}",
429
- "\u{1F34A}",
430
- "\u{1F34B}",
431
- "\u{1F34C}",
432
- "\u{1F34D}",
433
- "\u{1F34E}",
434
- "\u{1F350}",
435
- "\u{1F351}",
436
- "\u{1F352}",
437
- "\u{1F353}",
438
- "\u{1F95D}",
439
- "\u{1F951}",
440
- "\u{1F346}",
441
- "\u{1F955}",
442
- "\u{1F33D}",
443
- "\u{1F966}",
444
- "\u{1F952}",
445
- "\u{1F95C}",
446
- "\u{1F35E}",
447
- "\u{1F950}",
448
- "\u{1F9C0}",
449
- "\u{1F373}",
450
- "\u{1F95E}",
451
- "\u{1F354}",
452
- "\u{1F35F}",
453
- "\u{1F355}",
454
- "\u{1F32D}",
455
- "\u{1F32E}",
456
- "\u{1F32F}",
457
- "\u{1F35C}",
458
- "\u{1F360}",
459
- "\u{1F364}",
460
- "\u{1F365}",
461
- "\u{1F366}",
462
- "\u{1F368}",
463
- "\u{1F369}",
464
- "\u{1F36A}",
465
- "\u{1F382}",
466
- "\u{1F370}",
467
- "\u{1F36B}",
468
- "\u{1F36C}",
469
- "\u{1F36D}",
470
- "\u{1F36E}",
471
- "\u{1F36F}",
472
- "\u{1F37C}",
473
- "\u{1F95B}",
474
- "\u2615",
475
- "\u{1F375}",
476
- "\u{1F376}",
477
- "\u{1F37E}",
478
- "\u{1F377}",
479
- "\u{1F378}",
480
- "\u{1F379}",
481
- "\u{1F37A}",
482
- "\u{1F943}",
483
- "\u26BD",
484
- "\u{1F3C0}",
485
- "\u{1F3C8}",
486
- "\u26BE",
487
- "\u{1F3BE}",
488
- "\u{1F3B1}",
489
- "\u{1F3D3}",
490
- "\u{1F3F8}",
491
- "\u{1F945}",
492
- "\u{1F3D2}",
493
- "\u{1F3F9}",
494
- "\u{1F3A3}",
495
- "\u{1F94A}",
496
- "\u{1F3BF}",
497
- "\u{1F6F7}",
498
- "\u{1F3AF}",
499
- "\u{1F3AE}",
500
- "\u{1F3B2}",
501
- "\u{1F3B8}",
502
- "\u{1F3BA}",
503
- "\u{1F941}",
504
- "\u{1F3B9}",
505
- "\u{1F3A4}",
506
- "\u{1F3A7}",
507
- "\u{1F4FB}",
508
- "\u{1F3AC}",
509
- "\u{1F3A8}",
510
- "\u{1F3AA}",
511
- "\u{1F680}",
512
- "\u{1F6F8}",
513
- "\u{1F681}",
514
- "\u{1F682}",
515
- "\u{1F684}",
516
- "\u{1F68C}",
517
- "\u{1F691}",
518
- "\u{1F693}",
519
- "\u{1F695}",
520
- "\u{1F697}",
521
- "\u{1F69A}",
522
- "\u{1F69C}",
523
- "\u{1F6B2}",
524
- "\u26F5",
525
- "\u{1F6F6}",
526
- "\u{1F6A4}",
527
- "\u{1F5FF}",
528
- "\u{1F5FD}",
529
- "\u{1F3A1}",
530
- "\u{1F3A2}",
531
- "\u{1F3A0}",
532
- "\u26F2",
533
- "\u{1F3F0}",
534
- "\u{1F3DF}",
535
- "\u{1F3AD}",
536
- "\u{1F381}",
537
- "\u{1F9ED}",
538
- "\u231A",
539
- "\u{1F4F1}",
540
- "\u{1F4BB}",
541
- "\u{1F4F7}",
542
- "\u{1F4FA}",
543
- "\u{1F4DE}",
544
- "\u{1F4E0}",
545
- "\u{1F50B}",
546
- "\u{1F50C}",
547
- "\u{1F4A1}",
548
- "\u{1F526}",
549
- "\u{1F56F}",
550
- "\u{1F5D1}",
551
- "\u{1F4B8}",
552
- "\u{1F4B0}",
553
- "\u{1F4B3}",
554
- "\u{1F48E}",
555
- "\u{1F527}",
556
- "\u{1F528}",
557
- "\u{1F529}",
558
- "\u{1F52B}",
559
- "\u{1F4A3}",
560
- "\u{1F52A}",
561
- "\u{1F52E}",
562
- "\u{1F4FF}",
563
- "\u{1F52C}",
564
- "\u{1F4D4}",
565
- "\u{1F4DA}",
566
- "\u{1F31E}",
567
- "\u26C4",
568
- "\u2614",
569
- "\u{1F30B}",
570
- "\u{1F94C}",
571
- "\u{1F3E0}",
572
- "\u{1F3E5}",
573
- "\u{1F3ED}",
574
- "\u{1F511}",
575
- "\u{1F512}",
576
- "\u23F0",
577
- "\u231B",
578
- "\u{1F4C5}",
579
- "\u{1F4CC}",
580
- "\u{1F4CE}",
581
- "\u{1F4CF}",
582
- "\u{1F388}",
583
- "\u{1F383}",
584
- "\u{1F451}",
585
- "\u{1F453}",
586
- "\u{1F3A9}",
587
- "\u{1F45F}",
588
- "\u{1F462}",
589
- "\u{1F45C}",
590
- "\u{1F392}",
591
- "\u{1F9F2}",
592
- "\u{1F9EA}",
593
- "\u{1F9EC}",
594
- "\u{1F9F8}",
595
- "\u{1F9E6}",
596
- "\u{1F9E2}",
597
- "\u{1F3C6}",
598
- "\u{1F6F4}",
599
- "\u{1FA90}",
600
- "\u{1F3B3}",
601
- "\u{1F6A6}",
602
- "\u{1F9EF}",
603
- "\u{1F9F9}"
604
- ];
605
347
  function toHex(bytes) {
606
348
  return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
607
349
  }
608
- var CODE_LENGTH = 8;
609
- if (ALPHABET.length !== 256) {
610
- throw new Error(`agent-code alphabet must be exactly 256 entries, got ${ALPHABET.length}`);
611
- }
612
- function sha2563(bytes) {
613
- return sha2562(bytes);
614
- }
350
+ var CODE_MIN = 10;
351
+ var CODE_SPAN = 90;
615
352
  function codeFromDigest(digest) {
616
- const code = [];
617
- for (let i = 0; i < CODE_LENGTH; i += 1) code.push(ALPHABET[digest[i]]);
618
- return code;
353
+ const value = new DataView(digest.buffer, digest.byteOffset, 4).getUint32(0);
354
+ return String(CODE_MIN + value % CODE_SPAN);
619
355
  }
620
356
  async function deriveAgentCode(publicKey) {
621
357
  if (publicKey.length !== 32) {
622
358
  throw new Error(`public key must be 32 bytes, got ${publicKey.length}`);
623
359
  }
624
- return codeFromDigest(sha2563(publicKey));
360
+ return codeFromDigest(sha2562(publicKey));
625
361
  }
626
362
 
627
363
  // src/pairing.ts
@@ -681,15 +417,153 @@ async function pairSigningKey(opts) {
681
417
  if (status === "cancelled") throw new Error("that request was declined in PayBox");
682
418
  if (status === "expired") throw new Error("that request expired \u2014 run `paybox connect` again");
683
419
  }
684
- throw new Error("timed out waiting for you to pick the code in PayBox");
420
+ throw new Error("timed out waiting for you to pick the number in PayBox");
421
+ }
422
+
423
+ // src/self-update.ts
424
+ import { spawnSync } from "node:child_process";
425
+ import { closeSync, mkdirSync, openSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
426
+ import { constants } from "node:os";
427
+ import { isAbsolute, join, relative } from "node:path";
428
+ var PKG = "@paybox-sh/sdk";
429
+ var DAY_MS = 24 * 60 * 60 * 1e3;
430
+ var LOCK_STALE_MS = 10 * 60 * 1e3;
431
+ var NPM_TIMEOUT_MS = 2 * 60 * 1e3;
432
+ var SEMVER = /^\d+\.\d+\.\d+$/;
433
+ var NO_AUTO_UPDATE = /* @__PURE__ */ new Set(["version", "update", "uninstall"]);
434
+ function isNewer(a, b) {
435
+ const pa = a.split(".").map(Number);
436
+ const pb = b.split(".").map(Number);
437
+ for (let i = 0; i < 3; i++) {
438
+ const x = pa[i] ?? 0;
439
+ const y = pb[i] ?? 0;
440
+ if (x !== y) return x > y;
441
+ }
442
+ return false;
443
+ }
444
+ function registry() {
445
+ return (process.env.PAYBOX_UPDATE_REGISTRY || "https://registry.npmjs.org").replace(/\/+$/, "");
446
+ }
447
+ async function latestVersion() {
448
+ try {
449
+ const res = await fetch(`${registry()}/${PKG}/latest`, { signal: AbortSignal.timeout(3e3) });
450
+ if (!res.ok) return void 0;
451
+ const { version } = await res.json();
452
+ return typeof version === "string" && SEMVER.test(version) ? version : void 0;
453
+ } catch {
454
+ return void 0;
455
+ }
456
+ }
457
+ function installArgs(version) {
458
+ const pinned = `${registry()}/`;
459
+ return ["install", "-g", `${PKG}@${version}`, "--registry", pinned, `--@paybox-sh:registry=${pinned}`];
460
+ }
461
+ function npm(args, stdio) {
462
+ const background = stdio === "pipe";
463
+ const env = Object.fromEntries(
464
+ Object.entries(process.env).filter(([key]) => !(background && /^npm_config_.*registry$/i.test(key)))
465
+ );
466
+ const options = {
467
+ stdio,
468
+ encoding: "utf8",
469
+ timeout: background ? NPM_TIMEOUT_MS : void 0,
470
+ cwd: background ? configDir() : void 0
471
+ };
472
+ if (process.platform !== "win32") return spawnSync("npm", args, { ...options, env });
473
+ return spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", "npm", ...args], {
474
+ ...options,
475
+ env: { ...env, NoDefaultCurrentDirectoryInExePath: "1" }
476
+ });
477
+ }
478
+ function available(latest, current) {
479
+ return `PayBox CLI v${latest} is available (this is v${current}). Run \`paybox update\`.`;
480
+ }
481
+ function globalCli() {
482
+ const root = npm(["root", "-g"], "pipe");
483
+ if (root.status !== 0 || !process.argv[1]) return void 0;
484
+ const pkgDir = join(root.stdout.trim(), PKG);
485
+ try {
486
+ const rel = relative(realpathSync(pkgDir), realpathSync(process.argv[1]));
487
+ return rel && !rel.startsWith("..") && !isAbsolute(rel) ? join(pkgDir, "dist", "cli.js") : void 0;
488
+ } catch {
489
+ return void 0;
490
+ }
491
+ }
492
+ function unlock(lock) {
493
+ try {
494
+ rmSync(lock, { force: true });
495
+ } catch {
496
+ }
497
+ }
498
+ function startCheck(dir) {
499
+ const cache = join(dir, "update-check.json");
500
+ const lock = join(dir, "update.lock");
501
+ try {
502
+ const age = Date.now() - JSON.parse(readFileSync(cache, "utf8")).checkedAt;
503
+ if (age >= 0 && age < DAY_MS) return false;
504
+ } catch {
505
+ }
506
+ try {
507
+ mkdirSync(dir, { recursive: true, mode: 448 });
508
+ closeSync(openSync(lock, "wx"));
509
+ } catch {
510
+ try {
511
+ if (Date.now() - statSync(lock).mtimeMs > LOCK_STALE_MS) unlock(lock);
512
+ } catch {
513
+ }
514
+ return false;
515
+ }
516
+ try {
517
+ writeFileSync(cache, JSON.stringify({ checkedAt: Date.now() }));
518
+ return true;
519
+ } catch {
520
+ unlock(lock);
521
+ return false;
522
+ }
523
+ }
524
+ async function install(current) {
525
+ const latest = await latestVersion();
526
+ if (!latest || !isNewer(latest, current)) return void 0;
527
+ const cli = latest.split(".")[0] === current.split(".")[0] ? globalCli() : void 0;
528
+ if (!cli) {
529
+ console.error(available(latest, current));
530
+ return void 0;
531
+ }
532
+ if (npm(installArgs(latest), "pipe").status !== 0) {
533
+ console.error(`Could not update PayBox CLI to v${latest}; continuing with v${current}. Run \`paybox update\` to see why.`);
534
+ return void 0;
535
+ }
536
+ return { cli, latest };
537
+ }
538
+ async function autoUpdate(command, current) {
539
+ if (NO_AUTO_UPDATE.has(command) || process.env.PAYBOX_NO_AUTO_UPDATE === "1") return;
540
+ const updated = process.env.PAYBOX_AUTO_UPDATED;
541
+ if (updated) {
542
+ if (isNewer(updated, current)) console.error(available(updated, current));
543
+ return;
544
+ }
545
+ const dir = configDir();
546
+ if (!startCheck(dir)) return;
547
+ let next;
548
+ try {
549
+ next = await install(current);
550
+ } finally {
551
+ unlock(join(dir, "update.lock"));
552
+ }
553
+ if (!next) return;
554
+ console.error(`Updated PayBox CLI v${current} \u2192 v${next.latest}.`);
555
+ const child = spawnSync(process.execPath, [next.cli, ...process.argv.slice(2)], {
556
+ stdio: "inherit",
557
+ env: { ...process.env, PAYBOX_AUTO_UPDATED: next.latest }
558
+ });
559
+ process.exit(child.status ?? (child.signal ? 128 + constants.signals[child.signal] : 1));
685
560
  }
686
561
 
687
562
  // src/cli.ts
688
563
  var DEFAULT_BASE_URL = "https://api.paybox.sh";
689
- var PKG = "@paybox-sh/sdk";
690
564
  var VERSION = createRequire(import.meta.url)("../package.json").version;
691
565
  var program = new Command();
692
- program.name("paybox").description("PayBox agent CLI \u2014 vaulted cards, wallets, and secrets for agents").version(VERSION).option("--base-url <url>", "API origin (default https://api.paybox.sh or stored)").option("--json", "machine-readable JSON output");
566
+ program.name("paybox").description("PayBox agent CLI \u2014 vaulted cards, wallets, and secrets for agents").version(VERSION).option("--base-url <url>", "API origin (default https://api.paybox.sh or stored)").option("--json", "machine-readable JSON output").hook("preAction", (_, command) => autoUpdate(command.name(), VERSION));
693
567
  function jsonMode() {
694
568
  return Boolean(program.opts().json);
695
569
  }
@@ -711,7 +585,7 @@ function client() {
711
585
  return PayboxClient.fromConfig({ baseUrl: program.opts().baseUrl });
712
586
  }
713
587
  function readIntent(value) {
714
- const raw = value.startsWith("@") ? readFileSync(value.slice(1), "utf8") : value;
588
+ const raw = value.startsWith("@") ? readFileSync2(value.slice(1), "utf8") : value;
715
589
  return JSON.parse(raw);
716
590
  }
717
591
  function readObject(value) {
@@ -750,22 +624,13 @@ function uuid(value, what, hint) {
750
624
  if (UUID.test(value)) return value;
751
625
  throw new Error(`${what}: got ${JSON.stringify(value)}. ${hint}`);
752
626
  }
753
- function isNewer(a, b) {
754
- const pa = a.split(".").map(Number);
755
- const pb = b.split(".").map(Number);
756
- for (let i = 0; i < 3; i++) {
757
- const x = pa[i] ?? 0;
758
- const y = pb[i] ?? 0;
759
- if (x !== y) return x > y;
760
- }
761
- return false;
762
- }
763
627
  async function run(fn) {
764
628
  try {
765
629
  await fn();
766
630
  } catch (err) {
767
631
  const message = err instanceof Error ? err.message : String(err);
768
- if (jsonMode()) console.log(JSON.stringify({ error: message }, null, 2));
632
+ const reason = err instanceof PayboxError ? err.reason : void 0;
633
+ if (jsonMode()) console.log(JSON.stringify({ error: message, reason }, null, 2));
769
634
  else console.error(message);
770
635
  process.exitCode = 1;
771
636
  }
@@ -807,7 +672,7 @@ program.command("login").description("Authenticate via OAuth device approval, or
807
672
  }
808
673
  })
809
674
  );
810
- program.command("connect").description("Connect a wallet signing key to this machine by picking a code in PayBox").option("--app-url <url>", "PayBox app origin (default: derived from --base-url)").option("--no-open", "print the pairing link without opening a browser").action(
675
+ program.command("connect").description("Connect a wallet signing key to this machine by picking a number in PayBox").option("--app-url <url>", "PayBox app origin (default: derived from --base-url)").option("--no-open", "print the pairing link without opening a browser").action(
811
676
  (opts) => run(async () => {
812
677
  const cfg = loadConfig();
813
678
  const baseUrl = resolveBaseUrl();
@@ -826,9 +691,9 @@ program.command("connect").description("Connect a wallet signing key to this mac
826
691
  bearer,
827
692
  onCode(code, url) {
828
693
  console.error(`
829
- ${code.join(" ")}
694
+ ${code}
830
695
  `);
831
- console.error(`Open this on any device and pick that code to connect this machine:
696
+ console.error(`Open this on any device and pick that number to connect this machine:
832
697
  ${url}
833
698
  `);
834
699
  if (opts.open) openBrowser(url);
@@ -1037,12 +902,7 @@ program.command("request <id>").description("Poll a request's status").option("-
1037
902
  );
1038
903
  program.command("version").description("Show the installed CLI version (and whether an update is available)").action(
1039
904
  () => run(async () => {
1040
- let latest;
1041
- try {
1042
- const res = await fetch(`https://registry.npmjs.org/${PKG}/latest`);
1043
- if (res.ok) latest = (await res.json()).version;
1044
- } catch {
1045
- }
905
+ const latest = await latestVersion();
1046
906
  const updateAvailable = Boolean(latest && isNewer(latest, VERSION));
1047
907
  if (jsonMode()) {
1048
908
  emit({
@@ -1061,13 +921,13 @@ program.command("version").description("Show the installed CLI version (and whet
1061
921
  );
1062
922
  program.command("update").description("Update the globally-installed paybox CLI to the latest version").option("--print", "print the update command instead of running it").action(
1063
923
  (opts) => run(async () => {
1064
- const args = ["install", "-g", `${PKG}@latest`];
924
+ const args = installArgs("latest");
1065
925
  if (opts.print) {
1066
926
  console.log(`npm ${args.join(" ")}`);
1067
927
  return;
1068
928
  }
1069
929
  console.error(`Updating ${PKG} (npm ${args.join(" ")})\u2026`);
1070
- const r = spawnSync("npm", args, { stdio: "inherit" });
930
+ const r = npm(args, "inherit");
1071
931
  if (r.status !== 0) {
1072
932
  console.error(
1073
933
  `
@@ -1080,7 +940,7 @@ If this isn't a global install: run \`pnpm up ${PKG}\` in your project, or use \
1080
940
  program.command("uninstall").description("Remove stored PayBox credentials/config (and show how to remove the CLI)").action(
1081
941
  () => run(async () => {
1082
942
  const dir = configDir();
1083
- rmSync(dir, { recursive: true, force: true });
943
+ rmSync2(dir, { recursive: true, force: true });
1084
944
  console.error(`Removed stored config at ${dir}.`);
1085
945
  console.error(
1086
946
  `The CLI binary is managed by npm \u2014 remove it with: