near-safe 0.8.8 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. package/dist/cjs/decode/explain.d.ts +38 -0
  2. package/dist/cjs/decode/explain.js +97 -0
  3. package/dist/cjs/decode/index.d.ts +2 -8
  4. package/dist/cjs/decode/index.js +16 -50
  5. package/dist/cjs/decode/sign-request.d.ts +26 -0
  6. package/dist/cjs/decode/sign-request.js +83 -0
  7. package/dist/cjs/decode/util.d.ts +0 -10
  8. package/dist/cjs/index.d.ts +2 -2
  9. package/dist/cjs/index.js +4 -3
  10. package/dist/cjs/lib/bundler.d.ts +7 -2
  11. package/dist/cjs/lib/bundler.js +8 -12
  12. package/dist/cjs/lib/safe-message.d.ts +1 -3
  13. package/dist/cjs/lib/safe-message.js +0 -30
  14. package/dist/cjs/near-safe.js +2 -2
  15. package/dist/cjs/types/guards.d.ts +6 -0
  16. package/dist/cjs/types/guards.js +90 -0
  17. package/dist/cjs/{types.d.ts → types/index.d.ts} +5 -9
  18. package/dist/cjs/types/index.js +28 -0
  19. package/dist/cjs/util.d.ts +2 -1
  20. package/dist/cjs/util.js +23 -2
  21. package/dist/esm/decode/explain.d.ts +38 -0
  22. package/dist/esm/decode/explain.js +91 -0
  23. package/dist/esm/decode/index.d.ts +2 -8
  24. package/dist/esm/decode/index.js +2 -49
  25. package/dist/esm/decode/sign-request.d.ts +26 -0
  26. package/dist/esm/decode/sign-request.js +79 -0
  27. package/dist/esm/decode/util.d.ts +0 -10
  28. package/dist/esm/index.d.ts +2 -2
  29. package/dist/esm/index.js +3 -2
  30. package/dist/esm/lib/bundler.d.ts +7 -2
  31. package/dist/esm/lib/bundler.js +9 -13
  32. package/dist/esm/lib/safe-message.d.ts +1 -3
  33. package/dist/esm/lib/safe-message.js +1 -29
  34. package/dist/esm/near-safe.js +2 -2
  35. package/dist/esm/types/guards.d.ts +6 -0
  36. package/dist/esm/types/guards.js +83 -0
  37. package/dist/esm/{types.d.ts → types/index.d.ts} +5 -9
  38. package/dist/esm/{types.js → types/index.js} +1 -0
  39. package/dist/esm/util.d.ts +2 -1
  40. package/dist/esm/util.js +22 -2
  41. package/package.json +2 -2
  42. package/dist/cjs/types.js +0 -13
