near-safe 0.8.2 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ import { DecodedTxData, SafeEncodedSignRequest } from "../types";
2
+ /**
3
+ * Decodes transaction data for a given EVM transaction and extracts relevant details.
4
+ *
5
+ * @param {EvmTransactionData} data - The raw transaction data to be decoded.
6
+ * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
7
+ */
8
+ export declare function decodeTxData({ evmMessage, chainId, }: Omit<SafeEncodedSignRequest, "hashToSign">): DecodedTxData;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeTxData = decodeTxData;
4
+ const safe_message_1 = require("../lib/safe-message");
5
+ const util_1 = require("./util");
6
+ /**
7
+ * Decodes transaction data for a given EVM transaction and extracts relevant details.
8
+ *
9
+ * @param {EvmTransactionData} data - The raw transaction data to be decoded.
10
+ * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
11
+ */
12
+ function decodeTxData({ evmMessage, chainId, }) {
13
+ const data = evmMessage;
14
+ if ((0, safe_message_1.isRlpHex)(evmMessage)) {
15
+ return (0, util_1.decodeRlpHex)(chainId, evmMessage);
16
+ }
17
+ if ((0, safe_message_1.isTransactionSerializable)(data)) {
18
+ return (0, util_1.decodeTransactionSerializable)(chainId, data);
19
+ }
20
+ if (typeof data !== "string") {
21
+ return (0, util_1.decodeTypedData)(chainId, data);
22
+ }
23
+ try {
24
+ // Stringified UserOperation.
25
+ const userOp = JSON.parse(data);
26
+ return (0, util_1.decodeUserOperation)(chainId, userOp);
27
+ }
28
+ catch (error) {
29
+ if (error instanceof SyntaxError) {
30
+ // Raw message string.
31
+ return {
32
+ chainId,
33
+ costEstimate: "0",
34
+ transactions: [],
35
+ message: data,
36
+ };
37
+ }
38
+ else {
39
+ // TODO: This shouldn't happen anymore and can probably be reverted.
40
+ // We keep it here now, because near-ca might not have adapted its router.
41
+ console.warn("Failed UserOp Parsing, try TypedData Parsing", error);
42
+ try {
43
+ const typedData = JSON.parse(data);
44
+ return (0, util_1.decodeTypedData)(chainId, typedData);
45
+ }
46
+ catch (error) {
47
+ const message = error instanceof Error ? error.message : String(error);
48
+ throw new Error(`decodeTxData: Unexpected error - ${message}`);
49
+ }
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,7 @@
1
+ import { EIP712TypedData } from "near-ca";
2
+ import { Hex, TransactionSerializable } from "viem";
3
+ import { DecodedTxData, UserOperation } from "../types";
4
+ export declare function decodeTransactionSerializable(chainId: number, tx: TransactionSerializable): DecodedTxData;
5
+ export declare function decodeRlpHex(chainId: number, tx: Hex): DecodedTxData;
6
+ export declare function decodeTypedData(chainId: number, data: EIP712TypedData): DecodedTxData;
7
+ export declare function decodeUserOperation(chainId: number, userOp: UserOperation): DecodedTxData;
@@ -1,62 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeTxData = decodeTxData;
4
3
  exports.decodeTransactionSerializable = decodeTransactionSerializable;
5
4
  exports.decodeRlpHex = decodeRlpHex;
6
5
  exports.decodeTypedData = decodeTypedData;
7
6
  exports.decodeUserOperation = decodeUserOperation;
8
7
  const ethers_multisend_1 = require("ethers-multisend");
9
8
  const viem_1 = require("viem");
10
- const deployments_1 = require("./_gen/deployments");
11
- const multisend_1 = require("./lib/multisend");
12
- const safe_message_1 = require("./lib/safe-message");
13
- /**
14
- * Decodes transaction data for a given EVM transaction and extracts relevant details.
15
- *
16
- * @param {EvmTransactionData} data - The raw transaction data to be decoded.
17
- * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
18
- */
19
- function decodeTxData({ evmMessage, chainId, }) {
20
- const data = evmMessage;
21
- if ((0, safe_message_1.isRlpHex)(evmMessage)) {
22
- return decodeRlpHex(chainId, evmMessage);
23
- }
24
- if ((0, safe_message_1.isTransactionSerializable)(data)) {
25
- return decodeTransactionSerializable(chainId, data);
26
- }
27
- if (typeof data !== "string") {
28
- return decodeTypedData(chainId, data);
29
- }
30
- try {
31
- // Stringified UserOperation.
32
- const userOp = JSON.parse(data);
33
- return decodeUserOperation(chainId, userOp);
34
- }
35
- catch (error) {
36
- if (error instanceof SyntaxError) {
37
- // Raw message string.
38
- return {
39
- chainId,
40
- costEstimate: "0",
41
- transactions: [],
42
- message: data,
43
- };
44
- }
45
- else {
46
- // TODO: This shouldn't happen anymore and can probably be reverted.
47
- // We keep it here now, because near-ca might not have adapted its router.
48
- console.warn("Failed UserOp Parsing, try TypedData Parsing", error);
49
- try {
50
- const typedData = JSON.parse(data);
51
- return decodeTypedData(chainId, typedData);
52
- }
53
- catch (error) {
54
- const message = error instanceof Error ? error.message : String(error);
55
- throw new Error(`decodeTxData: Unexpected error - ${message}`);
56
- }
57
- }
58
- }
59
- }
9
+ const deployments_1 = require("../_gen/deployments");
10
+ const multisend_1 = require("../lib/multisend");
60
11
  function decodeTransactionSerializable(chainId, tx) {
61
12
  const { gas, maxFeePerGas, maxPriorityFeePerGas, to } = tx;
62
13
  if (chainId !== tx.chainId) {
@@ -2,5 +2,6 @@ export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
4
  export * from "./constants";
5
- export * from "./decode";
6
- export { Network, BaseTx, SignRequestData, populateTx, NetworkFields, signatureFromOutcome, signatureFromTxHash, requestRouter as mpcRequestRouter, } from "near-ca";
5
+ export { decodeTxData } from "./decode";
6
+ export * from "./lib/safe-message";
7
+ export { Network, BaseTx, SignRequestData, populateTx, NetworkFields, signatureFromOutcome, signatureFromTxHash, requestRouter as mpcRequestRouter, EthTransactionParams, } from "near-ca";
package/dist/cjs/index.js CHANGED
@@ -14,12 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.mpcRequestRouter = exports.signatureFromTxHash = exports.signatureFromOutcome = exports.populateTx = exports.Network = void 0;
17
+ exports.mpcRequestRouter = exports.signatureFromTxHash = exports.signatureFromOutcome = exports.populateTx = exports.Network = exports.decodeTxData = void 0;
18
18
  __exportStar(require("./near-safe"), exports);
19
19
  __exportStar(require("./types"), exports);
20
20
  __exportStar(require("./util"), exports);
21
21
  __exportStar(require("./constants"), exports);
22
- __exportStar(require("./decode"), exports);
22
+ var decode_1 = require("./decode");
23
+ Object.defineProperty(exports, "decodeTxData", { enumerable: true, get: function () { return decode_1.decodeTxData; } });
24
+ __exportStar(require("./lib/safe-message"), exports);
23
25
  var near_ca_1 = require("near-ca");
24
26
  Object.defineProperty(exports, "Network", { enumerable: true, get: function () { return near_ca_1.Network; } });
25
27
  Object.defineProperty(exports, "populateTx", { enumerable: true, get: function () { return near_ca_1.populateTx; } });
@@ -337,13 +337,15 @@ class NearSafe {
337
337
  }
338
338
  }
339
339
  encodeForSafe(from) {
340
- const fromLower = from.toLowerCase();
341
- if (![this.address, this.mpcAddress]
342
- .map((t) => t.toLowerCase())
343
- .includes(fromLower)) {
340
+ const lowerFrom = from.toLowerCase();
341
+ const lowerZero = viem_1.zeroAddress.toLowerCase();
342
+ const lowerSafe = this.address.toLowerCase();
343
+ const lowerMpc = this.mpcAddress.toLowerCase();
344
+ // We allow zeroAddress (and and treat is as from = safe)
345
+ if (![lowerSafe, lowerMpc, lowerZero].includes(lowerFrom)) {
344
346
  throw new Error(`Unexpected from address ${from}`);
345
347
  }
346
- return this.address.toLowerCase() === fromLower;
348
+ return [this.address.toLowerCase(), lowerZero].includes(lowerFrom);
347
349
  }
348
350
  async policyForChainId(chainId) {
349
351
  const bundler = this.bundlerForChainId(chainId);
@@ -0,0 +1,8 @@
1
+ import { DecodedTxData, SafeEncodedSignRequest } from "../types";
2
+ /**
3
+ * Decodes transaction data for a given EVM transaction and extracts relevant details.
4
+ *
5
+ * @param {EvmTransactionData} data - The raw transaction data to be decoded.
6
+ * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
7
+ */
8
+ export declare function decodeTxData({ evmMessage, chainId, }: Omit<SafeEncodedSignRequest, "hashToSign">): DecodedTxData;
@@ -0,0 +1,49 @@
1
+ import { isRlpHex, isTransactionSerializable } from "../lib/safe-message";
2
+ import { decodeRlpHex, decodeTransactionSerializable, decodeTypedData, decodeUserOperation, } from "./util";
3
+ /**
4
+ * Decodes transaction data for a given EVM transaction and extracts relevant details.
5
+ *
6
+ * @param {EvmTransactionData} data - The raw transaction data to be decoded.
7
+ * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
8
+ */
9
+ export function decodeTxData({ evmMessage, chainId, }) {
10
+ const data = evmMessage;
11
+ if (isRlpHex(evmMessage)) {
12
+ return decodeRlpHex(chainId, evmMessage);
13
+ }
14
+ if (isTransactionSerializable(data)) {
15
+ return decodeTransactionSerializable(chainId, data);
16
+ }
17
+ if (typeof data !== "string") {
18
+ return decodeTypedData(chainId, data);
19
+ }
20
+ try {
21
+ // Stringified UserOperation.
22
+ const userOp = JSON.parse(data);
23
+ return decodeUserOperation(chainId, userOp);
24
+ }
25
+ catch (error) {
26
+ if (error instanceof SyntaxError) {
27
+ // Raw message string.
28
+ return {
29
+ chainId,
30
+ costEstimate: "0",
31
+ transactions: [],
32
+ message: data,
33
+ };
34
+ }
35
+ else {
36
+ // TODO: This shouldn't happen anymore and can probably be reverted.
37
+ // We keep it here now, because near-ca might not have adapted its router.
38
+ console.warn("Failed UserOp Parsing, try TypedData Parsing", error);
39
+ try {
40
+ const typedData = JSON.parse(data);
41
+ return decodeTypedData(chainId, typedData);
42
+ }
43
+ catch (error) {
44
+ const message = error instanceof Error ? error.message : String(error);
45
+ throw new Error(`decodeTxData: Unexpected error - ${message}`);
46
+ }
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,7 @@
1
+ import { EIP712TypedData } from "near-ca";
2
+ import { Hex, TransactionSerializable } from "viem";
3
+ import { DecodedTxData, UserOperation } from "../types";
4
+ export declare function decodeTransactionSerializable(chainId: number, tx: TransactionSerializable): DecodedTxData;
5
+ export declare function decodeRlpHex(chainId: number, tx: Hex): DecodedTxData;
6
+ export declare function decodeTypedData(chainId: number, data: EIP712TypedData): DecodedTxData;
7
+ export declare function decodeUserOperation(chainId: number, userOp: UserOperation): DecodedTxData;
@@ -1,55 +1,7 @@
1
1
  import { decodeMulti } from "ethers-multisend";
2
2
  import { decodeFunctionData, formatEther, parseTransaction, serializeTransaction, } from "viem";
3
- import { SAFE_DEPLOYMENTS } from "./_gen/deployments";
4
- import { isMultisendTx } from "./lib/multisend";
5
- import { isRlpHex, isTransactionSerializable } from "./lib/safe-message";
6
- /**
7
- * Decodes transaction data for a given EVM transaction and extracts relevant details.
8
- *
9
- * @param {EvmTransactionData} data - The raw transaction data to be decoded.
10
- * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
11
- */
12
- export function decodeTxData({ evmMessage, chainId, }) {
13
- const data = evmMessage;
14
- if (isRlpHex(evmMessage)) {
15
- return decodeRlpHex(chainId, evmMessage);
16
- }
17
- if (isTransactionSerializable(data)) {
18
- return decodeTransactionSerializable(chainId, data);
19
- }
20
- if (typeof data !== "string") {
21
- return decodeTypedData(chainId, data);
22
- }
23
- try {
24
- // Stringified UserOperation.
25
- const userOp = JSON.parse(data);
26
- return decodeUserOperation(chainId, userOp);
27
- }
28
- catch (error) {
29
- if (error instanceof SyntaxError) {
30
- // Raw message string.
31
- return {
32
- chainId,
33
- costEstimate: "0",
34
- transactions: [],
35
- message: data,
36
- };
37
- }
38
- else {
39
- // TODO: This shouldn't happen anymore and can probably be reverted.
40
- // We keep it here now, because near-ca might not have adapted its router.
41
- console.warn("Failed UserOp Parsing, try TypedData Parsing", error);
42
- try {
43
- const typedData = JSON.parse(data);
44
- return decodeTypedData(chainId, typedData);
45
- }
46
- catch (error) {
47
- const message = error instanceof Error ? error.message : String(error);
48
- throw new Error(`decodeTxData: Unexpected error - ${message}`);
49
- }
50
- }
51
- }
52
- }
3
+ import { SAFE_DEPLOYMENTS } from "../_gen/deployments";
4
+ import { isMultisendTx } from "../lib/multisend";
53
5
  export function decodeTransactionSerializable(chainId, tx) {
54
6
  const { gas, maxFeePerGas, maxPriorityFeePerGas, to } = tx;
55
7
  if (chainId !== tx.chainId) {
@@ -2,5 +2,6 @@ export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
4
  export * from "./constants";
5
- export * from "./decode";
6
- export { Network, BaseTx, SignRequestData, populateTx, NetworkFields, signatureFromOutcome, signatureFromTxHash, requestRouter as mpcRequestRouter, } from "near-ca";
5
+ export { decodeTxData } from "./decode";
6
+ export * from "./lib/safe-message";
7
+ export { Network, BaseTx, SignRequestData, populateTx, NetworkFields, signatureFromOutcome, signatureFromTxHash, requestRouter as mpcRequestRouter, EthTransactionParams, } from "near-ca";
package/dist/esm/index.js CHANGED
@@ -2,5 +2,6 @@ export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
4
  export * from "./constants";
5
- export * from "./decode";
5
+ export { decodeTxData } from "./decode";
6
+ export * from "./lib/safe-message";
6
7
  export { Network, populateTx, signatureFromOutcome, signatureFromTxHash, requestRouter as mpcRequestRouter, } from "near-ca";
@@ -1,5 +1,5 @@
1
1
  import { setupAdapter, signatureFromOutcome, toPayload, requestRouter as mpcRequestRouter, } from "near-ca";
2
- import { serializeSignature } from "viem";
2
+ import { serializeSignature, zeroAddress } from "viem";
3
3
  import { DEFAULT_SAFE_SALT_NONCE } from "./constants";
4
4
  import { Erc4337Bundler } from "./lib/bundler";
5
5
  import { encodeMulti } from "./lib/multisend";
@@ -340,13 +340,15 @@ export class NearSafe {
340
340
  }
341
341
  }
342
342
  encodeForSafe(from) {
343
- const fromLower = from.toLowerCase();
344
- if (![this.address, this.mpcAddress]
345
- .map((t) => t.toLowerCase())
346
- .includes(fromLower)) {
343
+ const lowerFrom = from.toLowerCase();
344
+ const lowerZero = zeroAddress.toLowerCase();
345
+ const lowerSafe = this.address.toLowerCase();
346
+ const lowerMpc = this.mpcAddress.toLowerCase();
347
+ // We allow zeroAddress (and and treat is as from = safe)
348
+ if (![lowerSafe, lowerMpc, lowerZero].includes(lowerFrom)) {
347
349
  throw new Error(`Unexpected from address ${from}`);
348
350
  }
349
- return this.address.toLowerCase() === fromLower;
351
+ return [this.address.toLowerCase(), lowerZero].includes(lowerFrom);
350
352
  }
351
353
  async policyForChainId(chainId) {
352
354
  const bundler = this.bundlerForChainId(chainId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "near-safe",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "license": "MIT",
5
5
  "description": "An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.",
6
6
  "author": "bh2smith",
@@ -1,14 +0,0 @@
1
- import { EIP712TypedData } from "near-ca";
2
- import { Hex, TransactionSerializable } from "viem";
3
- import { DecodedTxData, SafeEncodedSignRequest, UserOperation } from "./types";
4
- /**
5
- * Decodes transaction data for a given EVM transaction and extracts relevant details.
6
- *
7
- * @param {EvmTransactionData} data - The raw transaction data to be decoded.
8
- * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
9
- */
10
- export declare function decodeTxData({ evmMessage, chainId, }: Omit<SafeEncodedSignRequest, "hashToSign">): DecodedTxData;
11
- export declare function decodeTransactionSerializable(chainId: number, tx: TransactionSerializable): DecodedTxData;
12
- export declare function decodeRlpHex(chainId: number, tx: Hex): DecodedTxData;
13
- export declare function decodeTypedData(chainId: number, data: EIP712TypedData): DecodedTxData;
14
- export declare function decodeUserOperation(chainId: number, userOp: UserOperation): DecodedTxData;
@@ -1,14 +0,0 @@
1
- import { EIP712TypedData } from "near-ca";
2
- import { Hex, TransactionSerializable } from "viem";
3
- import { DecodedTxData, SafeEncodedSignRequest, UserOperation } from "./types";
4
- /**
5
- * Decodes transaction data for a given EVM transaction and extracts relevant details.
6
- *
7
- * @param {EvmTransactionData} data - The raw transaction data to be decoded.
8
- * @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
9
- */
10
- export declare function decodeTxData({ evmMessage, chainId, }: Omit<SafeEncodedSignRequest, "hashToSign">): DecodedTxData;
11
- export declare function decodeTransactionSerializable(chainId: number, tx: TransactionSerializable): DecodedTxData;
12
- export declare function decodeRlpHex(chainId: number, tx: Hex): DecodedTxData;
13
- export declare function decodeTypedData(chainId: number, data: EIP712TypedData): DecodedTxData;
14
- export declare function decodeUserOperation(chainId: number, userOp: UserOperation): DecodedTxData;