shogun-core 6.2.3 → 6.3.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.
Files changed (233) hide show
  1. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +93 -341
  2. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -1
  3. package/dist/browser/shogun-core.js +138850 -146638
  4. package/dist/browser/shogun-core.js.map +1 -1
  5. package/dist/{core.js → src/core.js} +167 -107
  6. package/dist/src/crypto/asymmetric.js +168 -0
  7. package/dist/src/crypto/double-ratchet.js +908 -0
  8. package/dist/src/crypto/file-encryption.js +352 -0
  9. package/dist/src/crypto/hashing.js +160 -0
  10. package/dist/src/crypto/index.js +18 -0
  11. package/dist/{crypto → src/crypto}/mls-codec.js +24 -34
  12. package/dist/src/crypto/mls.js +734 -0
  13. package/dist/src/crypto/pgp.js +619 -0
  14. package/dist/{crypto → src/crypto}/random-generation.js +125 -103
  15. package/dist/src/crypto/sframe.js +466 -0
  16. package/dist/src/crypto/signal-protocol.js +943 -0
  17. package/dist/src/crypto/symmetric.js +165 -0
  18. package/dist/src/crypto/utils.js +220 -0
  19. package/dist/src/examples/auth-test.js +535 -0
  20. package/dist/src/examples/crypto-identity-example.js +294 -0
  21. package/dist/src/examples/crypto-working-test.js +149 -0
  22. package/dist/src/examples/double-ratchet-test.js +240 -0
  23. package/dist/src/examples/mls-3-member-test.js +183 -0
  24. package/dist/src/examples/mls-multi-member.js +439 -0
  25. package/dist/src/examples/mls-sframe-test.js +491 -0
  26. package/dist/src/examples/mls-simple-test.js +122 -0
  27. package/dist/src/examples/pgp-example.js +354 -0
  28. package/dist/src/examples/random-generation-test.js +191 -0
  29. package/dist/src/examples/shogun-core-example.js +204 -0
  30. package/dist/src/examples/signal-protocol-test.js +82 -0
  31. package/dist/src/examples/zkproof-credentials-example.js +357 -0
  32. package/dist/src/examples/zkproof-example.js +357 -0
  33. package/dist/src/gundb/crypto.js +420 -0
  34. package/dist/src/gundb/db.js +728 -0
  35. package/dist/src/gundb/derive.js +327 -0
  36. package/dist/src/gundb/errors.js +115 -0
  37. package/dist/src/gundb/gun-es.js +8 -0
  38. package/dist/src/gundb/index.js +5 -0
  39. package/dist/{gundb → src/gundb}/rxjs.js +147 -111
  40. package/dist/{gundb → src/gundb}/types.js +1 -2
  41. package/dist/src/index.js +19 -0
  42. package/dist/src/interfaces/events.js +57 -0
  43. package/dist/{interfaces → src/interfaces}/shogun.js +4 -7
  44. package/dist/src/managers/AuthManager.js +301 -0
  45. package/dist/src/managers/CoreInitializer.js +304 -0
  46. package/dist/src/managers/CryptoIdentityManager.js +230 -0
  47. package/dist/{managers → src/managers}/EventManager.js +19 -21
  48. package/dist/{managers → src/managers}/PluginManager.js +123 -89
  49. package/dist/src/plugins/base.js +90 -0
  50. package/dist/src/plugins/index.js +17 -0
  51. package/dist/src/plugins/nostr/index.js +4 -0
  52. package/dist/src/plugins/nostr/nostrConnector.js +539 -0
  53. package/dist/src/plugins/nostr/nostrConnectorPlugin.js +663 -0
  54. package/dist/src/plugins/nostr/nostrSigner.js +414 -0
  55. package/dist/src/plugins/smartwallet/index.js +2 -0
  56. package/dist/src/plugins/smartwallet/smartWalletPlugin.js +824 -0
  57. package/dist/src/plugins/web3/index.js +4 -0
  58. package/dist/src/plugins/web3/types.js +1 -0
  59. package/dist/src/plugins/web3/web3Connector.js +738 -0
  60. package/dist/src/plugins/web3/web3ConnectorPlugin.js +639 -0
  61. package/dist/src/plugins/web3/web3Signer.js +432 -0
  62. package/dist/src/plugins/webauthn/index.js +3 -0
  63. package/dist/{plugins → src/plugins}/webauthn/types.js +2 -5
  64. package/dist/src/plugins/webauthn/webauthn.js +647 -0
  65. package/dist/src/plugins/webauthn/webauthnPlugin.js +689 -0
  66. package/dist/src/plugins/webauthn/webauthnSigner.js +419 -0
  67. package/dist/{plugins → src/plugins}/zkproof/index.js +3 -10
  68. package/dist/src/plugins/zkproof/types.js +1 -0
  69. package/dist/src/plugins/zkproof/zkCredentials.js +287 -0
  70. package/dist/src/plugins/zkproof/zkProofConnector.js +267 -0
  71. package/dist/src/plugins/zkproof/zkProofPlugin.js +405 -0
  72. package/dist/src/storage/storage.js +189 -0
  73. package/dist/src/utils/errorHandler.js +339 -0
  74. package/dist/{utils → src/utils}/eventEmitter.js +26 -26
  75. package/dist/{utils → src/utils}/seedPhrase.js +23 -32
  76. package/dist/{utils → src/utils}/validation.js +14 -21
  77. package/dist/tsconfig.tsbuildinfo +1 -0
  78. package/dist/types/{crypto → src/crypto}/double-ratchet.d.ts +1 -1
  79. package/dist/types/{crypto → src/crypto}/signal-protocol.d.ts +25 -0
  80. package/dist/types/{crypto → src/crypto}/types.d.ts +3 -1
  81. package/dist/types/src/examples/crypto-working-test.d.ts +1 -0
  82. package/dist/types/src/examples/double-ratchet-test.d.ts +1 -0
  83. package/dist/types/src/examples/mls-sframe-test.d.ts +1 -0
  84. package/dist/types/src/examples/random-generation-test.d.ts +1 -0
  85. package/dist/types/src/examples/signal-protocol-test.d.ts +1 -0
  86. package/dist/types/{gundb → src/gundb}/db.d.ts +14 -1
  87. package/dist/types/src/gundb/gun-es.d.ts +8 -0
  88. package/dist/types/src/gundb/min.d.ts +3 -0
  89. package/dist/types/{index.d.ts → src/index.d.ts} +1 -0
  90. package/package.json +14 -11
  91. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +0 -1651
  92. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +0 -1
  93. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +0 -1608
  94. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +0 -1
  95. package/dist/crypto/asymmetric.js +0 -99
  96. package/dist/crypto/double-ratchet.js +0 -370
  97. package/dist/crypto/file-encryption.js +0 -213
  98. package/dist/crypto/hashing.js +0 -87
  99. package/dist/crypto/index.js +0 -34
  100. package/dist/crypto/mls.js +0 -569
  101. package/dist/crypto/pgp.js +0 -390
  102. package/dist/crypto/sframe.js +0 -352
  103. package/dist/crypto/signal-protocol.js +0 -456
  104. package/dist/crypto/symmetric.js +0 -91
  105. package/dist/crypto/types.js +0 -2
  106. package/dist/crypto/utils.js +0 -140
  107. package/dist/examples/auth-test.js +0 -453
  108. package/dist/examples/crypto-identity-example.js +0 -196
  109. package/dist/examples/crypto-working-test.js +0 -83
  110. package/dist/examples/double-ratchet-test.js +0 -155
  111. package/dist/examples/mls-3-member-test.js +0 -97
  112. package/dist/examples/mls-multi-member.js +0 -153
  113. package/dist/examples/mls-sframe-test.js +0 -307
  114. package/dist/examples/mls-simple-test.js +0 -58
  115. package/dist/examples/pgp-example.js +0 -200
  116. package/dist/examples/random-generation-test.js +0 -151
  117. package/dist/examples/shogun-core-example.js +0 -150
  118. package/dist/examples/signal-protocol-test.js +0 -38
  119. package/dist/examples/zkproof-credentials-example.js +0 -217
  120. package/dist/examples/zkproof-example.js +0 -242
  121. package/dist/gundb/crypto.js +0 -306
  122. package/dist/gundb/db.js +0 -485
  123. package/dist/gundb/derive.js +0 -232
  124. package/dist/gundb/errors.js +0 -76
  125. package/dist/gundb/gun-es.js +0 -12
  126. package/dist/gundb/index.js +0 -21
  127. package/dist/gundb/min.js +0 -10
  128. package/dist/index.esm.js +0 -22
  129. package/dist/index.js +0 -47
  130. package/dist/interfaces/common.js +0 -2
  131. package/dist/interfaces/events.js +0 -40
  132. package/dist/interfaces/plugin.js +0 -2
  133. package/dist/managers/AuthManager.js +0 -226
  134. package/dist/managers/CoreInitializer.js +0 -250
  135. package/dist/managers/CryptoIdentityManager.js +0 -138
  136. package/dist/plugins/base.js +0 -50
  137. package/dist/plugins/index.js +0 -32
  138. package/dist/plugins/nostr/index.js +0 -20
  139. package/dist/plugins/nostr/nostrConnector.js +0 -419
  140. package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -453
  141. package/dist/plugins/nostr/nostrSigner.js +0 -319
  142. package/dist/plugins/nostr/types.js +0 -2
  143. package/dist/plugins/smartwallet/index.js +0 -18
  144. package/dist/plugins/smartwallet/smartWalletPlugin.js +0 -511
  145. package/dist/plugins/smartwallet/types.js +0 -2
  146. package/dist/plugins/web3/index.js +0 -20
  147. package/dist/plugins/web3/types.js +0 -2
  148. package/dist/plugins/web3/web3Connector.js +0 -533
  149. package/dist/plugins/web3/web3ConnectorPlugin.js +0 -455
  150. package/dist/plugins/web3/web3Signer.js +0 -314
  151. package/dist/plugins/webauthn/index.js +0 -19
  152. package/dist/plugins/webauthn/webauthn.js +0 -496
  153. package/dist/plugins/webauthn/webauthnPlugin.js +0 -490
  154. package/dist/plugins/webauthn/webauthnSigner.js +0 -310
  155. package/dist/plugins/zkproof/types.js +0 -2
  156. package/dist/plugins/zkproof/zkCredentials.js +0 -216
  157. package/dist/plugins/zkproof/zkProofConnector.js +0 -198
  158. package/dist/plugins/zkproof/zkProofPlugin.js +0 -272
  159. package/dist/storage/storage.js +0 -145
  160. package/dist/types/gundb/gun-es.d.ts +0 -8
  161. package/dist/utils/errorHandler.js +0 -246
  162. /package/dist/{types/examples/crypto-working-test.d.ts → src/crypto/types.js} +0 -0
  163. /package/dist/{types/gundb/min.d.ts → src/gundb/min.js} +0 -0
  164. /package/dist/{types/examples/double-ratchet-test.d.ts → src/interfaces/common.js} +0 -0
  165. /package/dist/{types/examples/mls-sframe-test.d.ts → src/interfaces/plugin.js} +0 -0
  166. /package/dist/{types/examples/random-generation-test.d.ts → src/plugins/nostr/types.js} +0 -0
  167. /package/dist/{types/examples/signal-protocol-test.d.ts → src/plugins/smartwallet/types.js} +0 -0
  168. /package/dist/types/{core.d.ts → src/core.d.ts} +0 -0
  169. /package/dist/types/{crypto → src/crypto}/asymmetric.d.ts +0 -0
  170. /package/dist/types/{crypto → src/crypto}/file-encryption.d.ts +0 -0
  171. /package/dist/types/{crypto → src/crypto}/hashing.d.ts +0 -0
  172. /package/dist/types/{crypto → src/crypto}/index.d.ts +0 -0
  173. /package/dist/types/{crypto → src/crypto}/mls-codec.d.ts +0 -0
  174. /package/dist/types/{crypto → src/crypto}/mls.d.ts +0 -0
  175. /package/dist/types/{crypto → src/crypto}/pgp.d.ts +0 -0
  176. /package/dist/types/{crypto → src/crypto}/random-generation.d.ts +0 -0
  177. /package/dist/types/{crypto → src/crypto}/sframe.d.ts +0 -0
  178. /package/dist/types/{crypto → src/crypto}/symmetric.d.ts +0 -0
  179. /package/dist/types/{crypto → src/crypto}/utils.d.ts +0 -0
  180. /package/dist/types/{examples → src/examples}/auth-test.d.ts +0 -0
  181. /package/dist/types/{examples → src/examples}/crypto-identity-example.d.ts +0 -0
  182. /package/dist/types/{examples → src/examples}/mls-3-member-test.d.ts +0 -0
  183. /package/dist/types/{examples → src/examples}/mls-multi-member.d.ts +0 -0
  184. /package/dist/types/{examples → src/examples}/mls-simple-test.d.ts +0 -0
  185. /package/dist/types/{examples → src/examples}/pgp-example.d.ts +0 -0
  186. /package/dist/types/{examples → src/examples}/shogun-core-example.d.ts +0 -0
  187. /package/dist/types/{examples → src/examples}/zkproof-credentials-example.d.ts +0 -0
  188. /package/dist/types/{examples → src/examples}/zkproof-example.d.ts +0 -0
  189. /package/dist/types/{gundb → src/gundb}/crypto.d.ts +0 -0
  190. /package/dist/types/{gundb → src/gundb}/derive.d.ts +0 -0
  191. /package/dist/types/{gundb → src/gundb}/errors.d.ts +0 -0
  192. /package/dist/types/{gundb → src/gundb}/index.d.ts +0 -0
  193. /package/dist/types/{gundb → src/gundb}/rxjs.d.ts +0 -0
  194. /package/dist/types/{gundb → src/gundb}/types.d.ts +0 -0
  195. /package/dist/types/{interfaces → src/interfaces}/common.d.ts +0 -0
  196. /package/dist/types/{interfaces → src/interfaces}/events.d.ts +0 -0
  197. /package/dist/types/{interfaces → src/interfaces}/plugin.d.ts +0 -0
  198. /package/dist/types/{interfaces → src/interfaces}/shogun.d.ts +0 -0
  199. /package/dist/types/{managers → src/managers}/AuthManager.d.ts +0 -0
  200. /package/dist/types/{managers → src/managers}/CoreInitializer.d.ts +0 -0
  201. /package/dist/types/{managers → src/managers}/CryptoIdentityManager.d.ts +0 -0
  202. /package/dist/types/{managers → src/managers}/EventManager.d.ts +0 -0
  203. /package/dist/types/{managers → src/managers}/PluginManager.d.ts +0 -0
  204. /package/dist/types/{plugins → src/plugins}/base.d.ts +0 -0
  205. /package/dist/types/{plugins → src/plugins}/index.d.ts +0 -0
  206. /package/dist/types/{plugins → src/plugins}/nostr/index.d.ts +0 -0
  207. /package/dist/types/{plugins → src/plugins}/nostr/nostrConnector.d.ts +0 -0
  208. /package/dist/types/{plugins → src/plugins}/nostr/nostrConnectorPlugin.d.ts +0 -0
  209. /package/dist/types/{plugins → src/plugins}/nostr/nostrSigner.d.ts +0 -0
  210. /package/dist/types/{plugins → src/plugins}/nostr/types.d.ts +0 -0
  211. /package/dist/types/{plugins → src/plugins}/smartwallet/index.d.ts +0 -0
  212. /package/dist/types/{plugins → src/plugins}/smartwallet/smartWalletPlugin.d.ts +0 -0
  213. /package/dist/types/{plugins → src/plugins}/smartwallet/types.d.ts +0 -0
  214. /package/dist/types/{plugins → src/plugins}/web3/index.d.ts +0 -0
  215. /package/dist/types/{plugins → src/plugins}/web3/types.d.ts +0 -0
  216. /package/dist/types/{plugins → src/plugins}/web3/web3Connector.d.ts +0 -0
  217. /package/dist/types/{plugins → src/plugins}/web3/web3ConnectorPlugin.d.ts +0 -0
  218. /package/dist/types/{plugins → src/plugins}/web3/web3Signer.d.ts +0 -0
  219. /package/dist/types/{plugins → src/plugins}/webauthn/index.d.ts +0 -0
  220. /package/dist/types/{plugins → src/plugins}/webauthn/types.d.ts +0 -0
  221. /package/dist/types/{plugins → src/plugins}/webauthn/webauthn.d.ts +0 -0
  222. /package/dist/types/{plugins → src/plugins}/webauthn/webauthnPlugin.d.ts +0 -0
  223. /package/dist/types/{plugins → src/plugins}/webauthn/webauthnSigner.d.ts +0 -0
  224. /package/dist/types/{plugins → src/plugins}/zkproof/index.d.ts +0 -0
  225. /package/dist/types/{plugins → src/plugins}/zkproof/types.d.ts +0 -0
  226. /package/dist/types/{plugins → src/plugins}/zkproof/zkCredentials.d.ts +0 -0
  227. /package/dist/types/{plugins → src/plugins}/zkproof/zkProofConnector.d.ts +0 -0
  228. /package/dist/types/{plugins → src/plugins}/zkproof/zkProofPlugin.d.ts +0 -0
  229. /package/dist/types/{storage → src/storage}/storage.d.ts +0 -0
  230. /package/dist/types/{utils → src/utils}/errorHandler.d.ts +0 -0
  231. /package/dist/types/{utils → src/utils}/eventEmitter.d.ts +0 -0
  232. /package/dist/types/{utils → src/utils}/seedPhrase.d.ts +0 -0
  233. /package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
