@monygroupcorp/micro-web3 1.2.0 → 1.2.2
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 +2 -2
- package/dist/micro-web3.cjs.map +1 -1
- package/dist/micro-web3.esm.js +2 -2
- package/dist/micro-web3.esm.js.map +1 -1
- package/dist/micro-web3.umd.js +1 -1
- package/dist/micro-web3.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/components/FloatingWalletButton/FloatingWalletButton.js +37 -21
- package/src/services/WalletService.js +87 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monygroupcorp/micro-web3",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
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",
|
|
@@ -49,30 +49,31 @@ export class FloatingWalletButton extends Component {
|
|
|
49
49
|
let isConnected = this.walletService.isConnected();
|
|
50
50
|
let address = this.walletService.getAddress();
|
|
51
51
|
|
|
52
|
-
// If not connected,
|
|
52
|
+
// If not connected, check if window.ethereum already has connected accounts
|
|
53
53
|
if (!isConnected && typeof window.ethereum !== 'undefined') {
|
|
54
54
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
console.log('[FloatingWalletButton] Auto-reconnect not possible');
|
|
55
|
+
// eth_accounts returns already-authorized accounts without prompting
|
|
56
|
+
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
|
|
57
|
+
const hasAccounts = accounts && accounts.length > 0;
|
|
58
|
+
|
|
59
|
+
if (hasAccounts) {
|
|
60
|
+
// Detect which wallet is active
|
|
61
|
+
const walletType = this._detectWalletType();
|
|
62
|
+
console.log('[FloatingWalletButton] Detected existing connection:', walletType, accounts[0]);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
await this.walletService.selectWallet(walletType);
|
|
66
|
+
await this.walletService.connect();
|
|
67
|
+
isConnected = this.walletService.isConnected();
|
|
68
|
+
address = this.walletService.getAddress();
|
|
69
|
+
|
|
70
|
+
if (isConnected) {
|
|
71
|
+
console.log('[FloatingWalletButton] Auto-connected to existing session:', walletType);
|
|
72
|
+
// Update lastWallet for future sessions
|
|
73
|
+
localStorage.setItem('ms2fun_lastWallet', walletType);
|
|
75
74
|
}
|
|
75
|
+
} catch (connectError) {
|
|
76
|
+
console.log('[FloatingWalletButton] Could not auto-connect:', connectError.message);
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
} catch (error) {
|
|
@@ -91,6 +92,21 @@ export class FloatingWalletButton extends Component {
|
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Detect which wallet type is currently active based on provider flags
|
|
97
|
+
*/
|
|
98
|
+
_detectWalletType() {
|
|
99
|
+
if (typeof window.ethereum === 'undefined') return 'metamask';
|
|
100
|
+
|
|
101
|
+
// Check specific wallet flags (order matters - more specific first)
|
|
102
|
+
if (window.ethereum.isRabby) return 'rabby';
|
|
103
|
+
if (window.ethereum.isRainbow) return 'rainbow';
|
|
104
|
+
if (window.phantom?.ethereum) return 'phantom';
|
|
105
|
+
|
|
106
|
+
// Default to metamask for any EIP-1193 provider
|
|
107
|
+
return 'metamask';
|
|
108
|
+
}
|
|
109
|
+
|
|
94
110
|
/**
|
|
95
111
|
* Load wallet info (balance, EXEC tokens, vault benefactor status)
|
|
96
112
|
*/
|
|
@@ -56,39 +56,108 @@ class WalletService {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
|
-
* Initialize the wallet service
|
|
59
|
+
* Initialize the wallet service
|
|
60
|
+
* @param {Object} options - Initialization options
|
|
61
|
+
* @param {boolean} options.autoReconnect - If true, automatically reconnect to previously authorized wallets
|
|
62
|
+
* @returns {Promise<boolean>} True if initialized successfully
|
|
60
63
|
*/
|
|
61
|
-
async initialize() {
|
|
64
|
+
async initialize(options = {}) {
|
|
62
65
|
try {
|
|
63
66
|
console.log('Initializing WalletService...');
|
|
64
|
-
|
|
67
|
+
|
|
65
68
|
// Check if window.ethereum exists
|
|
66
69
|
if (typeof window.ethereum !== 'undefined') {
|
|
67
70
|
// Log that we found a wallet provider
|
|
68
71
|
console.log('Found Ethereum provider');
|
|
69
|
-
|
|
72
|
+
|
|
70
73
|
// Let other components know a wallet was detected
|
|
71
74
|
this.eventBus.emit('wallet:detected');
|
|
72
|
-
|
|
75
|
+
|
|
73
76
|
// Check if the provider is in MetaMask compatibility mode
|
|
74
77
|
this.isMetaMask = window.ethereum.isMetaMask;
|
|
75
|
-
|
|
78
|
+
|
|
76
79
|
// Set up event listeners for wallet changes
|
|
77
80
|
this.setupEventListeners();
|
|
78
81
|
} else {
|
|
79
82
|
console.log('No Ethereum provider found');
|
|
80
83
|
this.eventBus.emit('wallet:notdetected');
|
|
81
84
|
}
|
|
82
|
-
|
|
85
|
+
|
|
83
86
|
// Mark as initialized
|
|
84
87
|
this.isInitialized = true;
|
|
85
|
-
|
|
88
|
+
|
|
89
|
+
// Auto-reconnect if requested
|
|
90
|
+
if (options.autoReconnect) {
|
|
91
|
+
await this.tryAutoConnect();
|
|
92
|
+
}
|
|
93
|
+
|
|
86
94
|
return true;
|
|
87
95
|
} catch (error) {
|
|
88
96
|
console.error('Error initializing WalletService:', error);
|
|
89
97
|
throw error;
|
|
90
98
|
}
|
|
91
99
|
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Attempt to auto-connect to a previously authorized wallet.
|
|
103
|
+
* Uses eth_accounts (non-prompting) to check for existing authorization.
|
|
104
|
+
* @returns {Promise<string|null>} The connected address, or null if no authorized wallet found
|
|
105
|
+
*/
|
|
106
|
+
async tryAutoConnect() {
|
|
107
|
+
try {
|
|
108
|
+
if (typeof window.ethereum === 'undefined') {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// eth_accounts returns already-authorized accounts without prompting
|
|
113
|
+
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
|
|
114
|
+
|
|
115
|
+
if (!accounts || accounts.length === 0) {
|
|
116
|
+
console.log('[WalletService] No previously authorized accounts found');
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
console.log('[WalletService] Found authorized account:', accounts[0]);
|
|
121
|
+
|
|
122
|
+
// Detect which wallet type is active
|
|
123
|
+
const walletType = this._detectWalletType();
|
|
124
|
+
console.log('[WalletService] Detected wallet type:', walletType);
|
|
125
|
+
|
|
126
|
+
// Select and connect (this won't prompt since already authorized)
|
|
127
|
+
await this.selectWallet(walletType);
|
|
128
|
+
const address = await this.connect();
|
|
129
|
+
|
|
130
|
+
console.log('[WalletService] Auto-reconnected to:', address);
|
|
131
|
+
return address;
|
|
132
|
+
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.warn('[WalletService] Auto-connect failed:', error.message);
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Detect which wallet type is currently active based on provider flags
|
|
141
|
+
* @private
|
|
142
|
+
* @returns {string} The detected wallet type
|
|
143
|
+
*/
|
|
144
|
+
_detectWalletType() {
|
|
145
|
+
if (typeof window.ethereum === 'undefined') return 'metamask';
|
|
146
|
+
|
|
147
|
+
// Check specific wallet flags (order matters - more specific first)
|
|
148
|
+
if (window.ethereum.isRabby) return 'rabby';
|
|
149
|
+
if (window.ethereum.isRainbow) return 'rainbow';
|
|
150
|
+
if (window.phantom?.ethereum) return 'phantom';
|
|
151
|
+
|
|
152
|
+
// Check localStorage for last used wallet as a hint
|
|
153
|
+
const lastWallet = localStorage.getItem('ms2fun_lastWallet');
|
|
154
|
+
if (lastWallet && this.providerMap[lastWallet]) {
|
|
155
|
+
return lastWallet;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Default to metamask for any EIP-1193 provider
|
|
159
|
+
return 'metamask';
|
|
160
|
+
}
|
|
92
161
|
|
|
93
162
|
/**
|
|
94
163
|
* Format error to provide better context
|
|
@@ -287,11 +356,18 @@ class WalletService {
|
|
|
287
356
|
}
|
|
288
357
|
|
|
289
358
|
/**
|
|
290
|
-
* Connect to
|
|
291
|
-
* @
|
|
359
|
+
* Connect to a wallet
|
|
360
|
+
* @param {string} [walletType] - Optional wallet type to connect to directly (e.g., 'metamask', 'rabby')
|
|
361
|
+
* If not provided, requires prior call to selectWallet()
|
|
362
|
+
* @returns {Promise<string>} The connected address
|
|
292
363
|
*/
|
|
293
|
-
async connect() {
|
|
364
|
+
async connect(walletType) {
|
|
294
365
|
try {
|
|
366
|
+
// If walletType provided, select it first
|
|
367
|
+
if (walletType) {
|
|
368
|
+
await this.selectWallet(walletType);
|
|
369
|
+
}
|
|
370
|
+
|
|
295
371
|
if (!this.selectedWallet || !this.provider) {
|
|
296
372
|
throw new Error('Please select a wallet first');
|
|
297
373
|
}
|