@injectivelabs/wallet-turnkey 1.19.21 → 1.19.23

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") {
package/dist/esm/index.js CHANGED
@@ -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") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@injectivelabs/wallet-turnkey",
3
- "version": "1.19.21",
3
+ "version": "1.19.23",
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.21",
49
- "@injectivelabs/utils": "1.19.21",
50
- "@injectivelabs/wallet-base": "1.19.21",
51
- "@injectivelabs/sdk-ts": "1.19.21",
52
- "@injectivelabs/ts-types": "1.19.21"
48
+ "@injectivelabs/ts-types": "1.19.23",
49
+ "@injectivelabs/utils": "1.19.23",
50
+ "@injectivelabs/wallet-base": "1.19.23",
51
+ "@injectivelabs/exceptions": "1.19.23",
52
+ "@injectivelabs/sdk-ts": "1.19.23"
53
53
  },
54
54
  "publishConfig": {
55
55
  "access": "public"