@monygroupcorp/micro-web3 1.2.2 → 1.2.4
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/micro-web3.cjs +1 -1
- package/dist/micro-web3.cjs.map +1 -1
- package/dist/micro-web3.esm.js +1 -1
- package/dist/micro-web3.esm.js.map +1 -1
- package/dist/micro-web3.umd.js +2 -2
- package/dist/micro-web3.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/services/WalletService.js +105 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monygroupcorp/micro-web3",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "A lean, reusable Web3 toolkit with components for wallet connection, IPFS, and common Web3 UI patterns.",
|
|
5
5
|
"main": "dist/micro-web3.cjs",
|
|
6
6
|
"module": "dist/micro-web3.esm.js",
|
|
@@ -56,44 +56,77 @@ class WalletService {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
|
-
* Initialize the wallet service
|
|
59
|
+
* Initialize the wallet service.
|
|
60
|
+
* Automatically reconnects to previously authorized wallets by default.
|
|
60
61
|
* @param {Object} options - Initialization options
|
|
61
|
-
* @param {boolean} options.autoReconnect -
|
|
62
|
+
* @param {boolean} [options.autoReconnect=true] - Set to false to disable auto-reconnect
|
|
62
63
|
* @returns {Promise<boolean>} True if initialized successfully
|
|
63
64
|
*/
|
|
64
65
|
async initialize(options = {}) {
|
|
66
|
+
const { autoReconnect = true } = options;
|
|
67
|
+
|
|
65
68
|
try {
|
|
66
69
|
console.log('Initializing WalletService...');
|
|
67
70
|
|
|
68
71
|
// Check if window.ethereum exists
|
|
69
|
-
if (typeof window.ethereum
|
|
70
|
-
// Log that we found a wallet provider
|
|
71
|
-
console.log('Found Ethereum provider');
|
|
72
|
-
|
|
73
|
-
// Let other components know a wallet was detected
|
|
74
|
-
this.eventBus.emit('wallet:detected');
|
|
75
|
-
|
|
76
|
-
// Check if the provider is in MetaMask compatibility mode
|
|
77
|
-
this.isMetaMask = window.ethereum.isMetaMask;
|
|
78
|
-
|
|
79
|
-
// Set up event listeners for wallet changes
|
|
80
|
-
this.setupEventListeners();
|
|
81
|
-
} else {
|
|
72
|
+
if (typeof window.ethereum === 'undefined') {
|
|
82
73
|
console.log('No Ethereum provider found');
|
|
83
74
|
this.eventBus.emit('wallet:notdetected');
|
|
75
|
+
this.isInitialized = true;
|
|
76
|
+
return true;
|
|
84
77
|
}
|
|
85
78
|
|
|
86
|
-
|
|
87
|
-
this.
|
|
79
|
+
console.log('Found Ethereum provider');
|
|
80
|
+
this.eventBus.emit('wallet:detected');
|
|
81
|
+
this.isMetaMask = window.ethereum.isMetaMask;
|
|
82
|
+
|
|
83
|
+
// Check for previously authorized accounts (standard Web3 UX pattern)
|
|
84
|
+
if (autoReconnect) {
|
|
85
|
+
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
|
|
86
|
+
|
|
87
|
+
if (accounts?.length > 0) {
|
|
88
|
+
console.log('[WalletService] Found authorized account:', accounts[0]);
|
|
89
|
+
|
|
90
|
+
// Detect wallet type and update internal state
|
|
91
|
+
const walletType = this._detectWalletType();
|
|
92
|
+
this.selectedWallet = walletType;
|
|
93
|
+
this.provider = window.ethereum;
|
|
94
|
+
this.connectedAddress = accounts[0];
|
|
95
|
+
this.connected = true;
|
|
88
96
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
// Create ethers provider and signer
|
|
98
|
+
this.ethersProvider = new ethers.providers.Web3Provider(window.ethereum, 'any');
|
|
99
|
+
this.signer = this.ethersProvider.getSigner();
|
|
100
|
+
|
|
101
|
+
// Set up event listeners
|
|
102
|
+
this.setupEventListeners();
|
|
103
|
+
|
|
104
|
+
// Store for future sessions
|
|
105
|
+
localStorage.setItem('ms2fun_lastWallet', walletType);
|
|
106
|
+
|
|
107
|
+
console.log('[WalletService] Auto-connected to:', accounts[0]);
|
|
108
|
+
|
|
109
|
+
// Emit connected event
|
|
110
|
+
this.eventBus.emit('wallet:connected', {
|
|
111
|
+
address: this.connectedAddress,
|
|
112
|
+
walletType: this.selectedWallet,
|
|
113
|
+
provider: this.provider,
|
|
114
|
+
ethersProvider: this.ethersProvider,
|
|
115
|
+
signer: this.signer
|
|
116
|
+
});
|
|
117
|
+
}
|
|
92
118
|
}
|
|
93
119
|
|
|
120
|
+
// Set up listeners even if not connected (for future connections)
|
|
121
|
+
if (!this.connected) {
|
|
122
|
+
this.setupEventListeners();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this.isInitialized = true;
|
|
94
126
|
return true;
|
|
95
127
|
} catch (error) {
|
|
96
128
|
console.error('Error initializing WalletService:', error);
|
|
129
|
+
this.isInitialized = true; // Still mark as initialized
|
|
97
130
|
throw error;
|
|
98
131
|
}
|
|
99
132
|
}
|
|
@@ -566,7 +599,58 @@ class WalletService {
|
|
|
566
599
|
signer: this.signer
|
|
567
600
|
};
|
|
568
601
|
}
|
|
569
|
-
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Programmatically set the connected state.
|
|
605
|
+
* Useful for migrating from manual workarounds or external connection management.
|
|
606
|
+
*
|
|
607
|
+
* @param {Object} state - The connection state to set
|
|
608
|
+
* @param {string} state.address - The connected wallet address
|
|
609
|
+
* @param {Object} state.provider - The raw provider (window.ethereum or similar)
|
|
610
|
+
* @param {Object} [state.ethersProvider] - Optional ethers Web3Provider (created if not provided)
|
|
611
|
+
* @param {Object} [state.signer] - Optional ethers signer (created if not provided)
|
|
612
|
+
* @param {string} [state.walletType='metamask'] - The wallet type identifier
|
|
613
|
+
* @param {boolean} [emitEvent=true] - Whether to emit wallet:connected event
|
|
614
|
+
* @returns {void}
|
|
615
|
+
*/
|
|
616
|
+
setConnectedState({ address, provider, ethersProvider, signer, walletType = 'metamask' }, emitEvent = true) {
|
|
617
|
+
if (!address) {
|
|
618
|
+
throw new Error('setConnectedState requires an address');
|
|
619
|
+
}
|
|
620
|
+
if (!provider) {
|
|
621
|
+
throw new Error('setConnectedState requires a provider');
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Update internal state
|
|
625
|
+
this.provider = provider;
|
|
626
|
+
this.connectedAddress = address;
|
|
627
|
+
this.connected = true;
|
|
628
|
+
this.selectedWallet = walletType;
|
|
629
|
+
|
|
630
|
+
// Create ethers provider/signer if not provided
|
|
631
|
+
this.ethersProvider = ethersProvider || new ethers.providers.Web3Provider(provider, 'any');
|
|
632
|
+
this.signer = signer || this.ethersProvider.getSigner();
|
|
633
|
+
|
|
634
|
+
// Set up event listeners for the provider
|
|
635
|
+
this.setupEventListeners();
|
|
636
|
+
|
|
637
|
+
// Store wallet type for future auto-reconnect
|
|
638
|
+
localStorage.setItem('ms2fun_lastWallet', walletType);
|
|
639
|
+
|
|
640
|
+
console.log('[WalletService] State set programmatically:', address);
|
|
641
|
+
|
|
642
|
+
// Emit event so UI components update
|
|
643
|
+
if (emitEvent) {
|
|
644
|
+
this.eventBus.emit('wallet:connected', {
|
|
645
|
+
address: this.connectedAddress,
|
|
646
|
+
walletType: this.selectedWallet,
|
|
647
|
+
provider: this.provider,
|
|
648
|
+
ethersProvider: this.ethersProvider,
|
|
649
|
+
signer: this.signer
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
570
654
|
/**
|
|
571
655
|
* Handle network changes
|
|
572
656
|
* @private
|