@injectivelabs/wallet-turnkey 1.16.25-alpha.0 → 1.16.25

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.
Files changed (41) hide show
  1. package/dist/cjs/index.d.ts +3 -0
  2. package/dist/cjs/index.js +21 -0
  3. package/dist/cjs/package.json +2 -2
  4. package/dist/cjs/strategy/Eip1193Provider.d.ts +3 -0
  5. package/dist/cjs/strategy/Eip1193Provider.js +173 -0
  6. package/dist/cjs/strategy/consts.d.ts +13 -0
  7. package/dist/cjs/strategy/consts.js +16 -0
  8. package/dist/cjs/strategy/strategy.d.ts +48 -0
  9. package/dist/cjs/strategy/strategy.js +295 -0
  10. package/dist/cjs/strategy/turnkey/oauth.d.ts +16 -0
  11. package/dist/cjs/strategy/turnkey/oauth.js +53 -0
  12. package/dist/cjs/strategy/turnkey/otp.d.ts +23 -0
  13. package/dist/cjs/strategy/turnkey/otp.js +65 -0
  14. package/dist/cjs/strategy/turnkey/turnkey.d.ts +35 -0
  15. package/dist/cjs/strategy/turnkey/turnkey.js +259 -0
  16. package/dist/cjs/strategy/types.d.ts +28 -0
  17. package/dist/cjs/strategy/types.js +6 -0
  18. package/dist/cjs/utils.d.ts +7 -0
  19. package/dist/cjs/utils.js +10 -0
  20. package/dist/esm/index.d.ts +3 -92
  21. package/dist/esm/index.js +3 -711
  22. package/dist/esm/package.json +2 -2
  23. package/dist/esm/strategy/Eip1193Provider.d.ts +3 -0
  24. package/dist/esm/strategy/Eip1193Provider.js +136 -0
  25. package/dist/esm/strategy/consts.d.ts +13 -0
  26. package/dist/esm/strategy/consts.js +13 -0
  27. package/dist/esm/strategy/strategy.d.ts +48 -0
  28. package/dist/esm/strategy/strategy.js +291 -0
  29. package/dist/esm/strategy/turnkey/oauth.d.ts +16 -0
  30. package/dist/esm/strategy/turnkey/oauth.js +49 -0
  31. package/dist/esm/strategy/turnkey/otp.d.ts +23 -0
  32. package/dist/esm/strategy/turnkey/otp.js +61 -0
  33. package/dist/esm/strategy/turnkey/turnkey.d.ts +35 -0
  34. package/dist/esm/strategy/turnkey/turnkey.js +255 -0
  35. package/dist/esm/strategy/types.d.ts +28 -0
  36. package/dist/esm/strategy/types.js +3 -0
  37. package/dist/esm/utils.d.ts +7 -0
  38. package/dist/esm/utils.js +7 -0
  39. package/package.json +20 -18
  40. package/dist/cjs/index.cjs +0 -736
  41. package/dist/cjs/index.d.cts +0 -92
