@injectivelabs/wallet-turnkey 1.16.10 → 1.16.11-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.
@@ -0,0 +1,3 @@
1
+ import { Eip1193Provider } from '@injectivelabs/wallet-base';
2
+ import { LocalAccount } from 'viem';
3
+ export declare const getEip1193ProviderForTurnkey: (account: LocalAccount, chainId: string) => Promise<Eip1193Provider>;
@@ -0,0 +1,151 @@
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 viem_1 = require("viem");
38
+ const viemChains = __importStar(require("viem/chains"));
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 preparedTransaction = await accountClient.prepareTransactionRequest(args.params[0]);
126
+ const signedTransaction = await this.signTransaction(preparedTransaction);
127
+ const tx = await client.sendRawTransaction({
128
+ serializedTransaction: signedTransaction,
129
+ });
130
+ return tx;
131
+ }
132
+ if (args.method === 'eth_getTransactionCount') {
133
+ if (!args.params) {
134
+ throw new Error('params is required');
135
+ }
136
+ const client = (0, viem_1.createPublicClient)({
137
+ chain: this.getChain(),
138
+ transport: (0, viem_1.http)(),
139
+ });
140
+ const count = await client.getTransactionCount({
141
+ address: this.address,
142
+ blockTag: 'pending',
143
+ });
144
+ return `0x${count.toString(16)}`;
145
+ }
146
+ return this.getClient().request({
147
+ method: args.method,
148
+ params: args.params,
149
+ });
150
+ }
151
+ }
@@ -1,5 +1,5 @@
1
1
  import { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
2
- import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEvmOptions, ConcreteEvmWalletStrategyArgs } from '@injectivelabs/wallet-base';
2
+ import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEvmOptions, ConcreteEvmWalletStrategyArgs, Eip1193Provider } from '@injectivelabs/wallet-base';
3
3
  import { HttpRestClient } from '@injectivelabs/utils';
4
4
  import { TurnkeyIndexedDbClient } from '@turnkey/sdk-browser';
5
5
  import { AccountAddress, EvmChainId } from '@injectivelabs/ts-types';
@@ -42,4 +42,5 @@ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implemen
42
42
  getPubKey(): Promise<string>;
43
43
  getIndexedDbClient(): Promise<TurnkeyIndexedDbClient>;
44
44
  private getTurnkeyWallet;
45
+ getEip1193Provider(): Promise<Eip1193Provider>;
45
46
  }
@@ -10,6 +10,7 @@ const utils_1 = require("@injectivelabs/utils");
10
10
  const types_js_1 = require("./types.js");
11
11
  const turnkey_js_1 = require("./turnkey/turnkey.js");
12
12
  const consts_js_1 = require("./consts.js");
13
+ const Eip1193Provider_js_1 = require("./Eip1193Provider.js");
13
14
  class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
14
15
  turnkeyWallet;
15
16
  evmOptions;
@@ -261,5 +262,14 @@ class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
261
262
  }
262
263
  return this.turnkeyWallet;
263
264
  }
265
+ async getEip1193Provider() {
266
+ const turnkeyWallet = await this.getTurnkeyWallet();
267
+ const addresses = await turnkeyWallet.getAccounts();
268
+ //? Turnkey expects the case sensitive address and the current impl of getChecksumAddress from sdk-ts doesn't play nice with browser envs
269
+ const checksumAddress = (0, viem_1.getAddress)((0, sdk_ts_1.getEthereumAddress)(addresses[0]));
270
+ const account = await turnkeyWallet.getOrCreateAndGetAccount(checksumAddress);
271
+ const eip1193Provider = await (0, Eip1193Provider_js_1.getEip1193ProviderForTurnkey)(account, String(this.evmOptions.evmChainId));
272
+ return eip1193Provider;
273
+ }
264
274
  }
265
275
  exports.TurnkeyWalletStrategy = TurnkeyWalletStrategy;
