@injectivelabs/wallet-turnkey 1.19.22 → 1.19.24

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.
@@ -471,11 +471,50 @@ async function createTurnkeyClient(metadata) {
471
471
 
472
472
  //#endregion
473
473
  //#region src/strategy/Eip1193Provider.ts
474
+ const signTypedDataMethods = new Set([
475
+ "eth_signTypedData",
476
+ "eth_signTypedData_v3",
477
+ "eth_signTypedData_v4"
478
+ ]);
479
+ const signMessageMethods = new Set([
480
+ "eth_sign",
481
+ "personal_sign",
482
+ "eth_signMessage"
483
+ ]);
474
484
  const parseChainId = (chainId) => {
475
485
  if (typeof chainId === "number") return chainId;
476
486
  if (chainId.startsWith("0x")) return parseInt(chainId.replace("0x", ""), 16);
477
487
  return parseInt(chainId, 10);
478
488
  };
489
+ const isEthAddress = (value) => typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
490
+ const getTypedDataParam = (method, params) => {
491
+ if (!(params === null || params === void 0 ? void 0 : params.length)) throw new Error("params is required");
492
+ const typedData = isEthAddress(params[0]) ? params[1] : params[0];
493
+ if (typedData == null || typeof typedData === "string" && typedData.length === 0 || typeof typedData !== "string" && typeof typedData !== "object") throw new Error(`Missing typed data parameter for ${method}`);
494
+ const parsedTypedData = (() => {
495
+ if (typeof typedData !== "string") return typedData;
496
+ try {
497
+ return JSON.parse(typedData);
498
+ } catch (_unused) {
499
+ throw new Error(`Invalid typed data parameter for ${method}`);
500
+ }
501
+ })();
502
+ if (!parsedTypedData || typeof parsedTypedData !== "object" || Array.isArray(parsedTypedData) || !parsedTypedData.domain || !parsedTypedData.types || typeof parsedTypedData.primaryType !== "string" || !("message" in parsedTypedData)) throw new Error(`Invalid typed data parameter for ${method}`);
503
+ return parsedTypedData;
504
+ };
505
+ const getMessageParam = (method, params) => {
506
+ if (!(params === null || params === void 0 ? void 0 : params.length)) throw new Error("params is required");
507
+ let message = params[0];
508
+ if (method === "eth_sign") message = params[1];
509
+ if (method === "personal_sign" && isEthAddress(params[0])) message = params[1];
510
+ if (!(message instanceof Uint8Array) && (typeof message !== "string" || message.length === 0) && !(message && typeof message === "object" && "message" in message)) throw new Error(`Missing message parameter for ${method}`);
511
+ return message;
512
+ };
513
+ const getSignableMessage = (message) => {
514
+ if (message && typeof message === "object" && "message" in message) return message;
515
+ if (message instanceof Uint8Array || typeof message === "string" && message.startsWith("0x")) return { message: { raw: message } };
516
+ return { message };
517
+ };
479
518
  const getEip1193ProviderForTurnkey = async (account, chainId, params = {}) => {
480
519
  var _account$sign;
481
520
  return new CustomEip1193Provider({
@@ -536,10 +575,9 @@ var CustomEip1193Provider = class {
536
575
  throw new Error("Not implemented!");
537
576
  }
538
577
  async request(args) {
539
- if (args.method === "eth_requestAccounts") return this.requestAccounts();
540
- if (args.method === "eth_signTypedData") {
541
- if (!args.params) throw new Error("params is required");
542
- const typedData = args.params[0];
578
+ if (args.method === "eth_requestAccounts" || args.method === "eth_accounts") return this.requestAccounts();
579
+ if (signTypedDataMethods.has(args.method)) {
580
+ const typedData = getTypedDataParam(args.method, args.params);
543
581
  if (this.sign) {
544
582
  const typedDataHash = (0, viem.hashTypedData)({
545
583
  domain: {
@@ -554,9 +592,13 @@ var CustomEip1193Provider = class {
554
592
  }
555
593
  return this.signTypedData(typedData);
556
594
  }
557
- if (args.method === "eth_signMessage") {
595
+ if (signMessageMethods.has(args.method)) {
596
+ const message = getMessageParam(args.method, args.params);
597
+ return this.signMessage(getSignableMessage(message));
598
+ }
599
+ if (args.method === "eth_signTransaction") {
558
600
  if (!args.params) throw new Error("params is required");
559
- return this.signMessage(args.params[0]);
601
+ return this.signTransaction(args.params[0]);
560
602
  }
561
603
  if (args.method === "eth_chainId") return `0x${this.chainId.toString(16)}`;
562
604
  if (args.method === "wallet_switchEthereumChain") {
@@ -781,12 +823,25 @@ var TurnkeyWalletStrategy = class extends __injectivelabs_wallet_base.BaseConcre
781
823
  contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
782
824
  });
783
825
  }
784
- async signArbitrary(_signer, _data) {
785
- throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("This wallet does not support signArbitrary"), {
786
- code: __injectivelabs_exceptions.UnspecifiedErrorCode,
787
- type: __injectivelabs_exceptions.ErrorType.WalletError,
788
- contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
789
- });
826
+ async signArbitrary(signer, data) {
827
+ try {
828
+ const turnkeyWallet = await this.getTurnkeyWallet();
829
+ const checksumAddress = (0, viem.getAddress)((0, __injectivelabs_sdk_ts_utils.getEthereumAddress)(signer));
830
+ return await (await getEip1193ProviderForTurnkey(await turnkeyWallet.getOrCreateAndGetAccount(checksumAddress), String(this.evmOptions.evmChainId), {
831
+ rpcUrl: this.evmOptions.rpcUrl,
832
+ rpcUrls: this.evmOptions.rpcUrls
833
+ })).request({
834
+ method: "personal_sign",
835
+ params: [(0, __injectivelabs_sdk_ts_utils.toUtf8)(data), checksumAddress]
836
+ });
837
+ } catch (e) {
838
+ const message = e instanceof Error ? e.message : String(e);
839
+ throw new __injectivelabs_exceptions.WalletException(new Error(message), {
840
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
841
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
842
+ contextModule: __injectivelabs_wallet_base.WalletAction.SignArbitrary
843
+ });
844
+ }
790
845
  }
791
846
  async getEthereumChainId() {
792
847
  throw new __injectivelabs_exceptions.CosmosWalletException(/* @__PURE__ */ new Error("getEthereumChainId is not supported on Turnkey wallet"), {
@@ -44,7 +44,7 @@ declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements Conc
44
44
  address: string;
45
45
  signDoc: StdSignDoc;
46
46
  }): Promise<AminoSignResponse>;
47
- signArbitrary(_signer: AccountAddress, _data: string | Uint8Array): Promise<string>;
47
+ signArbitrary(signer: AccountAddress, data: string | Uint8Array): Promise<string>;
48
48
  getEthereumChainId(): Promise<string>;
49
49
  getEvmTransactionReceipt(txHash: string, evmChainId?: EvmChainId): Promise<Record<string, any>>;
50
50
  getPubKey(): Promise<string>;
@@ -44,7 +44,7 @@ declare class TurnkeyWalletStrategy extends BaseConcreteStrategy implements Conc
44
44
  address: string;
45
45
  signDoc: StdSignDoc;
46
46
  }): Promise<AminoSignResponse>;
47
- signArbitrary(_signer: AccountAddress, _data: string | Uint8Array): Promise<string>;
47
+ signArbitrary(signer: AccountAddress, data: string | Uint8Array): Promise<string>;
48
48
  getEthereumChainId(): Promise<string>;
49
49
  getEvmTransactionReceipt(txHash: string, evmChainId?: EvmChainId): Promise<Record<string, any>>;
50
50
  getPubKey(): Promise<string>;
package/dist/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { getAddress, hashTypedData } from "viem";
2
2
  import { HttpRestClient } from "@injectivelabs/utils";
3
3
  import { TxGrpcApi } from "@injectivelabs/sdk-ts/core/tx";
4
- import { getEthereumAddress, getInjectiveAddress, sha256 } from "@injectivelabs/sdk-ts/utils";
4
+ import { getEthereumAddress, getInjectiveAddress, sha256, toUtf8 } from "@injectivelabs/sdk-ts/utils";
5
5
  import { CosmosWalletException, ErrorType, GeneralException, TransactionException, TurnkeyWalletSessionException, UnspecifiedErrorCode, WalletException } from "@injectivelabs/exceptions";
6
6
  import { BaseConcreteStrategy, TurnkeyProvider, WalletAction, WalletDeviceType, getEvmChainConfig, getViemPublicClient, getViemWalletClient } from "@injectivelabs/wallet-base";
7
7
  import { createAccount } from "@turnkey/viem";
@@ -471,11 +471,50 @@ async function createTurnkeyClient(metadata) {
471
471
 
472
472
  //#endregion
473
473
  //#region src/strategy/Eip1193Provider.ts
474
+ const signTypedDataMethods = new Set([
475
+ "eth_signTypedData",
476
+ "eth_signTypedData_v3",
477
+ "eth_signTypedData_v4"
478
+ ]);
479
+ const signMessageMethods = new Set([
480
+ "eth_sign",
481
+ "personal_sign",
482
+ "eth_signMessage"
483
+ ]);
474
484
  const parseChainId = (chainId) => {
475
485
  if (typeof chainId === "number") return chainId;
476
486
  if (chainId.startsWith("0x")) return parseInt(chainId.replace("0x", ""), 16);
477
487
  return parseInt(chainId, 10);
478
488
  };
489
+ const isEthAddress = (value) => typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
490
+ const getTypedDataParam = (method, params) => {
491
+ if (!(params === null || params === void 0 ? void 0 : params.length)) throw new Error("params is required");
492
+ const typedData = isEthAddress(params[0]) ? params[1] : params[0];
493
+ if (typedData == null || typeof typedData === "string" && typedData.length === 0 || typeof typedData !== "string" && typeof typedData !== "object") throw new Error(`Missing typed data parameter for ${method}`);
494
+ const parsedTypedData = (() => {
495
+ if (typeof typedData !== "string") return typedData;
496
+ try {
497
+ return JSON.parse(typedData);
498
+ } catch (_unused) {
499
+ throw new Error(`Invalid typed data parameter for ${method}`);
500
+ }
501
+ })();
502
+ if (!parsedTypedData || typeof parsedTypedData !== "object" || Array.isArray(parsedTypedData) || !parsedTypedData.domain || !parsedTypedData.types || typeof parsedTypedData.primaryType !== "string" || !("message" in parsedTypedData)) throw new Error(`Invalid typed data parameter for ${method}`);
503
+ return parsedTypedData;
504
+ };
505
+ const getMessageParam = (method, params) => {
506
+ if (!(params === null || params === void 0 ? void 0 : params.length)) throw new Error("params is required");
507
+ let message = params[0];
508
+ if (method === "eth_sign") message = params[1];
509
+ if (method === "personal_sign" && isEthAddress(params[0])) message = params[1];
510
+ if (!(message instanceof Uint8Array) && (typeof message !== "string" || message.length === 0) && !(message && typeof message === "object" && "message" in message)) throw new Error(`Missing message parameter for ${method}`);
511
+ return message;
512
+ };
513
+ const getSignableMessage = (message) => {
514
+ if (message && typeof message === "object" && "message" in message) return message;
515
+ if (message instanceof Uint8Array || typeof message === "string" && message.startsWith("0x")) return { message: { raw: message } };
516
+ return { message };
517
+ };
479
518
  const getEip1193ProviderForTurnkey = async (account, chainId, params = {}) => {
480
519
  var _account$sign;
481
520
  return new CustomEip1193Provider({
@@ -536,10 +575,9 @@ var CustomEip1193Provider = class {
536
575
  throw new Error("Not implemented!");
537
576
  }
538
577
  async request(args) {
539
- if (args.method === "eth_requestAccounts") return this.requestAccounts();
540
- if (args.method === "eth_signTypedData") {
541
- if (!args.params) throw new Error("params is required");
542
- const typedData = args.params[0];
578
+ if (args.method === "eth_requestAccounts" || args.method === "eth_accounts") return this.requestAccounts();
579
+ if (signTypedDataMethods.has(args.method)) {
580
+ const typedData = getTypedDataParam(args.method, args.params);
543
581
  if (this.sign) {
544
582
  const typedDataHash = hashTypedData({
545
583
  domain: {
@@ -554,9 +592,13 @@ var CustomEip1193Provider = class {
554
592
  }
555
593
  return this.signTypedData(typedData);
556
594
  }
557
- if (args.method === "eth_signMessage") {
595
+ if (signMessageMethods.has(args.method)) {
596
+ const message = getMessageParam(args.method, args.params);
597
+ return this.signMessage(getSignableMessage(message));
598
+ }
599
+ if (args.method === "eth_signTransaction") {
558
600
  if (!args.params) throw new Error("params is required");
559
- return this.signMessage(args.params[0]);
601
+ return this.signTransaction(args.params[0]);
560
602
  }
561
603
  if (args.method === "eth_chainId") return `0x${this.chainId.toString(16)}`;
562
604
  if (args.method === "wallet_switchEthereumChain") {
@@ -781,12 +823,25 @@ var TurnkeyWalletStrategy = class extends BaseConcreteStrategy {
781
823
  contextModule: WalletAction.SignTransaction
782
824
  });
783
825
  }
784
- async signArbitrary(_signer, _data) {
785
- throw new WalletException(/* @__PURE__ */ new Error("This wallet does not support signArbitrary"), {
786
- code: UnspecifiedErrorCode,
787
- type: ErrorType.WalletError,
788
- contextModule: WalletAction.SignTransaction
789
- });
826
+ async signArbitrary(signer, data) {
827
+ try {
828
+ const turnkeyWallet = await this.getTurnkeyWallet();
829
+ const checksumAddress = getAddress(getEthereumAddress(signer));
830
+ return await (await getEip1193ProviderForTurnkey(await turnkeyWallet.getOrCreateAndGetAccount(checksumAddress), String(this.evmOptions.evmChainId), {
831
+ rpcUrl: this.evmOptions.rpcUrl,
832
+ rpcUrls: this.evmOptions.rpcUrls
833
+ })).request({
834
+ method: "personal_sign",
835
+ params: [toUtf8(data), checksumAddress]
836
+ });
837
+ } catch (e) {
838
+ const message = e instanceof Error ? e.message : String(e);
839
+ throw new WalletException(new Error(message), {
840
+ code: UnspecifiedErrorCode,
841
+ type: ErrorType.WalletError,
842
+ contextModule: WalletAction.SignArbitrary
843
+ });
844
+ }
790
845
  }
791
846
  async getEthereumChainId() {
792
847
  throw new CosmosWalletException(/* @__PURE__ */ new Error("getEthereumChainId is not supported on Turnkey wallet"), {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@injectivelabs/wallet-turnkey",
3
- "version": "1.19.22",
3
+ "version": "1.19.24",
4
4
  "description": "Turnkey wallet strategy for use with @injectivelabs/wallet-core.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -45,11 +45,11 @@
45
45
  "@turnkey/sdk-browser": "5.16.1",
46
46
  "@turnkey/viem": "0.13.1",
47
47
  "viem": "^2.41.2",
48
- "@injectivelabs/exceptions": "1.19.22",
49
- "@injectivelabs/sdk-ts": "1.19.22",
50
- "@injectivelabs/ts-types": "1.19.22",
51
- "@injectivelabs/wallet-base": "1.19.22",
52
- "@injectivelabs/utils": "1.19.22"
48
+ "@injectivelabs/exceptions": "1.19.24",
49
+ "@injectivelabs/sdk-ts": "1.19.24",
50
+ "@injectivelabs/wallet-base": "1.19.24",
51
+ "@injectivelabs/ts-types": "1.19.24",
52
+ "@injectivelabs/utils": "1.19.24"
53
53
  },
54
54
  "publishConfig": {
55
55
  "access": "public"