@@ -0,0 +1,3 @@
1
+ export { TurnkeyWalletStrategy } from './strategy/strategy.js';
2
+ export * from './strategy/strategy.js';
3
+ export * from './strategy/turnkey/turnkey.js';
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
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);
21
+ __exportStar(require("./strategy/turnkey/turnkey.js"), exports);
@@ -1,3 +1,3 @@
1
1
  {
2
- "type": "commonjs"
3
- }
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,3 @@
1
+ import type { LocalAccount } from 'viem';
2
+ import type { Eip1193Provider } from '@injectivelabs/wallet-base';
3
+ export declare const getEip1193ProviderForTurnkey: (account: LocalAccount, chainId: string) => Promise<Eip1193Provider>;
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.getEip1193ProviderForTurnkey = void 0;
37
+ const viemChains = __importStar(require("viem/chains"));
38
+ const viem_1 = require("viem");
39
+ const getEip1193ProviderForTurnkey = async (account, chainId) => {
40
+ const provider = new CustomEip1193Provider({
41
+ chainId: parseInt(chainId, 16),
42
+ signTypedData: account.signTypedData.bind(account),
43
+ signMessage: account.signMessage.bind(account),
44
+ signTransaction: account.signTransaction.bind(account),
45
+ account,
46
+ address: account.address,
47
+ });
48
+ return provider;
49
+ };
50
+ exports.getEip1193ProviderForTurnkey = getEip1193ProviderForTurnkey;
51
+ class CustomEip1193Provider {
52
+ chainId;
53
+ signTypedData;
54
+ signMessage;
55
+ signTransaction;
56
+ account;
57
+ address;
58
+ constructor(args) {
59
+ this.chainId = args.chainId ?? 1;
60
+ this.signTypedData = args.signTypedData;
61
+ this.signMessage = args.signMessage;
62
+ this.account = args.account;
63
+ this.address = args.address;
64
+ this.signTransaction = args.signTransaction;
65
+ }
66
+ async requestAccounts() {
67
+ return [this.address];
68
+ }
69
+ getClient() {
70
+ return (0, viem_1.createWalletClient)({
71
+ chain: this.getChain(),
72
+ transport: (0, viem_1.http)(),
73
+ });
74
+ }
75
+ getChain() {
76
+ const chain = (0, viem_1.extractChain)({
77
+ id: this.chainId,
78
+ chains: Object.values(viemChains),
79
+ });
80
+ return chain;
81
+ }
82
+ on(_event, _listener) {
83
+ throw new Error('Not implemented');
84
+ }
85
+ removeListener(..._args) {
86
+ throw new Error('Not implemented!');
87
+ }
88
+ async request(args) {
89
+ if (args.method === 'eth_requestAccounts') {
90
+ return this.requestAccounts();
91
+ }
92
+ if (args.method === 'eth_signTypedData') {
93
+ if (!args.params) {
94
+ throw new Error('params is required');
95
+ }
96
+ return this.signTypedData(args.params[0]);
97
+ }
98
+ if (args.method === 'eth_signMessage') {
99
+ if (!args.params) {
100
+ throw new Error('params is required');
101
+ }
102
+ return this.signMessage(args.params[0]);
103
+ }
104
+ if (args.method === 'eth_chainId') {
105
+ return this.chainId;
106
+ }
107
+ if (args.method === 'wallet_switchEthereumChain') {
108
+ if (!args.params) {
109
+ throw new Error('params is required');
110
+ }
111
+ const chainId = String(args.params[0].chainId).replace('0x', '');
112
+ this.chainId = parseInt(chainId, 16);
113
+ return true;
114
+ }
115
+ if (args.method === 'eth_sendTransaction') {
116
+ if (!args.params) {
117
+ throw new Error('params is required');
118
+ }
119
+ const accountClient = (0, viem_1.createWalletClient)({
120
+ account: this.account,
121
+ chain: this.getChain(),
122
+ transport: (0, viem_1.http)(),
123
+ });
124
+ const client = this.getClient();
125
+ const parseHexValue = (value) => {
126
+ if (typeof value === 'string') {
127
+ const hexValue = value.startsWith('0x') ? value : `0x${value}`;
128
+ return BigInt(hexValue);
129
+ }
130
+ return BigInt(value);
131
+ };
132
+ const txData = args.params[0];
133
+ const processedTransaction = { ...txData };
134
+ const hexFields = [
135
+ 'value',
136
+ 'gas',
137
+ 'gasLimit',
138
+ 'gasPrice',
139
+ 'maxFeePerGas',
140
+ 'maxPriorityFeePerGas',
141
+ ];
142
+ for (const field of hexFields) {
143
+ if (processedTransaction[field] !== undefined) {
144
+ processedTransaction[field] = parseHexValue(processedTransaction[field]);
145
+ }
146
+ }
147
+ const preparedTransaction = await accountClient.prepareTransactionRequest(processedTransaction);
148
+ const signedTransaction = await this.signTransaction(preparedTransaction);
149
+ const tx = await client.sendRawTransaction({
150
+ serializedTransaction: signedTransaction,
151
+ });
152
+ return tx;
153
+ }
154
+ if (args.method === 'eth_getTransactionCount') {
155
+ if (!args.params) {
156
+ throw new Error('params is required');
157
+ }
158
+ const client = (0, viem_1.createPublicClient)({
159
+ chain: this.getChain(),
160
+ transport: (0, viem_1.http)(),
161
+ });
162
+ const count = await client.getTransactionCount({
163
+ address: this.address,
164
+ blockTag: 'pending',
165
+ });
166
+ return `0x${count.toString(16)}`;
167
+ }
168
+ return this.getClient().request({
169
+ method: args.method,
170
+ params: args.params,
171
+ });
172
+ }
173
+ }
@@ -0,0 +1,13 @@
1
+ export declare const TURNKEY_OAUTH_PATH = "turnkey/oauth";
2
+ export declare const TURNKEY_OTP_PATH = "turnkey/otp";
3
+ export declare const TURNKEY_OTP_INIT_PATH = "turnkey/otp/init";
4
+ export declare const TURNKEY_OTP_VERIFY_PATH = "turnkey/otp/verify";
5
+ export declare const DEFAULT_TURNKEY_REFRESH_SECONDS = "86400";
6
+ export declare const DEFAULT_EVM_CHAIN_CONFIG: {
7
+ name: string;
8
+ nativeCurrency: {
9
+ name: string;
10
+ symbol: string;
11
+ decimals: number;
12
+ };
13
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_EVM_CHAIN_CONFIG = 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
+ exports.TURNKEY_OAUTH_PATH = 'turnkey/oauth';
5
+ exports.TURNKEY_OTP_PATH = 'turnkey/otp';
6
+ exports.TURNKEY_OTP_INIT_PATH = `${exports.TURNKEY_OTP_PATH}/init`;
7
+ exports.TURNKEY_OTP_VERIFY_PATH = `${exports.TURNKEY_OTP_PATH}/verify`;
8
+ exports.DEFAULT_TURNKEY_REFRESH_SECONDS = '86400';
9
+ exports.DEFAULT_EVM_CHAIN_CONFIG = {
10
+ name: 'Injective',
11
+ nativeCurrency: {
12
+ name: 'Injective',
13
+ symbol: 'INJ',
14
+ decimals: 18,
15
+ },
16
+ };
@@ -0,0 +1,48 @@
1
+ import { HttpRestClient } from '@injectivelabs/utils';
2
+ import { WalletDeviceType, type WalletMetadata, BaseConcreteStrategy } from '@injectivelabs/wallet-base';
3
+ import { TurnkeyWallet } from './turnkey/turnkey.js';
4
+ import type { EvmChainId } from '@injectivelabs/ts-types';
5
+ import type { AccountAddress } from '@injectivelabs/ts-types';
6
+ import type { TurnkeyIndexedDbClient } from '@turnkey/sdk-browser';
7
+ import type { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
8
+ import type { StdSignDoc, Eip1193Provider, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEvmOptions, ConcreteEvmWalletStrategyArgs } from '@injectivelabs/wallet-base';
9
+ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
10
+ turnkeyWallet?: TurnkeyWallet;
11
+ evmOptions: WalletStrategyEvmOptions;
12
+ client: HttpRestClient;
13
+ constructor(args: ConcreteEvmWalletStrategyArgs & {
14
+ apiServerEndpoint?: string;
15
+ });
16
+ getWalletDeviceType(): Promise<WalletDeviceType>;
17
+ setMetadata(metadata?: {
18
+ turnkey?: Partial<WalletMetadata['turnkey']>;
19
+ }): void;
20
+ enable(): Promise<boolean>;
21
+ disconnect(): Promise<void>;
22
+ getAddresses(): Promise<string[]>;
23
+ getSessionOrConfirm(_address?: string): Promise<string>;
24
+ getWalletClient<TurnkeyWallet>(): Promise<TurnkeyWallet>;
25
+ sendEvmTransaction(transaction: unknown, args: {
26
+ address: AccountAddress;
27
+ evmChainId: EvmChainId;
28
+ }): Promise<string>;
29
+ sendTransaction(transaction: TxRaw, options: SendTransactionOptions): Promise<any>;
30
+ signEip712TypedData(eip712json: string, address: AccountAddress): Promise<string>;
31
+ signCosmosTransaction(_transaction: {
32
+ txRaw: TxRaw;
33
+ accountNumber: number;
34
+ chainId: string;
35
+ address: string;
36
+ }): Promise<DirectSignResponse>;
37
+ signAminoCosmosTransaction(_transaction: {
38
+ address: string;
39
+ signDoc: StdSignDoc;
40
+ }): Promise<AminoSignResponse>;
41
+ signArbitrary(_signer: AccountAddress, _data: string | Uint8Array): Promise<string>;
42
+ getEthereumChainId(): Promise<string>;
43
+ getEvmTransactionReceipt(txHash: string, evmChainId?: EvmChainId): Promise<Record<string, any>>;
44
+ getPubKey(): Promise<string>;
45
+ getIndexedDbClient(): Promise<TurnkeyIndexedDbClient>;
46
+ private getTurnkeyWallet;
47
+ getEip1193Provider(): Promise<Eip1193Provider>;
48
+ }
@@ -0,0 +1,295 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TurnkeyWalletStrategy = void 0;
4
+ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
5
+ const sdk_ts_2 = require("@injectivelabs/sdk-ts");
6
+ const utils_1 = require("@injectivelabs/utils");
7
+ const viem_1 = require("viem");
8
+ const wallet_base_1 = require("@injectivelabs/wallet-base");
9
+ const exceptions_1 = require("@injectivelabs/exceptions");
10
+ const types_js_1 = require("./types.js");
11
+ const turnkey_js_1 = require("./turnkey/turnkey.js");
12
+ const consts_js_1 = require("./consts.js");
13
+ const Eip1193Provider_js_1 = require("./Eip1193Provider.js");
14
+ class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
15
+ turnkeyWallet;
16
+ evmOptions;
17
+ client;
18
+ constructor(args) {
19
+ super(args);
20
+ const endpoint = args.apiServerEndpoint || this.metadata?.turnkey?.apiServerEndpoint;
21
+ if (!endpoint) {
22
+ throw new exceptions_1.WalletException(new Error('apiServerEndpoint is required'));
23
+ }
24
+ this.client = new utils_1.HttpRestClient(endpoint);
25
+ this.evmOptions = args.evmOptions;
26
+ }
27
+ async getWalletDeviceType() {
28
+ return Promise.resolve(wallet_base_1.WalletDeviceType.Browser);
29
+ }
30
+ setMetadata(metadata) {
31
+ if (metadata?.turnkey) {
32
+ this.metadata = {
33
+ ...this.metadata,
34
+ turnkey: {
35
+ ...this.metadata?.turnkey,
36
+ ...metadata.turnkey,
37
+ },
38
+ };
39
+ this.turnkeyWallet?.setMetadata(this.metadata?.turnkey);
40
+ }
41
+ }
42
+ async enable() {
43
+ const turnkeyWallet = await this.getTurnkeyWallet();
44
+ try {
45
+ const session = await turnkeyWallet.getSession();
46
+ if (session.session) {
47
+ // User is already logged in, we don't need to do anything in the next steps
48
+ if (this.metadata?.turnkey) {
49
+ this.metadata.turnkey.session = session.session;
50
+ }
51
+ return true;
52
+ }
53
+ return !!(await turnkeyWallet.getIndexedDbClient());
54
+ }
55
+ catch {
56
+ return false;
57
+ }
58
+ }
59
+ async disconnect() {
60
+ const turnkeyWallet = await this.getTurnkeyWallet();
61
+ const turnkey = await turnkeyWallet.getTurnkey();
62
+ const indexedDbClient = await turnkeyWallet.getIndexedDbClient();
63
+ const isUserLoggedIn = await turnkey.getSession();
64
+ if (!isUserLoggedIn) {
65
+ return;
66
+ }
67
+ await Promise.allSettled([turnkey.logout(), indexedDbClient.clear()]);
68
+ }
69
+ async getAddresses() {
70
+ const turnkeyWallet = await this.getTurnkeyWallet();
71
+ try {
72
+ return await turnkeyWallet.getAccounts();
73
+ }
74
+ catch (e) {
75
+ if (e.contextCode === types_js_1.TurnkeyErrorCodes.UserLoggedOut) {
76
+ await this.disconnect();
77
+ throw e;
78
+ }
79
+ throw new exceptions_1.WalletException(new Error(e.message), {
80
+ code: exceptions_1.UnspecifiedErrorCode,
81
+ type: exceptions_1.ErrorType.WalletError,
82
+ contextModule: wallet_base_1.WalletAction.GetAccounts,
83
+ });
84
+ }
85
+ }
86
+ async getSessionOrConfirm(_address) {
87
+ const turnkeyWallet = await this.getTurnkeyWallet();
88
+ return await turnkeyWallet.refreshSession();
89
+ }
90
+ async getWalletClient() {
91
+ return (await this.getTurnkeyWallet());
92
+ }
93
+ async sendEvmTransaction(transaction, args) {
94
+ try {
95
+ const options = this.evmOptions;
96
+ const turnkeyWallet = await this.getTurnkeyWallet();
97
+ const chainId = args.evmChainId || options.evmChainId;
98
+ const url = options.rpcUrl || options.rpcUrls?.[args.evmChainId];
99
+ if (!url) {
100
+ throw new exceptions_1.WalletException(new Error('Please pass rpcUrl within the evmOptions'), {
101
+ code: exceptions_1.UnspecifiedErrorCode,
102
+ context: wallet_base_1.WalletAction.SendEvmTransaction,
103
+ });
104
+ }
105
+ const account = await turnkeyWallet.getOrCreateAndGetAccount((0, viem_1.getAddress)(args.address));
106
+ const accountClient = (0, viem_1.createWalletClient)({
107
+ account: account,
108
+ chain: {
109
+ ...consts_js_1.DEFAULT_EVM_CHAIN_CONFIG,
110
+ id: chainId,
111
+ rpcUrls: {
112
+ default: {
113
+ http: [url],
114
+ },
115
+ },
116
+ },
117
+ transport: (0, viem_1.http)(url),
118
+ });
119
+ const parseHexValue = (value) => {
120
+ if (typeof value === 'string') {
121
+ const hexValue = value.startsWith('0x') ? value : `0x${value}`;
122
+ return BigInt(hexValue);
123
+ }
124
+ return BigInt(value);
125
+ };
126
+ const txData = transaction;
127
+ const processedTransaction = { ...txData };
128
+ const hexFields = [
129
+ 'value',
130
+ 'gas',
131
+ 'gasLimit',
132
+ 'gasPrice',
133
+ 'maxFeePerGas',
134
+ 'maxPriorityFeePerGas',
135
+ ];
136
+ for (const field of hexFields) {
137
+ if (processedTransaction[field] !== undefined) {
138
+ processedTransaction[field] = parseHexValue(processedTransaction[field]);
139
+ }
140
+ }
141
+ const preparedTransaction = await accountClient.prepareTransactionRequest(processedTransaction);
142
+ delete preparedTransaction.account;
143
+ const signedTransaction = await accountClient.signTransaction(preparedTransaction);
144
+ const tx = await accountClient.sendRawTransaction({
145
+ serializedTransaction: signedTransaction,
146
+ });
147
+ return tx;
148
+ }
149
+ catch (e) {
150
+ throw new exceptions_1.WalletException(e, {
151
+ code: exceptions_1.UnspecifiedErrorCode,
152
+ context: wallet_base_1.WalletAction.SendEvmTransaction,
153
+ });
154
+ }
155
+ }
156
+ async sendTransaction(transaction, options) {
157
+ const { endpoints, txTimeout } = options;
158
+ if (!endpoints) {
159
+ throw new exceptions_1.WalletException(new Error('You have to pass endpoints.grpc within the options for using Turnkey wallet'));
160
+ }
161
+ const txApi = new sdk_ts_1.TxGrpcApi(endpoints.grpc);
162
+ const response = await txApi.broadcast(transaction, { txTimeout });
163
+ if (response.code !== 0) {
164
+ throw new exceptions_1.TransactionException(new Error(response.rawLog), {
165
+ code: exceptions_1.UnspecifiedErrorCode,
166
+ contextCode: response.code,
167
+ contextModule: response.codespace,
168
+ });
169
+ }
170
+ return response;
171
+ }
172
+ async signEip712TypedData(eip712json, address) {
173
+ const turnkeyWallet = await this.getTurnkeyWallet();
174
+ //? Turnkey expects the case sensitive address and the current impl of getChecksumAddress from sdk-ts doesn't play nice with browser envs
175
+ const checksumAddress = (0, viem_1.getAddress)(address);
176
+ const account = await turnkeyWallet.getOrCreateAndGetAccount(checksumAddress);
177
+ if (!account) {
178
+ throw new exceptions_1.WalletException(new Error('Turnkey account not found'));
179
+ }
180
+ let parsedData;
181
+ try {
182
+ parsedData = JSON.parse(eip712json);
183
+ }
184
+ catch {
185
+ throw new exceptions_1.WalletException(new Error('Failed to parse EIP-712 data: Invalid JSON format'), {
186
+ code: exceptions_1.UnspecifiedErrorCode,
187
+ type: exceptions_1.ErrorType.WalletError,
188
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
189
+ });
190
+ }
191
+ const signature = await account.signTypedData(parsedData);
192
+ return signature;
193
+ }
194
+ async signCosmosTransaction(_transaction) {
195
+ throw new exceptions_1.WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
196
+ code: exceptions_1.UnspecifiedErrorCode,
197
+ type: exceptions_1.ErrorType.WalletError,
198
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
199
+ });
200
+ }
201
+ async signAminoCosmosTransaction(_transaction) {
202
+ throw new exceptions_1.WalletException(new Error('This wallet does not support signAminoCosmosTransaction'), {
203
+ code: exceptions_1.UnspecifiedErrorCode,
204
+ type: exceptions_1.ErrorType.WalletError,
205
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
206
+ });
207
+ }
208
+ async signArbitrary(_signer, _data) {
209
+ throw new exceptions_1.WalletException(new Error('This wallet does not support signArbitrary'), {
210
+ code: exceptions_1.UnspecifiedErrorCode,
211
+ type: exceptions_1.ErrorType.WalletError,
212
+ contextModule: wallet_base_1.WalletAction.SignTransaction,
213
+ });
214
+ }
215
+ async getEthereumChainId() {
216
+ throw new exceptions_1.CosmosWalletException(new Error('getEthereumChainId is not supported on Turnkey wallet'), {
217
+ code: exceptions_1.UnspecifiedErrorCode,
218
+ context: wallet_base_1.WalletAction.GetChainId,
219
+ });
220
+ }
221
+ async getEvmTransactionReceipt(txHash, evmChainId) {
222
+ const options = this.evmOptions;
223
+ const maxAttempts = 10;
224
+ const interval = 3000;
225
+ const chainId = evmChainId || options.evmChainId;
226
+ const url = options.rpcUrl || options.rpcUrls?.[chainId];
227
+ if (!url) {
228
+ throw new exceptions_1.WalletException(new Error('Please pass rpcUrl within the evmOptions'), {
229
+ code: exceptions_1.UnspecifiedErrorCode,
230
+ context: wallet_base_1.WalletAction.GetEvmTransactionReceipt,
231
+ });
232
+ }
233
+ const publicClient = (0, viem_1.createPublicClient)({
234
+ chain: {
235
+ ...consts_js_1.DEFAULT_EVM_CHAIN_CONFIG,
236
+ id: chainId,
237
+ rpcUrls: {
238
+ default: {
239
+ http: [url],
240
+ },
241
+ },
242
+ },
243
+ transport: (0, viem_1.http)(url),
244
+ });
245
+ let attempts = 0;
246
+ while (attempts < maxAttempts) {
247
+ attempts++;
248
+ await (0, utils_1.sleep)(interval);
249
+ try {
250
+ const receipt = await publicClient.getTransactionReceipt({
251
+ hash: txHash,
252
+ });
253
+ if (receipt) {
254
+ return receipt;
255
+ }
256
+ }
257
+ catch { }
258
+ }
259
+ throw new Error(`Failed to retrieve transaction receipt for txHash: ${txHash}`);
260
+ }
261
+ async getPubKey() {
262
+ throw new exceptions_1.WalletException(new Error('You can only fetch PubKey from Cosmos native wallets'));
263
+ }
264
+ async getIndexedDbClient() {
265
+ const turnkeyWallet = await this.getTurnkeyWallet();
266
+ const indexedDbClient = await turnkeyWallet.getIndexedDbClient();
267
+ return indexedDbClient;
268
+ }
269
+ async getTurnkeyWallet() {
270
+ const { metadata } = this;
271
+ if (!this.turnkeyWallet) {
272
+ if (!metadata?.turnkey) {
273
+ throw new exceptions_1.WalletException(new Error('Turnkey metadata is required'));
274
+ }
275
+ if (!metadata.turnkey.apiBaseUrl) {
276
+ throw new exceptions_1.WalletException(new Error('Turnkey apiBaseUrl is required'));
277
+ }
278
+ if (!metadata.turnkey.apiServerEndpoint) {
279
+ throw new exceptions_1.WalletException(new Error('Turnkey apiServerEndpoint is required'));
280
+ }
281
+ this.turnkeyWallet = new turnkey_js_1.TurnkeyWallet(metadata.turnkey);
282
+ }
283
+ return this.turnkeyWallet;
284
+ }
285
+ async getEip1193Provider() {
286
+ const turnkeyWallet = await this.getTurnkeyWallet();
287
+ const addresses = await turnkeyWallet.getAccounts();
288
+ //? Turnkey expects the case sensitive address and the current impl of getChecksumAddress from sdk-ts doesn't play nice with browser envs
289
+ const checksumAddress = (0, viem_1.getAddress)((0, sdk_ts_2.getEthereumAddress)(addresses[0]));
290
+ const account = await turnkeyWallet.getOrCreateAndGetAccount(checksumAddress);
291
+ const eip1193Provider = await (0, Eip1193Provider_js_1.getEip1193ProviderForTurnkey)(account, String(this.evmOptions.evmChainId));
292
+ return eip1193Provider;
293
+ }
294
+ }
295
+ exports.TurnkeyWalletStrategy = TurnkeyWalletStrategy;
@@ -0,0 +1,16 @@
1
+ import type { HttpRestClient } from '@injectivelabs/utils';
2
+ import type { TurnkeyIndexedDbClient } from '@turnkey/sdk-browser';
3
+ export declare class TurnkeyOauthWallet {
4
+ static generateOAuthNonce(indexedDbClient: TurnkeyIndexedDbClient): Promise<string>;
5
+ static oauthLogin(args: {
6
+ oidcToken: string;
7
+ client: HttpRestClient;
8
+ oauthLoginPath?: string;
9
+ providerName: 'google' | 'apple';
10
+ indexedDbClient: TurnkeyIndexedDbClient;
11
+ expirationSeconds?: number;
12
+ }): Promise<{
13
+ organizationId: string;
14
+ credentialBundle: string;
15
+ } | undefined>;
16
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TurnkeyOauthWallet = void 0;
4
+ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
5
+ const exceptions_1 = require("@injectivelabs/exceptions");
6
+ const consts_js_1 = require("../consts.js");
7
+ class TurnkeyOauthWallet {
8
+ static async generateOAuthNonce(indexedDbClient) {
9
+ try {
10
+ await indexedDbClient.resetKeyPair();
11
+ const targetPublicKey = await indexedDbClient.getPublicKey();
12
+ if (!targetPublicKey) {
13
+ throw new exceptions_1.WalletException(new Error('Target public key not found'));
14
+ }
15
+ return Array.from((0, sdk_ts_1.sha256)(new TextEncoder().encode(targetPublicKey)))
16
+ .map((b) => b.toString(16).padStart(2, '0'))
17
+ .join('');
18
+ }
19
+ catch (e) {
20
+ throw new exceptions_1.WalletException(new Error(e.message), {
21
+ code: exceptions_1.UnspecifiedErrorCode,
22
+ type: exceptions_1.ErrorType.WalletError,
23
+ contextModule: 'turnkey-generate-oauth-nonce',
24
+ });
25
+ }
26
+ }
27
+ static async oauthLogin(args) {
28
+ const { client, indexedDbClient, expirationSeconds } = args;
29
+ const path = args.oauthLoginPath || consts_js_1.TURNKEY_OAUTH_PATH;
30
+ try {
31
+ const targetPublicKey = await indexedDbClient.getPublicKey();
32
+ if (!targetPublicKey) {
33
+ throw new exceptions_1.WalletException(new Error('Target public key not found'));
34
+ }
35
+ // client.$post is undefined, resorting to this for now
36
+ const response = await client.post(path, {
37
+ targetPublicKey,
38
+ oidcToken: args.oidcToken,
39
+ providerName: args.providerName,
40
+ expirationSeconds: (expirationSeconds || consts_js_1.DEFAULT_TURNKEY_REFRESH_SECONDS)?.toString(),
41
+ });
42
+ return response.data;
43
+ }
44
+ catch (e) {
45
+ throw new exceptions_1.WalletException(new Error(e.message), {
46
+ code: exceptions_1.UnspecifiedErrorCode,
47
+ type: exceptions_1.ErrorType.WalletError,
48
+ contextModule: 'turnkey-oauth-login',
49
+ });
50
+ }
51
+ }
52
+ }
53
+ exports.TurnkeyOauthWallet = TurnkeyOauthWallet;
@@ -0,0 +1,23 @@
1
+ import { type TurnkeyConfirmEmailOTPResponse, type TurnkeyOTPCredentialsResponse } from './../types.js';
2
+ import type { HttpRestClient } from '@injectivelabs/utils';
3
+ import type { TurnkeyIndexedDbClient } from '@turnkey/sdk-browser';
4
+ export declare class TurnkeyOtpWallet {
5
+ static initEmailOTP(args: {
6
+ email: string;
7
+ subOrgId?: string;
8
+ otpInitPath?: string;
9
+ client: HttpRestClient;
10
+ indexedDbClient: TurnkeyIndexedDbClient;
11
+ invalidateExistingSessions?: boolean;
12
+ expirationSeconds?: number;
13
+ }): Promise<TurnkeyOTPCredentialsResponse | undefined>;
14
+ static confirmEmailOTP(args: {
15
+ otpCode: string;
16
+ emailOTPId: string;
17
+ client: HttpRestClient;
18
+ targetPublicKey: string;
19
+ organizationId: string;
20
+ otpVerifyPath?: string;
21
+ expirationSeconds?: number;
22
+ }): Promise<TurnkeyConfirmEmailOTPResponse | undefined>;
23
+ }