@@ -0,0 +1,83 @@
1
+ import { isEIP712TypedData } from "near-ca";
2
+ import { isAddress, isHex } from "viem";
3
+ export const isUserOperation = (data) => {
4
+ if (typeof data !== "object" || data === null)
5
+ return false;
6
+ const candidate = data;
7
+ // Required fields
8
+ const hasRequiredFields = "sender" in candidate &&
9
+ "nonce" in candidate &&
10
+ "callData" in candidate &&
11
+ "maxPriorityFeePerGas" in candidate &&
12
+ "maxFeePerGas" in candidate &&
13
+ "verificationGasLimit" in candidate &&
14
+ "callGasLimit" in candidate &&
15
+ "preVerificationGas" in candidate;
16
+ if (!hasRequiredFields)
17
+ return false;
18
+ // Type checks for required fields
19
+ const hasValidRequiredTypes = typeof candidate.sender === "string" &&
20
+ isAddress(candidate.sender) &&
21
+ typeof candidate.nonce === "string" &&
22
+ isHex(candidate.callData) &&
23
+ isHex(candidate.maxPriorityFeePerGas) &&
24
+ isHex(candidate.maxFeePerGas) &&
25
+ isHex(candidate.verificationGasLimit) &&
26
+ isHex(candidate.callGasLimit) &&
27
+ isHex(candidate.preVerificationGas);
28
+ if (!hasValidRequiredTypes)
29
+ return false;
30
+ // Optional fields type checks
31
+ if ("factory" in candidate && candidate.factory !== undefined) {
32
+ if (typeof candidate.factory !== "string" || !isAddress(candidate.factory))
33
+ return false;
34
+ }
35
+ if ("factoryData" in candidate && candidate.factoryData !== undefined) {
36
+ if (!isHex(candidate.factoryData))
37
+ return false;
38
+ }
39
+ if ("signature" in candidate && candidate.signature !== undefined) {
40
+ if (!isHex(candidate.signature))
41
+ return false;
42
+ }
43
+ if ("paymaster" in candidate && candidate.paymaster !== undefined) {
44
+ if (typeof candidate.paymaster !== "string" ||
45
+ !isAddress(candidate.paymaster))
46
+ return false;
47
+ }
48
+ if ("paymasterData" in candidate && candidate.paymasterData !== undefined) {
49
+ if (!isHex(candidate.paymasterData))
50
+ return false;
51
+ }
52
+ if ("paymasterVerificationGasLimit" in candidate &&
53
+ candidate.paymasterVerificationGasLimit !== undefined) {
54
+ if (!isHex(candidate.paymasterVerificationGasLimit))
55
+ return false;
56
+ }
57
+ if ("paymasterPostOpGasLimit" in candidate &&
58
+ candidate.paymasterPostOpGasLimit !== undefined) {
59
+ if (!isHex(candidate.paymasterPostOpGasLimit))
60
+ return false;
61
+ }
62
+ return true;
63
+ };
64
+ export const parseWithTypeGuard = (data, typeGuard) => {
65
+ // Case 1: Already the correct type
66
+ if (typeGuard(data)) {
67
+ return data;
68
+ }
69
+ // Case 2: Stringified data
70
+ if (typeof data === "string") {
71
+ try {
72
+ const parsed = JSON.parse(data);
73
+ return typeGuard(parsed) ? parsed : null;
74
+ }
75
+ catch (error) {
76
+ return null;
77
+ }
78
+ }
79
+ // Neither valid type nor valid stringified type
80
+ return null;
81
+ };
82
+ export const parseUserOperation = (data) => parseWithTypeGuard(data, isUserOperation);
83
+ export const parseEip712TypedData = (data) => parseWithTypeGuard(data, isEIP712TypedData);
@@ -1,5 +1,6 @@
1
1
  import { EIP712TypedData, EncodedSignRequest, FunctionCallTransaction, SignArgs } from "near-ca";
2
2
  import { Address, Hex, ParseAbi } from "viem";
3
+ export * from "./guards";
3
4
  /**
4
5
  * Represents a collection of Safe contract deployments, each with its own address and ABI.
5
6
  */
