@injectivelabs/wallet-turnkey 1.15.17 → 1.15.18

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.
@@ -1,4 +1,3 @@
1
- export { TurnkeyOtpWalletStrategy } from './strategy/strategy/otp.js';
2
- export { TurnkeyOauthWalletStrategy } from './strategy/strategy/oauth.js';
3
- export * from './strategy/strategy/base.js';
1
+ export { TurnkeyWalletStrategy } from './strategy/strategy.js';
2
+ export * from './strategy/strategy.js';
4
3
  export * from './strategy/turnkey/turnkey.js';
package/dist/cjs/index.js CHANGED
@@ -14,10 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.TurnkeyOauthWalletStrategy = exports.TurnkeyOtpWalletStrategy = void 0;
18
- var otp_js_1 = require("./strategy/strategy/otp.js");
19
- Object.defineProperty(exports, "TurnkeyOtpWalletStrategy", { enumerable: true, get: function () { return otp_js_1.TurnkeyOtpWalletStrategy; } });
20
- var oauth_js_1 = require("./strategy/strategy/oauth.js");
21
- Object.defineProperty(exports, "TurnkeyOauthWalletStrategy", { enumerable: true, get: function () { return oauth_js_1.TurnkeyOauthWalletStrategy; } });
22
- __exportStar(require("./strategy/strategy/base.js"), exports);
17
+ exports.TurnkeyWalletStrategy = void 0;
18
+ var strategy_js_1 = require("./strategy/strategy.js");
19
+ Object.defineProperty(exports, "TurnkeyWalletStrategy", { enumerable: true, get: function () { return strategy_js_1.TurnkeyWalletStrategy; } });
20
+ __exportStar(require("./strategy/strategy.js"), exports);
23
21
  __exportStar(require("./strategy/turnkey/turnkey.js"), exports);
@@ -2,3 +2,4 @@ export declare const TURNKEY_OAUTH_PATH = "turnkey/oauth";
2
2
  export declare const TURNKEY_OTP_PATH = "turnkey/otp";
3
3
  export declare const TURNKEY_OTP_INIT_PATH = "turnkey/otp/init";
4
4
  export declare const TURNKEY_OTP_VERIFY_PATH = "turnkey/otp/verify";
5
+ export declare const DEFAULT_TURNKEY_REFRESH_SECONDS = "86400";
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TURNKEY_OTP_VERIFY_PATH = exports.TURNKEY_OTP_INIT_PATH = exports.TURNKEY_OTP_PATH = exports.TURNKEY_OAUTH_PATH = void 0;
3
+ exports.DEFAULT_TURNKEY_REFRESH_SECONDS = exports.TURNKEY_OTP_VERIFY_PATH = exports.TURNKEY_OTP_INIT_PATH = exports.TURNKEY_OTP_PATH = exports.TURNKEY_OAUTH_PATH = void 0;
4
4
  exports.TURNKEY_OAUTH_PATH = 'turnkey/oauth';
5
5
  exports.TURNKEY_OTP_PATH = 'turnkey/otp';
6
6
  exports.TURNKEY_OTP_INIT_PATH = `${exports.TURNKEY_OTP_PATH}/init`;
7
7
  exports.TURNKEY_OTP_VERIFY_PATH = `${exports.TURNKEY_OTP_PATH}/verify`;
8
+ exports.DEFAULT_TURNKEY_REFRESH_SECONDS = '86400';
@@ -1,26 +1,24 @@
1
1
  import { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
2
2
  import { HttpRestClient } from '@injectivelabs/utils';
3
3
  import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
4
- import { StdSignDoc, TurnkeyProvider, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
5
- import { TurnkeyWallet } from '../turnkey/turnkey.js';
6
4
  import { TurnkeyIframeClient } from '@turnkey/sdk-browser';
7
- export declare class BaseTurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
5
+ import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
6
+ import { TurnkeyWallet } from './turnkey/turnkey.js';
7
+ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
8
8
  turnkeyWallet: TurnkeyWallet | undefined;
9
- turnkeyProvider: TurnkeyProvider;
10
9
  client: HttpRestClient;
11
10
  constructor(args: ConcreteEthereumWalletStrategyArgs & {
12
- provider: TurnkeyProvider;
13
11
  apiServerEndpoint?: string;
14
12
  });
15
13
  getWalletDeviceType(): Promise<WalletDeviceType>;
16
14
  setMetadata(metadata?: {
17
15
  turnkey?: Partial<WalletMetadata['turnkey']>;
18
16
  }): void;
19
- private setOrganizationId;
20
17
  enable(): Promise<boolean>;
21
18
  disconnect(): Promise<void>;
22
19
  getAddresses(): Promise<string[]>;
23
- getSessionOrConfirm(): Promise<string>;
20
+ getSessionOrConfirm(_address?: string): Promise<string>;
21
+ getWalletClient<TurnkeyWallet>(): Promise<TurnkeyWallet>;
24
22
  sendEthereumTransaction(_transaction: unknown, _options: {
25
23
  address: AccountAddress;
26
24
  ethereumChainId: EthereumChainId;
@@ -1,25 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseTurnkeyWalletStrategy = void 0;
3
+ exports.TurnkeyWalletStrategy = void 0;
4
4
  /* eslint-disable class-methods-use-this */
5
- const exceptions_1 = require("@injectivelabs/exceptions");
6
5
  const sdk_ts_1 = require("@injectivelabs/sdk-ts");
6
+ const exceptions_1 = require("@injectivelabs/exceptions");
7
+ const viem_1 = require("viem");
7
8
  const utils_1 = require("@injectivelabs/utils");
8
9
  const wallet_base_1 = require("@injectivelabs/wallet-base");
9
- const turnkey_js_1 = require("../turnkey/turnkey.js");
10
- const types_js_1 = require("../types.js");
11
- const otp_js_1 = require("../turnkey/otp.js");
12
- const oauth_js_1 = require("../turnkey/oauth.js");
13
- const sdk_browser_1 = require("@turnkey/sdk-browser");
14
- const viem_1 = require("viem");
15
- const utils_js_1 = require("../../utils.js");
16
- class BaseTurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
10
+ const types_js_1 = require("./types.js");
11
+ const turnkey_js_1 = require("./turnkey/turnkey.js");
12
+ class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
17
13
  turnkeyWallet;
18
- turnkeyProvider;
19
14
  client;
20
15
  constructor(args) {
21
16
  super(args);
22
- this.turnkeyProvider = args.provider;
23
17
  const endpoint = args.apiServerEndpoint || this.metadata?.turnkey?.apiServerEndpoint;
24
18
  if (!endpoint) {
25
19
  throw new exceptions_1.WalletException(new Error('apiServerEndpoint is required'));
@@ -38,13 +32,9 @@ class BaseTurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
38
32
  ...metadata.turnkey,
39
33
  },
40
34
  };
35
+ this.turnkeyWallet?.setMetadata(this.metadata?.turnkey);
41
36
  }
42
37
  }
43
- //? This is here specifically because we have to ensure we set the organizationId on TurnkeyWallet as well
44
- setOrganizationId(organizationId) {
45
- this.setMetadata({ turnkey: { organizationId } });
46
- this.turnkeyWallet?.setMetadata({ organizationId });
47
- }
48
38
  async enable() {
49
39
  const turnkeyWallet = await this.getTurnkeyWallet();
50
40
  try {
@@ -73,48 +63,7 @@ class BaseTurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
73
63
  }
74
64
  async getAddresses() {
75
65
  const turnkeyWallet = await this.getTurnkeyWallet();
76
- const organizationId = await this.getOrganizationId();
77
- // CHeck if the user is already connected
78
- const session = await turnkeyWallet.getSession();
79
- if (!session.session) {
80
- const iframeClient = await turnkeyWallet.getIframeClient();
81
- // Check the provider type and perform auth step accordingly
82
- if (this.turnkeyProvider === wallet_base_1.TurnkeyProvider.Email) {
83
- if (!this.metadata?.turnkey?.otpId) {
84
- throw new exceptions_1.WalletException(new Error('OTP ID is required'));
85
- }
86
- if (!this.metadata?.turnkey?.otpCode) {
87
- throw new exceptions_1.WalletException(new Error('OTP code is required'));
88
- }
89
- const result = await otp_js_1.TurnkeyOtpWallet.confirmEmailOTP({
90
- iframeClient,
91
- organizationId,
92
- client: this.client,
93
- otpCode: this.metadata?.turnkey?.otpCode,
94
- emailOTPId: this.metadata?.turnkey?.otpId,
95
- });
96
- if (result?.credentialBundle) {
97
- this.metadata.turnkey.credentialBundle = result.credentialBundle;
98
- await turnkeyWallet.injectAndRefresh(result.credentialBundle);
99
- }
100
- }
101
- else {
102
- if (!this.metadata?.turnkey?.oidcToken) {
103
- throw new exceptions_1.WalletException(new Error('Oidc token is required'));
104
- }
105
- const result = await oauth_js_1.TurnkeyOauthWallet.oauthLogin({
106
- client: this.client,
107
- iframeClient,
108
- oidcToken: this.metadata?.turnkey?.oidcToken,
109
- providerName: this.turnkeyProvider.toString(),
110
- });
111
- if (result?.credentialBundle) {
112
- this.metadata.turnkey.organizationId = result.organizationId;
113
- this.metadata.turnkey.credentialBundle = result.credentialBundle;
114
- await turnkeyWallet.injectAndRefresh(result.credentialBundle);
115
- }
116
- }
117
- }
66
+ await turnkeyWallet.getSession();
118
67
  try {
119
68
  return await turnkeyWallet.getAccounts();
120
69
  }
@@ -130,82 +79,12 @@ class BaseTurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
130
79
  });
131
80
  }
132
81
  }
