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.
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +93 -341
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -1
- package/dist/browser/shogun-core.js +138850 -146638
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/{core.js → src/core.js} +167 -107
- package/dist/src/crypto/asymmetric.js +168 -0
- package/dist/src/crypto/double-ratchet.js +908 -0
- package/dist/src/crypto/file-encryption.js +352 -0
- package/dist/src/crypto/hashing.js +160 -0
- package/dist/src/crypto/index.js +18 -0
- package/dist/{crypto → src/crypto}/mls-codec.js +24 -34
- package/dist/src/crypto/mls.js +734 -0
- package/dist/src/crypto/pgp.js +619 -0
- package/dist/{crypto → src/crypto}/random-generation.js +125 -103
- package/dist/src/crypto/sframe.js +466 -0
- package/dist/src/crypto/signal-protocol.js +943 -0
- package/dist/src/crypto/symmetric.js +165 -0
- package/dist/src/crypto/utils.js +220 -0
- package/dist/src/examples/auth-test.js +535 -0
- package/dist/src/examples/crypto-identity-example.js +294 -0
- package/dist/src/examples/crypto-working-test.js +149 -0
- package/dist/src/examples/double-ratchet-test.js +240 -0
- package/dist/src/examples/mls-3-member-test.js +183 -0
- package/dist/src/examples/mls-multi-member.js +439 -0
- package/dist/src/examples/mls-sframe-test.js +491 -0
- package/dist/src/examples/mls-simple-test.js +122 -0
- package/dist/src/examples/pgp-example.js +354 -0
- package/dist/src/examples/random-generation-test.js +191 -0
- package/dist/src/examples/shogun-core-example.js +204 -0
- package/dist/src/examples/signal-protocol-test.js +82 -0
- package/dist/src/examples/zkproof-credentials-example.js +357 -0
- package/dist/src/examples/zkproof-example.js +357 -0
- package/dist/src/gundb/crypto.js +420 -0
- package/dist/src/gundb/db.js +728 -0
- package/dist/src/gundb/derive.js +327 -0
- package/dist/src/gundb/errors.js +115 -0
- package/dist/src/gundb/gun-es.js +8 -0
- package/dist/src/gundb/index.js +5 -0
- package/dist/{gundb → src/gundb}/rxjs.js +147 -111
- package/dist/{gundb → src/gundb}/types.js +1 -2
- package/dist/src/index.js +19 -0
- package/dist/src/interfaces/events.js +57 -0
- package/dist/{interfaces → src/interfaces}/shogun.js +4 -7
- package/dist/src/managers/AuthManager.js +301 -0
- package/dist/src/managers/CoreInitializer.js +304 -0
- package/dist/src/managers/CryptoIdentityManager.js +230 -0
- package/dist/{managers → src/managers}/EventManager.js +19 -21
- package/dist/{managers → src/managers}/PluginManager.js +123 -89
- package/dist/src/plugins/base.js +90 -0
- package/dist/src/plugins/index.js +17 -0
- package/dist/src/plugins/nostr/index.js +4 -0
- package/dist/src/plugins/nostr/nostrConnector.js +539 -0
- package/dist/src/plugins/nostr/nostrConnectorPlugin.js +663 -0
- package/dist/src/plugins/nostr/nostrSigner.js +414 -0
- package/dist/src/plugins/smartwallet/index.js +2 -0
- package/dist/src/plugins/smartwallet/smartWalletPlugin.js +824 -0
- package/dist/src/plugins/web3/index.js +4 -0
- package/dist/src/plugins/web3/types.js +1 -0
- package/dist/src/plugins/web3/web3Connector.js +738 -0
- package/dist/src/plugins/web3/web3ConnectorPlugin.js +639 -0
- package/dist/src/plugins/web3/web3Signer.js +432 -0
- package/dist/src/plugins/webauthn/index.js +3 -0
- package/dist/{plugins → src/plugins}/webauthn/types.js +2 -5
- package/dist/src/plugins/webauthn/webauthn.js +647 -0
- package/dist/src/plugins/webauthn/webauthnPlugin.js +689 -0
- package/dist/src/plugins/webauthn/webauthnSigner.js +419 -0
- package/dist/{plugins → src/plugins}/zkproof/index.js +3 -10
- package/dist/src/plugins/zkproof/types.js +1 -0
- package/dist/src/plugins/zkproof/zkCredentials.js +287 -0
- package/dist/src/plugins/zkproof/zkProofConnector.js +267 -0
- package/dist/src/plugins/zkproof/zkProofPlugin.js +405 -0
- package/dist/src/storage/storage.js +189 -0
- package/dist/src/utils/errorHandler.js +339 -0
- package/dist/{utils → src/utils}/eventEmitter.js +26 -26
- package/dist/{utils → src/utils}/seedPhrase.js +23 -32
- package/dist/{utils → src/utils}/validation.js +14 -21
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/{crypto → src/crypto}/double-ratchet.d.ts +1 -1
- package/dist/types/{crypto → src/crypto}/signal-protocol.d.ts +25 -0
- package/dist/types/{crypto → src/crypto}/types.d.ts +3 -1
- package/dist/types/src/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/src/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/src/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/src/examples/random-generation-test.d.ts +1 -0
- package/dist/types/src/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/{gundb → src/gundb}/db.d.ts +14 -1
- package/dist/types/src/gundb/gun-es.d.ts +8 -0
- package/dist/types/src/gundb/min.d.ts +3 -0
- package/dist/types/{index.d.ts → src/index.d.ts} +1 -0
- package/package.json +14 -11
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +0 -1651
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +0 -1
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +0 -1608
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +0 -1
- package/dist/crypto/asymmetric.js +0 -99
- package/dist/crypto/double-ratchet.js +0 -370
- package/dist/crypto/file-encryption.js +0 -213
- package/dist/crypto/hashing.js +0 -87
- package/dist/crypto/index.js +0 -34
- package/dist/crypto/mls.js +0 -569
- package/dist/crypto/pgp.js +0 -390
- package/dist/crypto/sframe.js +0 -352
- package/dist/crypto/signal-protocol.js +0 -456
- package/dist/crypto/symmetric.js +0 -91
- package/dist/crypto/types.js +0 -2
- package/dist/crypto/utils.js +0 -140
- package/dist/examples/auth-test.js +0 -453
- package/dist/examples/crypto-identity-example.js +0 -196
- package/dist/examples/crypto-working-test.js +0 -83
- package/dist/examples/double-ratchet-test.js +0 -155
- package/dist/examples/mls-3-member-test.js +0 -97
- package/dist/examples/mls-multi-member.js +0 -153
- package/dist/examples/mls-sframe-test.js +0 -307
- package/dist/examples/mls-simple-test.js +0 -58
- package/dist/examples/pgp-example.js +0 -200
- package/dist/examples/random-generation-test.js +0 -151
- package/dist/examples/shogun-core-example.js +0 -150
- package/dist/examples/signal-protocol-test.js +0 -38
- package/dist/examples/zkproof-credentials-example.js +0 -217
- package/dist/examples/zkproof-example.js +0 -242
- package/dist/gundb/crypto.js +0 -306
- package/dist/gundb/db.js +0 -485
- package/dist/gundb/derive.js +0 -232
- package/dist/gundb/errors.js +0 -76
- package/dist/gundb/gun-es.js +0 -12
- package/dist/gundb/index.js +0 -21
- package/dist/gundb/min.js +0 -10
- package/dist/index.esm.js +0 -22
- package/dist/index.js +0 -47
- package/dist/interfaces/common.js +0 -2
- package/dist/interfaces/events.js +0 -40
- package/dist/interfaces/plugin.js +0 -2
- package/dist/managers/AuthManager.js +0 -226
- package/dist/managers/CoreInitializer.js +0 -250
- package/dist/managers/CryptoIdentityManager.js +0 -138
- package/dist/plugins/base.js +0 -50
- package/dist/plugins/index.js +0 -32
- package/dist/plugins/nostr/index.js +0 -20
- package/dist/plugins/nostr/nostrConnector.js +0 -419
- package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -453
- package/dist/plugins/nostr/nostrSigner.js +0 -319
- package/dist/plugins/nostr/types.js +0 -2
- package/dist/plugins/smartwallet/index.js +0 -18
- package/dist/plugins/smartwallet/smartWalletPlugin.js +0 -511
- package/dist/plugins/smartwallet/types.js +0 -2
- package/dist/plugins/web3/index.js +0 -20
- package/dist/plugins/web3/types.js +0 -2
- package/dist/plugins/web3/web3Connector.js +0 -533
- package/dist/plugins/web3/web3ConnectorPlugin.js +0 -455
- package/dist/plugins/web3/web3Signer.js +0 -314
- package/dist/plugins/webauthn/index.js +0 -19
- package/dist/plugins/webauthn/webauthn.js +0 -496
- package/dist/plugins/webauthn/webauthnPlugin.js +0 -490
- package/dist/plugins/webauthn/webauthnSigner.js +0 -310
- package/dist/plugins/zkproof/types.js +0 -2
- package/dist/plugins/zkproof/zkCredentials.js +0 -216
- package/dist/plugins/zkproof/zkProofConnector.js +0 -198
- package/dist/plugins/zkproof/zkProofPlugin.js +0 -272
- package/dist/storage/storage.js +0 -145
- package/dist/types/gundb/gun-es.d.ts +0 -8
- package/dist/utils/errorHandler.js +0 -246
- /package/dist/{types/examples/crypto-working-test.d.ts → src/crypto/types.js} +0 -0
- /package/dist/{types/gundb/min.d.ts → src/gundb/min.js} +0 -0
- /package/dist/{types/examples/double-ratchet-test.d.ts → src/interfaces/common.js} +0 -0
- /package/dist/{types/examples/mls-sframe-test.d.ts → src/interfaces/plugin.js} +0 -0
- /package/dist/{types/examples/random-generation-test.d.ts → src/plugins/nostr/types.js} +0 -0
- /package/dist/{types/examples/signal-protocol-test.d.ts → src/plugins/smartwallet/types.js} +0 -0
- /package/dist/types/{core.d.ts → src/core.d.ts} +0 -0
- /package/dist/types/{crypto → src/crypto}/asymmetric.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/file-encryption.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/hashing.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/index.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/mls-codec.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/mls.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/pgp.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/random-generation.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/sframe.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/symmetric.d.ts +0 -0
- /package/dist/types/{crypto → src/crypto}/utils.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/auth-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/crypto-identity-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/mls-3-member-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/mls-multi-member.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/mls-simple-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/pgp-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/shogun-core-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/zkproof-credentials-example.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/zkproof-example.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/crypto.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/derive.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/errors.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/index.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/rxjs.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/types.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/common.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/events.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/plugin.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/shogun.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/AuthManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/CoreInitializer.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/CryptoIdentityManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/EventManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/PluginManager.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/base.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrConnector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrConnectorPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrSigner.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/smartwallet/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/smartwallet/smartWalletPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/smartwallet/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3Connector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3ConnectorPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3Signer.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthn.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthnPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthnSigner.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/zkCredentials.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/zkProofConnector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/zkproof/zkProofPlugin.d.ts +0 -0
- /package/dist/types/{storage → src/storage}/storage.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/errorHandler.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/eventEmitter.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/seedPhrase.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
|
@@ -0,0 +1,738 @@
|
|
|
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
|
+
var __values = (this && this.__values) || function(o) {
|
|
64
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
65
|
+
if (m) return m.call(o);
|
|
66
|
+
if (o && typeof o.length === "number") return {
|
|
67
|
+
next: function () {
|
|
68
|
+
if (o && i >= o.length) o = void 0;
|
|
69
|
+
return { value: o && o[i++], done: !o };
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The MetaMaskAuth class provides functionality for connecting, signing up, and logging in using MetaMask.
|
|
76
|
+
*/
|
|
77
|
+
import { ethers } from "ethers";
|
|
78
|
+
import { ErrorHandler, ErrorType } from "../../utils/errorHandler.js";
|
|
79
|
+
import { EventEmitter } from "../../utils/eventEmitter.js";
|
|
80
|
+
import derive from "../../gundb/derive.js";
|
|
81
|
+
/**
|
|
82
|
+
* Class for MetaMask connection
|
|
83
|
+
*/
|
|
84
|
+
var Web3Connector = /** @class */ (function (_super) {
|
|
85
|
+
__extends(Web3Connector, _super);
|
|
86
|
+
function Web3Connector(config) {
|
|
87
|
+
if (config === void 0) { config = {}; }
|
|
88
|
+
var _this = _super.call(this) || this;
|
|
89
|
+
_this.MESSAGE_TO_SIGN = "I Love Shogun!";
|
|
90
|
+
_this.DEFAULT_CONFIG = {
|
|
91
|
+
cacheDuration: 30 * 60 * 1000, // 30 minutes
|
|
92
|
+
maxRetries: 3,
|
|
93
|
+
retryDelay: 1000,
|
|
94
|
+
timeout: 60000,
|
|
95
|
+
};
|
|
96
|
+
_this.signatureCache = new Map();
|
|
97
|
+
_this.provider = null;
|
|
98
|
+
_this.customProvider = null;
|
|
99
|
+
_this.customWallet = null;
|
|
100
|
+
_this.config = __assign(__assign({}, _this.DEFAULT_CONFIG), config);
|
|
101
|
+
_this.initProvider();
|
|
102
|
+
_this.setupEventListeners();
|
|
103
|
+
return _this;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Initialize the provider synchronously with fallback mechanisms
|
|
107
|
+
* to handle conflicts between multiple wallet providers
|
|
108
|
+
*/
|
|
109
|
+
Web3Connector.prototype.initProvider = function () {
|
|
110
|
+
if (typeof window !== "undefined") {
|
|
111
|
+
try {
|
|
112
|
+
// Check if ethereum is available from any provider
|
|
113
|
+
var ethereumProvider = this.getAvailableEthereumProvider();
|
|
114
|
+
if (ethereumProvider) {
|
|
115
|
+
this.provider = new ethers.BrowserProvider(ethereumProvider);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
console.warn("No compatible Ethereum provider found");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
console.error("Failed to initialize BrowserProvider", error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
console.warn("Window object not available (non-browser environment)");
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Get available Ethereum provider from multiple possible sources
|
|
131
|
+
*/
|
|
132
|
+
Web3Connector.prototype.getAvailableEthereumProvider = function () {
|
|
133
|
+
var e_1, _a;
|
|
134
|
+
if (typeof window === "undefined")
|
|
135
|
+
return undefined;
|
|
136
|
+
// Define provider sources with priority order
|
|
137
|
+
var providerSources = [
|
|
138
|
+
// Check if we have providers in the _ethereumProviders registry (from index.html)
|
|
139
|
+
{
|
|
140
|
+
source: function () { return window._ethereumProviders && window._ethereumProviders[0]; },
|
|
141
|
+
name: "Registry Primary",
|
|
142
|
+
},
|
|
143
|
+
{ source: function () { return window.ethereum; }, name: "Standard ethereum" },
|
|
144
|
+
{
|
|
145
|
+
source: function () { var _a; return (_a = window.web3) === null || _a === void 0 ? void 0 : _a.currentProvider; },
|
|
146
|
+
name: "Legacy web3",
|
|
147
|
+
},
|
|
148
|
+
{ source: function () { return window.metamask; }, name: "MetaMask specific" },
|
|
149
|
+
{
|
|
150
|
+
source: function () { var _a, _b; return (_b = (_a = window.ethereum) === null || _a === void 0 ? void 0 : _a.providers) === null || _b === void 0 ? void 0 : _b.find(function (p) { return p.isMetaMask; }); },
|
|
151
|
+
name: "MetaMask from providers array",
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
source: function () { var _a, _b; return (_b = (_a = window.ethereum) === null || _a === void 0 ? void 0 : _a.providers) === null || _b === void 0 ? void 0 : _b[0]; },
|
|
155
|
+
name: "First provider in array",
|
|
156
|
+
},
|
|
157
|
+
// Try known provider names
|
|
158
|
+
{
|
|
159
|
+
source: function () { var _a, _b; return (_b = (_a = window.enkrypt) === null || _a === void 0 ? void 0 : _a.providers) === null || _b === void 0 ? void 0 : _b.ethereum; },
|
|
160
|
+
name: "Enkrypt",
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
source: function () { return window.coinbaseWalletExtension; },
|
|
164
|
+
name: "Coinbase",
|
|
165
|
+
},
|
|
166
|
+
{ source: function () { return window.trustWallet; }, name: "Trust Wallet" },
|
|
167
|
+
// Use special registry if available
|
|
168
|
+
{
|
|
169
|
+
source: function () {
|
|
170
|
+
return Array.isArray(window._ethereumProviders)
|
|
171
|
+
? window._ethereumProviders.find(function (p) { return !p._isProxy; })
|
|
172
|
+
: undefined;
|
|
173
|
+
},
|
|
174
|
+
name: "Registry non-proxy",
|
|
175
|
+
},
|
|
176
|
+
];
|
|
177
|
+
try {
|
|
178
|
+
// Try each provider source
|
|
179
|
+
for (var providerSources_1 = __values(providerSources), providerSources_1_1 = providerSources_1.next(); !providerSources_1_1.done; providerSources_1_1 = providerSources_1.next()) {
|
|
180
|
+
var _b = providerSources_1_1.value, source = _b.source, name_1 = _b.name;
|
|
181
|
+
try {
|
|
182
|
+
var provider = source();
|
|
183
|
+
if (provider && typeof provider.request === "function") {
|
|
184
|
+
return provider;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
// Continue to next provider source
|
|
189
|
+
console.warn("Error checking provider ".concat(name_1, ":"), error);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
195
|
+
finally {
|
|
196
|
+
try {
|
|
197
|
+
if (providerSources_1_1 && !providerSources_1_1.done && (_a = providerSources_1.return)) _a.call(providerSources_1);
|
|
198
|
+
}
|
|
199
|
+
finally { if (e_1) throw e_1.error; }
|
|
200
|
+
}
|
|
201
|
+
// No provider found
|
|
202
|
+
console.warn("No compatible Ethereum provider found");
|
|
203
|
+
return undefined;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Initialize the BrowserProvider (async method for explicit calls)
|
|
207
|
+
*/
|
|
208
|
+
Web3Connector.prototype.setupProvider = function () {
|
|
209
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
210
|
+
var ethereumProvider;
|
|
211
|
+
return __generator(this, function (_a) {
|
|
212
|
+
try {
|
|
213
|
+
if (typeof window !== "undefined") {
|
|
214
|
+
ethereumProvider = this.getAvailableEthereumProvider();
|
|
215
|
+
if (ethereumProvider) {
|
|
216
|
+
this.provider = new ethers.BrowserProvider(ethereumProvider);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
console.warn("No compatible Ethereum provider found");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
console.warn("Window object not available (non-browser environment)");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
console.error("Failed to initialize BrowserProvider", error);
|
|
228
|
+
}
|
|
229
|
+
return [2 /*return*/];
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* Setup MetaMask event listeners using BrowserProvider
|
|
235
|
+
*/
|
|
236
|
+
Web3Connector.prototype.setupEventListeners = function () {
|
|
237
|
+
var _this = this;
|
|
238
|
+
if (this.provider) {
|
|
239
|
+
// Listen for network changes through ethers provider
|
|
240
|
+
this.provider.on("network", function (newNetwork, oldNetwork) {
|
|
241
|
+
_this.emit("chainChanged", newNetwork);
|
|
242
|
+
});
|
|
243
|
+
// Listen for account changes through the detected provider
|
|
244
|
+
try {
|
|
245
|
+
var ethereumProvider = this.getAvailableEthereumProvider();
|
|
246
|
+
if (ethereumProvider === null || ethereumProvider === void 0 ? void 0 : ethereumProvider.on) {
|
|
247
|
+
ethereumProvider.on("accountsChanged", function (accounts) {
|
|
248
|
+
_this.emit("accountsChanged", accounts);
|
|
249
|
+
});
|
|
250
|
+
// Also listen for chainChanged events directly
|
|
251
|
+
ethereumProvider.on("chainChanged", function (chainId) {
|
|
252
|
+
_this.emit("chainChanged", { chainId: chainId });
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
console.warn("Failed to setup account change listeners", error);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Cleanup event listeners
|
|
263
|
+
*/
|
|
264
|
+
Web3Connector.prototype.cleanup = function () {
|
|
265
|
+
if (this.provider) {
|
|
266
|
+
this.provider.removeAllListeners();
|
|
267
|
+
}
|
|
268
|
+
this.removeAllListeners();
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Get cached signature if valid
|
|
272
|
+
*/
|
|
273
|
+
Web3Connector.prototype.getCachedSignature = function (address) {
|
|
274
|
+
var cached = this.signatureCache.get(address);
|
|
275
|
+
if (!cached)
|
|
276
|
+
return null;
|
|
277
|
+
var now = Date.now();
|
|
278
|
+
if (now - cached.timestamp > this.config.cacheDuration) {
|
|
279
|
+
this.signatureCache.delete(address);
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
// Check for invalid/empty signature
|
|
283
|
+
if (!cached.signature ||
|
|
284
|
+
typeof cached.signature !== "string" ||
|
|
285
|
+
cached.signature.length < 16) {
|
|
286
|
+
console.warn("Invalid cached signature for address ".concat(address, " (length: ").concat(cached.signature ? cached.signature.length : 0, "), deleting from cache."));
|
|
287
|
+
this.signatureCache.delete(address);
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
return cached.signature;
|
|
291
|
+
};
|
|
292
|
+
/**
|
|
293
|
+
* Cache signature
|
|
294
|
+
*/
|
|
295
|
+
Web3Connector.prototype.cacheSignature = function (address, signature) {
|
|
296
|
+
this.signatureCache.set(address, {
|
|
297
|
+
signature: signature,
|
|
298
|
+
timestamp: Date.now(),
|
|
299
|
+
address: address,
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
/**
|
|
303
|
+
* Validates that the address is valid
|
|
304
|
+
*/
|
|
305
|
+
Web3Connector.prototype.validateAddress = function (address) {
|
|
306
|
+
if (!address) {
|
|
307
|
+
throw new Error("Address not provided");
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
var normalizedAddress = String(address).trim().toLowerCase();
|
|
311
|
+
if (!ethers.isAddress(normalizedAddress)) {
|
|
312
|
+
throw new Error("Invalid address format");
|
|
313
|
+
}
|
|
314
|
+
return ethers.getAddress(normalizedAddress);
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
ErrorHandler.handle(ErrorType.VALIDATION, "INVALID_ADDRESS", "Invalid Ethereum address provided", error);
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
/**
|
|
322
|
+
* Connects to MetaMask with retry logic using BrowserProvider
|
|
323
|
+
*/
|
|
324
|
+
Web3Connector.prototype.connectMetaMask = function () {
|
|
325
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
326
|
+
var ethereumProvider, accounts, requestError_1, fallbackError_1, attempt, signer, address, error_1, error_2;
|
|
327
|
+
var _this = this;
|
|
328
|
+
var _a;
|
|
329
|
+
return __generator(this, function (_b) {
|
|
330
|
+
switch (_b.label) {
|
|
331
|
+
case 0:
|
|
332
|
+
_b.trys.push([0, 19, , 20]);
|
|
333
|
+
if (!this.provider) {
|
|
334
|
+
this.initProvider();
|
|
335
|
+
if (!this.provider) {
|
|
336
|
+
throw new Error("MetaMask is not available. Please install MetaMask extension.");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
ethereumProvider = this.getAvailableEthereumProvider();
|
|
340
|
+
if (!ethereumProvider) {
|
|
341
|
+
throw new Error("No compatible Ethereum provider found");
|
|
342
|
+
}
|
|
343
|
+
accounts = [];
|
|
344
|
+
_b.label = 1;
|
|
345
|
+
case 1:
|
|
346
|
+
_b.trys.push([1, 3, , 10]);
|
|
347
|
+
return [4 /*yield*/, ethereumProvider.request({
|
|
348
|
+
method: "eth_requestAccounts",
|
|
349
|
+
})];
|
|
350
|
+
case 2:
|
|
351
|
+
// Try the provider we found first
|
|
352
|
+
accounts = _b.sent();
|
|
353
|
+
return [3 /*break*/, 10];
|
|
354
|
+
case 3:
|
|
355
|
+
requestError_1 = _b.sent();
|
|
356
|
+
console.warn("First account request failed, trying window.ethereum:", requestError_1);
|
|
357
|
+
if (!(window.ethereum && window.ethereum !== ethereumProvider)) return [3 /*break*/, 8];
|
|
358
|
+
_b.label = 4;
|
|
359
|
+
case 4:
|
|
360
|
+
_b.trys.push([4, 6, , 7]);
|
|
361
|
+
return [4 /*yield*/, window.ethereum.request({
|
|
362
|
+
method: "eth_requestAccounts",
|
|
363
|
+
})];
|
|
364
|
+
case 5:
|
|
365
|
+
accounts = _b.sent();
|
|
366
|
+
return [3 /*break*/, 7];
|
|
367
|
+
case 6:
|
|
368
|
+
fallbackError_1 = _b.sent();
|
|
369
|
+
console.error("All account request methods failed", fallbackError_1);
|
|
370
|
+
throw new Error("User denied account access");
|
|
371
|
+
case 7: return [3 /*break*/, 9];
|
|
372
|
+
case 8: throw new Error("User denied account access");
|
|
373
|
+
case 9: return [3 /*break*/, 10];
|
|
374
|
+
case 10:
|
|
375
|
+
if (!accounts || accounts.length === 0) {
|
|
376
|
+
}
|
|
377
|
+
attempt = 1;
|
|
378
|
+
_b.label = 11;
|
|
379
|
+
case 11:
|
|
380
|
+
if (!(attempt <= this.config.maxRetries)) return [3 /*break*/, 18];
|
|
381
|
+
_b.label = 12;
|
|
382
|
+
case 12:
|
|
383
|
+
_b.trys.push([12, 15, , 17]);
|
|
384
|
+
return [4 /*yield*/, this.provider.getSigner()];
|
|
385
|
+
case 13:
|
|
386
|
+
signer = _b.sent();
|
|
387
|
+
return [4 /*yield*/, signer.getAddress()];
|
|
388
|
+
case 14:
|
|
389
|
+
address = _b.sent();
|
|
390
|
+
if (!address) {
|
|
391
|
+
console.error("No address returned from signer");
|
|
392
|
+
throw new Error("No address returned from signer");
|
|
393
|
+
}
|
|
394
|
+
this.emit("connected", { address: address });
|
|
395
|
+
return [2 /*return*/, {
|
|
396
|
+
success: true,
|
|
397
|
+
address: address,
|
|
398
|
+
}];
|
|
399
|
+
case 15:
|
|
400
|
+
error_1 = _b.sent();
|
|
401
|
+
console.error("Attempt ".concat(attempt, " failed:"), error_1);
|
|
402
|
+
if (attempt === this.config.maxRetries) {
|
|
403
|
+
throw error_1;
|
|
404
|
+
}
|
|
405
|
+
// Wait before retrying
|
|
406
|
+
return [4 /*yield*/, new Promise(function (resolve) {
|
|
407
|
+
return setTimeout(resolve, _this.config.retryDelay);
|
|
408
|
+
})];
|
|
409
|
+
case 16:
|
|
410
|
+
// Wait before retrying
|
|
411
|
+
_b.sent();
|
|
412
|
+
return [3 /*break*/, 17];
|
|
413
|
+
case 17:
|
|
414
|
+
attempt++;
|
|
415
|
+
return [3 /*break*/, 11];
|
|
416
|
+
case 18: throw new Error("Failed to get signer after all attempts");
|
|
417
|
+
case 19:
|
|
418
|
+
error_2 = _b.sent();
|
|
419
|
+
console.error("Failed to connect to MetaMask:", error_2);
|
|
420
|
+
ErrorHandler.handle(ErrorType.WEBAUTHN, "METAMASK_CONNECTION_ERROR", (_a = error_2.message) !== null && _a !== void 0 ? _a : "Unknown error while connecting to MetaMask", error_2);
|
|
421
|
+
return [2 /*return*/, { success: false, error: error_2.message }];
|
|
422
|
+
case 20: return [2 /*return*/];
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
};
|
|
427
|
+
/**
|
|
428
|
+
* Generates credentials for the given address
|
|
429
|
+
*/
|
|
430
|
+
Web3Connector.prototype.generateCredentials = function (address) {
|
|
431
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
432
|
+
var validAddress, cachedSignature, signature, signingError_1, error_3;
|
|
433
|
+
var _a;
|
|
434
|
+
return __generator(this, function (_b) {
|
|
435
|
+
switch (_b.label) {
|
|
436
|
+
case 0:
|
|
437
|
+
_b.trys.push([0, 5, , 6]);
|
|
438
|
+
validAddress = this.validateAddress(address);
|
|
439
|
+
cachedSignature = this.getCachedSignature(validAddress);
|
|
440
|
+
if (cachedSignature) {
|
|
441
|
+
return [2 /*return*/, this.generateCredentialsFromSignature(validAddress, cachedSignature)];
|
|
442
|
+
}
|
|
443
|
+
signature = void 0;
|
|
444
|
+
_b.label = 1;
|
|
445
|
+
case 1:
|
|
446
|
+
_b.trys.push([1, 3, , 4]);
|
|
447
|
+
return [4 /*yield*/, this.requestSignatureWithTimeout(validAddress, this.MESSAGE_TO_SIGN, this.config.timeout)];
|
|
448
|
+
case 2:
|
|
449
|
+
signature = _b.sent();
|
|
450
|
+
return [3 /*break*/, 4];
|
|
451
|
+
case 3:
|
|
452
|
+
signingError_1 = _b.sent();
|
|
453
|
+
// Gestione del fallimento di firma
|
|
454
|
+
console.warn("Failed to get signature: ".concat(signingError_1, ". Using fallback method."));
|
|
455
|
+
throw signingError_1;
|
|
456
|
+
case 4:
|
|
457
|
+
// Cache the signature
|
|
458
|
+
this.cacheSignature(validAddress, signature);
|
|
459
|
+
return [2 /*return*/, this.generateCredentialsFromSignature(validAddress, signature)];
|
|
460
|
+
case 5:
|
|
461
|
+
error_3 = _b.sent();
|
|
462
|
+
ErrorHandler.handle(ErrorType.WEBAUTHN, "CREDENTIALS_GENERATION_ERROR", (_a = error_3.message) !== null && _a !== void 0 ? _a : "Error generating MetaMask credentials", error_3);
|
|
463
|
+
throw error_3;
|
|
464
|
+
case 6: return [2 /*return*/];
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
};
|
|
469
|
+
/**
|
|
470
|
+
* Generates credentials from a signature
|
|
471
|
+
*/
|
|
472
|
+
Web3Connector.prototype.generateCredentialsFromSignature = function (address, signature) {
|
|
473
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
474
|
+
var hashedAddress, salt;
|
|
475
|
+
return __generator(this, function (_a) {
|
|
476
|
+
switch (_a.label) {
|
|
477
|
+
case 0:
|
|
478
|
+
hashedAddress = ethers.keccak256(ethers.toUtf8Bytes(address));
|
|
479
|
+
salt = "".concat(address, "_").concat(signature);
|
|
480
|
+
return [4 /*yield*/, derive(hashedAddress, salt, {
|
|
481
|
+
includeP256: true,
|
|
482
|
+
})];
|
|
483
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
};
|
|
488
|
+
/**
|
|
489
|
+
* Generates fallback credentials (for testing/development)
|
|
490
|
+
*/
|
|
491
|
+
Web3Connector.prototype.generateFallbackCredentials = function (address) {
|
|
492
|
+
console.warn("Using fallback credentials generation for address:", address);
|
|
493
|
+
// Generate a deterministic but insecure fallback
|
|
494
|
+
var fallbackSignature = ethers.keccak256(ethers.toUtf8Bytes(address + "fallback"));
|
|
495
|
+
return {
|
|
496
|
+
username: address.toLowerCase(),
|
|
497
|
+
password: fallbackSignature,
|
|
498
|
+
message: this.MESSAGE_TO_SIGN,
|
|
499
|
+
signature: fallbackSignature,
|
|
500
|
+
};
|
|
501
|
+
};
|
|
502
|
+
/**
|
|
503
|
+
* Checks if MetaMask is available
|
|
504
|
+
*/
|
|
505
|
+
Web3Connector.isMetaMaskAvailable = function () {
|
|
506
|
+
var e_2, _a;
|
|
507
|
+
if (typeof window === "undefined") {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
// Check multiple possible sources
|
|
511
|
+
var sources = [
|
|
512
|
+
function () { return window.ethereum; },
|
|
513
|
+
function () { var _a; return (_a = window.web3) === null || _a === void 0 ? void 0 : _a.currentProvider; },
|
|
514
|
+
function () { return window.metamask; },
|
|
515
|
+
function () { var _a; return (_a = window._ethereumProviders) === null || _a === void 0 ? void 0 : _a[0]; },
|
|
516
|
+
];
|
|
517
|
+
try {
|
|
518
|
+
for (var sources_1 = __values(sources), sources_1_1 = sources_1.next(); !sources_1_1.done; sources_1_1 = sources_1.next()) {
|
|
519
|
+
var source = sources_1_1.value;
|
|
520
|
+
try {
|
|
521
|
+
var provider = source();
|
|
522
|
+
if (provider && typeof provider.request === "function") {
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
catch (_b) {
|
|
527
|
+
// Continue to next source
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
532
|
+
finally {
|
|
533
|
+
try {
|
|
534
|
+
if (sources_1_1 && !sources_1_1.done && (_a = sources_1.return)) _a.call(sources_1);
|
|
535
|
+
}
|
|
536
|
+
finally { if (e_2) throw e_2.error; }
|
|
537
|
+
}
|
|
538
|
+
return false;
|
|
539
|
+
};
|
|
540
|
+
/**
|
|
541
|
+
* Requests signature with timeout
|
|
542
|
+
*/
|
|
543
|
+
Web3Connector.prototype.requestSignatureWithTimeout = function (address, message, timeout) {
|
|
544
|
+
var _this = this;
|
|
545
|
+
if (timeout === void 0) { timeout = 30000; }
|
|
546
|
+
return new Promise(function (resolve, reject) {
|
|
547
|
+
var timeoutId = setTimeout(function () {
|
|
548
|
+
reject(new Error("Signature request timed out"));
|
|
549
|
+
}, timeout);
|
|
550
|
+
var cleanup = function () {
|
|
551
|
+
clearTimeout(timeoutId);
|
|
552
|
+
};
|
|
553
|
+
var errorHandler = function (error) {
|
|
554
|
+
cleanup();
|
|
555
|
+
reject(error);
|
|
556
|
+
};
|
|
557
|
+
var initializeAndSign = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
558
|
+
var signer, signerAddress, signature, error_4;
|
|
559
|
+
return __generator(this, function (_a) {
|
|
560
|
+
switch (_a.label) {
|
|
561
|
+
case 0:
|
|
562
|
+
_a.trys.push([0, 4, , 5]);
|
|
563
|
+
return [4 /*yield*/, this.provider.getSigner()];
|
|
564
|
+
case 1:
|
|
565
|
+
signer = _a.sent();
|
|
566
|
+
return [4 /*yield*/, signer.getAddress()];
|
|
567
|
+
case 2:
|
|
568
|
+
signerAddress = _a.sent();
|
|
569
|
+
// Verify the signer address matches the expected address
|
|
570
|
+
if (signerAddress.toLowerCase() !== address.toLowerCase()) {
|
|
571
|
+
throw new Error("Signer address (".concat(signerAddress, ") does not match expected address (").concat(address, ")"));
|
|
572
|
+
}
|
|
573
|
+
return [4 /*yield*/, signer.signMessage(message)];
|
|
574
|
+
case 3:
|
|
575
|
+
signature = _a.sent();
|
|
576
|
+
cleanup();
|
|
577
|
+
resolve(signature);
|
|
578
|
+
return [3 /*break*/, 5];
|
|
579
|
+
case 4:
|
|
580
|
+
error_4 = _a.sent();
|
|
581
|
+
console.error("Failed to request signature:", error_4);
|
|
582
|
+
errorHandler(error_4);
|
|
583
|
+
return [3 /*break*/, 5];
|
|
584
|
+
case 5: return [2 /*return*/];
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}); };
|
|
588
|
+
initializeAndSign();
|
|
589
|
+
});
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
592
|
+
* Checks if the connector is available
|
|
593
|
+
*/
|
|
594
|
+
Web3Connector.prototype.isAvailable = function () {
|
|
595
|
+
return Web3Connector.isMetaMaskAvailable();
|
|
596
|
+
};
|
|
597
|
+
/**
|
|
598
|
+
* Sets a custom provider for testing/development
|
|
599
|
+
*/
|
|
600
|
+
Web3Connector.prototype.setCustomProvider = function (rpcUrl, privateKey) {
|
|
601
|
+
var _a;
|
|
602
|
+
try {
|
|
603
|
+
this.customProvider = new ethers.JsonRpcProvider(rpcUrl);
|
|
604
|
+
this.customWallet = new ethers.Wallet(privateKey, this.customProvider);
|
|
605
|
+
}
|
|
606
|
+
catch (error) {
|
|
607
|
+
throw new Error("Error configuring provider: ".concat((_a = error.message) !== null && _a !== void 0 ? _a : "Unknown error"));
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
/**
|
|
611
|
+
* Get active signer instance using BrowserProvider
|
|
612
|
+
*/
|
|
613
|
+
Web3Connector.prototype.getSigner = function () {
|
|
614
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
615
|
+
var error_5;
|
|
616
|
+
return __generator(this, function (_a) {
|
|
617
|
+
switch (_a.label) {
|
|
618
|
+
case 0:
|
|
619
|
+
_a.trys.push([0, 2, , 3]);
|
|
620
|
+
if (this.customWallet) {
|
|
621
|
+
return [2 /*return*/, this.customWallet];
|
|
622
|
+
}
|
|
623
|
+
if (!this.provider) {
|
|
624
|
+
this.initProvider();
|
|
625
|
+
}
|
|
626
|
+
if (!this.provider) {
|
|
627
|
+
throw new Error("Provider not initialized");
|
|
628
|
+
}
|
|
629
|
+
return [4 /*yield*/, this.provider.getSigner()];
|
|
630
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
631
|
+
case 2:
|
|
632
|
+
error_5 = _a.sent();
|
|
633
|
+
throw new Error("Unable to get Ethereum signer: ".concat(error_5.message || "Unknown error"));
|
|
634
|
+
case 3: return [2 /*return*/];
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
};
|
|
639
|
+
/**
|
|
640
|
+
* Get active provider instance using BrowserProvider
|
|
641
|
+
*/
|
|
642
|
+
Web3Connector.prototype.getProvider = function () {
|
|
643
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
644
|
+
return __generator(this, function (_a) {
|
|
645
|
+
if (this.customProvider) {
|
|
646
|
+
return [2 /*return*/, this.customProvider];
|
|
647
|
+
}
|
|
648
|
+
if (!this.provider) {
|
|
649
|
+
this.initProvider();
|
|
650
|
+
}
|
|
651
|
+
return [2 /*return*/, this.provider];
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
};
|
|
655
|
+
/**
|
|
656
|
+
* Generate deterministic password from signature
|
|
657
|
+
* @param signature - Cryptographic signature
|
|
658
|
+
* @returns 64-character hex string
|
|
659
|
+
* @throws {Error} For invalid signature
|
|
660
|
+
*/
|
|
661
|
+
Web3Connector.prototype.generatePassword = function (signature) {
|
|
662
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
663
|
+
var hash;
|
|
664
|
+
return __generator(this, function (_a) {
|
|
665
|
+
if (!signature) {
|
|
666
|
+
throw new Error("Invalid signature");
|
|
667
|
+
}
|
|
668
|
+
hash = ethers.keccak256(ethers.toUtf8Bytes(signature));
|
|
669
|
+
return [2 /*return*/, hash.slice(2, 66)]; // Remove 0x and use first 32 bytes
|
|
670
|
+
});
|
|
671
|
+
});
|
|
672
|
+
};
|
|
673
|
+
/**
|
|
674
|
+
* Verify message signature
|
|
675
|
+
* @param message - Original signed message
|
|
676
|
+
* @param signature - Cryptographic signature
|
|
677
|
+
* @returns Recovered Ethereum address
|
|
678
|
+
* @throws {Error} For invalid inputs
|
|
679
|
+
*/
|
|
680
|
+
Web3Connector.prototype.verifySignature = function (message, signature) {
|
|
681
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
682
|
+
return __generator(this, function (_a) {
|
|
683
|
+
if (!message || !signature) {
|
|
684
|
+
throw new Error("Invalid message or signature");
|
|
685
|
+
}
|
|
686
|
+
try {
|
|
687
|
+
return [2 /*return*/, ethers.verifyMessage(message, signature)];
|
|
688
|
+
}
|
|
689
|
+
catch (error) {
|
|
690
|
+
throw new Error("Invalid message or signature");
|
|
691
|
+
}
|
|
692
|
+
return [2 /*return*/];
|
|
693
|
+
});
|
|
694
|
+
});
|
|
695
|
+
};
|
|
696
|
+
/**
|
|
697
|
+
* Get browser-based Ethereum signer
|
|
698
|
+
* @returns Browser provider signer
|
|
699
|
+
* @throws {Error} If MetaMask not detected
|
|
700
|
+
*/
|
|
701
|
+
Web3Connector.prototype.getEthereumSigner = function () {
|
|
702
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
703
|
+
var ethereum, provider, error_6;
|
|
704
|
+
var _a;
|
|
705
|
+
return __generator(this, function (_b) {
|
|
706
|
+
switch (_b.label) {
|
|
707
|
+
case 0:
|
|
708
|
+
if (!Web3Connector.isMetaMaskAvailable()) {
|
|
709
|
+
throw new Error("MetaMask not found. Please install MetaMask to continue.");
|
|
710
|
+
}
|
|
711
|
+
_b.label = 1;
|
|
712
|
+
case 1:
|
|
713
|
+
_b.trys.push([1, 3, , 4]);
|
|
714
|
+
ethereum = window.ethereum;
|
|
715
|
+
return [4 /*yield*/, ethereum.request({
|
|
716
|
+
method: "eth_requestAccounts",
|
|
717
|
+
})];
|
|
718
|
+
case 2:
|
|
719
|
+
_b.sent();
|
|
720
|
+
provider = new ethers.BrowserProvider(ethereum);
|
|
721
|
+
return [2 /*return*/, provider.getSigner()];
|
|
722
|
+
case 3:
|
|
723
|
+
error_6 = _b.sent();
|
|
724
|
+
throw new Error("Error accessing MetaMask: ".concat((_a = error_6.message) !== null && _a !== void 0 ? _a : "Unknown error"));
|
|
725
|
+
case 4: return [2 /*return*/];
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
});
|
|
729
|
+
};
|
|
730
|
+
return Web3Connector;
|
|
731
|
+
}(EventEmitter));
|
|
732
|
+
if (typeof window !== "undefined") {
|
|
733
|
+
window.Web3Connector = Web3Connector;
|
|
734
|
+
}
|
|
735
|
+
else if (typeof global !== "undefined") {
|
|
736
|
+
global.Web3Connector = Web3Connector;
|
|
737
|
+
}
|
|
738
|
+
export { Web3Connector };
|