@@ -46,24 +47,20 @@ export interface UnsignedUserOperation {
46
47
  /**
47
48
  * Supported representation of a user operation for EntryPoint version 0.7, including gas limits and signature.
48
49
  */
49
- export interface UserOperation extends UnsignedUserOperation {
50
- /** The gas limit for verification of the operation. */
51
- verificationGasLimit: Hex;
52
- /** The gas limit for the execution of the operation call. */
53
- callGasLimit: Hex;
54
- /** The gas used before verification begins. */
55
- preVerificationGas: Hex;
50
+ export interface UserOperation extends UnsignedUserOperation, PaymasterData {
56
51
  /** Optional signature for the user operation. */
57
52
  signature?: Hex;
58
53
  }
59
54
  /**
60
55
  * Represents additional paymaster-related data for a user operation.
61
56
  */
62
- export interface PaymasterData {
57
+ export interface PaymasterData extends UserOperationGas {
63
58
  /** Optional paymaster address responsible for covering gas costs. */
64
59
  paymaster?: Address;
65
60
  /** Optional additional data required by the paymaster. */
66
61
  paymasterData?: Hex;
62
+ }
63
+ export interface UserOperationGas {
67
64
  /** The gas limit for paymaster verification. */
68
65
  paymasterVerificationGasLimit?: Hex;
69
66
  /** The gas limit for paymaster post-operation execution. */
@@ -270,4 +267,3 @@ export interface SpendingLimit {
270
267
  export interface ChainIds {
271
268
  allowlist: number[];
272
269
  }
273
- export {};
@@ -1,3 +1,4 @@
1
+ export * from "./guards";
1
2
  /**
2
3
  * Enum representing the type of operation in a meta-transaction.
3
4
  */
@@ -1,6 +1,6 @@
1
1
  import { SessionRequestParams } from "near-ca";
2
2
  import { Address, Hex, PublicClient } from "viem";
3
- import { PaymasterData, MetaTransaction } from "./types";
3
+ import { PaymasterData, MetaTransaction, UserOperation } from "./types";
4
4
  export declare const PLACEHOLDER_SIG: `0x${string}`;
5
5
  type IntLike = Hex | bigint | string | number;
6
6
  export declare const packGas: (hi: IntLike, lo: IntLike) => string;
@@ -38,4 +38,5 @@ export declare function signatureFromTxHash(txHash: string, accountId?: string):
38
38
  */
39
39
  export declare function raceToFirstResolve<T>(promises: Promise<T>[]): Promise<T>;
40
40
  export declare function assertUnique<T>(iterable: Iterable<T>, errorMessage?: string): void;
41
+ export declare function userOpTransactionCost(userOp: UserOperation): bigint;
41
42
  export {};
package/dist/esm/util.js CHANGED
@@ -17,13 +17,13 @@ export function packPaymasterData(data) {
17
17
  : "0x");
18
18
  }
19
19
  export function containsValue(transactions) {
20
- return transactions.some((tx) => tx.value !== "0");
20
+ return transactions.some((tx) => BigInt(tx.value) !== 0n);
21
21
  }
22
22
  export async function isContract(address, chainId) {
23
23
  return (await getClient(chainId).getCode({ address })) !== undefined;
24
24
  }
25
25
  export function getClient(chainId) {
26
- // TODO(bh2smith)
26
+ // TODO(bh2smith): Update defailt client URL in viem for sepolia.
27
27
  if (chainId === 11155111) {
28
28
  return createPublicClient({ transport: http(DEFAULT_SETUP_RPC) });
29
29
  }
@@ -130,3 +130,23 @@ export function assertUnique(iterable, errorMessage = "The collection contains m
130
130
  throw new Error(errorMessage);
131
131
  }
132
132
  }
133
+ export function userOpTransactionCost(userOp) {
134
+ // Convert values from hex to decimal
135
+ const preVerificationGas = BigInt(userOp.preVerificationGas);
136
+ const verificationGasLimit = BigInt(userOp.verificationGasLimit);
137
+ const callGasLimit = BigInt(userOp.callGasLimit);
138
+ const paymasterVerificationGasLimit = BigInt(userOp.paymasterVerificationGasLimit || "0x0");
139
+ const paymasterPostOpGasLimit = BigInt(userOp.paymasterPostOpGasLimit || "0x0");
140
+ // Sum total gas
141
+ const totalGasUsed = preVerificationGas +
142
+ verificationGasLimit +
143
+ callGasLimit +
144
+ paymasterVerificationGasLimit +
145
+ paymasterPostOpGasLimit;
146
+ // Convert maxFeePerGas from hex to decimal
147
+ const maxFeePerGas = BigInt(userOp.maxFeePerGas);
148
+ // Calculate total cost in wei
149
+ const totalCostInWei = totalGasUsed * maxFeePerGas;
150
+ // Convert to Ether for a human-readable value
151
+ return totalCostInWei;
152
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "near-safe",
3
- "version": "0.8.8",
3
+ "version": "0.9.1",
4
4
  "license": "MIT",
5
5
  "description": "An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.",
6
6
  "author": "bh2smith",
@@ -43,7 +43,7 @@
43
43
  "dependencies": {
44
44
  "@safe-global/safe-gateway-typescript-sdk": "^3.22.2",
45
45
  "near-api-js": "^5.0.1",
46
- "near-ca": "^0.7.0",
46
+ "near-ca": "^0.7.2",
47
47
  "semver": "^7.6.3",
48
48
  "viem": "^2.21.41"
49
49
  },
package/dist/cjs/types.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OperationType = void 0;
4
- /**
5
- * Enum representing the type of operation in a meta-transaction.
6
- */
7
- var OperationType;
8
- (function (OperationType) {
9
- /** Standard call operation (0). */
10
- OperationType[OperationType["Call"] = 0] = "Call";
11
- /** Delegate call operation (1). */
12
- OperationType[OperationType["DelegateCall"] = 1] = "DelegateCall";
13
- })(OperationType || (exports.OperationType = OperationType = {}));