133
- async getSessionOrConfirm() {
134
- const { turnkeyProvider, metadata, client } = this;
82
+ async getSessionOrConfirm(_address) {
135
83
  const turnkeyWallet = await this.getTurnkeyWallet();
136
- const iframeClient = await turnkeyWallet.getIframeClient();
137
- const session = await turnkeyWallet.getSession();
138
- if (
139
- // If either of these values exist on the metadata, then we want to proceed with the login flow
140
- !this.metadata?.turnkey?.email &&
141
- !this.metadata?.turnkey?.oidcToken &&
142
- session.session?.token) {
143
- await iframeClient.injectCredentialBundle(session.session?.token);
144
- this.setOrganizationId(session.organizationId);
145
- await iframeClient.refreshSession({
146
- sessionType: sdk_browser_1.SessionType.READ_WRITE,
147
- targetPublicKey: iframeClient.iframePublicKey,
148
- expirationSeconds: '900',
149
- });
150
- return session.session.token;
151
- }
152
- if (turnkeyProvider === wallet_base_1.TurnkeyProvider.Email) {
153
- if (!metadata?.turnkey?.email) {
154
- throw new exceptions_1.WalletException(new Error('Email is required'));
155
- }
156
- const result = await otp_js_1.TurnkeyOtpWallet.initEmailOTP({
157
- client,
158
- iframeClient,
159
- email: metadata.turnkey.email,
160
- otpInitPath: metadata.turnkey.otpInitPath,
161
- });
162
- if (result?.organizationId && this.metadata?.turnkey) {
163
- this.metadata.turnkey.organizationId = result.organizationId;
164
- }
165
- if (result?.otpId && this.metadata?.turnkey) {
166
- this.metadata.turnkey.otpId = result.otpId;
167
- }
168
- return result?.otpId || '';
169
- }
170
- if ([wallet_base_1.TurnkeyProvider.Google, wallet_base_1.TurnkeyProvider.Apple].includes(turnkeyProvider)) {
171
- if (metadata?.turnkey?.oidcToken) {
172
- const oauthResult = await oauth_js_1.TurnkeyOauthWallet.oauthLogin({
173
- client,
174
- iframeClient,
175
- oidcToken: metadata.turnkey.oidcToken,
176
- providerName: turnkeyProvider.toString(),
177
- oauthLoginPath: metadata.turnkey.oauthLoginPath,
178
- });
179
- if (oauthResult?.credentialBundle) {
180
- await turnkeyWallet.injectAndRefresh(oauthResult.credentialBundle);
181
- }
182
- if (oauthResult?.credentialBundle) {
183
- const session = await turnkeyWallet.getSession();
184
- if (this.metadata?.turnkey && session.organizationId) {
185
- this.metadata.turnkey.organizationId = session.organizationId;
186
- }
187
- return oauthResult.credentialBundle;
188
- }
189
- throw new exceptions_1.WalletException(new Error('Oauth result not found'));
190
- }
191
- else {
192
- const nonce = await oauth_js_1.TurnkeyOauthWallet.generateOAuthNonce(iframeClient);
193
- if (!metadata?.turnkey?.googleClientId ||
194
- !metadata?.turnkey?.googleRedirectUri) {
195
- throw new exceptions_1.WalletException(new Error('googleClientId and googleRedirectUri are required'));
196
- }
197
- if (turnkeyProvider === wallet_base_1.TurnkeyProvider.Google) {
198
- return (0, utils_js_1.generateGoogleUrl)({
199
- nonce,
200
- clientId: metadata.turnkey.googleClientId,
201
- redirectUri: metadata.turnkey.googleRedirectUri,
202
- });
203
- }
204
- //? When we add Apple support, we might also want to return the URL as well
205
- return nonce;
206
- }
207
- }
208
- return '';
84
+ return await turnkeyWallet.refreshSession();
85
+ }
86
+ async getWalletClient() {
87
+ return (await this.getTurnkeyWallet());
209
88
  }
210
89
  async sendEthereumTransaction(_transaction, _options) {
211
90
  throw new exceptions_1.WalletException(new Error('sendEthereumTransaction is not supported. Turnkey only supports sending cosmos transactions'), {
@@ -324,4 +203,4 @@ class BaseTurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
324
203
  return organizationId;
325
204
  }
326
205
  }
327
- exports.BaseTurnkeyWalletStrategy = BaseTurnkeyWalletStrategy;
206
+ exports.TurnkeyWalletStrategy = TurnkeyWalletStrategy;
@@ -12,9 +12,9 @@ export declare class TurnkeyOtpWallet {
12
12
  }): Promise<TurnkeyOTPCredentialsResponse | undefined>;
13
13
  static confirmEmailOTP(args: {
14
14
  otpCode: string;
15
- emailOTPId?: string;
15
+ emailOTPId: string;
16
16
  client: HttpRestClient;
17
- organizationId?: string;
17
+ organizationId: string;
18
18
  iframeClient: TurnkeyIframeClient;
19
19
  otpVerifyPath?: string;
20
20
  }): Promise<TurnkeyConfirmEmailOTPResponse | undefined>;
@@ -1,11 +1,13 @@
1
- import { Turnkey, TurnkeyIframeClient } from '@turnkey/sdk-browser';
2
- import { TurnkeyMetadata } from '@injectivelabs/wallet-base';
1
+ import { TurnkeyMetadata, TurnkeyProvider } from '@injectivelabs/wallet-base';
3
2
  import { HttpRestClient } from '@injectivelabs/utils';
3
+ import { Turnkey, TurnkeyIframeClient } from '@turnkey/sdk-browser';
4
4
  export declare class TurnkeyWallet {
5
- protected iframeClient: TurnkeyIframeClient | undefined;
6
- protected turnkey: Turnkey | undefined;
7
5
  protected client: HttpRestClient;
8
6
  private metadata;
7
+ protected turnkey: Turnkey | undefined;
8
+ protected iframeClient: TurnkeyIframeClient | undefined;
9
+ organizationId: string;
10
+ private otpId;
9
11
  private accountMap;
10
12
  setMetadata(metadata: Partial<TurnkeyMetadata>): void;
11
13
  constructor(metadata: TurnkeyMetadata);
@@ -20,7 +22,7 @@ export declare class TurnkeyWallet {
20
22
  organizationId: string;
21
23
  }>;
22
24
  getAccounts(): Promise<string[]>;
23
- getOrCreateAndGetAccount(address: string, organizationId?: string): Promise<{
25
+ getOrCreateAndGetAccount(address: string, organizationId: string): Promise<{
24
26
  address: import("abitype").Address;
25
27
  nonceManager?: import("viem").NonceManager | undefined;
26
28
  sign?: ((parameters: {
@@ -38,6 +40,14 @@ export declare class TurnkeyWallet {
38
40
  source: string;
39
41
  type: "local";
40
42
  }>;
41
- injectAndRefresh(credentialBundle: string): Promise<void>;
43
+ injectAndRefresh(credentialBundle: string, options: {
44
+ expirationSeconds?: string;
45
+ organizationId?: string;
46
+ }): Promise<void>;
47
+ initOTP(email: string): Promise<import("../types.js").TurnkeyOTPCredentialsResponse>;
48
+ confirmOTP(otpCode: string): Promise<import("../types.js").TurnkeyConfirmEmailOTPResponse>;
49
+ initOAuth(provider: TurnkeyProvider.Google | TurnkeyProvider.Apple): Promise<string>;
50
+ confirmOAuth(provider: TurnkeyProvider.Google | TurnkeyProvider.Apple, oidcToken: string): Promise<string>;
51
+ refreshSession(): Promise<string>;
42
52
  private initFrame;
43
53
  }
@@ -1,24 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TurnkeyWallet = void 0;
4
- const sdk_browser_1 = require("@turnkey/sdk-browser");
5
4
  const exceptions_1 = require("@injectivelabs/exceptions");
6
5
  const wallet_base_1 = require("@injectivelabs/wallet-base");
7
- const sdk_ts_1 = require("@injectivelabs/sdk-ts");
8
- const utils_1 = require("@injectivelabs/utils");
9
6
  const viem_1 = require("@turnkey/viem");
7
+ const utils_1 = require("@injectivelabs/utils");
8
+ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
9
+ const sdk_browser_1 = require("@turnkey/sdk-browser");
10
+ const consts_js_1 = require("../consts.js");
11
+ const otp_js_1 = require("./otp.js");
10
12
  const types_js_1 = require("../types.js");
13
+ const oauth_js_1 = require("./oauth.js");
14
+ const utils_js_1 = require("../../utils.js");
11
15
  class TurnkeyWallet {
12
- iframeClient;
13
- turnkey;
14
16
  client;
15
17
  metadata;
18
+ turnkey;
19
+ iframeClient;
20
+ organizationId;
21
+ otpId;
16
22
  accountMap = {};
17
23
  setMetadata(metadata) {
18
24
  this.metadata = { ...this.metadata, ...metadata };
19
25
  }
20
26
  constructor(metadata) {
21
27
  this.metadata = metadata;
28
+ this.organizationId = metadata.organizationId;
22
29
  this.client = new utils_1.HttpRestClient(metadata.apiServerEndpoint);
23
30
  }
24
31
  static async getTurnkeyInstance(metadata) {
@@ -93,16 +100,16 @@ class TurnkeyWallet {
93
100
  }
94
101
  async getAccounts() {
95
102
  const iframeClient = await this.getIframeClient();
96
- if (!this.metadata.organizationId) {
103
+ if (!this.organizationId) {
97
104
  return [];
98
105
  }
99
106
  try {
100
107
  const response = await iframeClient.getWallets({
101
- organizationId: this.metadata.organizationId,
108
+ organizationId: this.organizationId,
102
109
  });
103
110
  const accounts = await Promise.allSettled(response.wallets.map((wallet) => iframeClient.getWalletAccounts({
104
111
  walletId: wallet.walletId,
105
- organizationId: this.metadata.organizationId,
112
+ organizationId: this.organizationId,
106
113
  })));
107
114
  const filteredAccounts = accounts
108
115
  .filter((account) => account.status === 'fulfilled')
@@ -149,16 +156,120 @@ class TurnkeyWallet {
149
156
  this.accountMap[address] = turnkeyAccount;
150
157
  return turnkeyAccount;
151
158
  }
152
- async injectAndRefresh(credentialBundle) {
159
+ async injectAndRefresh(credentialBundle, options) {
160
+ const expirationSeconds = options.expirationSeconds || consts_js_1.DEFAULT_TURNKEY_REFRESH_SECONDS;
153
161
  const iframeClient = await this.getIframeClient();
154
162
  await iframeClient.injectCredentialBundle(credentialBundle);
155
163
  await iframeClient.refreshSession({
156
164
  sessionType: sdk_browser_1.SessionType.READ_WRITE,
165
+ expirationSeconds: options.expirationSeconds,
157
166
  targetPublicKey: iframeClient.iframePublicKey,
158
- expirationSeconds: '900',
159
167
  });
168
+ // If we just logged in, we have the new org ID here and don't need to wait on getSession method
169
+ if (options.organizationId) {
170
+ this.organizationId = options.organizationId;
171
+ this.metadata.organizationId = options.organizationId;
172
+ }
173
+ else {
174
+ const session = await this.getSession();
175
+ this.organizationId = session.organizationId;
176
+ this.metadata.organizationId = session.organizationId;
177
+ }
178
+ // Refresh the session 2 minutes before it expires
179
+ setTimeout(() => {
180
+ iframeClient.refreshSession({
181
+ expirationSeconds,
182
+ sessionType: sdk_browser_1.SessionType.READ_WRITE,
183
+ targetPublicKey: iframeClient.iframePublicKey,
184
+ });
185
+ }, (parseInt(expirationSeconds) - 120) * 1000);
160
186
  return;
161
187
  }
188
+ async initOTP(email) {
189
+ const iframeClient = await this.getIframeClient();
190
+ const result = await otp_js_1.TurnkeyOtpWallet.initEmailOTP({
191
+ client: this.client,
192
+ iframeClient,
193
+ email,
194
+ otpInitPath: this.metadata.otpInitPath || consts_js_1.TURNKEY_OTP_INIT_PATH,
195
+ });
196
+ if (!result || !result.otpId) {
197
+ throw new exceptions_1.WalletException(new Error('Failed to initialize OTP'));
198
+ }
199
+ if (result?.organizationId) {
200
+ this.organizationId = result.organizationId;
201
+ }
202
+ if (result?.otpId) {
203
+ this.otpId = result.otpId;
204
+ }
205
+ return result;
206
+ }
207
+ async confirmOTP(otpCode) {
208
+ const iframeClient = await this.getIframeClient();
209
+ if (!this.otpId) {
210
+ throw new exceptions_1.WalletException(new Error('OTP ID is required'));
211
+ }
212
+ const result = await otp_js_1.TurnkeyOtpWallet.confirmEmailOTP({
213
+ otpCode,
214
+ iframeClient,
215
+ client: this.client,
216
+ emailOTPId: this.otpId,
217
+ organizationId: this.organizationId,
218
+ otpVerifyPath: this.metadata.otpVerifyPath || consts_js_1.TURNKEY_OTP_VERIFY_PATH,
219
+ });
220
+ if (!result || !result.credentialBundle) {
221
+ throw new exceptions_1.WalletException(new Error('Failed to confirm OTP'));
222
+ }
223
+ await this.injectAndRefresh(result.credentialBundle, {
224
+ organizationId: result.organizationId,
225
+ expirationSeconds: this.metadata.expirationSeconds,
226
+ });
227
+ return result;
228
+ }
229
+ async initOAuth(provider) {
230
+ const iframeClient = await this.getIframeClient();
231
+ const nonce = await oauth_js_1.TurnkeyOauthWallet.generateOAuthNonce(iframeClient);
232
+ if (provider === wallet_base_1.TurnkeyProvider.Apple) {
233
+ // TODO: implement the ability to generate Apple OAuth URL
234
+ return nonce;
235
+ }
236
+ if (!this.metadata?.googleClientId || !this.metadata?.googleRedirectUri) {
237
+ throw new exceptions_1.WalletException(new Error('googleClientId and googleRedirectUri are required'));
238
+ }
239
+ return (0, utils_js_1.generateGoogleUrl)({
240
+ nonce,
241
+ clientId: this.metadata.googleClientId,
242
+ redirectUri: this.metadata.googleRedirectUri,
243
+ });
244
+ }
245
+ async confirmOAuth(provider, oidcToken) {
246
+ const iframeClient = await this.getIframeClient();
247
+ const oauthResult = await oauth_js_1.TurnkeyOauthWallet.oauthLogin({
248
+ oidcToken,
249
+ iframeClient,
250
+ client: this.client,
251
+ providerName: provider.toString(),
252
+ oauthLoginPath: this.metadata.oauthLoginPath || consts_js_1.TURNKEY_OAUTH_PATH,
253
+ });
254
+ if (!oauthResult || !oauthResult.credentialBundle) {
255
+ throw new exceptions_1.WalletException(new Error('Unexpected OAuth result'));
256
+ }
257
+ await this.injectAndRefresh(oauthResult.credentialBundle, {
258
+ organizationId: oauthResult.organizationId,
259
+ expirationSeconds: this.metadata.expirationSeconds,
260
+ });
261
+ return oauthResult.credentialBundle;
262
+ }
263
+ async refreshSession() {
264
+ const session = await this.getSession();
265
+ if (session.session?.token) {
266
+ await this.injectAndRefresh(session.session.token, {
267
+ expirationSeconds: this.metadata.expirationSeconds,
268
+ });
269
+ return session.session.token;
270
+ }
271
+ throw new exceptions_1.TurnkeyWalletSessionException(new Error('Session expired. Please login again.'));
272
+ }
162
273
  async initFrame() {
163
274
  const { metadata } = this;
164
275
  const { turnkey, iframeClient } = await createTurnkeyIFrame(metadata);
@@ -18,6 +18,7 @@ export type TurnkeyOTPCredentialsResponse = {
18
18
  };
19
19
  export type TurnkeyConfirmEmailOTPResponse = {
20
20
  credentialBundle: string;
21
+ organizationId: string;
21
22
  };
22
23
  export type TurnkeyOauthLoginResponse = {
23
24
  organizationId: string;
@@ -1,4 +1,3 @@
1
- export { TurnkeyOtpWalletStrategy } from './strategy/strategy/otp.js';
2
- export { TurnkeyOauthWalletStrategy } from './strategy/strategy/oauth.js';
3
- export * from './strategy/strategy/base.js';
1
+ export { TurnkeyWalletStrategy } from './strategy/strategy.js';
2
+ export * from './strategy/strategy.js';
4
3
  export * from './strategy/turnkey/turnkey.js';
package/dist/esm/index.js CHANGED
@@ -1,4 +1,3 @@
1
- export { TurnkeyOtpWalletStrategy } from './strategy/strategy/otp.js';
2
- export { TurnkeyOauthWalletStrategy } from './strategy/strategy/oauth.js';
3
- export * from './strategy/strategy/base.js';
1
+ export { TurnkeyWalletStrategy } from './strategy/strategy.js';
2
+ export * from './strategy/strategy.js';
4
3
  export * from './strategy/turnkey/turnkey.js';
@@ -2,3 +2,4 @@ export declare const TURNKEY_OAUTH_PATH = "turnkey/oauth";
2
2
  export declare const TURNKEY_OTP_PATH = "turnkey/otp";
3
3
  export declare const TURNKEY_OTP_INIT_PATH = "turnkey/otp/init";
4
4
  export declare const TURNKEY_OTP_VERIFY_PATH = "turnkey/otp/verify";
5
+ export declare const DEFAULT_TURNKEY_REFRESH_SECONDS = "86400";
@@ -2,3 +2,4 @@ export const TURNKEY_OAUTH_PATH = 'turnkey/oauth';
2
2
  export const TURNKEY_OTP_PATH = 'turnkey/otp';
3
3
  export const TURNKEY_OTP_INIT_PATH = `${TURNKEY_OTP_PATH}/init`;
4
4
  export const TURNKEY_OTP_VERIFY_PATH = `${TURNKEY_OTP_PATH}/verify`;
5
+ export const DEFAULT_TURNKEY_REFRESH_SECONDS = '86400';
@@ -1,26 +1,24 @@
1
1
  import { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
2
2
  import { HttpRestClient } from '@injectivelabs/utils';
3
3
  import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
4
- import { StdSignDoc, TurnkeyProvider, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
5
- import { TurnkeyWallet } from '../turnkey/turnkey.js';
6
4
  import { TurnkeyIframeClient } from '@turnkey/sdk-browser';
7
- export declare class BaseTurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
5
+ import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
6
+ import { TurnkeyWallet } from './turnkey/turnkey.js';
7
+ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
8
8
  turnkeyWallet: TurnkeyWallet | undefined;
9
- turnkeyProvider: TurnkeyProvider;
10
9
  client: HttpRestClient;
11
10
  constructor(args: ConcreteEthereumWalletStrategyArgs & {
12
- provider: TurnkeyProvider;
13
11
  apiServerEndpoint?: string;
14
12
  });
15
13
  getWalletDeviceType(): Promise<WalletDeviceType>;
16
14
  setMetadata(metadata?: {
17
15
  turnkey?: Partial<WalletMetadata['turnkey']>;
18
16
  }): void;
19
- private setOrganizationId;
20
17
  enable(): Promise<boolean>;
21
18
  disconnect(): Promise<void>;
22
19
  getAddresses(): Promise<string[]>;
23
- getSessionOrConfirm(): Promise<string>;
20
+ getSessionOrConfirm(_address?: string): Promise<string>;
21
+ getWalletClient<TurnkeyWallet>(): Promise<TurnkeyWallet>;
24
22
  sendEthereumTransaction(_transaction: unknown, _options: {
25
23
  address: AccountAddress;
26
24
  ethereumChainId: EthereumChainId;
@@ -1,22 +1,16 @@
1
1
  /* eslint-disable class-methods-use-this */
2
- import { ErrorType, WalletException, UnspecifiedErrorCode, TransactionException, CosmosWalletException, } from '@injectivelabs/exceptions';
3
2
  import { TxGrpcApi, } from '@injectivelabs/sdk-ts';
4
- import { HttpRestClient } from '@injectivelabs/utils';
5
- import { WalletAction, TurnkeyProvider, WalletDeviceType, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
6
- import { TurnkeyWallet } from '../turnkey/turnkey.js';
7
- import { TurnkeyErrorCodes } from '../types.js';
8
- import { TurnkeyOtpWallet } from '../turnkey/otp.js';
9
- import { TurnkeyOauthWallet } from '../turnkey/oauth.js';
10
- import { SessionType } from '@turnkey/sdk-browser';
3
+ import { ErrorType, WalletException, UnspecifiedErrorCode, TransactionException, CosmosWalletException, } from '@injectivelabs/exceptions';
11
4
  import { getAddress } from 'viem';
12
- import { generateGoogleUrl } from '../../utils.js';
13
- export class BaseTurnkeyWalletStrategy extends BaseConcreteStrategy {
5
+ import { HttpRestClient } from '@injectivelabs/utils';
6
+ import { WalletAction, WalletDeviceType, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
7
+ import { TurnkeyErrorCodes } from './types.js';
8
+ import { TurnkeyWallet } from './turnkey/turnkey.js';
9
+ export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
14
10
  turnkeyWallet;
15
- turnkeyProvider;
16
11
  client;
17
12
  constructor(args) {
18
13
  super(args);
19
- this.turnkeyProvider = args.provider;
20
14
  const endpoint = args.apiServerEndpoint || this.metadata?.turnkey?.apiServerEndpoint;
21
15
  if (!endpoint) {
22
16
  throw new WalletException(new Error('apiServerEndpoint is required'));
@@ -35,13 +29,9 @@ export class BaseTurnkeyWalletStrategy extends BaseConcreteStrategy {
35
29
  ...metadata.turnkey,
36
30
  },
37
31
  };
32
+ this.turnkeyWallet?.setMetadata(this.metadata?.turnkey);
38
33
  }
39
34
  }
40
- //? This is here specifically because we have to ensure we set the organizationId on TurnkeyWallet as well
41
- setOrganizationId(organizationId) {
42
- this.setMetadata({ turnkey: { organizationId } });
43
- this.turnkeyWallet?.setMetadata({ organizationId });
44
- }
45
35
  async enable() {
46
36
  const turnkeyWallet = await this.getTurnkeyWallet();
47
37
  try {
@@ -70,48 +60,7 @@ export class BaseTurnkeyWalletStrategy extends BaseConcreteStrategy {
70
60
  }
71
61
  async getAddresses() {
72
62
  const turnkeyWallet = await this.getTurnkeyWallet();
73
- const organizationId = await this.getOrganizationId();
74
- // CHeck if the user is already connected
75
- const session = await turnkeyWallet.getSession();
76
- if (!session.session) {
77
- const iframeClient = await turnkeyWallet.getIframeClient();
78
- // Check the provider type and perform auth step accordingly
79
- if (this.turnkeyProvider === TurnkeyProvider.Email) {
80
- if (!this.metadata?.turnkey?.otpId) {
81
- throw new WalletException(new Error('OTP ID is required'));
82
- }
83
- if (!this.metadata?.turnkey?.otpCode) {
84
- throw new WalletException(new Error('OTP code is required'));
85
- }
86
- const result = await TurnkeyOtpWallet.confirmEmailOTP({
87
- iframeClient,
88
- organizationId,
89
- client: this.client,
90
- otpCode: this.metadata?.turnkey?.otpCode,
91
- emailOTPId: this.metadata?.turnkey?.otpId,
92
- });
93
- if (result?.credentialBundle) {
94
- this.metadata.turnkey.credentialBundle = result.credentialBundle;
95
- await turnkeyWallet.injectAndRefresh(result.credentialBundle);
96
- }
97
- }
98
- else {
99
- if (!this.metadata?.turnkey?.oidcToken) {
100
- throw new WalletException(new Error('Oidc token is required'));
101
- }
102
- const result = await TurnkeyOauthWallet.oauthLogin({
103
- client: this.client,
104
- iframeClient,
105
- oidcToken: this.metadata?.turnkey?.oidcToken,
106
- providerName: this.turnkeyProvider.toString(),
107
- });
108
- if (result?.credentialBundle) {
109
- this.metadata.turnkey.organizationId = result.organizationId;
110
- this.metadata.turnkey.credentialBundle = result.credentialBundle;
111
- await turnkeyWallet.injectAndRefresh(result.credentialBundle);
112
- }
113
- }
114
- }
63
+ await turnkeyWallet.getSession();
115
64
  try {
116
65
  return await turnkeyWallet.getAccounts();
117
66
  }
@@ -127,82 +76,12 @@ export class BaseTurnkeyWalletStrategy extends BaseConcreteStrategy {
127
76
  });
128
77
  }
129
78
  }
130
- async getSessionOrConfirm() {
131
- const { turnkeyProvider, metadata, client } = this;
79
+ async getSessionOrConfirm(_address) {
132
80
  const turnkeyWallet = await this.getTurnkeyWallet();
133
- const iframeClient = await turnkeyWallet.getIframeClient();
134
- const session = await turnkeyWallet.getSession();
135
- if (
136
- // If either of these values exist on the metadata, then we want to proceed with the login flow
137
- !this.metadata?.turnkey?.email &&
138
- !this.metadata?.turnkey?.oidcToken &&
139
- session.session?.token) {
140
- await iframeClient.injectCredentialBundle(session.session?.token);
141
- this.setOrganizationId(session.organizationId);
142
- await iframeClient.refreshSession({
143
- sessionType: SessionType.READ_WRITE,
144
- targetPublicKey: iframeClient.iframePublicKey,
145
- expirationSeconds: '900',
146
- });
147
- return session.session.token;
148
- }
149
- if (turnkeyProvider === TurnkeyProvider.Email) {
150
- if (!metadata?.turnkey?.email) {
151
- throw new WalletException(new Error('Email is required'));
152
- }
153
- const result = await TurnkeyOtpWallet.initEmailOTP({
154
- client,
155
- iframeClient,
156
- email: metadata.turnkey.email,
157
- otpInitPath: metadata.turnkey.otpInitPath,
158
- });
159
- if (result?.organizationId && this.metadata?.turnkey) {
160
- this.metadata.turnkey.organizationId = result.organizationId;
161
- }
162
- if (result?.otpId && this.metadata?.turnkey) {
163
- this.metadata.turnkey.otpId = result.otpId;
164
- }
165
- return result?.otpId || '';
166
- }
167
- if ([TurnkeyProvider.Google, TurnkeyProvider.Apple].includes(turnkeyProvider)) {
168
- if (metadata?.turnkey?.oidcToken) {
169
- const oauthResult = await TurnkeyOauthWallet.oauthLogin({
170
- client,
171
- iframeClient,
172
- oidcToken: metadata.turnkey.oidcToken,
173
- providerName: turnkeyProvider.toString(),
174
- oauthLoginPath: metadata.turnkey.oauthLoginPath,
175
- });
176
- if (oauthResult?.credentialBundle) {
177
- await turnkeyWallet.injectAndRefresh(oauthResult.credentialBundle);
178
- }
179
- if (oauthResult?.credentialBundle) {
180
- const session = await turnkeyWallet.getSession();
181
- if (this.metadata?.turnkey && session.organizationId) {
182
- this.metadata.turnkey.organizationId = session.organizationId;
183
- }
184
- return oauthResult.credentialBundle;
185
- }
186
- throw new WalletException(new Error('Oauth result not found'));
187
- }
188
- else {
189
- const nonce = await TurnkeyOauthWallet.generateOAuthNonce(iframeClient);
190
- if (!metadata?.turnkey?.googleClientId ||
191
- !metadata?.turnkey?.googleRedirectUri) {
192
- throw new WalletException(new Error('googleClientId and googleRedirectUri are required'));
193
- }
194
- if (turnkeyProvider === TurnkeyProvider.Google) {
195
- return generateGoogleUrl({
196
- nonce,
197
- clientId: metadata.turnkey.googleClientId,
198
- redirectUri: metadata.turnkey.googleRedirectUri,
199
- });
200
- }
201
- //? When we add Apple support, we might also want to return the URL as well
202
- return nonce;
203
- }
204
- }
205
- return '';
81
+ return await turnkeyWallet.refreshSession();
82
+ }
83
+ async getWalletClient() {
84
+ return (await this.getTurnkeyWallet());
206
85
  }
207
86
  async sendEthereumTransaction(_transaction, _options) {
208
87
  throw new WalletException(new Error('sendEthereumTransaction is not supported. Turnkey only supports sending cosmos transactions'), {
@@ -12,9 +12,9 @@ export declare class TurnkeyOtpWallet {
12
12
  }): Promise<TurnkeyOTPCredentialsResponse | undefined>;
13
13
  static confirmEmailOTP(args: {
14
14
  otpCode: string;
15
- emailOTPId?: string;
15
+ emailOTPId: string;
16
16
  client: HttpRestClient;
17
- organizationId?: string;
17
+ organizationId: string;
18
18
  iframeClient: TurnkeyIframeClient;
19
19
  otpVerifyPath?: string;
20
20
  }): Promise<TurnkeyConfirmEmailOTPResponse | undefined>;
@@ -1,11 +1,13 @@
1
- import { Turnkey, TurnkeyIframeClient } from '@turnkey/sdk-browser';
2
- import { TurnkeyMetadata } from '@injectivelabs/wallet-base';
1
+ import { TurnkeyMetadata, TurnkeyProvider } from '@injectivelabs/wallet-base';
3
2
  import { HttpRestClient } from '@injectivelabs/utils';
3
+ import { Turnkey, TurnkeyIframeClient } from '@turnkey/sdk-browser';
4
4
  export declare class TurnkeyWallet {
5
- protected iframeClient: TurnkeyIframeClient | undefined;
6
- protected turnkey: Turnkey | undefined;
7
5
  protected client: HttpRestClient;
8
6
  private metadata;
7
+ protected turnkey: Turnkey | undefined;
8
+ protected iframeClient: TurnkeyIframeClient | undefined;
9
+ organizationId: string;
10
+ private otpId;
9
11
  private accountMap;
10
12
  setMetadata(metadata: Partial<TurnkeyMetadata>): void;
11
13
  constructor(metadata: TurnkeyMetadata);
@@ -20,7 +22,7 @@ export declare class TurnkeyWallet {
20
22
  organizationId: string;
21
23
  }>;
22
24
  getAccounts(): Promise<string[]>;
23
- getOrCreateAndGetAccount(address: string, organizationId?: string): Promise<{
25
+ getOrCreateAndGetAccount(address: string, organizationId: string): Promise<{
24
26
  address: import("abitype").Address;
25
27
  nonceManager?: import("viem").NonceManager | undefined;
26
28
  sign?: ((parameters: {
@@ -38,6 +40,14 @@ export declare class TurnkeyWallet {
38
40
  source: string;
39
41
  type: "local";
40
42
  }>;
41
- injectAndRefresh(credentialBundle: string): Promise<void>;
43
+ injectAndRefresh(credentialBundle: string, options: {
44
+ expirationSeconds?: string;
45
+ organizationId?: string;
46
+ }): Promise<void>;
47
+ initOTP(email: string): Promise<import("../types.js").TurnkeyOTPCredentialsResponse>;
48
+ confirmOTP(otpCode: string): Promise<import("../types.js").TurnkeyConfirmEmailOTPResponse>;
49
+ initOAuth(provider: TurnkeyProvider.Google | TurnkeyProvider.Apple): Promise<string>;
50
+ confirmOAuth(provider: TurnkeyProvider.Google | TurnkeyProvider.Apple, oidcToken: string): Promise<string>;
51
+ refreshSession(): Promise<string>;
42
52
  private initFrame;
43
53
  }
@@ -1,21 +1,28 @@
1
- import { Turnkey, SessionType } from '@turnkey/sdk-browser';
2
- import { ErrorType, WalletException, GeneralException, UnspecifiedErrorCode, } from '@injectivelabs/exceptions';
3
- import { WalletAction } from '@injectivelabs/wallet-base';
4
- import { getInjectiveAddress } from '@injectivelabs/sdk-ts';
5
- import { HttpRestClient } from '@injectivelabs/utils';
1
+ import { ErrorType, WalletException, GeneralException, UnspecifiedErrorCode, TurnkeyWalletSessionException, } from '@injectivelabs/exceptions';
2
+ import { WalletAction, TurnkeyProvider, } from '@injectivelabs/wallet-base';
6
3
  import { createAccount } from '@turnkey/viem';
4
+ import { HttpRestClient } from '@injectivelabs/utils';
5
+ import { getInjectiveAddress } from '@injectivelabs/sdk-ts';
6
+ import { Turnkey, SessionType } from '@turnkey/sdk-browser';
7
+ import { TURNKEY_OAUTH_PATH, TURNKEY_OTP_INIT_PATH, TURNKEY_OTP_VERIFY_PATH, DEFAULT_TURNKEY_REFRESH_SECONDS, } from '../consts.js';
8
+ import { TurnkeyOtpWallet } from './otp.js';
7
9
  import { TurnkeyErrorCodes } from '../types.js';
10
+ import { TurnkeyOauthWallet } from './oauth.js';
11
+ import { generateGoogleUrl } from '../../utils.js';
8
12
  export class TurnkeyWallet {
9
- iframeClient;
10
- turnkey;
11
13
  client;
12
14
  metadata;
15
+ turnkey;
16
+ iframeClient;
17
+ organizationId;
18
+ otpId;
13
19
  accountMap = {};
14
20
  setMetadata(metadata) {
15
21
  this.metadata = { ...this.metadata, ...metadata };
16
22
  }
17
23
  constructor(metadata) {
18
24
  this.metadata = metadata;
25
+ this.organizationId = metadata.organizationId;
19
26
  this.client = new HttpRestClient(metadata.apiServerEndpoint);
20
27
  }
21
28
  static async getTurnkeyInstance(metadata) {
@@ -90,16 +97,16 @@ export class TurnkeyWallet {
90
97
  }
91
98
  async getAccounts() {
92
99
  const iframeClient = await this.getIframeClient();
93
- if (!this.metadata.organizationId) {
100
+ if (!this.organizationId) {
94
101
  return [];
95
102
  }
96
103
  try {
97
104
  const response = await iframeClient.getWallets({
98
- organizationId: this.metadata.organizationId,
105
+ organizationId: this.organizationId,
99
106
  });
100
107
  const accounts = await Promise.allSettled(response.wallets.map((wallet) => iframeClient.getWalletAccounts({
101
108
  walletId: wallet.walletId,
102
- organizationId: this.metadata.organizationId,
109
+ organizationId: this.organizationId,
103
110
  })));
104
111
  const filteredAccounts = accounts
105
112
  .filter((account) => account.status === 'fulfilled')
@@ -146,16 +153,120 @@ export class TurnkeyWallet {
146
153
  this.accountMap[address] = turnkeyAccount;
147
154
  return turnkeyAccount;
148
155
  }
149
- async injectAndRefresh(credentialBundle) {
156
+ async injectAndRefresh(credentialBundle, options) {
157
+ const expirationSeconds = options.expirationSeconds || DEFAULT_TURNKEY_REFRESH_SECONDS;
150
158
  const iframeClient = await this.getIframeClient();
151
159
  await iframeClient.injectCredentialBundle(credentialBundle);
152
160
  await iframeClient.refreshSession({
153
161
  sessionType: SessionType.READ_WRITE,
162
+ expirationSeconds: options.expirationSeconds,
154
163
  targetPublicKey: iframeClient.iframePublicKey,
155
- expirationSeconds: '900',
156
164
  });
165
+ // If we just logged in, we have the new org ID here and don't need to wait on getSession method
166
+ if (options.organizationId) {
167
+ this.organizationId = options.organizationId;
168
+ this.metadata.organizationId = options.organizationId;
169
+ }
170
+ else {
171
+ const session = await this.getSession();
172
+ this.organizationId = session.organizationId;
173
+ this.metadata.organizationId = session.organizationId;
174
+ }
175
+ // Refresh the session 2 minutes before it expires
176
+ setTimeout(() => {
177
+ iframeClient.refreshSession({
178
+ expirationSeconds,
179
+ sessionType: SessionType.READ_WRITE,
180
+ targetPublicKey: iframeClient.iframePublicKey,
181
+ });
182
+ }, (parseInt(expirationSeconds) - 120) * 1000);
157
183
  return;
158
184
  }
185
+ async initOTP(email) {
186
+ const iframeClient = await this.getIframeClient();
187
+ const result = await TurnkeyOtpWallet.initEmailOTP({
188
+ client: this.client,
189
+ iframeClient,
190
+ email,
191
+ otpInitPath: this.metadata.otpInitPath || TURNKEY_OTP_INIT_PATH,
192
+ });
193
+ if (!result || !result.otpId) {
194
+ throw new WalletException(new Error('Failed to initialize OTP'));
195
+ }
196
+ if (result?.organizationId) {
197
+ this.organizationId = result.organizationId;
198
+ }
199
+ if (result?.otpId) {
200
+ this.otpId = result.otpId;
201
+ }
202
+ return result;
203
+ }
204
+ async confirmOTP(otpCode) {
205
+ const iframeClient = await this.getIframeClient();
206
+ if (!this.otpId) {
207
+ throw new WalletException(new Error('OTP ID is required'));
208
+ }
209
+ const result = await TurnkeyOtpWallet.confirmEmailOTP({
210
+ otpCode,
211
+ iframeClient,
212
+ client: this.client,
213
+ emailOTPId: this.otpId,
214
+ organizationId: this.organizationId,
215
+ otpVerifyPath: this.metadata.otpVerifyPath || TURNKEY_OTP_VERIFY_PATH,
216
+ });
217
+ if (!result || !result.credentialBundle) {
218
+ throw new WalletException(new Error('Failed to confirm OTP'));
219
+ }
220
+ await this.injectAndRefresh(result.credentialBundle, {
221
+ organizationId: result.organizationId,
222
+ expirationSeconds: this.metadata.expirationSeconds,
223
+ });
224
+ return result;
225
+ }
226
+ async initOAuth(provider) {
227
+ const iframeClient = await this.getIframeClient();
228
+ const nonce = await TurnkeyOauthWallet.generateOAuthNonce(iframeClient);
229
+ if (provider === TurnkeyProvider.Apple) {
230
+ // TODO: implement the ability to generate Apple OAuth URL
231
+ return nonce;
232
+ }
233
+ if (!this.metadata?.googleClientId || !this.metadata?.googleRedirectUri) {
234
+ throw new WalletException(new Error('googleClientId and googleRedirectUri are required'));
235
+ }
236
+ return generateGoogleUrl({
237
+ nonce,
238
+ clientId: this.metadata.googleClientId,
239
+ redirectUri: this.metadata.googleRedirectUri,
240
+ });
241
+ }
242
+ async confirmOAuth(provider, oidcToken) {
243
+ const iframeClient = await this.getIframeClient();
244
+ const oauthResult = await TurnkeyOauthWallet.oauthLogin({
245
+ oidcToken,
246
+ iframeClient,
247
+ client: this.client,
248
+ providerName: provider.toString(),
249
+ oauthLoginPath: this.metadata.oauthLoginPath || TURNKEY_OAUTH_PATH,
250
+ });
251
+ if (!oauthResult || !oauthResult.credentialBundle) {
252
+ throw new WalletException(new Error('Unexpected OAuth result'));
253
+ }
254
+ await this.injectAndRefresh(oauthResult.credentialBundle, {
255
+ organizationId: oauthResult.organizationId,
256
+ expirationSeconds: this.metadata.expirationSeconds,
257
+ });
258
+ return oauthResult.credentialBundle;
259
+ }
260
+ async refreshSession() {
261
+ const session = await this.getSession();
262
+ if (session.session?.token) {
263
+ await this.injectAndRefresh(session.session.token, {
264
+ expirationSeconds: this.metadata.expirationSeconds,
265
+ });
266
+ return session.session.token;
267
+ }
268
+ throw new TurnkeyWalletSessionException(new Error('Session expired. Please login again.'));
269
+ }
159
270
  async initFrame() {
160
271
  const { metadata } = this;
161
272
  const { turnkey, iframeClient } = await createTurnkeyIFrame(metadata);
@@ -18,6 +18,7 @@ export type TurnkeyOTPCredentialsResponse = {
18
18
  };
19
19
  export type TurnkeyConfirmEmailOTPResponse = {
20
20
  credentialBundle: string;
21
+ organizationId: string;
21
22
  };
22
23
  export type TurnkeyOauthLoginResponse = {
23
24
  organizationId: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@injectivelabs/wallet-turnkey",
3
3
  "description": "Turnkey wallet strategy for use with @injectivelabs/wallet-core.",
4
- "version": "1.15.17",
4
+ "version": "1.15.18",
5
5
  "sideEffects": false,
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -59,11 +59,11 @@
59
59
  "start": "node dist/index.js"
60
60
  },
61
61
  "dependencies": {
62
- "@injectivelabs/exceptions": "^1.15.14",
63
- "@injectivelabs/sdk-ts": "^1.15.17",
64
- "@injectivelabs/ts-types": "^1.15.15",
65
- "@injectivelabs/utils": "^1.15.15",
66
- "@injectivelabs/wallet-base": "^1.15.17",
62
+ "@injectivelabs/exceptions": "^1.15.15",
63
+ "@injectivelabs/sdk-ts": "^1.15.18",
64
+ "@injectivelabs/ts-types": "^1.15.16",
65
+ "@injectivelabs/utils": "^1.15.16",
66
+ "@injectivelabs/wallet-base": "^1.15.18",
67
67
  "@turnkey/sdk-browser": "^4.1.0",
68
68
  "@turnkey/viem": "^0.9.0",
69
69
  "viem": "^2.28.1"
@@ -77,5 +77,5 @@
77
77
  "tsconfig-paths": "^4.2.0",
78
78
  "typescript": "^5.0.0"
79
79
  },
80
- "gitHead": "2f3036b124dd1d178fd354b0b37352700f9b060c"
80
+ "gitHead": "f27adbec3b9a84e4af2c2fbf5dbcac802b254130"
81
81
  }
@@ -1,5 +0,0 @@
1
- import { BaseTurnkeyWalletStrategy } from './base.js';
2
- import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
3
- export declare class TurnkeyOauthWalletStrategy extends BaseTurnkeyWalletStrategy {
4
- constructor(args: ConcreteEthereumWalletStrategyArgs);
5
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TurnkeyOauthWalletStrategy = void 0;
4
- const base_js_1 = require("./base.js");
5
- const wallet_base_1 = require("@injectivelabs/wallet-base");
6
- class TurnkeyOauthWalletStrategy extends base_js_1.BaseTurnkeyWalletStrategy {
7
- constructor(args) {
8
- super({
9
- ...args,
10
- provider: wallet_base_1.TurnkeyProvider.Google,
11
- });
12
- }
13
- }
14
- exports.TurnkeyOauthWalletStrategy = TurnkeyOauthWalletStrategy;
@@ -1,5 +0,0 @@
1
- import { BaseTurnkeyWalletStrategy } from './base.js';
2
- import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
3
- export declare class TurnkeyOtpWalletStrategy extends BaseTurnkeyWalletStrategy {
4
- constructor(args: ConcreteEthereumWalletStrategyArgs);
5
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TurnkeyOtpWalletStrategy = void 0;
4
- const base_js_1 = require("./base.js");
5
- const wallet_base_1 = require("@injectivelabs/wallet-base");
6
- class TurnkeyOtpWalletStrategy extends base_js_1.BaseTurnkeyWalletStrategy {
7
- constructor(args) {
8
- super({
9
- ...args,
10
- provider: wallet_base_1.TurnkeyProvider.Email,
11
- });
12
- }
13
- }
14
- exports.TurnkeyOtpWalletStrategy = TurnkeyOtpWalletStrategy;
@@ -1,5 +0,0 @@
1
- import { BaseTurnkeyWalletStrategy } from './base.js';
2
- import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
3
- export declare class TurnkeyOauthWalletStrategy extends BaseTurnkeyWalletStrategy {
4
- constructor(args: ConcreteEthereumWalletStrategyArgs);
5
- }
@@ -1,10 +0,0 @@
1
- import { BaseTurnkeyWalletStrategy } from './base.js';
2
- import { TurnkeyProvider, } from '@injectivelabs/wallet-base';
3
- export class TurnkeyOauthWalletStrategy extends BaseTurnkeyWalletStrategy {
4
- constructor(args) {
5
- super({
6
- ...args,
7
- provider: TurnkeyProvider.Google,
8
- });
9
- }
10
- }
@@ -1,5 +0,0 @@
1
- import { BaseTurnkeyWalletStrategy } from './base.js';
2
- import { ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
3
- export declare class TurnkeyOtpWalletStrategy extends BaseTurnkeyWalletStrategy {
4
- constructor(args: ConcreteEthereumWalletStrategyArgs);
5
- }
@@ -1,10 +0,0 @@
1
- import { BaseTurnkeyWalletStrategy } from './base.js';
2
- import { TurnkeyProvider } from '@injectivelabs/wallet-base';
3
- export class TurnkeyOtpWalletStrategy extends BaseTurnkeyWalletStrategy {
4
- constructor(args) {
5
- super({
6
- ...args,
7
- provider: TurnkeyProvider.Email,
8
- });
9
- }
10
- }