@@ -125,6 +125,7 @@ class TurnkeyWallet {
125
125
  const { accountMap } = this;
126
126
  const indexedDbClient = await this.getIndexedDbClient();
127
127
  const organizationId = this.userOrganizationId;
128
+ console.log('accountMap', accountMap);
128
129
  if (accountMap[address] || accountMap[address.toLowerCase()]) {
129
130
  return accountMap[address] || accountMap[address.toLowerCase()];
130
131
  }
@@ -135,6 +136,7 @@ class TurnkeyWallet {
135
136
  if (!address) {
136
137
  throw new exceptions_1.WalletException(new Error('Account address not found'));
137
138
  }
139
+ console.log('address', address);
138
140
  const turnkeyAccount = await (0, viem_1.createAccount)({
139
141
  organizationId,
140
142
  signWith: address,
@@ -0,0 +1,3 @@
1
+ import { Eip1193Provider } from '@injectivelabs/wallet-base';
2
+ import { LocalAccount } from 'viem';
3
+ export declare const getEip1193ProviderForTurnkey: (account: LocalAccount, chainId: string) => Promise<Eip1193Provider>;
@@ -0,0 +1,114 @@
1
+ import { extractChain, createWalletClient, http, createPublicClient, } from 'viem';
2
+ import * as viemChains from 'viem/chains';
3
+ export const getEip1193ProviderForTurnkey = async (account, chainId) => {
4
+ const provider = new CustomEip1193Provider({
5
+ chainId: parseInt(chainId, 16),
6
+ signTypedData: account.signTypedData.bind(account),
7
+ signMessage: account.signMessage.bind(account),
8
+ signTransaction: account.signTransaction.bind(account),
9
+ account,
10
+ address: account.address,
11
+ });
12
+ return provider;
13
+ };
14
+ class CustomEip1193Provider {
15
+ chainId;
16
+ signTypedData;
17
+ signMessage;
18
+ signTransaction;
19
+ account;
20
+ address;
21
+ constructor(args) {
22
+ this.chainId = args.chainId ?? 1;
23
+ this.signTypedData = args.signTypedData;
24
+ this.signMessage = args.signMessage;
25
+ this.account = args.account;
26
+ this.address = args.address;
27
+ this.signTransaction = args.signTransaction;
28
+ }
29
+ async requestAccounts() {
30
+ return [this.address];
31
+ }
32
+ getClient() {
33
+ return createWalletClient({
34
+ chain: this.getChain(),
35
+ transport: http(),
36
+ });
37
+ }
38
+ getChain() {
39
+ const chain = extractChain({
40
+ id: this.chainId,
41
+ chains: Object.values(viemChains),
42
+ });
43
+ return chain;
44
+ }
45
+ on(_event, _listener) {
46
+ throw new Error('Not implemented');
47
+ }
48
+ removeListener(..._args) {
49
+ throw new Error('Not implemented!');
50
+ }
51
+ async request(args) {
52
+ if (args.method === 'eth_requestAccounts') {
53
+ return this.requestAccounts();
54
+ }
55
+ if (args.method === 'eth_signTypedData') {
56
+ if (!args.params) {
57
+ throw new Error('params is required');
58
+ }
59
+ return this.signTypedData(args.params[0]);
60
+ }
61
+ if (args.method === 'eth_signMessage') {
62
+ if (!args.params) {
63
+ throw new Error('params is required');
64
+ }
65
+ return this.signMessage(args.params[0]);
66
+ }
67
+ if (args.method === 'eth_chainId') {
68
+ return this.chainId;
69
+ }
70
+ if (args.method === 'wallet_switchEthereumChain') {
71
+ if (!args.params) {
72
+ throw new Error('params is required');
73
+ }
74
+ const chainId = String(args.params[0].chainId).replace('0x', '');
75
+ this.chainId = parseInt(chainId, 16);
76
+ return true;
77
+ }
78
+ if (args.method === 'eth_sendTransaction') {
79
+ if (!args.params) {
80
+ throw new Error('params is required');
81
+ }
82
+ const accountClient = createWalletClient({
83
+ account: this.account,
84
+ chain: this.getChain(),
85
+ transport: http(),
86
+ });
87
+ const client = this.getClient();
88
+ const preparedTransaction = await accountClient.prepareTransactionRequest(args.params[0]);
89
+ const signedTransaction = await this.signTransaction(preparedTransaction);
90
+ const tx = await client.sendRawTransaction({
91
+ serializedTransaction: signedTransaction,
92
+ });
93
+ return tx;
94
+ }
95
+ if (args.method === 'eth_getTransactionCount') {
96
+ if (!args.params) {
97
+ throw new Error('params is required');
98
+ }
99
+ const client = createPublicClient({
100
+ chain: this.getChain(),
101
+ transport: http(),
102
+ });
103
+ const count = await client.getTransactionCount({
104
+ address: this.address,
105
+ blockTag: 'pending',
106
+ });
107
+ return `0x${count.toString(16)}`;
108
+ }
109
+ return this.getClient().request({
110
+ method: args.method,
111
+ params: args.params,
112
+ });
113
+ }
114
+ }
@@ -1,5 +1,5 @@
1
1
  import { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
2
- import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEvmOptions, ConcreteEvmWalletStrategyArgs } from '@injectivelabs/wallet-base';
2
+ import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEvmOptions, ConcreteEvmWalletStrategyArgs, Eip1193Provider } from '@injectivelabs/wallet-base';
3
3
  import { HttpRestClient } from '@injectivelabs/utils';
4
4
  import { TurnkeyIndexedDbClient } from '@turnkey/sdk-browser';
5
5
  import { AccountAddress, EvmChainId } from '@injectivelabs/ts-types';
@@ -42,4 +42,5 @@ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implemen
42
42
  getPubKey(): Promise<string>;
43
43
  getIndexedDbClient(): Promise<TurnkeyIndexedDbClient>;
44
44
  private getTurnkeyWallet;
45
+ getEip1193Provider(): Promise<Eip1193Provider>;
45
46
  }
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable class-methods-use-this */
2
- import { TxGrpcApi, } from '@injectivelabs/sdk-ts';
2
+ import { TxGrpcApi, getEthereumAddress, } from '@injectivelabs/sdk-ts';
3
3
  import { ErrorType, WalletException, UnspecifiedErrorCode, TransactionException, CosmosWalletException, } from '@injectivelabs/exceptions';
4
4
  import { http, getAddress, createPublicClient, createWalletClient, } from 'viem';
5
5
  import { WalletAction, WalletDeviceType, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
@@ -7,6 +7,7 @@ import { sleep, HttpRestClient } from '@injectivelabs/utils';
7
7
  import { TurnkeyErrorCodes } from './types.js';
8
8
  import { TurnkeyWallet } from './turnkey/turnkey.js';
9
9
  import { DEFAULT_EVM_CHAIN_CONFIG } from './consts.js';
10
+ import { getEip1193ProviderForTurnkey } from './Eip1193Provider.js';
10
11
  export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
11
12
  turnkeyWallet;
12
13
  evmOptions;
@@ -258,4 +259,13 @@ export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
258
259
  }
259
260
  return this.turnkeyWallet;
260
261
  }
262
+ async getEip1193Provider() {
263
+ const turnkeyWallet = await this.getTurnkeyWallet();
264
+ const addresses = await turnkeyWallet.getAccounts();
265
+ //? Turnkey expects the case sensitive address and the current impl of getChecksumAddress from sdk-ts doesn't play nice with browser envs
266
+ const checksumAddress = getAddress(getEthereumAddress(addresses[0]));
267
+ const account = await turnkeyWallet.getOrCreateAndGetAccount(checksumAddress);
268
+ const eip1193Provider = await getEip1193ProviderForTurnkey(account, String(this.evmOptions.evmChainId));
269
+ return eip1193Provider;
270
+ }
261
271
  }
