@zerodev/wallet-react 0.0.1-alpha.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/CHANGELOG.md +9 -0
- package/dist/_cjs/actions.js +193 -0
- package/dist/_cjs/connector.js +221 -0
- package/dist/_cjs/constants.js +4 -0
- package/dist/_cjs/hooks/useAuthenticateOAuth.js +18 -0
- package/dist/_cjs/hooks/useExportWallet.js +18 -0
- package/dist/_cjs/hooks/useLoginPasskey.js +18 -0
- package/dist/_cjs/hooks/useRefreshSession.js +18 -0
- package/dist/_cjs/hooks/useRegisterPasskey.js +18 -0
- package/dist/_cjs/hooks/useSendOTP.js +18 -0
- package/dist/_cjs/hooks/useVerifyOTP.js +18 -0
- package/dist/_cjs/index.js +23 -0
- package/dist/_cjs/oauth.js +84 -0
- package/dist/_cjs/package.json +1 -0
- package/dist/_cjs/provider.js +163 -0
- package/dist/_cjs/store.js +51 -0
- package/dist/_cjs/utils/aaUtils.js +7 -0
- package/dist/_cjs/utils/timers.js +50 -0
- package/dist/_esm/actions.js +225 -0
- package/dist/_esm/connector.js +248 -0
- package/dist/_esm/constants.js +1 -0
- package/dist/_esm/hooks/useAuthenticateOAuth.js +18 -0
- package/dist/_esm/hooks/useExportWallet.js +18 -0
- package/dist/_esm/hooks/useLoginPasskey.js +18 -0
- package/dist/_esm/hooks/useRefreshSession.js +18 -0
- package/dist/_esm/hooks/useRegisterPasskey.js +18 -0
- package/dist/_esm/hooks/useSendOTP.js +18 -0
- package/dist/_esm/hooks/useVerifyOTP.js +18 -0
- package/dist/_esm/index.js +10 -0
- package/dist/_esm/oauth.js +77 -0
- package/dist/_esm/package.json +1 -0
- package/dist/_esm/provider.js +169 -0
- package/dist/_esm/store.js +51 -0
- package/dist/_esm/utils/aaUtils.js +4 -0
- package/dist/_esm/utils/timers.js +55 -0
- package/dist/_types/actions.d.ts +124 -0
- package/dist/_types/actions.d.ts.map +1 -0
- package/dist/_types/connector.d.ts +18 -0
- package/dist/_types/connector.d.ts.map +1 -0
- package/dist/_types/constants.d.ts +2 -0
- package/dist/_types/constants.d.ts.map +1 -0
- package/dist/_types/hooks/useAuthenticateOAuth.d.ts +18 -0
- package/dist/_types/hooks/useAuthenticateOAuth.d.ts.map +1 -0
- package/dist/_types/hooks/useExportWallet.d.ts +18 -0
- package/dist/_types/hooks/useExportWallet.d.ts.map +1 -0
- package/dist/_types/hooks/useLoginPasskey.d.ts +18 -0
- package/dist/_types/hooks/useLoginPasskey.d.ts.map +1 -0
- package/dist/_types/hooks/useRefreshSession.d.ts +18 -0
- package/dist/_types/hooks/useRefreshSession.d.ts.map +1 -0
- package/dist/_types/hooks/useRegisterPasskey.d.ts +18 -0
- package/dist/_types/hooks/useRegisterPasskey.d.ts.map +1 -0
- package/dist/_types/hooks/useSendOTP.d.ts +18 -0
- package/dist/_types/hooks/useSendOTP.d.ts.map +1 -0
- package/dist/_types/hooks/useVerifyOTP.d.ts +18 -0
- package/dist/_types/hooks/useVerifyOTP.d.ts.map +1 -0
- package/dist/_types/index.d.ts +15 -0
- package/dist/_types/index.d.ts.map +1 -0
- package/dist/_types/oauth.d.ts +21 -0
- package/dist/_types/oauth.d.ts.map +1 -0
- package/dist/_types/provider.d.ts +19 -0
- package/dist/_types/provider.d.ts.map +1 -0
- package/dist/_types/store.d.ts +52 -0
- package/dist/_types/store.d.ts.map +1 -0
- package/dist/_types/utils/aaUtils.d.ts +2 -0
- package/dist/_types/utils/aaUtils.d.ts.map +1 -0
- package/dist/_types/utils/timers.d.ts +22 -0
- package/dist/_types/utils/timers.d.ts.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +48 -0
- package/package.json.type +1 -0
- package/src/actions.ts +402 -0
- package/src/connector.ts +336 -0
- package/src/constants.ts +1 -0
- package/src/hooks/useAuthenticateOAuth.ts +57 -0
- package/src/hooks/useExportWallet.ts +57 -0
- package/src/hooks/useLoginPasskey.ts +57 -0
- package/src/hooks/useRefreshSession.ts +57 -0
- package/src/hooks/useRegisterPasskey.ts +57 -0
- package/src/hooks/useSendOTP.ts +57 -0
- package/src/hooks/useVerifyOTP.ts +57 -0
- package/src/index.ts +14 -0
- package/src/oauth.ts +124 -0
- package/src/provider.ts +235 -0
- package/src/store.ts +113 -0
- package/src/utils/aaUtils.ts +5 -0
- package/src/utils/timers.ts +80 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.build.tsbuildinfo +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerPasskey = registerPasskey;
|
|
4
|
+
exports.loginPasskey = loginPasskey;
|
|
5
|
+
exports.authenticateOAuth = authenticateOAuth;
|
|
6
|
+
exports.sendOTP = sendOTP;
|
|
7
|
+
exports.verifyOTP = verifyOTP;
|
|
8
|
+
exports.refreshSession = refreshSession;
|
|
9
|
+
exports.exportWallet = exportWallet;
|
|
10
|
+
const actions_1 = require("@wagmi/core/actions");
|
|
11
|
+
const oauth_js_1 = require("./oauth.js");
|
|
12
|
+
function getZeroDevConnector(config) {
|
|
13
|
+
const connector = config.connectors.find((c) => c.id === 'zerodev-wallet');
|
|
14
|
+
if (!connector) {
|
|
15
|
+
throw new Error('ZeroDev connector not found in Wagmi config');
|
|
16
|
+
}
|
|
17
|
+
return connector;
|
|
18
|
+
}
|
|
19
|
+
async function registerPasskey(config, parameters) {
|
|
20
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
21
|
+
const store = await connector.getStore();
|
|
22
|
+
const wallet = store.getState().wallet;
|
|
23
|
+
if (!wallet)
|
|
24
|
+
throw new Error('Wallet not initialized');
|
|
25
|
+
await wallet.auth({
|
|
26
|
+
type: 'passkey',
|
|
27
|
+
email: parameters.email,
|
|
28
|
+
mode: 'register',
|
|
29
|
+
});
|
|
30
|
+
const [session, eoaAccount] = await Promise.all([
|
|
31
|
+
wallet.getSession(),
|
|
32
|
+
wallet.toAccount(),
|
|
33
|
+
]);
|
|
34
|
+
store.getState().setEoaAccount(eoaAccount);
|
|
35
|
+
store.getState().setSession(session || null);
|
|
36
|
+
await (0, actions_1.connect)(config, { connector });
|
|
37
|
+
}
|
|
38
|
+
async function loginPasskey(config, parameters) {
|
|
39
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
40
|
+
const store = await connector.getStore();
|
|
41
|
+
const wallet = store.getState().wallet;
|
|
42
|
+
if (!wallet)
|
|
43
|
+
throw new Error('Wallet not initialized');
|
|
44
|
+
await wallet.auth({
|
|
45
|
+
type: 'passkey',
|
|
46
|
+
email: parameters.email,
|
|
47
|
+
mode: 'login',
|
|
48
|
+
});
|
|
49
|
+
const [session, eoaAccount] = await Promise.all([
|
|
50
|
+
wallet.getSession(),
|
|
51
|
+
wallet.toAccount(),
|
|
52
|
+
]);
|
|
53
|
+
store.getState().setEoaAccount(eoaAccount);
|
|
54
|
+
store.getState().setSession(session || null);
|
|
55
|
+
await (0, actions_1.connect)(config, { connector });
|
|
56
|
+
}
|
|
57
|
+
async function authenticateOAuth(config, parameters) {
|
|
58
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
59
|
+
const store = await connector.getStore();
|
|
60
|
+
const wallet = store.getState().wallet;
|
|
61
|
+
const oauthConfig = store.getState().oauthConfig;
|
|
62
|
+
if (!wallet)
|
|
63
|
+
throw new Error('Wallet not initialized');
|
|
64
|
+
if (!oauthConfig) {
|
|
65
|
+
throw new Error('OAuth is not configured. Please provide oauthConfig to zeroDevWallet connector.');
|
|
66
|
+
}
|
|
67
|
+
let clientId = parameters.clientId;
|
|
68
|
+
if (!clientId) {
|
|
69
|
+
clientId = oauthConfig.googleClientId;
|
|
70
|
+
}
|
|
71
|
+
if (!clientId) {
|
|
72
|
+
throw new Error(`Client ID not configured for ${parameters.provider}`);
|
|
73
|
+
}
|
|
74
|
+
if (!oauthConfig.redirectUri) {
|
|
75
|
+
throw new Error('OAuth redirect URI is not configured.');
|
|
76
|
+
}
|
|
77
|
+
const publicKey = await wallet.getPublicKey();
|
|
78
|
+
if (!publicKey) {
|
|
79
|
+
throw new Error('Failed to get wallet public key');
|
|
80
|
+
}
|
|
81
|
+
const nonce = (0, oauth_js_1.generateOAuthNonce)(publicKey);
|
|
82
|
+
const oauthUrl = (0, oauth_js_1.buildOAuthUrl)({
|
|
83
|
+
provider: parameters.provider,
|
|
84
|
+
clientId,
|
|
85
|
+
redirectUri: oauthConfig.redirectUri,
|
|
86
|
+
nonce,
|
|
87
|
+
});
|
|
88
|
+
const authWindow = (0, oauth_js_1.openOAuthPopup)(oauthUrl);
|
|
89
|
+
if (!authWindow) {
|
|
90
|
+
throw new Error(`Failed to open ${parameters.provider} login window.`);
|
|
91
|
+
}
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
(0, oauth_js_1.pollOAuthPopup)(authWindow, window.location.origin, async (idToken) => {
|
|
94
|
+
try {
|
|
95
|
+
await wallet.auth({
|
|
96
|
+
type: 'oauth',
|
|
97
|
+
provider: parameters.provider,
|
|
98
|
+
credential: idToken,
|
|
99
|
+
});
|
|
100
|
+
const [session, eoaAccount] = await Promise.all([
|
|
101
|
+
wallet.getSession(),
|
|
102
|
+
wallet.toAccount(),
|
|
103
|
+
]);
|
|
104
|
+
store.getState().setEoaAccount(eoaAccount);
|
|
105
|
+
store.getState().setSession(session || null);
|
|
106
|
+
await (0, actions_1.connect)(config, { connector });
|
|
107
|
+
resolve();
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
reject(err);
|
|
111
|
+
}
|
|
112
|
+
}, reject);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async function sendOTP(config, parameters) {
|
|
116
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
117
|
+
const store = await connector.getStore();
|
|
118
|
+
const wallet = store.getState().wallet;
|
|
119
|
+
if (!wallet)
|
|
120
|
+
throw new Error('Wallet not initialized');
|
|
121
|
+
const result = await wallet.auth({
|
|
122
|
+
type: 'otp',
|
|
123
|
+
mode: 'sendOtp',
|
|
124
|
+
email: parameters.email,
|
|
125
|
+
contact: { type: 'email', contact: parameters.email },
|
|
126
|
+
...(parameters.emailCustomization && {
|
|
127
|
+
emailCustomization: parameters.emailCustomization,
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
otpId: result.otpId,
|
|
132
|
+
subOrganizationId: result.subOrganizationId,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
async function verifyOTP(config, parameters) {
|
|
136
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
137
|
+
const store = await connector.getStore();
|
|
138
|
+
const wallet = store.getState().wallet;
|
|
139
|
+
if (!wallet)
|
|
140
|
+
throw new Error('Wallet not initialized');
|
|
141
|
+
await wallet.auth({
|
|
142
|
+
type: 'otp',
|
|
143
|
+
mode: 'verifyOtp',
|
|
144
|
+
otpId: parameters.otpId,
|
|
145
|
+
otpCode: parameters.code,
|
|
146
|
+
subOrganizationId: parameters.subOrganizationId,
|
|
147
|
+
});
|
|
148
|
+
const [session, eoaAccount] = await Promise.all([
|
|
149
|
+
wallet.getSession(),
|
|
150
|
+
wallet.toAccount(),
|
|
151
|
+
]);
|
|
152
|
+
store.getState().setEoaAccount(eoaAccount);
|
|
153
|
+
store.getState().setSession(session || null);
|
|
154
|
+
await (0, actions_1.connect)(config, { connector });
|
|
155
|
+
}
|
|
156
|
+
async function refreshSession(config, parameters = {}) {
|
|
157
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
158
|
+
const store = await connector.getStore();
|
|
159
|
+
const wallet = store.getState().wallet;
|
|
160
|
+
const currentSession = store.getState().session;
|
|
161
|
+
if (!wallet || !currentSession) {
|
|
162
|
+
throw new Error('No active session to refresh');
|
|
163
|
+
}
|
|
164
|
+
const newSession = await wallet.refreshSession(currentSession.id);
|
|
165
|
+
store.getState().setSession(newSession || null);
|
|
166
|
+
return newSession;
|
|
167
|
+
}
|
|
168
|
+
async function exportWallet(config, parameters) {
|
|
169
|
+
const connector = parameters.connector ?? getZeroDevConnector(config);
|
|
170
|
+
const store = await connector.getStore();
|
|
171
|
+
const wallet = store.getState().wallet;
|
|
172
|
+
if (!wallet)
|
|
173
|
+
throw new Error('Wallet not initialized');
|
|
174
|
+
const { exportWallet: exportWalletSdk, createIframeStamper } = await Promise.resolve().then(() => require('@zerodev/wallet-core'));
|
|
175
|
+
const iframeContainer = document.getElementById(parameters.iframeContainerId);
|
|
176
|
+
if (!iframeContainer) {
|
|
177
|
+
throw new Error('Iframe container not found');
|
|
178
|
+
}
|
|
179
|
+
const iframeStamper = await createIframeStamper({
|
|
180
|
+
iframeUrl: 'https://export.turnkey.com',
|
|
181
|
+
iframeContainer,
|
|
182
|
+
iframeElementId: 'export-wallet-iframe',
|
|
183
|
+
});
|
|
184
|
+
const publicKey = await iframeStamper.init();
|
|
185
|
+
const { exportBundle, organizationId } = await exportWalletSdk({
|
|
186
|
+
wallet,
|
|
187
|
+
targetPublicKey: publicKey,
|
|
188
|
+
});
|
|
189
|
+
const success = await iframeStamper.injectWalletExportBundle(exportBundle, organizationId);
|
|
190
|
+
if (success !== true) {
|
|
191
|
+
throw new Error('Failed to inject export bundle');
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zeroDevWallet = zeroDevWallet;
|
|
4
|
+
const core_1 = require("@wagmi/core");
|
|
5
|
+
const sdk_1 = require("@zerodev/sdk");
|
|
6
|
+
const constants_1 = require("@zerodev/sdk/constants");
|
|
7
|
+
const wallet_core_1 = require("@zerodev/wallet-core");
|
|
8
|
+
const viem_1 = require("viem");
|
|
9
|
+
const provider_js_1 = require("./provider.js");
|
|
10
|
+
const store_js_1 = require("./store.js");
|
|
11
|
+
const aaUtils_js_1 = require("./utils/aaUtils.js");
|
|
12
|
+
function zeroDevWallet(params) {
|
|
13
|
+
return (0, core_1.createConnector)((wagmiConfig) => {
|
|
14
|
+
let store;
|
|
15
|
+
let provider;
|
|
16
|
+
let initPromise;
|
|
17
|
+
const transports = wagmiConfig.transports;
|
|
18
|
+
const initialize = async () => {
|
|
19
|
+
initPromise ??= (async () => {
|
|
20
|
+
console.log('Initializing ZeroDevWallet connector...');
|
|
21
|
+
const wallet = await (0, wallet_core_1.createZeroDevWallet)({
|
|
22
|
+
projectId: params.projectId,
|
|
23
|
+
...(params.organizationId && {
|
|
24
|
+
organizationId: params.organizationId,
|
|
25
|
+
}),
|
|
26
|
+
...(params.proxyBaseUrl && { proxyBaseUrl: params.proxyBaseUrl }),
|
|
27
|
+
...(params.sessionStorage && {
|
|
28
|
+
sessionStorage: params.sessionStorage,
|
|
29
|
+
}),
|
|
30
|
+
...(params.rpId && { rpId: params.rpId }),
|
|
31
|
+
});
|
|
32
|
+
store = (0, store_js_1.createZeroDevWalletStore)();
|
|
33
|
+
store.getState().setWallet(wallet);
|
|
34
|
+
const chainIds = params.chains.map((c) => c.id);
|
|
35
|
+
store.setState({ chainIds });
|
|
36
|
+
if (params.oauthConfig) {
|
|
37
|
+
store.getState().setOAuthConfig(params.oauthConfig);
|
|
38
|
+
}
|
|
39
|
+
provider = (0, provider_js_1.createProvider)({
|
|
40
|
+
store,
|
|
41
|
+
config: params,
|
|
42
|
+
chains: Array.from(params.chains),
|
|
43
|
+
});
|
|
44
|
+
const session = await wallet.getSession();
|
|
45
|
+
if (session) {
|
|
46
|
+
console.log('Found existing session, restoring...');
|
|
47
|
+
const eoaAccount = await wallet.toAccount();
|
|
48
|
+
store.getState().setEoaAccount(eoaAccount);
|
|
49
|
+
store.getState().setSession(session);
|
|
50
|
+
}
|
|
51
|
+
console.log('ZeroDevWallet connector initialized');
|
|
52
|
+
})();
|
|
53
|
+
return initPromise;
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
id: 'zerodev-wallet',
|
|
57
|
+
name: 'ZeroDevWallet',
|
|
58
|
+
type: 'injected',
|
|
59
|
+
async setup() {
|
|
60
|
+
if (typeof window !== 'undefined') {
|
|
61
|
+
await initialize();
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
async connect({ chainId, ...rest } = {}) {
|
|
65
|
+
const withCapabilities = ('withCapabilities' in rest && rest.withCapabilities) || false;
|
|
66
|
+
const isReconnecting = ('isReconnecting' in rest && rest.isReconnecting) || false;
|
|
67
|
+
await initialize();
|
|
68
|
+
console.log(isReconnecting
|
|
69
|
+
? 'Reconnecting ZeroDevWallet...'
|
|
70
|
+
: 'Connecting ZeroDevWallet...');
|
|
71
|
+
const state = store.getState();
|
|
72
|
+
const activeChainId = chainId || state.chainIds[0];
|
|
73
|
+
if (!activeChainId) {
|
|
74
|
+
throw new Error('No chain configured');
|
|
75
|
+
}
|
|
76
|
+
if (isReconnecting && state.kernelAccounts.has(activeChainId)) {
|
|
77
|
+
const kernelAccount = state.kernelAccounts.get(activeChainId);
|
|
78
|
+
if (kernelAccount?.address) {
|
|
79
|
+
console.log('Already connected:', kernelAccount.address);
|
|
80
|
+
return {
|
|
81
|
+
accounts: [kernelAccount.address],
|
|
82
|
+
chainId: activeChainId,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!state.eoaAccount) {
|
|
87
|
+
throw new Error('Not authenticated. Please authenticate first using passkey, OAuth, or OTP.');
|
|
88
|
+
}
|
|
89
|
+
if (!state.kernelAccounts.has(activeChainId)) {
|
|
90
|
+
const chain = params.chains.find((c) => c.id === activeChainId);
|
|
91
|
+
if (!chain) {
|
|
92
|
+
throw new Error(`Chain ${activeChainId} not found in config`);
|
|
93
|
+
}
|
|
94
|
+
const transport = transports?.[activeChainId] ?? (0, viem_1.http)();
|
|
95
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
96
|
+
chain,
|
|
97
|
+
transport,
|
|
98
|
+
});
|
|
99
|
+
console.log(`Creating kernel account for chain ${activeChainId}...`);
|
|
100
|
+
const kernelAccount = await (0, sdk_1.createKernelAccount)(publicClient, {
|
|
101
|
+
entryPoint: (0, constants_1.getEntryPoint)('0.7'),
|
|
102
|
+
kernelVersion: constants_1.KERNEL_V3_3,
|
|
103
|
+
eip7702Account: state.eoaAccount,
|
|
104
|
+
});
|
|
105
|
+
store.getState().setKernelAccount(activeChainId, kernelAccount);
|
|
106
|
+
const kernelClient = (0, sdk_1.createKernelAccountClient)({
|
|
107
|
+
account: kernelAccount,
|
|
108
|
+
bundlerTransport: (0, viem_1.http)((0, aaUtils_js_1.getAAUrl)(activeChainId, params.aaUrl)),
|
|
109
|
+
chain,
|
|
110
|
+
client: publicClient,
|
|
111
|
+
paymaster: (0, sdk_1.createZeroDevPaymasterClient)({
|
|
112
|
+
chain,
|
|
113
|
+
transport: (0, viem_1.http)((0, aaUtils_js_1.getAAUrl)(activeChainId, params.aaUrl)),
|
|
114
|
+
}),
|
|
115
|
+
});
|
|
116
|
+
store.getState().setKernelClient(activeChainId, kernelClient);
|
|
117
|
+
}
|
|
118
|
+
store.getState().setActiveChain(activeChainId);
|
|
119
|
+
const freshState = store.getState();
|
|
120
|
+
const kernelAccount = freshState.kernelAccounts.get(activeChainId);
|
|
121
|
+
console.log('ZeroDevWallet connected:', kernelAccount.address);
|
|
122
|
+
const address = kernelAccount.address;
|
|
123
|
+
return {
|
|
124
|
+
accounts: (withCapabilities
|
|
125
|
+
? [{ address, capabilities: {} }]
|
|
126
|
+
: [address]),
|
|
127
|
+
chainId: activeChainId,
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
async disconnect() {
|
|
131
|
+
console.log('Disconnecting ZeroDevWallet...');
|
|
132
|
+
if (!store)
|
|
133
|
+
return;
|
|
134
|
+
const wallet = store.getState().wallet;
|
|
135
|
+
provider?.destroy();
|
|
136
|
+
await wallet?.logout();
|
|
137
|
+
store.getState().clear();
|
|
138
|
+
},
|
|
139
|
+
async getAccounts() {
|
|
140
|
+
if (!store)
|
|
141
|
+
return [];
|
|
142
|
+
const { eoaAccount, kernelAccounts, chainIds } = store.getState();
|
|
143
|
+
if (eoaAccount) {
|
|
144
|
+
return [eoaAccount.address];
|
|
145
|
+
}
|
|
146
|
+
const activeAccount = chainIds[0]
|
|
147
|
+
? kernelAccounts.get(chainIds[0])
|
|
148
|
+
: null;
|
|
149
|
+
return activeAccount ? [activeAccount.address] : [];
|
|
150
|
+
},
|
|
151
|
+
async getChainId() {
|
|
152
|
+
if (!store)
|
|
153
|
+
return params.chains[0].id;
|
|
154
|
+
return store.getState().chainIds[0] || params.chains[0].id;
|
|
155
|
+
},
|
|
156
|
+
async getProvider() {
|
|
157
|
+
await initialize();
|
|
158
|
+
return provider;
|
|
159
|
+
},
|
|
160
|
+
async switchChain({ chainId }) {
|
|
161
|
+
await initialize();
|
|
162
|
+
console.log(`Switching to chain ${chainId}...`);
|
|
163
|
+
const state = store.getState();
|
|
164
|
+
if (!state.eoaAccount) {
|
|
165
|
+
throw new Error('Not authenticated');
|
|
166
|
+
}
|
|
167
|
+
store.getState().setActiveChain(chainId);
|
|
168
|
+
if (!state.kernelAccounts.has(chainId)) {
|
|
169
|
+
const chain = params.chains.find((c) => c.id === chainId);
|
|
170
|
+
if (!chain) {
|
|
171
|
+
throw new Error(`Chain ${chainId} not found in config`);
|
|
172
|
+
}
|
|
173
|
+
const transport = transports?.[chainId] ?? (0, viem_1.http)();
|
|
174
|
+
const publicClient = (0, viem_1.createPublicClient)({
|
|
175
|
+
chain,
|
|
176
|
+
transport,
|
|
177
|
+
});
|
|
178
|
+
console.log(`Creating kernel account for chain ${chainId}...`);
|
|
179
|
+
const kernelAccount = await (0, sdk_1.createKernelAccount)(publicClient, {
|
|
180
|
+
entryPoint: (0, constants_1.getEntryPoint)('0.7'),
|
|
181
|
+
kernelVersion: constants_1.KERNEL_V3_3,
|
|
182
|
+
eip7702Account: state.eoaAccount,
|
|
183
|
+
});
|
|
184
|
+
store.getState().setKernelAccount(chainId, kernelAccount);
|
|
185
|
+
const kernelClient = (0, sdk_1.createKernelAccountClient)({
|
|
186
|
+
account: kernelAccount,
|
|
187
|
+
bundlerTransport: (0, viem_1.http)((0, aaUtils_js_1.getAAUrl)(chainId, params.aaUrl)),
|
|
188
|
+
chain,
|
|
189
|
+
client: publicClient,
|
|
190
|
+
paymaster: (0, sdk_1.createZeroDevPaymasterClient)({
|
|
191
|
+
chain,
|
|
192
|
+
transport: (0, viem_1.http)((0, aaUtils_js_1.getAAUrl)(chainId, params.aaUrl)),
|
|
193
|
+
}),
|
|
194
|
+
});
|
|
195
|
+
store.getState().setKernelClient(chainId, kernelClient);
|
|
196
|
+
}
|
|
197
|
+
return params.chains.find((c) => c.id === chainId);
|
|
198
|
+
},
|
|
199
|
+
async isAuthorized() {
|
|
200
|
+
if (!store)
|
|
201
|
+
return false;
|
|
202
|
+
return !!store.getState().eoaAccount;
|
|
203
|
+
},
|
|
204
|
+
async getStore() {
|
|
205
|
+
await initialize();
|
|
206
|
+
return store;
|
|
207
|
+
},
|
|
208
|
+
onAccountsChanged() {
|
|
209
|
+
},
|
|
210
|
+
onChainChanged() {
|
|
211
|
+
},
|
|
212
|
+
onConnect() {
|
|
213
|
+
},
|
|
214
|
+
onDisconnect() {
|
|
215
|
+
console.log('Disconnect event');
|
|
216
|
+
provider?.destroy();
|
|
217
|
+
store.getState().clear();
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
});
|
|
221
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useAuthenticateOAuth = useAuthenticateOAuth;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useAuthenticateOAuth(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.authenticateOAuth)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['authenticateOAuth'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useExportWallet = useExportWallet;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useExportWallet(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.exportWallet)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['exportWallet'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useLoginPasskey = useLoginPasskey;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useLoginPasskey(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.loginPasskey)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['loginPasskey'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useRefreshSession = useRefreshSession;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useRefreshSession(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.refreshSession)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['refreshSession'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useRegisterPasskey = useRegisterPasskey;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useRegisterPasskey(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.registerPasskey)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['registerPasskey'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useSendOTP = useSendOTP;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useSendOTP(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.sendOTP)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['sendOTP'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useVerifyOTP = useVerifyOTP;
|
|
5
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
6
|
+
const wagmi_1 = require("wagmi");
|
|
7
|
+
const actions_js_1 = require("../actions.js");
|
|
8
|
+
function useVerifyOTP(parameters = {}) {
|
|
9
|
+
const { mutation } = parameters;
|
|
10
|
+
const config = (0, wagmi_1.useConfig)(parameters);
|
|
11
|
+
return (0, react_query_1.useMutation)({
|
|
12
|
+
...mutation,
|
|
13
|
+
async mutationFn(variables) {
|
|
14
|
+
return (0, actions_js_1.verifyOTP)(config, variables);
|
|
15
|
+
},
|
|
16
|
+
mutationKey: ['verifyOTP'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createZeroDevWalletStore = exports.OAUTH_PROVIDERS = exports.useVerifyOTP = exports.useSendOTP = exports.useRegisterPasskey = exports.useRefreshSession = exports.useLoginPasskey = exports.useExportWallet = exports.useAuthenticateOAuth = exports.zeroDevWallet = void 0;
|
|
4
|
+
var connector_js_1 = require("./connector.js");
|
|
5
|
+
Object.defineProperty(exports, "zeroDevWallet", { enumerable: true, get: function () { return connector_js_1.zeroDevWallet; } });
|
|
6
|
+
var useAuthenticateOAuth_js_1 = require("./hooks/useAuthenticateOAuth.js");
|
|
7
|
+
Object.defineProperty(exports, "useAuthenticateOAuth", { enumerable: true, get: function () { return useAuthenticateOAuth_js_1.useAuthenticateOAuth; } });
|
|
8
|
+
var useExportWallet_js_1 = require("./hooks/useExportWallet.js");
|
|
9
|
+
Object.defineProperty(exports, "useExportWallet", { enumerable: true, get: function () { return useExportWallet_js_1.useExportWallet; } });
|
|
10
|
+
var useLoginPasskey_js_1 = require("./hooks/useLoginPasskey.js");
|
|
11
|
+
Object.defineProperty(exports, "useLoginPasskey", { enumerable: true, get: function () { return useLoginPasskey_js_1.useLoginPasskey; } });
|
|
12
|
+
var useRefreshSession_js_1 = require("./hooks/useRefreshSession.js");
|
|
13
|
+
Object.defineProperty(exports, "useRefreshSession", { enumerable: true, get: function () { return useRefreshSession_js_1.useRefreshSession; } });
|
|
14
|
+
var useRegisterPasskey_js_1 = require("./hooks/useRegisterPasskey.js");
|
|
15
|
+
Object.defineProperty(exports, "useRegisterPasskey", { enumerable: true, get: function () { return useRegisterPasskey_js_1.useRegisterPasskey; } });
|
|
16
|
+
var useSendOTP_js_1 = require("./hooks/useSendOTP.js");
|
|
17
|
+
Object.defineProperty(exports, "useSendOTP", { enumerable: true, get: function () { return useSendOTP_js_1.useSendOTP; } });
|
|
18
|
+
var useVerifyOTP_js_1 = require("./hooks/useVerifyOTP.js");
|
|
19
|
+
Object.defineProperty(exports, "useVerifyOTP", { enumerable: true, get: function () { return useVerifyOTP_js_1.useVerifyOTP; } });
|
|
20
|
+
var oauth_js_1 = require("./oauth.js");
|
|
21
|
+
Object.defineProperty(exports, "OAUTH_PROVIDERS", { enumerable: true, get: function () { return oauth_js_1.OAUTH_PROVIDERS; } });
|
|
22
|
+
var store_js_1 = require("./store.js");
|
|
23
|
+
Object.defineProperty(exports, "createZeroDevWalletStore", { enumerable: true, get: function () { return store_js_1.createZeroDevWalletStore; } });
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OAUTH_PROVIDERS = void 0;
|
|
4
|
+
exports.buildOAuthUrl = buildOAuthUrl;
|
|
5
|
+
exports.openOAuthPopup = openOAuthPopup;
|
|
6
|
+
exports.extractOAuthToken = extractOAuthToken;
|
|
7
|
+
exports.pollOAuthPopup = pollOAuthPopup;
|
|
8
|
+
exports.generateOAuthNonce = generateOAuthNonce;
|
|
9
|
+
const viem_1 = require("viem");
|
|
10
|
+
exports.OAUTH_PROVIDERS = {
|
|
11
|
+
GOOGLE: 'google',
|
|
12
|
+
};
|
|
13
|
+
const GOOGLE_AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth';
|
|
14
|
+
const POPUP_WIDTH = 500;
|
|
15
|
+
const POPUP_HEIGHT = 600;
|
|
16
|
+
function buildOAuthUrl(params) {
|
|
17
|
+
const { provider, clientId, redirectUri, nonce, state } = params;
|
|
18
|
+
if (provider !== exports.OAUTH_PROVIDERS.GOOGLE) {
|
|
19
|
+
throw new Error(`Unsupported OAuth provider: ${provider}`);
|
|
20
|
+
}
|
|
21
|
+
const authUrl = new URL(GOOGLE_AUTH_URL);
|
|
22
|
+
authUrl.searchParams.set('client_id', clientId);
|
|
23
|
+
authUrl.searchParams.set('redirect_uri', redirectUri);
|
|
24
|
+
authUrl.searchParams.set('response_type', 'id_token');
|
|
25
|
+
authUrl.searchParams.set('scope', 'openid email profile');
|
|
26
|
+
authUrl.searchParams.set('nonce', nonce);
|
|
27
|
+
authUrl.searchParams.set('prompt', 'select_account');
|
|
28
|
+
let stateParam = `provider=${provider}`;
|
|
29
|
+
if (state) {
|
|
30
|
+
const additionalState = Object.entries(state)
|
|
31
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
32
|
+
.join('&');
|
|
33
|
+
if (additionalState) {
|
|
34
|
+
stateParam += `&${additionalState}`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
authUrl.searchParams.set('state', stateParam);
|
|
38
|
+
return authUrl.toString();
|
|
39
|
+
}
|
|
40
|
+
function openOAuthPopup(url) {
|
|
41
|
+
const width = POPUP_WIDTH;
|
|
42
|
+
const height = POPUP_HEIGHT;
|
|
43
|
+
const left = window.screenX + (window.innerWidth - width) / 2;
|
|
44
|
+
const top = window.screenY + (window.innerHeight - height) / 2;
|
|
45
|
+
const authWindow = window.open('about:blank', '_blank', `width=${width},height=${height},top=${top},left=${left},scrollbars=yes,resizable=yes`);
|
|
46
|
+
if (authWindow) {
|
|
47
|
+
authWindow.location.href = url;
|
|
48
|
+
}
|
|
49
|
+
return authWindow;
|
|
50
|
+
}
|
|
51
|
+
function extractOAuthToken(url) {
|
|
52
|
+
const hashParams = new URLSearchParams(url.split('#')[1]);
|
|
53
|
+
let idToken = hashParams.get('id_token');
|
|
54
|
+
if (!idToken) {
|
|
55
|
+
const queryParams = new URLSearchParams(url.split('?')[1]);
|
|
56
|
+
idToken = queryParams.get('id_token');
|
|
57
|
+
}
|
|
58
|
+
return idToken;
|
|
59
|
+
}
|
|
60
|
+
function pollOAuthPopup(authWindow, originUrl, onSuccess, onError) {
|
|
61
|
+
const interval = setInterval(() => {
|
|
62
|
+
try {
|
|
63
|
+
if (authWindow.closed) {
|
|
64
|
+
clearInterval(interval);
|
|
65
|
+
onError(new Error('Authentication window was closed'));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const url = authWindow.location.href || '';
|
|
69
|
+
if (url.startsWith(originUrl)) {
|
|
70
|
+
const token = extractOAuthToken(url);
|
|
71
|
+
if (token) {
|
|
72
|
+
authWindow.close();
|
|
73
|
+
clearInterval(interval);
|
|
74
|
+
onSuccess(token);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
}
|
|
80
|
+
}, 500);
|
|
81
|
+
}
|
|
82
|
+
function generateOAuthNonce(publicKey) {
|
|
83
|
+
return (0, viem_1.sha256)(publicKey).replace(/^0x/, '');
|
|
84
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|