tenjin-cli 0.1.0-alpha.4 → 0.1.0-alpha.5

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.
Files changed (108) hide show
  1. package/README.md +168 -11
  2. package/dist/{buy-OOOLUDND.js → buy-YMXXVNFH.js} +18 -17
  3. package/dist/{candidate-TWCWTFGA.js → candidate-H7FR4AU5.js} +8 -8
  4. package/dist/{chunk-MUBUQVC3.js → chunk-2JQ5JSG6.js} +3 -2
  5. package/dist/{chunk-MUBUQVC3.js.map → chunk-2JQ5JSG6.js.map} +1 -1
  6. package/dist/{chunk-NXCJN2FJ.js → chunk-3IRDP7TN.js} +48 -41
  7. package/dist/chunk-3IRDP7TN.js.map +1 -0
  8. package/dist/{chunk-4GR7DGD6.js → chunk-4WVKXKDK.js} +14 -28
  9. package/dist/chunk-4WVKXKDK.js.map +1 -0
  10. package/dist/chunk-5SCU253N.js +412 -0
  11. package/dist/chunk-5SCU253N.js.map +1 -0
  12. package/dist/chunk-ADJYSM45.js +135 -0
  13. package/dist/chunk-ADJYSM45.js.map +1 -0
  14. package/dist/{chunk-5JH7ZB6Z.js → chunk-BIKAUVC2.js} +40 -8
  15. package/dist/chunk-BIKAUVC2.js.map +1 -0
  16. package/dist/{chunk-S3KZ3PPK.js → chunk-C444K6MI.js} +254 -47
  17. package/dist/chunk-C444K6MI.js.map +1 -0
  18. package/dist/{chunk-XNNOR7F7.js → chunk-CVX6JNGD.js} +3 -3
  19. package/dist/{chunk-H5ETJRQD.js → chunk-HTDALK7M.js} +16 -11
  20. package/dist/chunk-HTDALK7M.js.map +1 -0
  21. package/dist/chunk-IOPWPMT2.js +239 -0
  22. package/dist/chunk-IOPWPMT2.js.map +1 -0
  23. package/dist/{chunk-MM4TGTST.js → chunk-L4IHCN6B.js} +8 -4
  24. package/dist/chunk-L4IHCN6B.js.map +1 -0
  25. package/dist/{chunk-EUQN3L5X.js → chunk-LAM66WFJ.js} +20 -6
  26. package/dist/chunk-LAM66WFJ.js.map +1 -0
  27. package/dist/{chunk-FHB3VKZW.js → chunk-MDDGEGSW.js} +5 -3
  28. package/dist/chunk-MDDGEGSW.js.map +1 -0
  29. package/dist/{chunk-333FOTUJ.js → chunk-MJZZTKCC.js} +108 -2
  30. package/dist/chunk-MJZZTKCC.js.map +1 -0
  31. package/dist/{chunk-VV4P5RVK.js → chunk-NFSOVL3C.js} +6 -3
  32. package/dist/{chunk-VV4P5RVK.js.map → chunk-NFSOVL3C.js.map} +1 -1
  33. package/dist/{chunk-QH6677FJ.js → chunk-NJF2WENW.js} +2 -2
  34. package/dist/{chunk-QH6677FJ.js.map → chunk-NJF2WENW.js.map} +1 -1
  35. package/dist/{chunk-6WWVYYSB.js → chunk-PAUXMRSF.js} +14 -3
  36. package/dist/{chunk-6WWVYYSB.js.map → chunk-PAUXMRSF.js.map} +1 -1
  37. package/dist/{chunk-ULLWHHIT.js → chunk-PYNSGYBE.js} +244 -32
  38. package/dist/chunk-PYNSGYBE.js.map +1 -0
  39. package/dist/{chunk-DPET3TW5.js → chunk-QONTBYQW.js} +12 -9
  40. package/dist/chunk-QONTBYQW.js.map +1 -0
  41. package/dist/chunk-QW2Q2KAH.js +27 -0
  42. package/dist/chunk-QW2Q2KAH.js.map +1 -0
  43. package/dist/chunk-RTJLLPFA.js +599 -0
  44. package/dist/chunk-RTJLLPFA.js.map +1 -0
  45. package/dist/{chunk-KXIX7YDV.js → chunk-TXPPMK4L.js} +5 -5
  46. package/dist/{chunk-C4TIWOAM.js → chunk-ZDY2KY5X.js} +2 -2
  47. package/dist/{chunk-DPUMEMGM.js → chunk-ZIYVN4CD.js} +3 -3
  48. package/dist/{chunk-OJD54DIU.js → chunk-ZW3B5LWX.js} +5 -5
  49. package/dist/{cli-AFSLAKR6.js → cli-IW76QTN3.js} +38 -25
  50. package/dist/{cli-AFSLAKR6.js.map → cli-IW76QTN3.js.map} +1 -1
  51. package/dist/{config-DBEEM3UA.js → config-VCOS7P5K.js} +8 -6
  52. package/dist/{doctor-XDA3YU6H.js → doctor-WS65JG3V.js} +9 -8
  53. package/dist/index.js +1 -1
  54. package/dist/{inspect-YBGDAG47.js → inspect-SFJWUQHO.js} +14 -14
  55. package/dist/{install-7J6KKEEP.js → install-RVPQPCKH.js} +177 -130
  56. package/dist/install-RVPQPCKH.js.map +1 -0
  57. package/dist/outcome-GNYCSZ7J.js +20 -0
  58. package/dist/{publish-IH7RJXGB.js → publish-DSFWQRUN.js} +14 -14
  59. package/dist/{run-DCZFEW2C.js → run-OMPKA43G.js} +29 -28
  60. package/dist/{run-DCZFEW2C.js.map → run-OMPKA43G.js.map} +1 -1
  61. package/dist/search-KOI6Y7P6.js +24 -0
  62. package/dist/send-3G4C64HJ.js +222 -0
  63. package/dist/send-3G4C64HJ.js.map +1 -0
  64. package/dist/{usdc-AU6ZIKA6.js → usdc-4VCDHFWX.js} +20 -4
  65. package/dist/{wallet-HKG3GSTX.js → wallet-B7KJS2R5.js} +17 -7
  66. package/dist/{wallet-UWHSHWC6.js → wallet-PP22LAX7.js} +8 -8
  67. package/package.json +1 -1
  68. package/skills/tenjin/SKILL.md +32 -7
  69. package/skills/tenjin-publish/SKILL.md +97 -12
  70. package/skills/tenjin-search/SKILL.md +115 -12
  71. package/dist/chunk-333FOTUJ.js.map +0 -1
  72. package/dist/chunk-4GR7DGD6.js.map +0 -1
  73. package/dist/chunk-5JH7ZB6Z.js.map +0 -1
  74. package/dist/chunk-BXUS5UCT.js +0 -212
  75. package/dist/chunk-BXUS5UCT.js.map +0 -1
  76. package/dist/chunk-CJU555P5.js +0 -333
  77. package/dist/chunk-CJU555P5.js.map +0 -1
  78. package/dist/chunk-DPET3TW5.js.map +0 -1
  79. package/dist/chunk-EUQN3L5X.js.map +0 -1
  80. package/dist/chunk-FHB3VKZW.js.map +0 -1
  81. package/dist/chunk-H5ETJRQD.js.map +0 -1
  82. package/dist/chunk-MM4TGTST.js.map +0 -1
  83. package/dist/chunk-NXCJN2FJ.js.map +0 -1
  84. package/dist/chunk-R7BMDJZM.js +0 -167
  85. package/dist/chunk-R7BMDJZM.js.map +0 -1
  86. package/dist/chunk-S3KZ3PPK.js.map +0 -1
  87. package/dist/chunk-ULLWHHIT.js.map +0 -1
  88. package/dist/chunk-YFREUEUI.js +0 -78
  89. package/dist/chunk-YFREUEUI.js.map +0 -1
  90. package/dist/install-7J6KKEEP.js.map +0 -1
  91. package/dist/outcome-ZFGHX2XW.js +0 -20
  92. package/dist/search-R6NIQFXI.js +0 -24
  93. /package/dist/{buy-OOOLUDND.js.map → buy-YMXXVNFH.js.map} +0 -0
  94. /package/dist/{candidate-TWCWTFGA.js.map → candidate-H7FR4AU5.js.map} +0 -0
  95. /package/dist/{chunk-XNNOR7F7.js.map → chunk-CVX6JNGD.js.map} +0 -0
  96. /package/dist/{chunk-KXIX7YDV.js.map → chunk-TXPPMK4L.js.map} +0 -0
  97. /package/dist/{chunk-C4TIWOAM.js.map → chunk-ZDY2KY5X.js.map} +0 -0
  98. /package/dist/{chunk-DPUMEMGM.js.map → chunk-ZIYVN4CD.js.map} +0 -0
  99. /package/dist/{chunk-OJD54DIU.js.map → chunk-ZW3B5LWX.js.map} +0 -0
  100. /package/dist/{config-DBEEM3UA.js.map → config-VCOS7P5K.js.map} +0 -0
  101. /package/dist/{doctor-XDA3YU6H.js.map → doctor-WS65JG3V.js.map} +0 -0
  102. /package/dist/{inspect-YBGDAG47.js.map → inspect-SFJWUQHO.js.map} +0 -0
  103. /package/dist/{outcome-ZFGHX2XW.js.map → outcome-GNYCSZ7J.js.map} +0 -0
  104. /package/dist/{publish-IH7RJXGB.js.map → publish-DSFWQRUN.js.map} +0 -0
  105. /package/dist/{search-R6NIQFXI.js.map → search-KOI6Y7P6.js.map} +0 -0
  106. /package/dist/{usdc-AU6ZIKA6.js.map → usdc-4VCDHFWX.js.map} +0 -0
  107. /package/dist/{wallet-HKG3GSTX.js.map → wallet-B7KJS2R5.js.map} +0 -0
  108. /package/dist/{wallet-UWHSHWC6.js.map → wallet-PP22LAX7.js.map} +0 -0
