@getpara/viem-v2-integration 2.0.0 → 2.2.0-dev.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.
@@ -67,8 +67,14 @@ __export(viemWalletClient_exports, {
67
67
  });
68
68
  module.exports = __toCommonJS(viemWalletClient_exports);
69
69
  var import_viem = require("viem");
70
+ var import_utils = require("viem/utils");
70
71
  var viemChains = __toESM(require("viem/chains"));
71
72
  var import_core_sdk = require("@getpara/core-sdk");
73
+ function normalizeSignature(rawSignature) {
74
+ const sigHex = `0x${rawSignature}`;
75
+ const parsed = (0, import_viem.parseSignature)(sigHex);
76
+ return (0, import_viem.serializeSignature)({ r: parsed.r, s: parsed.s, yParity: parsed.yParity });
77
+ }
72
78
  function createParaAccount(para, walletAddress) {
73
79
  let currentWallet;
74
80
  if (walletAddress) {
@@ -82,12 +88,23 @@ function createParaAccount(para, walletAddress) {
82
88
  publicKey: currentWallet.publicKey || "0x",
83
89
  source: "custom",
84
90
  type: "local",
91
+ /** Signs a raw hash directly. */
92
+ sign: (_0) => __async(this, [_0], function* ({ hash }) {
93
+ const res = yield para.signMessage({
94
+ walletId: currentWallet.id,
95
+ messageBase64: (0, import_core_sdk.hexStringToBase64)(hash)
96
+ });
97
+ const signature = res.signature;
98
+ return normalizeSignature(signature);
99
+ }),
100
+ /** Signs an EIP-191 personal message (prefixed and hashed). */
85
101
  signMessage: (_0) => __async(this, [_0], function* ({ message }) {
86
102
  const hashedMessage = (0, import_viem.hashMessage)(message);
87
103
  const res = yield para.signMessage({ walletId: currentWallet.id, messageBase64: (0, import_core_sdk.hexStringToBase64)(hashedMessage) });
88
104
  const signature = res.signature;
89
- return `0x${signature}`;
105
+ return normalizeSignature(signature);
90
106
  }),
107
+ /** Signs and serializes an Ethereum transaction. */
91
108
  signTransaction: (transaction, args) => __async(this, null, function* () {
92
109
  let { serializer } = args || {};
93
110
  if (!serializer) {
@@ -104,17 +121,47 @@ function createParaAccount(para, walletAddress) {
104
121
  chainId: `${transaction.chainId}`
105
122
  });
106
123
  const signature = res.signature;
107
- const formattedSig = (0, import_core_sdk.hexToSignature)(`0x${signature}`);
108
- formattedSig.v += BigInt(27);
109
- return serializer(transaction, formattedSig);
124
+ const normalizedSig = normalizeSignature(signature);
125
+ const parsed = (0, import_viem.parseSignature)(normalizedSig);
126
+ return serializer(transaction, {
127
+ r: parsed.r,
128
+ s: parsed.s,
129
+ v: parsed.v
130
+ // v is guaranteed after normalization (27n or 28n)
131
+ });
110
132
  }),
133
+ /** Signs EIP-712 typed data. */
111
134
  signTypedData: (typedDataDefinition) => __async(this, null, function* () {
112
135
  const res = yield para.signMessage({
113
136
  walletId: currentWallet.id,
114
137
  messageBase64: (0, import_core_sdk.hexStringToBase64)((0, import_viem.hashTypedData)(typedDataDefinition))
115
138
  });
116
139
  const signature = res.signature;
117
- return `0x${signature}`;
140
+ return normalizeSignature(signature);
141
+ }),
142
+ /**
143
+ * Signs an EIP-7702 authorization for account delegation.
144
+ * Returns yParity (0/1) per EIP-7702, not legacy v (27/28).
145
+ */
146
+ signAuthorization: (authorization) => __async(this, null, function* () {
147
+ var _a;
148
+ const address = (_a = authorization.contractAddress) != null ? _a : authorization.address;
149
+ const hash = (0, import_utils.hashAuthorization)({ address, chainId: authorization.chainId, nonce: authorization.nonce });
150
+ const res = yield para.signMessage({
151
+ walletId: currentWallet.id,
152
+ messageBase64: (0, import_core_sdk.hexStringToBase64)(hash)
153
+ });
154
+ const signature = res.signature;
155
+ const sigHex = `0x${signature}`;
156
+ const parsed = (0, import_viem.parseSignature)(sigHex);
157
+ return {
158
+ address,
159
+ chainId: authorization.chainId,
160
+ nonce: authorization.nonce,
161
+ r: parsed.r,
162
+ s: parsed.s,
163
+ yParity: parsed.yParity
164
+ };
118
165
  })
119
166
  };
120
167
  }
@@ -37,11 +37,19 @@ var __async = (__this, __arguments, generator) => {
37
37
  import {
38
38
  createWalletClient,
39
39
  hashMessage,
40
- serializeTransaction,
41
- hashTypedData
40
+ hashTypedData,
41
+ parseSignature,
42
+ serializeSignature,
43
+ serializeTransaction
42
44
  } from "viem";
45
+ import { hashAuthorization } from "viem/utils";
43
46
  import * as viemChains from "viem/chains";
44
- import { hexStringToBase64, hexToSignature } from "@getpara/core-sdk";
47
+ import { hexStringToBase64 } from "@getpara/core-sdk";
48
+ function normalizeSignature(rawSignature) {
49
+ const sigHex = `0x${rawSignature}`;
50
+ const parsed = parseSignature(sigHex);
51
+ return serializeSignature({ r: parsed.r, s: parsed.s, yParity: parsed.yParity });
52
+ }
45
53
  function createParaAccount(para, walletAddress) {
46
54
  let currentWallet;
47
55
  if (walletAddress) {
@@ -55,12 +63,23 @@ function createParaAccount(para, walletAddress) {
55
63
  publicKey: currentWallet.publicKey || "0x",
56
64
  source: "custom",
57
65
  type: "local",
66
+ /** Signs a raw hash directly. */
67
+ sign: (_0) => __async(this, [_0], function* ({ hash }) {
68
+ const res = yield para.signMessage({
69
+ walletId: currentWallet.id,
70
+ messageBase64: hexStringToBase64(hash)
71
+ });
72
+ const signature = res.signature;
73
+ return normalizeSignature(signature);
74
+ }),
75
+ /** Signs an EIP-191 personal message (prefixed and hashed). */
58
76
  signMessage: (_0) => __async(this, [_0], function* ({ message }) {
59
77
  const hashedMessage = hashMessage(message);
60
78
  const res = yield para.signMessage({ walletId: currentWallet.id, messageBase64: hexStringToBase64(hashedMessage) });
61
79
  const signature = res.signature;
62
- return `0x${signature}`;
80
+ return normalizeSignature(signature);
63
81
  }),
82
+ /** Signs and serializes an Ethereum transaction. */
64
83
  signTransaction: (transaction, args) => __async(this, null, function* () {
65
84
  let { serializer } = args || {};
66
85
  if (!serializer) {
@@ -77,17 +96,47 @@ function createParaAccount(para, walletAddress) {
77
96
  chainId: `${transaction.chainId}`
78
97
  });
79
98
  const signature = res.signature;
80
- const formattedSig = hexToSignature(`0x${signature}`);
81
- formattedSig.v += BigInt(27);
82
- return serializer(transaction, formattedSig);
99
+ const normalizedSig = normalizeSignature(signature);
100
+ const parsed = parseSignature(normalizedSig);
101
+ return serializer(transaction, {
102
+ r: parsed.r,
103
+ s: parsed.s,
104
+ v: parsed.v
105
+ // v is guaranteed after normalization (27n or 28n)
106
+ });
83
107
  }),
108
+ /** Signs EIP-712 typed data. */
84
109
  signTypedData: (typedDataDefinition) => __async(this, null, function* () {
85
110
  const res = yield para.signMessage({
86
111
  walletId: currentWallet.id,
87
112
  messageBase64: hexStringToBase64(hashTypedData(typedDataDefinition))
88
113
  });
89
114
  const signature = res.signature;
90
- return `0x${signature}`;
115
+ return normalizeSignature(signature);
116
+ }),
117
+ /**
118
+ * Signs an EIP-7702 authorization for account delegation.
119
+ * Returns yParity (0/1) per EIP-7702, not legacy v (27/28).
120
+ */
121
+ signAuthorization: (authorization) => __async(this, null, function* () {
122
+ var _a;
123
+ const address = (_a = authorization.contractAddress) != null ? _a : authorization.address;
124
+ const hash = hashAuthorization({ address, chainId: authorization.chainId, nonce: authorization.nonce });
125
+ const res = yield para.signMessage({
126
+ walletId: currentWallet.id,
127
+ messageBase64: hexStringToBase64(hash)
128
+ });
129
+ const signature = res.signature;
130
+ const sigHex = `0x${signature}`;
131
+ const parsed = parseSignature(sigHex);
132
+ return {
133
+ address,
134
+ chainId: authorization.chainId,
135
+ nonce: authorization.nonce,
136
+ r: parsed.r,
137
+ s: parsed.s,
138
+ yParity: parsed.yParity
139
+ };
91
140
  })
92
141
  };
93
142
  }
@@ -1,10 +1,73 @@
1
- import { WalletClient, WalletClientConfig, Hex, Transport, Chain, LocalAccount, RpcSchema } from 'viem';
1
+ import type { Chain, Hex, LocalAccount, RpcSchema, Transport, WalletClient, WalletClientConfig } from 'viem';
2
2
  import * as viemChains from 'viem/chains';
3
3
  import ParaCore from '@getpara/core-sdk';
4
+ /** Options for creating a Para viem client */
4
5
  interface ViemClientOpts {
6
+ /** If true, creates the client without an attached account */
5
7
  noAccount?: boolean;
6
8
  }
9
+ /**
10
+ * Creates a viem-compatible LocalAccount backed by a Para wallet.
11
+ *
12
+ * This account implements all standard signing methods with proper signature
13
+ * formatting for maximum compatibility:
14
+ * - `sign`, `signMessage`, `signTypedData`, `signTransaction`: Use normalized
15
+ * signatures with v=27/28 for legacy `ecrecover` compatibility
16
+ * - `signAuthorization`: Uses yParity=0/1 as required by EIP-7702
17
+ *
18
+ * @param para - The Para SDK instance
19
+ * @param walletAddress - Optional specific wallet address to use. If not provided,
20
+ * uses the first available EVM wallet.
21
+ * @returns A LocalAccount that can be used with viem's wallet client
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const account = createParaAccount(para);
26
+ * const signature = await account.signMessage({ message: 'Hello' });
27
+ * // signature is properly formatted for ecrecover
28
+ * ```
29
+ */
7
30
  export declare function createParaAccount(para: ParaCore, walletAddress?: Hex): LocalAccount;
31
+ /**
32
+ * Looks up a viem Chain object by chain ID.
33
+ *
34
+ * @param chainId - The chain ID as a string
35
+ * @returns The matching viem Chain object
36
+ * @throws Error if no chain with the given ID is found
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const mainnet = getViemChain('1');
41
+ * const polygon = getViemChain('137');
42
+ * ```
43
+ */
8
44
  export declare function getViemChain(chainId: string): viemChains.Chain;
45
+ /**
46
+ * Creates a viem WalletClient configured with a Para account.
47
+ *
48
+ * This is a convenience function that combines `createParaAccount` with viem's
49
+ * `createWalletClient`. The resulting client can be used for all standard
50
+ * wallet operations with properly formatted signatures.
51
+ *
52
+ * @param para - The Para SDK instance
53
+ * @param params - Standard viem WalletClientConfig (chain, transport, etc.)
54
+ * @param opts - Optional configuration
55
+ * @param opts.noAccount - If true, creates a client without an attached account
56
+ * @returns A viem WalletClient backed by the Para wallet
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * import { http } from 'viem';
61
+ * import { mainnet } from 'viem/chains';
62
+ *
63
+ * const client = createParaViemClient(para, {
64
+ * chain: mainnet,
65
+ * transport: http(),
66
+ * });
67
+ *
68
+ * // Use standard viem methods - signatures are properly formatted
69
+ * const hash = await client.sendTransaction({ to: '0x...', value: 1n });
70
+ * ```
71
+ */
9
72
  export declare function createParaViemClient(para: ParaCore, params: WalletClientConfig<Transport, Chain, LocalAccount, RpcSchema>, opts?: ViemClientOpts): WalletClient<Transport, Chain, LocalAccount, RpcSchema>;
10
73
  export {};
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@getpara/viem-v2-integration",
3
- "version": "2.0.0",
3
+ "version": "2.2.0-dev.0",
4
4
  "dependencies": {
5
- "@getpara/core-sdk": "2.0.0"
5
+ "@getpara/core-sdk": "2.1.0"
6
6
  },
7
7
  "devDependencies": {
8
8
  "typescript": "^5.8.3",
9
- "viem": "^2.38.5"
9
+ "viem": "^2.39.0"
10
10
  },
11
11
  "exports": {
12
12
  ".": {
@@ -19,7 +19,7 @@
19
19
  "dist",
20
20
  "package.json"
21
21
  ],
22
- "gitHead": "a64b6aa9b3c481a2d955022f621e495fb55e549e",
22
+ "gitHead": "e6e791d4e4f9afd94f2093d6045d686b85e5a682",
23
23
  "main": "dist/cjs/index.js",
24
24
  "module": "dist/esm/index.js",
25
25
  "peerDependencies": {