@@ -0,0 +1,539 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __assign = (this && this.__assign) || function () {
17
+ __assign = Object.assign || function(t) {
18
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
19
+ s = arguments[i];
20
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
21
+ t[p] = s[p];
22
+ }
23
+ return t;
24
+ };
25
+ return __assign.apply(this, arguments);
26
+ };
27
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
+ return new (P || (P = Promise))(function (resolve, reject) {
30
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
34
+ });
35
+ };
36
+ var __generator = (this && this.__generator) || function (thisArg, body) {
37
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
38
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
+ function verb(n) { return function (v) { return step([n, v]); }; }
40
+ function step(op) {
41
+ if (f) throw new TypeError("Generator is already executing.");
42
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
43
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
44
+ if (y = 0, t) op = [op[0] & 2, t.value];
45
+ switch (op[0]) {
46
+ case 0: case 1: t = op; break;
47
+ case 4: _.label++; return { value: op[1], done: false };
48
+ case 5: _.label++; y = op[1]; op = [0]; continue;
49
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
+ default:
51
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
+ if (t[2]) _.ops.pop();
56
+ _.trys.pop(); continue;
57
+ }
58
+ op = body.call(thisArg, _);
59
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
61
+ }
62
+ };
63
+ /**
64
+ * The BitcoinWallet class provides functionality for connecting, signing up, and logging in using Bitcoin wallets.
65
+ * Supports Alby and Nostr extensions, as well as manual key management.
66
+ */
67
+ import { ethers } from "ethers";
68
+ import { verifyEvent, finalizeEvent, utils as nostrUtils, getEventHash, } from "nostr-tools";
69
+ import { EventEmitter } from "../../utils/eventEmitter.js";
70
+ import derive from "../../gundb/derive.js";
71
+ import { generateUsernameFromIdentity } from "../../utils/validation.js";
72
+ export var MESSAGE_TO_SIGN = "I Love Shogun!";
73
+ /**
74
+ * Class for Bitcoin wallet connections and operations
75
+ */
76
+ var NostrConnector = /** @class */ (function (_super) {
77
+ __extends(NostrConnector, _super);
78
+ function NostrConnector(config) {
79
+ if (config === void 0) { config = {}; }
80
+ var _this = _super.call(this) || this;
81
+ _this.DEFAULT_CONFIG = {
82
+ cacheDuration: 24 * 60 * 60 * 1000, // 24 hours instead of 30 minutes for better UX
83
+ maxRetries: 3,
84
+ retryDelay: 1000,
85
+ timeout: 60000,
86
+ network: "mainnet",
87
+ useApi: false,
88
+ };
89
+ _this.signatureCache = new Map();
90
+ // Connection state
91
+ _this.connectedAddress = null;
92
+ _this.connectedType = null;
93
+ _this.manualKeyPair = null;
94
+ _this.config = __assign(__assign({}, _this.DEFAULT_CONFIG), config);
95
+ _this.setupEventListeners();
96
+ return _this;
97
+ }
98
+ /**
99
+ * Setup event listeners
100
+ */
101
+ NostrConnector.prototype.setupEventListeners = function () {
102
+ // Currently no global events to listen to
103
+ // This would be the place to add listeners for wallet connections/disconnections
104
+ };
105
+ /**
106
+ * Clear signature cache for a specific address or all addresses
107
+ */
108
+ NostrConnector.prototype.clearSignatureCache = function (address) {
109
+ if (address) {
110
+ // Clear cache for specific address
111
+ this.signatureCache.delete(address);
112
+ try {
113
+ var localStorageKey = "shogun_bitcoin_sig_".concat(address);
114
+ localStorage.removeItem(localStorageKey);
115
+ console.log("Cleared signature cache for address: ".concat(address.substring(0, 10), "..."));
116
+ }
117
+ catch (error) {
118
+ console.error("Error clearing signature cache from localStorage:", error);
119
+ }
120
+ }
121
+ else {
122
+ // Clear all signature caches
123
+ this.signatureCache.clear();
124
+ try {
125
+ // Find and remove all shogun_bitcoin_sig_ keys
126
+ var keysToRemove = [];
127
+ for (var i = 0; i < localStorage.length; i++) {
128
+ var key = localStorage.key(i);
129
+ if (key && key.startsWith("shogun_bitcoin_sig_")) {
130
+ keysToRemove.push(key);
131
+ }
132
+ }
133
+ keysToRemove.forEach(function (key) { return localStorage.removeItem(key); });
134
+ console.log("Cleared all signature caches (".concat(keysToRemove.length, " entries)"));
135
+ }
136
+ catch (error) {
137
+ console.error("Error clearing all signature caches from localStorage:", error);
138
+ }
139
+ }
140
+ };
141
+ /**
142
+ * Check if Nostr extension is available
143
+ */
144
+ NostrConnector.prototype.isNostrExtensionAvailable = function () {
145
+ return typeof window !== "undefined" && !!window.nostr;
146
+ };
147
+ /**
148
+ * Check if any Bitcoin wallet is available
149
+ */
150
+ NostrConnector.prototype.isAvailable = function () {
151
+ return this.isNostrExtensionAvailable() || this.manualKeyPair !== null;
152
+ };
153
+ /**
154
+ * Connect to a wallet type
155
+ */
156
+ NostrConnector.prototype.connectWallet = function () {
157
+ return __awaiter(this, arguments, void 0, function (type) {
158
+ var result, _a, error_1;
159
+ if (type === void 0) { type = "nostr"; }
160
+ return __generator(this, function (_b) {
161
+ switch (_b.label) {
162
+ case 0:
163
+ console.log("Connecting to Bitcoin wallet via ".concat(type, "..."));
164
+ _b.label = 1;
165
+ case 1:
166
+ _b.trys.push([1, 10, , 11]);
167
+ result = void 0;
168
+ _a = type;
169
+ switch (_a) {
170
+ case "alby": return [3 /*break*/, 2];
171
+ case "nostr": return [3 /*break*/, 4];
172
+ case "manual": return [3 /*break*/, 6];
173
+ }
174
+ return [3 /*break*/, 8];
175
+ case 2:
176
+ console.log("[nostrConnector] Alby is deprecated, redirecting to Nostr");
177
+ return [4 /*yield*/, this.connectNostr()];
178
+ case 3:
179
+ result = _b.sent();
180
+ return [3 /*break*/, 9];
181
+ case 4: return [4 /*yield*/, this.connectNostr()];
182
+ case 5:
183
+ result = _b.sent();
184
+ return [3 /*break*/, 9];
185
+ case 6: return [4 /*yield*/, this.connectManual()];
186
+ case 7:
187
+ result = _b.sent();
188
+ return [3 /*break*/, 9];
189
+ case 8: throw new Error("Unsupported wallet type: ".concat(type));
190
+ case 9:
191
+ if (result.success && result.address) {
192
+ this.connectedAddress = result.address;
193
+ this.connectedType = type;
194
+ console.log("Successfully connected to ".concat(type, " wallet: ").concat(result.address));
195
+ this.emit("wallet_connected", {
196
+ address: result.address,
197
+ type: this.connectedType,
198
+ });
199
+ }
200
+ return [2 /*return*/, result];
201
+ case 10:
202
+ error_1 = _b.sent();
203
+ console.error("Error connecting to ".concat(type, " wallet:"), error_1);
204
+ return [2 /*return*/, {
205
+ success: false,
206
+ error: error_1.message || "Failed to connect to wallet",
207
+ }];
208
+ case 11: return [2 /*return*/];
209
+ }
210
+ });
211
+ });
212
+ };
213
+ /**
214
+ * Connect to Nostr extension
215
+ */
216
+ NostrConnector.prototype.connectNostr = function () {
217
+ return __awaiter(this, void 0, void 0, function () {
218
+ var pubKey, username, error_2;
219
+ return __generator(this, function (_a) {
220
+ switch (_a.label) {
221
+ case 0:
222
+ if (!this.isNostrExtensionAvailable()) {
223
+ return [2 /*return*/, {
224
+ success: false,
225
+ error: "Nostr extension is not available. Please install a Nostr compatible extension like nos2x, Alby, or Coracle.",
226
+ }];
227
+ }
228
+ _a.label = 1;
229
+ case 1:
230
+ _a.trys.push([1, 3, , 4]);
231
+ console.log("[nostrConnector] Attempting to connect to Nostr extension...");
232
+ return [4 /*yield*/, window.nostr.getPublicKey()];
233
+ case 2:
234
+ pubKey = _a.sent();
235
+ if (!pubKey) {
236
+ throw new Error("Could not get public key from Nostr extension");
237
+ }
238
+ console.log("[nostrConnector] Successfully connected to Nostr extension: ".concat(pubKey.substring(0, 10), "..."));
239
+ this.connectedAddress = pubKey;
240
+ this.connectedType = "nostr";
241
+ // Emit connected event
242
+ this.emit("connected", { address: pubKey, type: "nostr" });
243
+ username = "nostr_".concat(pubKey.substring(0, 10));
244
+ return [2 /*return*/, {
245
+ success: true,
246
+ address: pubKey,
247
+ username: username,
248
+ extensionType: "nostr",
249
+ }];
250
+ case 3:
251
+ error_2 = _a.sent();
252
+ console.error("[nostrConnector] Nostr connection error:", error_2);
253
+ // Provide more specific error messages
254
+ if (error_2.message && error_2.message.includes("User rejected")) {
255
+ throw new Error("Nostr connection was rejected by the user");
256
+ }
257
+ else if (error_2.message && error_2.message.includes("not available")) {
258
+ throw new Error("Nostr extension is not available or not properly installed");
259
+ }
260
+ else {
261
+ throw new Error("Nostr connection error: ".concat(error_2.message));
262
+ }
263
+ return [3 /*break*/, 4];
264
+ case 4: return [2 /*return*/];
265
+ }
266
+ });
267
+ });
268
+ };
269
+ /**
270
+ * Set up manual key pair for connection
271
+ */
272
+ NostrConnector.prototype.connectManual = function () {
273
+ return __awaiter(this, void 0, void 0, function () {
274
+ var username;
275
+ return __generator(this, function (_a) {
276
+ // For manual connection, we'd need to have a keypair set
277
+ if (!this.manualKeyPair) {
278
+ return [2 /*return*/, {
279
+ success: false,
280
+ error: "No manual key pair configured. Use setKeyPair() first.",
281
+ }];
282
+ }
283
+ this.connectedAddress = this.manualKeyPair.address;
284
+ this.connectedType = "manual";
285
+ // Emit connected event
286
+ this.emit("connected", {
287
+ address: this.manualKeyPair.address,
288
+ type: "manual",
289
+ });
290
+ username = "btc_".concat(this.manualKeyPair.address.substring(0, 10));
291
+ return [2 /*return*/, {
292
+ success: true,
293
+ address: this.manualKeyPair.address,
294
+ username: username,
295
+ extensionType: "manual",
296
+ }];
297
+ });
298
+ });
299
+ };
300
+ /**
301
+ * Set a manual key pair for use
302
+ */
303
+ NostrConnector.prototype.setKeyPair = function (keyPair) {
304
+ this.manualKeyPair = keyPair;
305
+ if (keyPair.address) {
306
+ this.connectedAddress = keyPair.address;
307
+ this.connectedType = "manual";
308
+ }
309
+ };
310
+ /**
311
+ * Generate credentials using Nostr: username deterministico e chiave GunDB derivata dall'address
312
+ */
313
+ NostrConnector.prototype.generateCredentials = function (address, signature, message) {
314
+ return __awaiter(this, void 0, void 0, function () {
315
+ var username, hashedAddress, salt, key;
316
+ return __generator(this, function (_a) {
317
+ switch (_a.label) {
318
+ case 0:
319
+ username = generateUsernameFromIdentity("nostr", { id: address });
320
+ hashedAddress = ethers.keccak256(ethers.toUtf8Bytes(address));
321
+ salt = "".concat(username, "_").concat(address, "_").concat(message, "_").concat(signature);
322
+ return [4 /*yield*/, derive(hashedAddress, salt, { includeP256: true })];
323
+ case 1:
324
+ key = _a.sent();
325
+ return [2 /*return*/, { username: username, key: key, message: message, signature: signature }];
326
+ }
327
+ });
328
+ });
329
+ };
330
+ /**
331
+ * Generate a password from a signature
332
+ */
333
+ NostrConnector.prototype.generatePassword = function (signature) {
334
+ return __awaiter(this, void 0, void 0, function () {
335
+ var normalizedSig, passwordHash;
336
+ return __generator(this, function (_a) {
337
+ if (!signature) {
338
+ throw new Error("Invalid signature");
339
+ }
340
+ try {
341
+ normalizedSig = signature.toLowerCase().replace(/[^a-f0-9]/g, "");
342
+ passwordHash = ethers.sha256(ethers.toUtf8Bytes(normalizedSig));
343
+ return [2 /*return*/, passwordHash];
344
+ }
345
+ catch (error) {
346
+ console.error("Error generating password:", error);
347
+ throw new Error("Failed to generate password from signature");
348
+ }
349
+ return [2 /*return*/];
350
+ });
351
+ });
352
+ };
353
+ /**
354
+ * Verify a signature
355
+ */
356
+ NostrConnector.prototype.verifySignature = function (message, signature, address) {
357
+ return __awaiter(this, void 0, void 0, function () {
358
+ var addressStr, eventData, event_1, eventData, event_2;
359
+ return __generator(this, function (_a) {
360
+ try {
361
+ addressStr = typeof address === "object"
362
+ ? address.address || JSON.stringify(address)
363
+ : String(address);
364
+ console.log("Verifying signature for address: ".concat(addressStr));
365
+ if (!signature || !message || !addressStr) {
366
+ console.error("Invalid message, signature, or address for verification");
367
+ return [2 /*return*/, false];
368
+ }
369
+ // For Nostr wallet type, use nostr-tools for verification
370
+ if (this.connectedType === "nostr" || this.connectedType === "alby") {
371
+ try {
372
+ eventData = {
373
+ kind: 1,
374
+ created_at: 0, // IMPORTANT: Use the same fixed timestamp used for signing
375
+ tags: [],
376
+ content: message,
377
+ pubkey: addressStr,
378
+ };
379
+ event_1 = __assign(__assign({}, eventData), { id: getEventHash(eventData), sig: signature });
380
+ return [2 /*return*/, verifyEvent(event_1)];
381
+ }
382
+ catch (verifyError) {
383
+ console.error("Error in Nostr signature verification:", verifyError);
384
+ return [2 /*return*/, false];
385
+ }
386
+ }
387
+ else if (this.connectedType === "manual" && this.manualKeyPair) {
388
+ console.log("[nostrConnector] Manual verification for keypair");
389
+ // For manual keypairs, we MUST use a secure verification method.
390
+ if (!this.manualKeyPair.privateKey) {
391
+ console.error("Manual verification failed: private key is missing.");
392
+ return [2 /*return*/, false];
393
+ }
394
+ try {
395
+ eventData = {
396
+ kind: 1,
397
+ created_at: 0, // IMPORTANT: Use the same fixed timestamp used for signing
398
+ tags: [],
399
+ content: message,
400
+ pubkey: addressStr,
401
+ };
402
+ event_2 = __assign(__assign({}, eventData), { id: getEventHash(eventData), sig: signature });
403
+ return [2 /*return*/, verifyEvent(event_2)];
404
+ }
405
+ catch (manualVerifyError) {
406
+ console.error("Error in manual signature verification:", manualVerifyError);
407
+ return [2 /*return*/, false];
408
+ }
409
+ }
410
+ console.warn("No specific verification method available, signature cannot be fully verified");
411
+ return [2 /*return*/, false];
412
+ }
413
+ catch (error) {
414
+ console.error("Error verifying signature:", error);
415
+ return [2 /*return*/, false];
416
+ }
417
+ return [2 /*return*/];
418
+ });
419
+ });
420
+ };
421
+ /**
422
+ * Get the currently connected address
423
+ */
424
+ NostrConnector.prototype.getConnectedAddress = function () {
425
+ return this.connectedAddress;
426
+ };
427
+ /**
428
+ * Get the currently connected wallet type
429
+ */
430
+ NostrConnector.prototype.getConnectedType = function () {
431
+ return this.connectedType;
432
+ };
433
+ /**
434
+ * Request a signature from the connected wallet
435
+ */
436
+ NostrConnector.prototype.requestSignature = function (address, message) {
437
+ return __awaiter(this, void 0, void 0, function () {
438
+ var _a, eventData, nostrEvent, signedEvent, manualEventData, eventTemplate, privateKeyBytes, signedEventManual, error_3;
439
+ return __generator(this, function (_b) {
440
+ switch (_b.label) {
441
+ case 0:
442
+ if (!this.connectedType) {
443
+ throw new Error("No wallet connected");
444
+ }
445
+ _b.label = 1;
446
+ case 1:
447
+ _b.trys.push([1, 8, , 9]);
448
+ _a = this.connectedType;
449
+ switch (_a) {
450
+ case "alby": return [3 /*break*/, 2];
451
+ case "nostr": return [3 /*break*/, 2];
452
+ case "manual": return [3 /*break*/, 4];
453
+ }
454
+ return [3 /*break*/, 6];
455
+ case 2:
456
+ if (this.connectedType === "alby") {
457
+ console.warn("Alby is deprecated, using Nostr functionality for signature request");
458
+ }
459
+ console.log("[nostrConnector] Requesting Nostr signature for message:", message);
460
+ if (!window.nostr) {
461
+ throw new Error("Nostr extension not available");
462
+ }
463
+ eventData = {
464
+ kind: 1,
465
+ created_at: 0, // IMPORTANT: Use a fixed timestamp to make signatures verifiable
466
+ tags: [],
467
+ content: message,
468
+ pubkey: address,
469
+ };
470
+ nostrEvent = __assign(__assign({}, eventData), { id: getEventHash(eventData), sig: "" });
471
+ return [4 /*yield*/, window.nostr.signEvent(nostrEvent)];
472
+ case 3:
473
+ signedEvent = _b.sent();
474
+ console.log("Received Nostr signature:", signedEvent.sig.substring(0, 20) + "...");
475
+ return [2 /*return*/, signedEvent.sig];
476
+ case 4:
477
+ console.log("[nostrConnector] Using manual key pair for signature");
478
+ if (!this.manualKeyPair || !this.manualKeyPair.privateKey) {
479
+ throw new Error("No manual key pair available or private key missing");
480
+ }
481
+ manualEventData = {
482
+ kind: 1,
483
+ created_at: 0, // IMPORTANT: Use a fixed timestamp
484
+ tags: [],
485
+ content: message,
486
+ pubkey: this.manualKeyPair.address,
487
+ };
488
+ eventTemplate = __assign(__assign({}, manualEventData), { id: getEventHash(manualEventData), sig: "" });
489
+ privateKeyBytes = nostrUtils.hexToBytes(this.manualKeyPair.privateKey);
490
+ return [4 /*yield*/, finalizeEvent(eventTemplate, privateKeyBytes)];
491
+ case 5:
492
+ signedEventManual = _b.sent();
493
+ console.log("Generated manual signature:", signedEventManual.sig.substring(0, 20) + "...");
494
+ return [2 /*return*/, signedEventManual.sig];
495
+ case 6: throw new Error("Unsupported wallet type: ".concat(this.connectedType));
496
+ case 7: return [3 /*break*/, 9];
497
+ case 8:
498
+ error_3 = _b.sent();
499
+ console.error("Error requesting signature:", error_3);
500
+ throw new Error("Failed to get signature: ".concat(error_3.message));
501
+ case 9: return [2 /*return*/];
502
+ }
503
+ });
504
+ });
505
+ };
506
+ /**
507
+ * Cleanup event listeners
508
+ */
509
+ NostrConnector.prototype.cleanup = function () {
510
+ this.removeAllListeners();
511
+ this.connectedAddress = null;
512
+ this.connectedType = null;
513
+ this.manualKeyPair = null;
514
+ };
515
+ return NostrConnector;
516
+ }(EventEmitter));
517
+ // Funzione helper per derivare chiavi Nostr/Bitcoin (come per Web3/WebAuthn)
518
+ export function deriveNostrKeys(address, signature, message) {
519
+ return __awaiter(this, void 0, void 0, function () {
520
+ var salt;
521
+ return __generator(this, function (_a) {
522
+ switch (_a.label) {
523
+ case 0:
524
+ salt = "".concat(address, "_").concat(message);
525
+ return [4 /*yield*/, derive(address, salt, {
526
+ includeP256: true,
527
+ })];
528
+ case 1: return [2 /*return*/, _a.sent()];
529
+ }
530
+ });
531
+ });
532
+ }
533
+ if (typeof window !== "undefined") {
534
+ window.NostrConnector = NostrConnector;
535
+ }
536
+ else if (typeof global !== "undefined") {
537
+ global.NostrConnector = NostrConnector;
538
+ }
539
+ export { NostrConnector };