@@ -4,7 +4,10 @@ import {
4
4
  readWalletRecord,
5
5
  walletFileMode,
6
6
  writeWalletRecord
7
- } from "./chunk-DPET3TW5.js";
7
+ } from "./chunk-QONTBYQW.js";
8
+ import {
9
+ hasCode
10
+ } from "./chunk-2I4QTOLB.js";
8
11
  import {
9
12
  withFileLock
10
13
  } from "./chunk-YBMMV4Y4.js";
@@ -12,14 +15,17 @@ import {
12
15
  evaluateSpendPolicy
13
16
  } from "./chunk-UO22YNY4.js";
14
17
  import {
18
+ fsyncDir,
15
19
  writeFileAtomic
16
- } from "./chunk-MUBUQVC3.js";
20
+ } from "./chunk-2JQ5JSG6.js";
17
21
  import {
18
22
  CliError,
23
+ archivedWalletPath,
19
24
  external_exports,
20
25
  passphraseBlobPath,
26
+ passphraseBlobPathFor,
21
27
  walletPath
22
- } from "./chunk-6WWVYYSB.js";
28
+ } from "./chunk-PAUXMRSF.js";
23
29
  import {
24
30
  hashAuthorization,
25
31
  hashMessage,
@@ -62,6 +68,9 @@ import {
62
68
  u32
63
69
  } from "./chunk-KYQVEKP5.js";
64
70
 
71
+ // src/lib/wallet/local.ts
72
+ import { link, readdir, unlink as unlink2 } from "fs/promises";
73
+
65
74
  // node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/pbkdf2.js
66
75
  function pbkdf2Init(hash, _password, _salt, _opts) {
67
76
  ahash(hash);
@@ -761,10 +770,18 @@ function defineKey(key, options) {
761
770
  // src/lib/wallet/passphrase.ts
762
771
  import { execFile } from "child_process";
763
772
  import { randomBytes } from "crypto";
764
- import { readFile } from "fs/promises";
773
+ import { readFile, unlink } from "fs/promises";
765
774
  var KEYCHAIN_SERVICE = "tenjin-cli";
766
- var KEYCHAIN_ACCOUNT = "wallet";
775
+ var LEGACY_ACCOUNT = "wallet";
776
+ var ACCOUNT_ADDRESS_RE = /^0x[0-9a-f]{40}$/;
767
777
  var BASE64URL_RE = /^[A-Za-z0-9_-]+$/;
778
+ function walletStoreAccount(address) {
779
+ const account = address.toLowerCase();
780
+ if (!ACCOUNT_ADDRESS_RE.test(account)) {
781
+ throw new CliError("INTERNAL", `Not a valid wallet address for passphrase storage: ${address}`);
782
+ }
783
+ return account;
784
+ }
768
785
  var defaultExec = (file, args, stdin) => new Promise((resolve, reject) => {
769
786
  const child = execFile(file, args, { encoding: "utf8" }, (err, stdout, stderr) => {
770
787
  if (err) reject(err);
@@ -787,13 +804,22 @@ function storeFor(deps) {
787
804
  return null;
788
805
  }
789
806
  }
790
- async function resolvePassphrase(deps) {
807
+ async function resolvePassphrase(deps, address) {
791
808
  const fromEnv = envPassphrase(deps.env);
792
809
  if (fromEnv !== null) return { passphrase: fromEnv, source: "env" };
793
810
  const store = storeFor(deps);
794
811
  if (store !== null) {
795
- const fromStore = await store.read();
796
- if (fromStore !== null) return { passphrase: fromStore, source: store.source };
812
+ const account = walletStoreAccount(address);
813
+ const own = await store.read(account);
814
+ if (own !== null) return { passphrase: own, source: store.source };
815
+ const legacy = await store.read(LEGACY_ACCOUNT);
816
+ if (legacy !== null) {
817
+ return {
818
+ passphrase: legacy,
819
+ source: store.source,
820
+ migrateLegacy: () => migrateLegacyEntry(store, account, legacy)
821
+ };
822
+ }
797
823
  }
798
824
  if (canPrompt(deps)) {
799
825
  const entered = await promptOf(deps)("Wallet passphrase: ");
@@ -802,13 +828,29 @@ async function resolvePassphrase(deps) {
802
828
  }
803
829
  throw noPassphraseError();
804
830
  }
805
- async function resolvePassphraseForCreate(deps) {
831
+ async function migrateLegacyEntry(store, account, passphrase) {
832
+ if (!await store.store(account, passphrase)) return false;
833
+ if (await store.read(account) !== passphrase) return false;
834
+ await store.remove(LEGACY_ACCOUNT);
835
+ return true;
836
+ }
837
+ async function storePassphraseForWallet(deps, address, passphrase) {
838
+ const store = storeFor(deps);
839
+ if (store === null) return "no-store";
840
+ const account = walletStoreAccount(address);
841
+ if (await store.read(account) === passphrase) return "stored";
842
+ if (store.storable !== void 0 && !store.storable(passphrase)) return "rejected-characters";
843
+ if (!await store.store(account, passphrase)) return "store-failed";
844
+ return await store.read(account) === passphrase ? "stored" : "store-failed";
845
+ }
846
+ async function resolvePassphraseForCreate(deps, address) {
806
847
  const fromEnv = envPassphrase(deps.env);
807
848
  if (fromEnv !== null) return { passphrase: fromEnv, source: "env" };
808
849
  const store = storeFor(deps);
809
850
  if (store !== null) {
851
+ const account = walletStoreAccount(address);
810
852
  const generated = randomBytes(32).toString("base64url");
811
- if (await store.store(generated)) {
853
+ if (await store.store(account, generated) && await store.read(account) === generated) {
812
854
  return { passphrase: generated, source: store.source };
813
855
  }
814
856
  }
@@ -834,14 +876,14 @@ function keychainStore(deps) {
834
876
  const exec = deps.exec ?? defaultExec;
835
877
  return {
836
878
  source: "keychain",
837
- async read() {
879
+ async read(account) {
838
880
  try {
839
881
  const { stdout } = await exec("security", [
840
882
  "find-generic-password",
841
883
  "-s",
842
884
  KEYCHAIN_SERVICE,
843
885
  "-a",
844
- KEYCHAIN_ACCOUNT,
886
+ account,
845
887
  "-w"
846
888
  ]);
847
889
  return trimmedNonEmpty(stdout);
@@ -849,16 +891,28 @@ function keychainStore(deps) {
849
891
  return null;
850
892
  }
851
893
  },
852
- async store(passphrase) {
853
- if (!BASE64URL_RE.test(passphrase)) return false;
894
+ // The one backend that interpolates into a command line, so only base64url
895
+ // values pass; escaping is NOT a safe alternative (backslash bites even
896
+ // inside single quotes there).
897
+ storable: (passphrase) => BASE64URL_RE.test(passphrase),
898
+ async store(account, passphrase) {
899
+ if (!BASE64URL_RE.test(passphrase) || !BASE64URL_RE.test(account)) return false;
854
900
  try {
855
- const command = `add-generic-password -U -s ${KEYCHAIN_SERVICE} -a ${KEYCHAIN_ACCOUNT} -w '${passphrase}'
901
+ const command = `add-generic-password -s ${KEYCHAIN_SERVICE} -a ${account} -w '${passphrase}'
856
902
  `;
857
903
  await exec("security", ["-i"], command);
858
904
  return true;
859
905
  } catch {
860
906
  return false;
861
907
  }
908
+ },
909
+ async remove(account) {
910
+ try {
911
+ await exec("security", ["delete-generic-password", "-s", KEYCHAIN_SERVICE, "-a", account]);
912
+ return true;
913
+ } catch {
914
+ return false;
915
+ }
862
916
  }
863
917
  };
864
918
  }
@@ -881,13 +935,13 @@ function powershellArgs(script) {
881
935
  }
882
936
  function dpapiStore(deps) {
883
937
  const exec = deps.exec ?? defaultExec;
884
- const blobPath = passphraseBlobPath(deps.dir);
938
+ const blobPathFor = (account) => account === LEGACY_ACCOUNT ? passphraseBlobPath(deps.dir) : passphraseBlobPathFor(account, deps.dir);
885
939
  return {
886
940
  source: "dpapi",
887
- async read() {
941
+ async read(account) {
888
942
  let blob;
889
943
  try {
890
- blob = await readFile(blobPath, "utf8");
944
+ blob = await readFile(blobPathFor(account), "utf8");
891
945
  } catch {
892
946
  return null;
893
947
  }
@@ -903,7 +957,7 @@ function dpapiStore(deps) {
903
957
  return null;
904
958
  }
905
959
  },
906
- async store(passphrase) {
960
+ async store(account, passphrase) {
907
961
  try {
908
962
  const { stdout } = await exec(
909
963
  "powershell.exe",
@@ -912,7 +966,15 @@ function dpapiStore(deps) {
912
966
  );
913
967
  const blob = stdout.replace(/\r?\n$/, "").trim();
914
968
  if (blob.length === 0) return false;
915
- await writeFileAtomic(blobPath, blob, { mode: 384 });
969
+ await writeFileAtomic(blobPathFor(account), blob, { mode: 384 });
970
+ return true;
971
+ } catch {
972
+ return false;
973
+ }
974
+ },
975
+ async remove(account) {
976
+ try {
977
+ await unlink(blobPathFor(account));
916
978
  return true;
917
979
  } catch {
918
980
  return false;
@@ -924,31 +986,31 @@ function secretServiceStore(deps) {
924
986
  const exec = deps.exec ?? defaultExec;
925
987
  return {
926
988
  source: "secret-service",
927
- async read() {
989
+ async read(account) {
928
990
  try {
929
991
  const { stdout } = await exec("secret-tool", [
930
992
  "lookup",
931
993
  "service",
932
994
  KEYCHAIN_SERVICE,
933
995
  "account",
934
- KEYCHAIN_ACCOUNT
996
+ account
935
997
  ]);
936
998
  return trimmedNonEmpty(stdout);
937
999
  } catch {
938
1000
  return null;
939
1001
  }
940
1002
  },
941
- async store(passphrase) {
1003
+ async store(account, passphrase) {
942
1004
  try {
943
1005
  await exec(
944
1006
  "secret-tool",
945
1007
  [
946
1008
  "store",
947
- "--label=tenjin-cli-wallet",
1009
+ `--label=tenjin-cli wallet ${account}`,
948
1010
  "service",
949
1011
  KEYCHAIN_SERVICE,
950
1012
  "account",
951
- KEYCHAIN_ACCOUNT
1013
+ account
952
1014
  ],
953
1015
  passphrase
954
1016
  );
@@ -956,6 +1018,14 @@ function secretServiceStore(deps) {
956
1018
  } catch {
957
1019
  return false;
958
1020
  }
1021
+ },
1022
+ async remove(account) {
1023
+ try {
1024
+ await exec("secret-tool", ["clear", "service", KEYCHAIN_SERVICE, "account", account]);
1025
+ return true;
1026
+ } catch {
1027
+ return false;
1028
+ }
959
1029
  }
960
1030
  };
961
1031
  }
@@ -1034,7 +1104,7 @@ function createLocalProvider(deps) {
1034
1104
  return {
1035
1105
  id: "local",
1036
1106
  async describe() {
1037
- const cred = resolveCredentialOrThrow(await loadCredential(deps));
1107
+ const cred = await credentialOrThrow(deps);
1038
1108
  return {
1039
1109
  // A file credential's address is stored cleartext and returned as-is —
1040
1110
  // no decryption. An env credential derives (and thereby validates) its
@@ -1046,12 +1116,13 @@ function createLocalProvider(deps) {
1046
1116
  };
1047
1117
  },
1048
1118
  async getSigner() {
1049
- const cred = resolveCredentialOrThrow(await loadCredential(deps));
1119
+ const cred = await credentialOrThrow(deps);
1050
1120
  const account = await accountForSigning(cred, deps);
1051
1121
  return {
1052
1122
  address: account.address,
1053
1123
  signMessage: (args) => account.signMessage({ message: args.message }),
1054
- signTypedData: (args) => account.signTypedData(args)
1124
+ signTypedData: (args) => account.signTypedData(args),
1125
+ signTransaction: (tx) => account.signTransaction(tx)
1055
1126
  };
1056
1127
  },
1057
1128
  diagnostics() {
@@ -1079,18 +1150,133 @@ async function localWalletDiagnostics(deps) {
1079
1150
  }
1080
1151
  }
1081
1152
  const passphraseSource = deps.env.TENJIN_WALLET_PASSPHRASE !== void 0 && deps.env.TENJIN_WALLET_PASSPHRASE.length > 0 ? "TENJIN_WALLET_PASSPHRASE" : void 0;
1153
+ const archivedWallets = await listArchivedWallets(deps.dir);
1082
1154
  return {
1083
1155
  ...fileExists ? { walletPath: path, keyStorage: KEY_STORAGE } : {},
1084
1156
  ...fileExists && passphraseSource !== void 0 ? { passphraseSource } : {},
1157
+ ...archivedWallets.length > 0 ? { archivedWallets } : {},
1085
1158
  warnings
1086
1159
  };
1087
1160
  }
1088
- async function createLocalWallet(dir, passphrase) {
1161
+ var ARCHIVED_WALLET_RE = /^wallet\.(0x[0-9a-f]{40})\.json\.bak$/;
1162
+ async function listArchivedWallets(dir) {
1163
+ let names;
1164
+ try {
1165
+ names = await readdir(dir);
1166
+ } catch {
1167
+ return [];
1168
+ }
1169
+ return names.map((name) => ARCHIVED_WALLET_RE.exec(name)?.[1]).filter((address) => address !== void 0).sort();
1170
+ }
1171
+ async function prepareLocalWallet(passphrase) {
1089
1172
  const key = generatePrivateKey();
1090
1173
  const address = privateKeyToAccount(key).address;
1091
- const keystore = await encryptToKeystore(key, passphrase);
1092
- await writeWalletRecord(dir, walletRecord(address, keystore));
1093
- return { address, walletPath: walletPath(dir) };
1174
+ const resolved = typeof passphrase === "string" ? passphrase : await passphrase(address);
1175
+ const keystore = await encryptToKeystore(key, resolved);
1176
+ return { address, record: walletRecord(address, keystore) };
1177
+ }
1178
+ async function commitLocalWallet(dir, prepared) {
1179
+ await writeWalletRecord(dir, prepared.record);
1180
+ return { address: prepared.address, walletPath: walletPath(dir) };
1181
+ }
1182
+ async function createLocalWallet(dir, passphrase) {
1183
+ return commitLocalWallet(dir, await prepareLocalWallet(passphrase));
1184
+ }
1185
+ async function verifyAndPreserveOutgoingWallet(deps) {
1186
+ const record = await readWalletRecord(deps.dir);
1187
+ if (record === null) {
1188
+ throw new CliError("WALLET_MISSING", "No wallet found to archive.", {
1189
+ fix: "Run `tenjin wallet create` without --replace."
1190
+ });
1191
+ }
1192
+ const address = record.address;
1193
+ const account = walletStoreAccount(address);
1194
+ const passphraseDeps = { env: deps.env, dir: deps.dir, ...deps.passphrase };
1195
+ const resolved = await resolvePassphrase(passphraseDeps, address);
1196
+ let key;
1197
+ try {
1198
+ const derived = await toKeyAsync(record.keystore, { password: resolved.passphrase });
1199
+ key = decrypt(record.keystore, derived);
1200
+ } catch (err) {
1201
+ const escape = resolved.source === "prompt" ? "Retry and enter that wallet's correct passphrase at the prompt, or set TENJIN_WALLET_PASSPHRASE to it" : resolved.source === "env" ? "TENJIN_WALLET_PASSPHRASE is set but does not decrypt it; set it to the correct passphrase" : `Set TENJIN_WALLET_PASSPHRASE to its passphrase, or fix its OS credential store entry (service tenjin-cli, account ${account})`;
1202
+ throw new CliError(
1203
+ "REFUSED",
1204
+ `Cannot verify the passphrase of the existing wallet ${address}; --replace refused.`,
1205
+ {
1206
+ fix: `Replacing now could strand that wallet's funds. ${escape}, then retry.`,
1207
+ cause: err
1208
+ }
1209
+ );
1210
+ }
1211
+ if (privateKeyToAccount(key).address.toLowerCase() !== account) {
1212
+ throw new CliError(
1213
+ "REFUSED",
1214
+ `The wallet file's stored address ${address} does not match its key; --replace refused.`,
1215
+ { fix: "The wallet file may be tampered. Resolve that before replacing it." }
1216
+ );
1217
+ }
1218
+ if (resolved.source === "env") {
1219
+ return { address, account, passphraseLocation: "env" };
1220
+ }
1221
+ if (resolved.migrateLegacy !== void 0) {
1222
+ if (!await resolved.migrateLegacy()) {
1223
+ throw new CliError(
1224
+ "REFUSED",
1225
+ `Could not archive the existing wallet's passphrase under its own address; --replace refused.`,
1226
+ {
1227
+ fix: `The passphrase currently lives only in the legacy shared slot (service tenjin-cli, account "wallet"), and copying it to account ${account} failed. Retry, or check OS credential store access.`
1228
+ }
1229
+ );
1230
+ }
1231
+ return { address, account, passphraseLocation: "store" };
1232
+ }
1233
+ if (resolved.source === "prompt") {
1234
+ const outcome = await storePassphraseForWallet(passphraseDeps, address, resolved.passphrase);
1235
+ return outcome === "stored" ? { address, account, passphraseLocation: "store" } : { address, account, passphraseLocation: "unarchived", unarchivedReason: outcome };
1236
+ }
1237
+ return { address, account, passphraseLocation: "store" };
1238
+ }
1239
+ async function parkOutgoingWallet(dir, account) {
1240
+ const src = walletPath(dir);
1241
+ const dst = archivedWalletPath(account, dir);
1242
+ try {
1243
+ await link(src, dst);
1244
+ } catch (err) {
1245
+ if (hasCode(err, "EEXIST")) {
1246
+ throw new CliError("REFUSED", `An archived wallet already exists at ${dst}.`, {
1247
+ fix: `Move ${dst} aside, then retry \`tenjin wallet create --replace\`. Nothing has moved; the current wallet is still active.`,
1248
+ cause: err
1249
+ });
1250
+ }
1251
+ throw new CliError("INTERNAL", `Could not park the outgoing keystore at ${dst}.`, {
1252
+ fix: `Nothing has moved; the current wallet is still active at ${src}. Fix the underlying filesystem error and retry.`,
1253
+ cause: err
1254
+ });
1255
+ }
1256
+ try {
1257
+ await unlink2(src);
1258
+ } catch (err) {
1259
+ await unlink2(dst).catch(() => void 0);
1260
+ throw new CliError(
1261
+ "INTERNAL",
1262
+ `Could not clear the active wallet slot ${src} while parking the outgoing wallet.`,
1263
+ {
1264
+ fix: "The previous wallet is still active and nothing was lost. Fix the underlying filesystem error (on Windows, close any process holding the wallet file open) and retry.",
1265
+ cause: err
1266
+ }
1267
+ );
1268
+ }
1269
+ await syncWalletDir(dir);
1270
+ return dst;
1271
+ }
1272
+ async function restoreParkedWallet(dir, archivedPath) {
1273
+ await link(archivedPath, walletPath(dir));
1274
+ await unlink2(archivedPath).catch(() => void 0);
1275
+ await syncWalletDir(dir);
1276
+ }
1277
+ async function syncWalletDir(dir) {
1278
+ if (isWindows) return;
1279
+ await fsyncDir(dir);
1094
1280
  }
1095
1281
  async function loadCredential(deps) {
1096
1282
  const envKey = deps.env.TENJIN_WALLET_KEY;
@@ -1113,18 +1299,32 @@ async function accountForSigning(cred, deps) {
1113
1299
  if (cred.source === "env") return accountFromKey(cred.key, "env");
1114
1300
  const cached = signerCache.get(cred.keystore.id);
1115
1301
  if (cached !== void 0) return cached;
1116
- const { passphrase } = await resolvePassphrase({
1117
- env: deps.env,
1118
- dir: deps.dir,
1119
- ...deps.passphrase
1120
- });
1302
+ const resolved = await resolvePassphrase(
1303
+ {
1304
+ env: deps.env,
1305
+ dir: deps.dir,
1306
+ ...deps.passphrase
1307
+ },
1308
+ cred.address
1309
+ );
1121
1310
  let key;
1122
1311
  try {
1123
- const derived = await toKeyAsync(cred.keystore, { password: passphrase });
1312
+ const derived = await toKeyAsync(cred.keystore, { password: resolved.passphrase });
1124
1313
  key = decrypt(cred.keystore, derived);
1125
1314
  } catch (err) {
1315
+ if (resolved.migrateLegacy !== void 0) {
1316
+ throw new CliError(
1317
+ "WALLET_INVALID_KEY",
1318
+ 'The legacy shared passphrase entry (service tenjin-cli, account "wallet") does not decrypt this wallet.',
1319
+ {
1320
+ fix: "That entry likely belongs to a different wallet created later and was left untouched. If you know this wallet's passphrase, set TENJIN_WALLET_PASSPHRASE to it (a store-served passphrase is never re-asked at a prompt).",
1321
+ cause: err
1322
+ }
1323
+ );
1324
+ }
1325
+ const escape = resolved.source === "env" ? "TENJIN_WALLET_PASSPHRASE is set but does not decrypt it; set it to the correct passphrase" : resolved.source === "prompt" ? "Retry and enter the correct passphrase at the prompt, or set TENJIN_WALLET_PASSPHRASE to it" : `The OS credential store entry (service tenjin-cli, account ${cred.address.toLowerCase()}) does not decrypt it; set TENJIN_WALLET_PASSPHRASE to the correct passphrase`;
1126
1326
  throw new CliError("WALLET_INVALID_KEY", "Could not decrypt the wallet keystore.", {
1127
- fix: "Check the passphrase: TENJIN_WALLET_PASSPHRASE, the macOS keychain (service tenjin-cli), or the one you enter when prompted.",
1327
+ fix: `${escape}.`,
1128
1328
  cause: err
1129
1329
  });
1130
1330
  }
@@ -1138,6 +1338,7 @@ async function accountForSigning(cred, deps) {
1138
1338
  }
1139
1339
  );
1140
1340
  }
1341
+ if (resolved.migrateLegacy !== void 0) await resolved.migrateLegacy();
1141
1342
  signerCache.set(cred.keystore.id, account);
1142
1343
  return account;
1143
1344
  }
@@ -1155,13 +1356,14 @@ async function encryptToKeystore(key, passphrase) {
1155
1356
  const [derivedKey, opts] = await scryptAsync2({ password: passphrase });
1156
1357
  return encrypt2(key, derivedKey, opts);
1157
1358
  }
1158
- function resolveCredentialOrThrow(cred) {
1159
- if (cred === null) {
1160
- throw new CliError("WALLET_MISSING", "No wallet found.", {
1161
- fix: "Run `tenjin wallet create` to create one."
1162
- });
1163
- }
1164
- return cred;
1359
+ async function credentialOrThrow(deps) {
1360
+ const cred = await loadCredential(deps);
1361
+ if (cred !== null) return cred;
1362
+ const archived = await listArchivedWallets(deps.dir);
1363
+ const hint = archived.length > 0 ? ` Archived wallets exist (${archived.join(", ")}); restore one with \`mv ${archivedWalletPath("<address>", deps.dir)} ${walletPath(deps.dir)}\`.` : "";
1364
+ throw new CliError("WALLET_MISSING", "No wallet found.", {
1365
+ fix: `Run \`tenjin wallet create\` to create one.${hint}`
1366
+ });
1165
1367
  }
1166
1368
  function walletRecord(address, keystore) {
1167
1369
  return {
@@ -1320,7 +1522,12 @@ async function describeWallet(provider) {
1320
1522
 
1321
1523
  export {
1322
1524
  resolvePassphraseForCreate,
1525
+ prepareLocalWallet,
1526
+ commitLocalWallet,
1323
1527
  createLocalWallet,
1528
+ verifyAndPreserveOutgoingWallet,
1529
+ parkOutgoingWallet,
1530
+ restoreParkedWallet,
1324
1531
  createLocalSpendAuthorizer,
1325
1532
  resolveWalletProvider,
1326
1533
  resolveSpendAuthorizer,
@@ -1331,4 +1538,4 @@ export {
1331
1538
  @noble/ciphers/esm/utils.js:
1332
1539
  (*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
1333
1540
  */
1334
- //# sourceMappingURL=chunk-S3KZ3PPK.js.map
1541
+ //# sourceMappingURL=chunk-C444K6MI.js.map