@@ -122,6 +122,7 @@ export class TurnkeyWallet {
122
122
  const { accountMap } = this;
123
123
  const indexedDbClient = await this.getIndexedDbClient();
124
124
  const organizationId = this.userOrganizationId;
125
+ console.log('accountMap', accountMap);
125
126
  if (accountMap[address] || accountMap[address.toLowerCase()]) {
126
127
  return accountMap[address] || accountMap[address.toLowerCase()];
127
128
  }
@@ -132,6 +133,7 @@ export class TurnkeyWallet {
132
133
  if (!address) {
133
134
  throw new WalletException(new Error('Account address not found'));
134
135
  }
136
+ console.log('address', address);
135
137
  const turnkeyAccount = await createAccount({
136
138
  organizationId,
137
139
  signWith: address,
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.16.10",
4
+ "version": "1.16.11-alpha.0",
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.16.10",
63
- "@injectivelabs/sdk-ts": "1.16.10",
64
- "@injectivelabs/ts-types": "1.16.10",
65
- "@injectivelabs/utils": "1.16.10",
66
- "@injectivelabs/wallet-base": "1.16.10",
62
+ "@injectivelabs/exceptions": "1.16.11-alpha.0",
63
+ "@injectivelabs/sdk-ts": "1.16.11-alpha.0",
64
+ "@injectivelabs/ts-types": "1.16.11-alpha.0",
65
+ "@injectivelabs/utils": "1.16.11-alpha.0",
66
+ "@injectivelabs/wallet-base": "1.16.11-alpha.0",
67
67
  "@turnkey/sdk-browser": "5.2.3",
68
68
  "@turnkey/viem": "^0.9.10",
69
69
  "viem": "^2.33.2"
@@ -77,5 +77,5 @@
77
77
  "tsconfig-paths": "^4.2.0",
78
78
  "typescript": "^5.0.0"
79
79
  },
80
- "gitHead": "77a3be1fe388487b0fd4bdc9b2d9179ccff164d3"
80
+ "gitHead": "640edf06e57aa38d3176e38fa03dddfc72e901cf"
81
81
  }