@injectivelabs/wallet-turnkey 1.15.31 → 1.15.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/strategy/consts.d.ts +8 -0
- package/dist/cjs/strategy/consts.js +9 -1
- package/dist/cjs/strategy/strategy.d.ts +5 -4
- package/dist/cjs/strategy/strategy.js +83 -10
- package/dist/cjs/strategy/turnkey/turnkey.d.ts +7 -23
- package/dist/cjs/strategy/turnkey/turnkey.js +8 -4
- package/dist/esm/strategy/consts.d.ts +8 -0
- package/dist/esm/strategy/consts.js +8 -0
- package/dist/esm/strategy/strategy.d.ts +5 -4
- package/dist/esm/strategy/strategy.js +84 -11
- package/dist/esm/strategy/turnkey/turnkey.d.ts +7 -23
- package/dist/esm/strategy/turnkey/turnkey.js +9 -5
- package/package.json +10 -10
|
@@ -3,3 +3,11 @@ 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
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
|
+
};
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
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
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
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
|
+
};
|
|
@@ -2,10 +2,11 @@ import { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk
|
|
|
2
2
|
import { HttpRestClient } from '@injectivelabs/utils';
|
|
3
3
|
import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
|
|
4
4
|
import { TurnkeyIframeClient } from '@turnkey/sdk-browser';
|
|
5
|
-
import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
|
|
5
|
+
import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEthereumOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
|
|
6
6
|
import { TurnkeyWallet } from './turnkey/turnkey.js';
|
|
7
7
|
export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
|
|
8
|
-
turnkeyWallet
|
|
8
|
+
turnkeyWallet?: TurnkeyWallet;
|
|
9
|
+
ethereumOptions: WalletStrategyEthereumOptions;
|
|
9
10
|
client: HttpRestClient;
|
|
10
11
|
constructor(args: ConcreteEthereumWalletStrategyArgs & {
|
|
11
12
|
apiServerEndpoint?: string;
|
|
@@ -19,7 +20,7 @@ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implemen
|
|
|
19
20
|
getAddresses(): Promise<string[]>;
|
|
20
21
|
getSessionOrConfirm(_address?: string): Promise<string>;
|
|
21
22
|
getWalletClient<TurnkeyWallet>(): Promise<TurnkeyWallet>;
|
|
22
|
-
|
|
23
|
+
sendEvmTransaction(transaction: unknown, args: {
|
|
23
24
|
address: AccountAddress;
|
|
24
25
|
ethereumChainId: EthereumChainId;
|
|
25
26
|
}): Promise<string>;
|
|
@@ -37,7 +38,7 @@ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implemen
|
|
|
37
38
|
}): Promise<AminoSignResponse>;
|
|
38
39
|
signArbitrary(_signer: AccountAddress, _data: string | Uint8Array): Promise<string>;
|
|
39
40
|
getEthereumChainId(): Promise<string>;
|
|
40
|
-
|
|
41
|
+
getEvmTransactionReceipt(txHash: string, ethereumChainId?: EthereumChainId): Promise<Record<string, any>>;
|
|
41
42
|
getPubKey(): Promise<string>;
|
|
42
43
|
getIframeClient(): Promise<TurnkeyIframeClient>;
|
|
43
44
|
private getTurnkeyWallet;
|
|
@@ -6,11 +6,14 @@ const sdk_ts_1 = require("@injectivelabs/sdk-ts");
|
|
|
6
6
|
const exceptions_1 = require("@injectivelabs/exceptions");
|
|
7
7
|
const viem_1 = require("viem");
|
|
8
8
|
const utils_1 = require("@injectivelabs/utils");
|
|
9
|
+
const viem_2 = require("viem");
|
|
9
10
|
const wallet_base_1 = require("@injectivelabs/wallet-base");
|
|
10
11
|
const types_js_1 = require("./types.js");
|
|
11
12
|
const turnkey_js_1 = require("./turnkey/turnkey.js");
|
|
13
|
+
const consts_js_1 = require("./consts.js");
|
|
12
14
|
class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
|
|
13
15
|
turnkeyWallet;
|
|
16
|
+
ethereumOptions;
|
|
14
17
|
client;
|
|
15
18
|
constructor(args) {
|
|
16
19
|
super(args);
|
|
@@ -19,6 +22,7 @@ class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
|
|
|
19
22
|
throw new exceptions_1.WalletException(new Error('apiServerEndpoint is required'));
|
|
20
23
|
}
|
|
21
24
|
this.client = new utils_1.HttpRestClient(endpoint);
|
|
25
|
+
this.ethereumOptions = args.ethereumOptions;
|
|
22
26
|
}
|
|
23
27
|
async getWalletDeviceType() {
|
|
24
28
|
return Promise.resolve(wallet_base_1.WalletDeviceType.Browser);
|
|
@@ -86,11 +90,47 @@ class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
|
|
|
86
90
|
async getWalletClient() {
|
|
87
91
|
return (await this.getTurnkeyWallet());
|
|
88
92
|
}
|
|
89
|
-
async
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
async sendEvmTransaction(transaction, args) {
|
|
94
|
+
try {
|
|
95
|
+
const options = this.ethereumOptions;
|
|
96
|
+
const turnkeyWallet = await this.getTurnkeyWallet();
|
|
97
|
+
const organizationId = await this.getOrganizationId();
|
|
98
|
+
const chainId = args.ethereumChainId || options.ethereumChainId;
|
|
99
|
+
const url = options.rpcUrl || options.rpcUrls?.[args.ethereumChainId];
|
|
100
|
+
if (!url) {
|
|
101
|
+
throw new exceptions_1.WalletException(new Error('Please pass rpcUrl within the ethereumOptions'), {
|
|
102
|
+
code: exceptions_1.UnspecifiedErrorCode,
|
|
103
|
+
context: wallet_base_1.WalletAction.SendEvmTransaction,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const account = await turnkeyWallet.getOrCreateAndGetAccount((0, viem_1.getAddress)(args.address), organizationId);
|
|
107
|
+
const accountClient = (0, viem_2.createWalletClient)({
|
|
108
|
+
account: account,
|
|
109
|
+
chain: {
|
|
110
|
+
...consts_js_1.DEFAULT_EVM_CHAIN_CONFIG,
|
|
111
|
+
id: chainId,
|
|
112
|
+
rpcUrls: {
|
|
113
|
+
default: {
|
|
114
|
+
http: [url],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
transport: (0, viem_2.http)(url),
|
|
119
|
+
});
|
|
120
|
+
const preparedTransaction = await accountClient.prepareTransactionRequest(transaction);
|
|
121
|
+
delete preparedTransaction.account;
|
|
122
|
+
const signedTransaction = await accountClient.signTransaction(preparedTransaction);
|
|
123
|
+
const tx = await accountClient.sendRawTransaction({
|
|
124
|
+
serializedTransaction: signedTransaction,
|
|
125
|
+
});
|
|
126
|
+
return tx;
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
throw new exceptions_1.WalletException(e, {
|
|
130
|
+
code: exceptions_1.UnspecifiedErrorCode,
|
|
131
|
+
context: wallet_base_1.WalletAction.SendEvmTransaction,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
94
134
|
}
|
|
95
135
|
async sendTransaction(transaction, options) {
|
|
96
136
|
const { endpoints, txTimeout } = options;
|
|
@@ -159,12 +199,45 @@ class TurnkeyWalletStrategy extends wallet_base_1.BaseConcreteStrategy {
|
|
|
159
199
|
context: wallet_base_1.WalletAction.GetChainId,
|
|
160
200
|
});
|
|
161
201
|
}
|
|
162
|
-
async
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
202
|
+
async getEvmTransactionReceipt(txHash, ethereumChainId) {
|
|
203
|
+
const options = this.ethereumOptions;
|
|
204
|
+
const maxAttempts = 5;
|
|
205
|
+
const interval = 1000;
|
|
206
|
+
const chainId = ethereumChainId || options.ethereumChainId;
|
|
207
|
+
const url = options.rpcUrl || options.rpcUrls?.[chainId];
|
|
208
|
+
if (!url) {
|
|
209
|
+
throw new exceptions_1.WalletException(new Error('Please pass rpcUrl within the ethereumOptions'), {
|
|
210
|
+
code: exceptions_1.UnspecifiedErrorCode,
|
|
211
|
+
context: wallet_base_1.WalletAction.GetEvmTransactionReceipt,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const publicClient = (0, viem_2.createPublicClient)({
|
|
215
|
+
chain: {
|
|
216
|
+
...consts_js_1.DEFAULT_EVM_CHAIN_CONFIG,
|
|
217
|
+
id: chainId,
|
|
218
|
+
rpcUrls: {
|
|
219
|
+
default: {
|
|
220
|
+
http: [url],
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
transport: (0, viem_2.http)(url),
|
|
167
225
|
});
|
|
226
|
+
let attempts = 0;
|
|
227
|
+
while (attempts < maxAttempts) {
|
|
228
|
+
attempts++;
|
|
229
|
+
await (0, utils_1.sleep)(interval);
|
|
230
|
+
try {
|
|
231
|
+
const receipt = await publicClient.getTransactionReceipt({
|
|
232
|
+
hash: txHash,
|
|
233
|
+
});
|
|
234
|
+
if (receipt) {
|
|
235
|
+
return receipt;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch { }
|
|
239
|
+
}
|
|
240
|
+
throw new Error(`Failed to retrieve transaction receipt for txHash: ${txHash}`);
|
|
168
241
|
}
|
|
169
242
|
// eslint-disable-next-line class-methods-use-this
|
|
170
243
|
async getPubKey() {
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { TurnkeyMetadata, TurnkeyProvider } from '@injectivelabs/wallet-base';
|
|
2
|
+
import { createAccount } from '@turnkey/viem';
|
|
2
3
|
import { HttpRestClient } from '@injectivelabs/utils';
|
|
3
4
|
import { Turnkey, TurnkeyIframeClient } from '@turnkey/sdk-browser';
|
|
4
5
|
export declare class TurnkeyWallet {
|
|
6
|
+
private otpId?;
|
|
7
|
+
protected turnkey?: Turnkey;
|
|
8
|
+
organizationId: string;
|
|
5
9
|
protected client: HttpRestClient;
|
|
6
10
|
private metadata;
|
|
7
|
-
protected
|
|
8
|
-
protected iframeClient: TurnkeyIframeClient | undefined;
|
|
9
|
-
organizationId: string;
|
|
10
|
-
private otpId;
|
|
11
|
+
protected iframeClient?: TurnkeyIframeClient;
|
|
11
12
|
private accountMap;
|
|
12
13
|
setMetadata(metadata: Partial<TurnkeyMetadata>): void;
|
|
13
14
|
constructor(metadata: TurnkeyMetadata);
|
|
@@ -18,28 +19,11 @@ export declare class TurnkeyWallet {
|
|
|
18
19
|
getTurnkey(): Promise<Turnkey>;
|
|
19
20
|
getIframeClient(): Promise<TurnkeyIframeClient>;
|
|
20
21
|
getSession(existingCredentialBundle?: string): Promise<{
|
|
21
|
-
session: import("@turnkey/sdk-
|
|
22
|
+
session: import("@turnkey/sdk-types").Session | undefined;
|
|
22
23
|
organizationId: string;
|
|
23
24
|
}>;
|
|
24
25
|
getAccounts(): Promise<string[]>;
|
|
25
|
-
getOrCreateAndGetAccount(address: string, organizationId: string): Promise<
|
|
26
|
-
address: import("abitype").Address;
|
|
27
|
-
nonceManager?: import("viem").NonceManager | undefined;
|
|
28
|
-
sign?: ((parameters: {
|
|
29
|
-
hash: import("viem").Hash;
|
|
30
|
-
}) => Promise<import("viem").Hex>) | undefined | undefined;
|
|
31
|
-
signAuthorization?: ((parameters: import("viem/_types/types/authorization.js").AuthorizationRequest) => Promise<import("viem/accounts").SignAuthorizationReturnType>) | undefined | undefined;
|
|
32
|
-
signMessage: ({ message }: {
|
|
33
|
-
message: import("viem").SignableMessage;
|
|
34
|
-
}) => Promise<import("viem").Hex>;
|
|
35
|
-
signTransaction: <serializer extends import("viem").SerializeTransactionFn<import("viem").TransactionSerializable> = import("viem").SerializeTransactionFn<import("viem").TransactionSerializable>, transaction extends Parameters<serializer>[0] = Parameters<serializer>[0]>(transaction: transaction, options?: {
|
|
36
|
-
serializer?: serializer | undefined;
|
|
37
|
-
} | undefined) => Promise<import("viem").IsNarrowable<import("viem").TransactionSerialized<import("viem").GetTransactionType<transaction>>, import("viem").Hex> extends true ? import("viem").TransactionSerialized<import("viem").GetTransactionType<transaction>> : import("viem").Hex>;
|
|
38
|
-
signTypedData: <const typedData extends import("abitype").TypedData | Record<string, unknown>, primaryType extends keyof typedData | "EIP712Domain" = keyof typedData>(parameters: import("viem").TypedDataDefinition<typedData, primaryType>) => Promise<import("viem").Hex>;
|
|
39
|
-
publicKey: import("viem").Hex;
|
|
40
|
-
source: string;
|
|
41
|
-
type: "local";
|
|
42
|
-
}>;
|
|
26
|
+
getOrCreateAndGetAccount(address: string, organizationId: string): Promise<ReturnType<typeof createAccount>>;
|
|
43
27
|
injectAndRefresh(credentialBundle: string, options: {
|
|
44
28
|
expirationSeconds?: string;
|
|
45
29
|
organizationId?: string;
|
|
@@ -13,12 +13,12 @@ const types_js_1 = require("../types.js");
|
|
|
13
13
|
const oauth_js_1 = require("./oauth.js");
|
|
14
14
|
const utils_js_1 = require("../../utils.js");
|
|
15
15
|
class TurnkeyWallet {
|
|
16
|
+
otpId;
|
|
17
|
+
turnkey;
|
|
18
|
+
organizationId;
|
|
16
19
|
client;
|
|
17
20
|
metadata;
|
|
18
|
-
turnkey;
|
|
19
21
|
iframeClient;
|
|
20
|
-
organizationId;
|
|
21
|
-
otpId;
|
|
22
22
|
accountMap = {};
|
|
23
23
|
setMetadata(metadata) {
|
|
24
24
|
this.metadata = { ...this.metadata, ...metadata };
|
|
@@ -72,7 +72,7 @@ class TurnkeyWallet {
|
|
|
72
72
|
await iframeClient.refreshSession({
|
|
73
73
|
sessionType: sdk_browser_1.SessionType.READ_WRITE,
|
|
74
74
|
targetPublicKey: iframeClient.iframePublicKey,
|
|
75
|
-
expirationSeconds:
|
|
75
|
+
expirationSeconds: this.metadata.expirationSeconds,
|
|
76
76
|
});
|
|
77
77
|
const [session, user] = await Promise.all([
|
|
78
78
|
turnkey.getSession(),
|
|
@@ -160,6 +160,10 @@ class TurnkeyWallet {
|
|
|
160
160
|
const expirationSeconds = options.expirationSeconds || consts_js_1.DEFAULT_TURNKEY_REFRESH_SECONDS;
|
|
161
161
|
const iframeClient = await this.getIframeClient();
|
|
162
162
|
await iframeClient.injectCredentialBundle(credentialBundle);
|
|
163
|
+
await iframeClient.loginWithBundle({
|
|
164
|
+
bundle: credentialBundle,
|
|
165
|
+
expirationSeconds,
|
|
166
|
+
});
|
|
163
167
|
await iframeClient.refreshSession({
|
|
164
168
|
sessionType: sdk_browser_1.SessionType.READ_WRITE,
|
|
165
169
|
targetPublicKey: iframeClient.iframePublicKey,
|
|
@@ -3,3 +3,11 @@ 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
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
|
+
};
|
|
@@ -3,3 +3,11 @@ 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
5
|
export const DEFAULT_TURNKEY_REFRESH_SECONDS = '86400';
|
|
6
|
+
export const DEFAULT_EVM_CHAIN_CONFIG = {
|
|
7
|
+
name: 'Injective',
|
|
8
|
+
nativeCurrency: {
|
|
9
|
+
name: 'Injective',
|
|
10
|
+
symbol: 'INJ',
|
|
11
|
+
decimals: 18,
|
|
12
|
+
},
|
|
13
|
+
};
|
|
@@ -2,10 +2,11 @@ import { TxRaw, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk
|
|
|
2
2
|
import { HttpRestClient } from '@injectivelabs/utils';
|
|
3
3
|
import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
|
|
4
4
|
import { TurnkeyIframeClient } from '@turnkey/sdk-browser';
|
|
5
|
-
import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
|
|
5
|
+
import { StdSignDoc, WalletDeviceType, type WalletMetadata, BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, WalletStrategyEthereumOptions, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
|
|
6
6
|
import { TurnkeyWallet } from './turnkey/turnkey.js';
|
|
7
7
|
export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
|
|
8
|
-
turnkeyWallet
|
|
8
|
+
turnkeyWallet?: TurnkeyWallet;
|
|
9
|
+
ethereumOptions: WalletStrategyEthereumOptions;
|
|
9
10
|
client: HttpRestClient;
|
|
10
11
|
constructor(args: ConcreteEthereumWalletStrategyArgs & {
|
|
11
12
|
apiServerEndpoint?: string;
|
|
@@ -19,7 +20,7 @@ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implemen
|
|
|
19
20
|
getAddresses(): Promise<string[]>;
|
|
20
21
|
getSessionOrConfirm(_address?: string): Promise<string>;
|
|
21
22
|
getWalletClient<TurnkeyWallet>(): Promise<TurnkeyWallet>;
|
|
22
|
-
|
|
23
|
+
sendEvmTransaction(transaction: unknown, args: {
|
|
23
24
|
address: AccountAddress;
|
|
24
25
|
ethereumChainId: EthereumChainId;
|
|
25
26
|
}): Promise<string>;
|
|
@@ -37,7 +38,7 @@ export declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implemen
|
|
|
37
38
|
}): Promise<AminoSignResponse>;
|
|
38
39
|
signArbitrary(_signer: AccountAddress, _data: string | Uint8Array): Promise<string>;
|
|
39
40
|
getEthereumChainId(): Promise<string>;
|
|
40
|
-
|
|
41
|
+
getEvmTransactionReceipt(txHash: string, ethereumChainId?: EthereumChainId): Promise<Record<string, any>>;
|
|
41
42
|
getPubKey(): Promise<string>;
|
|
42
43
|
getIframeClient(): Promise<TurnkeyIframeClient>;
|
|
43
44
|
private getTurnkeyWallet;
|
|
@@ -2,12 +2,15 @@
|
|
|
2
2
|
import { TxGrpcApi, } from '@injectivelabs/sdk-ts';
|
|
3
3
|
import { ErrorType, WalletException, UnspecifiedErrorCode, TransactionException, CosmosWalletException, } from '@injectivelabs/exceptions';
|
|
4
4
|
import { getAddress } from 'viem';
|
|
5
|
-
import { HttpRestClient } from '@injectivelabs/utils';
|
|
5
|
+
import { sleep, HttpRestClient } from '@injectivelabs/utils';
|
|
6
|
+
import { http, createPublicClient, createWalletClient, } from 'viem';
|
|
6
7
|
import { WalletAction, WalletDeviceType, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
|
|
7
8
|
import { TurnkeyErrorCodes } from './types.js';
|
|
8
9
|
import { TurnkeyWallet } from './turnkey/turnkey.js';
|
|
10
|
+
import { DEFAULT_EVM_CHAIN_CONFIG } from './consts.js';
|
|
9
11
|
export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
|
|
10
12
|
turnkeyWallet;
|
|
13
|
+
ethereumOptions;
|
|
11
14
|
client;
|
|
12
15
|
constructor(args) {
|
|
13
16
|
super(args);
|
|
@@ -16,6 +19,7 @@ export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
|
|
|
16
19
|
throw new WalletException(new Error('apiServerEndpoint is required'));
|
|
17
20
|
}
|
|
18
21
|
this.client = new HttpRestClient(endpoint);
|
|
22
|
+
this.ethereumOptions = args.ethereumOptions;
|
|
19
23
|
}
|
|
20
24
|
async getWalletDeviceType() {
|
|
21
25
|
return Promise.resolve(WalletDeviceType.Browser);
|
|
@@ -83,11 +87,47 @@ export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
|
|
|
83
87
|
async getWalletClient() {
|
|
84
88
|
return (await this.getTurnkeyWallet());
|
|
85
89
|
}
|
|
86
|
-
async
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
90
|
+
async sendEvmTransaction(transaction, args) {
|
|
91
|
+
try {
|
|
92
|
+
const options = this.ethereumOptions;
|
|
93
|
+
const turnkeyWallet = await this.getTurnkeyWallet();
|
|
94
|
+
const organizationId = await this.getOrganizationId();
|
|
95
|
+
const chainId = args.ethereumChainId || options.ethereumChainId;
|
|
96
|
+
const url = options.rpcUrl || options.rpcUrls?.[args.ethereumChainId];
|
|
97
|
+
if (!url) {
|
|
98
|
+
throw new WalletException(new Error('Please pass rpcUrl within the ethereumOptions'), {
|
|
99
|
+
code: UnspecifiedErrorCode,
|
|
100
|
+
context: WalletAction.SendEvmTransaction,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const account = await turnkeyWallet.getOrCreateAndGetAccount(getAddress(args.address), organizationId);
|
|
104
|
+
const accountClient = createWalletClient({
|
|
105
|
+
account: account,
|
|
106
|
+
chain: {
|
|
107
|
+
...DEFAULT_EVM_CHAIN_CONFIG,
|
|
108
|
+
id: chainId,
|
|
109
|
+
rpcUrls: {
|
|
110
|
+
default: {
|
|
111
|
+
http: [url],
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
transport: http(url),
|
|
116
|
+
});
|
|
117
|
+
const preparedTransaction = await accountClient.prepareTransactionRequest(transaction);
|
|
118
|
+
delete preparedTransaction.account;
|
|
119
|
+
const signedTransaction = await accountClient.signTransaction(preparedTransaction);
|
|
120
|
+
const tx = await accountClient.sendRawTransaction({
|
|
121
|
+
serializedTransaction: signedTransaction,
|
|
122
|
+
});
|
|
123
|
+
return tx;
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
throw new WalletException(e, {
|
|
127
|
+
code: UnspecifiedErrorCode,
|
|
128
|
+
context: WalletAction.SendEvmTransaction,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
91
131
|
}
|
|
92
132
|
async sendTransaction(transaction, options) {
|
|
93
133
|
const { endpoints, txTimeout } = options;
|
|
@@ -156,12 +196,45 @@ export class TurnkeyWalletStrategy extends BaseConcreteStrategy {
|
|
|
156
196
|
context: WalletAction.GetChainId,
|
|
157
197
|
});
|
|
158
198
|
}
|
|
159
|
-
async
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
199
|
+
async getEvmTransactionReceipt(txHash, ethereumChainId) {
|
|
200
|
+
const options = this.ethereumOptions;
|
|
201
|
+
const maxAttempts = 5;
|
|
202
|
+
const interval = 1000;
|
|
203
|
+
const chainId = ethereumChainId || options.ethereumChainId;
|
|
204
|
+
const url = options.rpcUrl || options.rpcUrls?.[chainId];
|
|
205
|
+
if (!url) {
|
|
206
|
+
throw new WalletException(new Error('Please pass rpcUrl within the ethereumOptions'), {
|
|
207
|
+
code: UnspecifiedErrorCode,
|
|
208
|
+
context: WalletAction.GetEvmTransactionReceipt,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const publicClient = createPublicClient({
|
|
212
|
+
chain: {
|
|
213
|
+
...DEFAULT_EVM_CHAIN_CONFIG,
|
|
214
|
+
id: chainId,
|
|
215
|
+
rpcUrls: {
|
|
216
|
+
default: {
|
|
217
|
+
http: [url],
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
transport: http(url),
|
|
164
222
|
});
|
|
223
|
+
let attempts = 0;
|
|
224
|
+
while (attempts < maxAttempts) {
|
|
225
|
+
attempts++;
|
|
226
|
+
await sleep(interval);
|
|
227
|
+
try {
|
|
228
|
+
const receipt = await publicClient.getTransactionReceipt({
|
|
229
|
+
hash: txHash,
|
|
230
|
+
});
|
|
231
|
+
if (receipt) {
|
|
232
|
+
return receipt;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
catch { }
|
|
236
|
+
}
|
|
237
|
+
throw new Error(`Failed to retrieve transaction receipt for txHash: ${txHash}`);
|
|
165
238
|
}
|
|
166
239
|
// eslint-disable-next-line class-methods-use-this
|
|
167
240
|
async getPubKey() {
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { TurnkeyMetadata, TurnkeyProvider } from '@injectivelabs/wallet-base';
|
|
2
|
+
import { createAccount } from '@turnkey/viem';
|
|
2
3
|
import { HttpRestClient } from '@injectivelabs/utils';
|
|
3
4
|
import { Turnkey, TurnkeyIframeClient } from '@turnkey/sdk-browser';
|
|
4
5
|
export declare class TurnkeyWallet {
|
|
6
|
+
private otpId?;
|
|
7
|
+
protected turnkey?: Turnkey;
|
|
8
|
+
organizationId: string;
|
|
5
9
|
protected client: HttpRestClient;
|
|
6
10
|
private metadata;
|
|
7
|
-
protected
|
|
8
|
-
protected iframeClient: TurnkeyIframeClient | undefined;
|
|
9
|
-
organizationId: string;
|
|
10
|
-
private otpId;
|
|
11
|
+
protected iframeClient?: TurnkeyIframeClient;
|
|
11
12
|
private accountMap;
|
|
12
13
|
setMetadata(metadata: Partial<TurnkeyMetadata>): void;
|
|
13
14
|
constructor(metadata: TurnkeyMetadata);
|
|
@@ -18,28 +19,11 @@ export declare class TurnkeyWallet {
|
|
|
18
19
|
getTurnkey(): Promise<Turnkey>;
|
|
19
20
|
getIframeClient(): Promise<TurnkeyIframeClient>;
|
|
20
21
|
getSession(existingCredentialBundle?: string): Promise<{
|
|
21
|
-
session: import("@turnkey/sdk-
|
|
22
|
+
session: import("@turnkey/sdk-types").Session | undefined;
|
|
22
23
|
organizationId: string;
|
|
23
24
|
}>;
|
|
24
25
|
getAccounts(): Promise<string[]>;
|
|
25
|
-
getOrCreateAndGetAccount(address: string, organizationId: string): Promise<
|
|
26
|
-
address: import("abitype").Address;
|
|
27
|
-
nonceManager?: import("viem").NonceManager | undefined;
|
|
28
|
-
sign?: ((parameters: {
|
|
29
|
-
hash: import("viem").Hash;
|
|
30
|
-
}) => Promise<import("viem").Hex>) | undefined | undefined;
|
|
31
|
-
signAuthorization?: ((parameters: import("node_modules/viem/_types/types/authorization.js").AuthorizationRequest) => Promise<import("viem/accounts").SignAuthorizationReturnType>) | undefined | undefined;
|
|
32
|
-
signMessage: ({ message }: {
|
|
33
|
-
message: import("viem").SignableMessage;
|
|
34
|
-
}) => Promise<import("viem").Hex>;
|
|
35
|
-
signTransaction: <serializer extends import("viem").SerializeTransactionFn<import("viem").TransactionSerializable> = import("viem").SerializeTransactionFn<import("viem").TransactionSerializable>, transaction extends Parameters<serializer>[0] = Parameters<serializer>[0]>(transaction: transaction, options?: {
|
|
36
|
-
serializer?: serializer | undefined;
|
|
37
|
-
} | undefined) => Promise<import("viem").IsNarrowable<import("viem").TransactionSerialized<import("viem").GetTransactionType<transaction>>, import("viem").Hex> extends true ? import("viem").TransactionSerialized<import("viem").GetTransactionType<transaction>> : import("viem").Hex>;
|
|
38
|
-
signTypedData: <const typedData extends import("abitype").TypedData | Record<string, unknown>, primaryType extends keyof typedData | "EIP712Domain" = keyof typedData>(parameters: import("viem").TypedDataDefinition<typedData, primaryType>) => Promise<import("viem").Hex>;
|
|
39
|
-
publicKey: import("viem").Hex;
|
|
40
|
-
source: string;
|
|
41
|
-
type: "local";
|
|
42
|
-
}>;
|
|
26
|
+
getOrCreateAndGetAccount(address: string, organizationId: string): Promise<ReturnType<typeof createAccount>>;
|
|
43
27
|
injectAndRefresh(credentialBundle: string, options: {
|
|
44
28
|
expirationSeconds?: string;
|
|
45
29
|
organizationId?: string;
|
|
@@ -3,19 +3,19 @@ import { WalletAction, TurnkeyProvider, } from '@injectivelabs/wallet-base';
|
|
|
3
3
|
import { createAccount } from '@turnkey/viem';
|
|
4
4
|
import { HttpRestClient } from '@injectivelabs/utils';
|
|
5
5
|
import { getInjectiveAddress } from '@injectivelabs/sdk-ts';
|
|
6
|
-
import {
|
|
6
|
+
import { SessionType, Turnkey } from '@turnkey/sdk-browser';
|
|
7
7
|
import { TURNKEY_OAUTH_PATH, TURNKEY_OTP_INIT_PATH, TURNKEY_OTP_VERIFY_PATH, DEFAULT_TURNKEY_REFRESH_SECONDS, } from '../consts.js';
|
|
8
8
|
import { TurnkeyOtpWallet } from './otp.js';
|
|
9
9
|
import { TurnkeyErrorCodes } from '../types.js';
|
|
10
10
|
import { TurnkeyOauthWallet } from './oauth.js';
|
|
11
11
|
import { generateGoogleUrl } from '../../utils.js';
|
|
12
12
|
export class TurnkeyWallet {
|
|
13
|
+
otpId;
|
|
14
|
+
turnkey;
|
|
15
|
+
organizationId;
|
|
13
16
|
client;
|
|
14
17
|
metadata;
|
|
15
|
-
turnkey;
|
|
16
18
|
iframeClient;
|
|
17
|
-
organizationId;
|
|
18
|
-
otpId;
|
|
19
19
|
accountMap = {};
|
|
20
20
|
setMetadata(metadata) {
|
|
21
21
|
this.metadata = { ...this.metadata, ...metadata };
|
|
@@ -69,7 +69,7 @@ export class TurnkeyWallet {
|
|
|
69
69
|
await iframeClient.refreshSession({
|
|
70
70
|
sessionType: SessionType.READ_WRITE,
|
|
71
71
|
targetPublicKey: iframeClient.iframePublicKey,
|
|
72
|
-
expirationSeconds:
|
|
72
|
+
expirationSeconds: this.metadata.expirationSeconds,
|
|
73
73
|
});
|
|
74
74
|
const [session, user] = await Promise.all([
|
|
75
75
|
turnkey.getSession(),
|
|
@@ -157,6 +157,10 @@ export class TurnkeyWallet {
|
|
|
157
157
|
const expirationSeconds = options.expirationSeconds || DEFAULT_TURNKEY_REFRESH_SECONDS;
|
|
158
158
|
const iframeClient = await this.getIframeClient();
|
|
159
159
|
await iframeClient.injectCredentialBundle(credentialBundle);
|
|
160
|
+
await iframeClient.loginWithBundle({
|
|
161
|
+
bundle: credentialBundle,
|
|
162
|
+
expirationSeconds,
|
|
163
|
+
});
|
|
160
164
|
await iframeClient.refreshSession({
|
|
161
165
|
sessionType: SessionType.READ_WRITE,
|
|
162
166
|
targetPublicKey: iframeClient.iframePublicKey,
|
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.
|
|
4
|
+
"version": "1.15.33",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -59,14 +59,14 @@
|
|
|
59
59
|
"start": "node dist/index.js"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@injectivelabs/exceptions": "^1.15.
|
|
63
|
-
"@injectivelabs/sdk-ts": "^1.15.
|
|
64
|
-
"@injectivelabs/ts-types": "^1.15.
|
|
65
|
-
"@injectivelabs/utils": "^1.15.
|
|
66
|
-
"@injectivelabs/wallet-base": "^1.15.
|
|
67
|
-
"@turnkey/sdk-browser": "
|
|
68
|
-
"@turnkey/viem": "0.9.
|
|
69
|
-
"viem": "2.
|
|
62
|
+
"@injectivelabs/exceptions": "^1.15.30",
|
|
63
|
+
"@injectivelabs/sdk-ts": "^1.15.33",
|
|
64
|
+
"@injectivelabs/ts-types": "^1.15.31",
|
|
65
|
+
"@injectivelabs/utils": "^1.15.31",
|
|
66
|
+
"@injectivelabs/wallet-base": "^1.15.33",
|
|
67
|
+
"@turnkey/sdk-browser": "5.2.3",
|
|
68
|
+
"@turnkey/viem": "^0.9.9",
|
|
69
|
+
"viem": "^2.31.3"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"jest": "^29.0.0",
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"tsconfig-paths": "^4.2.0",
|
|
78
78
|
"typescript": "^5.0.0"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "a214c118cc513d4f635e040efdc64d108913ff96"
|
|
81
81
|
}
|