near-safe 0.6.1 → 0.6.2

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.
@@ -0,0 +1,2 @@
1
+ export declare const USER_OP_IDENTIFIER: `0x${string}`;
2
+ export declare const DEFAULT_SAFE_SALT_NONCE: string;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_SAFE_SALT_NONCE = exports.USER_OP_IDENTIFIER = void 0;
4
+ const viem_1 = require("viem");
5
+ const DOMAIN_SEPARATOR = "bitte/near-safe";
6
+ // 0x62697474652f6e6561722d7361666500
7
+ exports.USER_OP_IDENTIFIER = (0, viem_1.toHex)(DOMAIN_SEPARATOR, { size: 16 });
8
+ // 130811896738364114529934864114944206080
9
+ exports.DEFAULT_SAFE_SALT_NONCE = BigInt(exports.USER_OP_IDENTIFIER).toString();
@@ -1,4 +1,5 @@
1
1
  export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
+ export * from "./constants";
4
5
  export { Network, BaseTx, SignRequestData, populateTx } from "near-ca";
package/dist/cjs/index.js CHANGED
@@ -18,6 +18,7 @@ exports.populateTx = exports.Network = void 0;
18
18
  __exportStar(require("./near-safe"), exports);
19
19
  __exportStar(require("./types"), exports);
20
20
  __exportStar(require("./util"), exports);
21
+ __exportStar(require("./constants"), exports);
21
22
  var near_ca_1 = require("near-ca");
22
23
  Object.defineProperty(exports, "Network", { enumerable: true, get: function () { return near_ca_1.Network; } });
23
24
  Object.defineProperty(exports, "populateTx", { enumerable: true, get: function () { return near_ca_1.populateTx; } });
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SafeContractSuite = void 0;
4
4
  const viem_1 = require("viem");
5
5
  const deployments_1 = require("../_gen/deployments");
6
+ const constants_1 = require("../constants");
6
7
  const util_1 = require("../util");
7
8
  /**
8
9
  * All contracts used in account creation & execution
@@ -104,16 +105,20 @@ class SafeContractSuite {
104
105
  nonce: (0, viem_1.toHex)(nonce),
105
106
  ...this.factoryDataForSetup(safeNotDeployed, setup, safeSaltNonce),
106
107
  // <https://github.com/safe-global/safe-modules/blob/9a18245f546bf2a8ed9bdc2b04aae44f949ec7a0/modules/4337/contracts/Safe4337Module.sol#L172>
107
- callData: (0, viem_1.encodeFunctionData)({
108
- abi: this.m4337.abi,
109
- functionName: "executeUserOp",
110
- args: [
111
- txData.to,
112
- BigInt(txData.value),
113
- txData.data,
114
- txData.operation || 0,
115
- ],
116
- }),
108
+ callData: (0, viem_1.concat)([
109
+ (0, viem_1.encodeFunctionData)({
110
+ abi: this.m4337.abi,
111
+ functionName: "executeUserOp",
112
+ args: [
113
+ txData.to,
114
+ BigInt(txData.value),
115
+ txData.data,
116
+ txData.operation || 0,
117
+ ],
118
+ }),
119
+ // Append On-Chain Identifier:
120
+ constants_1.USER_OP_IDENTIFIER,
121
+ ]),
117
122
  ...feeData,
118
123
  };
119
124
  }
@@ -7,9 +7,9 @@ import { DecodedMultisend, EncodedTxData, EvmTransactionData, MetaTransaction, U
7
7
  export interface NearSafeConfig {
8
8
  accountId: string;
9
9
  mpcContractId: string;
10
- pimlicoKey: string;
11
10
  nearConfig?: NearConfig;
12
11
  privateKey?: string;
12
+ pimlicoKey: string;
13
13
  safeSaltNonce?: string;
14
14
  }
15
15
  export declare class NearSafe {
@@ -4,6 +4,7 @@ exports.NearSafe = void 0;
4
4
  const ethers_multisend_1 = require("ethers-multisend");
5
5
  const near_ca_1 = require("near-ca");
6
6
  const viem_1 = require("viem");
7
+ const constants_1 = require("./constants");
7
8
  const bundler_1 = require("./lib/bundler");
8
9
  const multisend_1 = require("./lib/multisend");
9
10
  const safe_1 = require("./lib/safe");
@@ -18,6 +19,7 @@ class NearSafe {
18
19
  */
19
20
  static async create(config) {
20
21
  const { pimlicoKey, safeSaltNonce } = config;
22
+ // const nearAdapter = await mockAdapter();
21
23
  const nearAdapter = await (0, near_ca_1.setupAdapter)({ ...config });
22
24
  const safePack = new safe_1.SafeContractSuite();
23
25
  const setup = safePack.getSetup([nearAdapter.address]);
@@ -28,7 +30,7 @@ class NearSafe {
28
30
  MPC EOA: ${nearAdapter.address}
29
31
  Safe: ${safeAddress}
30
32
  `);
31
- return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce || "0");
33
+ return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce || constants_1.DEFAULT_SAFE_SALT_NONCE);
32
34
  }
33
35
  /**
34
36
  * Constructs a new `NearSafe` object with the provided parameters.
@@ -62,7 +64,7 @@ class NearSafe {
62
64
  * @returns {string} - The contract ID of the MPC contract.
63
65
  */
64
66
  get mpcContractId() {
65
- return this.nearAdapter.mpcContract.contract.contractId;
67
+ return this.nearAdapter.mpcContract.accountId();
66
68
  }
67
69
  /**
68
70
  * Retrieves the balance of the Safe account on the specified EVM chain.
@@ -89,6 +91,7 @@ class NearSafe {
89
91
  if (transactions.length === 0) {
90
92
  throw new Error("Empty transaction set!");
91
93
  }
94
+ console.log(`Building UserOp on chainId ${chainId} with ${transactions.length} transaction(s)`);
92
95
  const bundler = this.bundlerForChainId(chainId);
93
96
  const [gasFees, nonce, safeDeployed] = await Promise.all([
94
97
  bundler.getGasPrice(),
@@ -10,4 +10,5 @@ export declare function containsValue(transactions: MetaTransaction[]): boolean;
10
10
  export declare function isContract(address: Address, chainId: number): Promise<boolean>;
11
11
  export declare function getClient(chainId: number): PublicClient;
12
12
  export declare function metaTransactionsFromRequest(params: SessionRequestParams): MetaTransaction[];
13
+ export declare function saltNonceFromMessage(input: string): string;
13
14
  export {};
package/dist/cjs/util.js CHANGED
@@ -7,6 +7,7 @@ exports.containsValue = containsValue;
7
7
  exports.isContract = isContract;
8
8
  exports.getClient = getClient;
9
9
  exports.metaTransactionsFromRequest = metaTransactionsFromRequest;
10
+ exports.saltNonceFromMessage = saltNonceFromMessage;
10
11
  const near_ca_1 = require("near-ca");
11
12
  const viem_1 = require("viem");
12
13
  //
@@ -58,3 +59,10 @@ function metaTransactionsFromRequest(params) {
58
59
  data: tx.data || "0x",
59
60
  }));
60
61
  }
62
+ function saltNonceFromMessage(input) {
63
+ // Convert the string to bytes (UTF-8 encoding)
64
+ // Compute the keccak256 hash of the input bytes
65
+ // Convert the resulting hash (which is in hex) to a BigInt
66
+ // Return string for readability and transport.
67
+ return BigInt((0, viem_1.keccak256)((0, viem_1.toBytes)(input))).toString();
68
+ }
@@ -0,0 +1,2 @@
1
+ export declare const USER_OP_IDENTIFIER: `0x${string}`;
2
+ export declare const DEFAULT_SAFE_SALT_NONCE: string;
@@ -0,0 +1,6 @@
1
+ import { toHex } from "viem";
2
+ const DOMAIN_SEPARATOR = "bitte/near-safe";
3
+ // 0x62697474652f6e6561722d7361666500
4
+ export const USER_OP_IDENTIFIER = toHex(DOMAIN_SEPARATOR, { size: 16 });
5
+ // 130811896738364114529934864114944206080
6
+ export const DEFAULT_SAFE_SALT_NONCE = BigInt(USER_OP_IDENTIFIER).toString();
@@ -1,4 +1,5 @@
1
1
  export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
+ export * from "./constants";
4
5
  export { Network, BaseTx, SignRequestData, populateTx } from "near-ca";
package/dist/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
+ export * from "./constants";
4
5
  export { Network, populateTx } from "near-ca";
@@ -1,5 +1,6 @@
1
- import { encodeFunctionData, encodePacked, getCreate2Address, keccak256, toHex, zeroAddress, } from "viem";
1
+ import { concat, encodeFunctionData, encodePacked, getCreate2Address, keccak256, toHex, zeroAddress, } from "viem";
2
2
  import { SAFE_DEPLOYMENTS } from "../_gen/deployments";
3
+ import { USER_OP_IDENTIFIER } from "../constants";
3
4
  import { PLACEHOLDER_SIG, getClient, packGas, packPaymasterData, } from "../util";
4
5
  /**
5
6
  * All contracts used in account creation & execution
@@ -108,16 +109,20 @@ export class SafeContractSuite {
108
109
  nonce: toHex(nonce),
109
110
  ...this.factoryDataForSetup(safeNotDeployed, setup, safeSaltNonce),
110
111
  // <https://github.com/safe-global/safe-modules/blob/9a18245f546bf2a8ed9bdc2b04aae44f949ec7a0/modules/4337/contracts/Safe4337Module.sol#L172>
111
- callData: encodeFunctionData({
112
- abi: this.m4337.abi,
113
- functionName: "executeUserOp",
114
- args: [
115
- txData.to,
116
- BigInt(txData.value),
117
- txData.data,
118
- txData.operation || 0,
119
- ],
120
- }),
112
+ callData: concat([
113
+ encodeFunctionData({
114
+ abi: this.m4337.abi,
115
+ functionName: "executeUserOp",
116
+ args: [
117
+ txData.to,
118
+ BigInt(txData.value),
119
+ txData.data,
120
+ txData.operation || 0,
121
+ ],
122
+ }),
123
+ // Append On-Chain Identifier:
124
+ USER_OP_IDENTIFIER,
125
+ ]),
121
126
  ...feeData,
122
127
  };
123
128
  }
@@ -7,9 +7,9 @@ import { DecodedMultisend, EncodedTxData, EvmTransactionData, MetaTransaction, U
7
7
  export interface NearSafeConfig {
8
8
  accountId: string;
9
9
  mpcContractId: string;
10
- pimlicoKey: string;
11
10
  nearConfig?: NearConfig;
12
11
  privateKey?: string;
12
+ pimlicoKey: string;
13
13
  safeSaltNonce?: string;
14
14
  }
15
15
  export declare class NearSafe {
@@ -1,6 +1,7 @@
1
1
  import { decodeMulti } from "ethers-multisend";
2
2
  import { setupAdapter, signatureFromOutcome, toPayload, } from "near-ca";
3
3
  import { decodeFunctionData, formatEther, serializeSignature, } from "viem";
4
+ import { DEFAULT_SAFE_SALT_NONCE } from "./constants";
4
5
  import { Erc4337Bundler } from "./lib/bundler";
5
6
  import { encodeMulti, isMultisendTx } from "./lib/multisend";
6
7
  import { SafeContractSuite } from "./lib/safe";
@@ -21,6 +22,7 @@ export class NearSafe {
21
22
  */
22
23
  static async create(config) {
23
24
  const { pimlicoKey, safeSaltNonce } = config;
25
+ // const nearAdapter = await mockAdapter();
24
26
  const nearAdapter = await setupAdapter({ ...config });
25
27
  const safePack = new SafeContractSuite();
26
28
  const setup = safePack.getSetup([nearAdapter.address]);
@@ -31,7 +33,7 @@ export class NearSafe {
31
33
  MPC EOA: ${nearAdapter.address}
32
34
  Safe: ${safeAddress}
33
35
  `);
34
- return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce || "0");
36
+ return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce || DEFAULT_SAFE_SALT_NONCE);
35
37
  }
36
38
  /**
37
39
  * Constructs a new `NearSafe` object with the provided parameters.
@@ -65,7 +67,7 @@ export class NearSafe {
65
67
  * @returns {string} - The contract ID of the MPC contract.
66
68
  */
67
69
  get mpcContractId() {
68
- return this.nearAdapter.mpcContract.contract.contractId;
70
+ return this.nearAdapter.mpcContract.accountId();
69
71
  }
70
72
  /**
71
73
  * Retrieves the balance of the Safe account on the specified EVM chain.
@@ -92,6 +94,7 @@ export class NearSafe {
92
94
  if (transactions.length === 0) {
93
95
  throw new Error("Empty transaction set!");
94
96
  }
97
+ console.log(`Building UserOp on chainId ${chainId} with ${transactions.length} transaction(s)`);
95
98
  const bundler = this.bundlerForChainId(chainId);
96
99
  const [gasFees, nonce, safeDeployed] = await Promise.all([
97
100
  bundler.getGasPrice(),
@@ -10,4 +10,5 @@ export declare function containsValue(transactions: MetaTransaction[]): boolean;
10
10
  export declare function isContract(address: Address, chainId: number): Promise<boolean>;
11
11
  export declare function getClient(chainId: number): PublicClient;
12
12
  export declare function metaTransactionsFromRequest(params: SessionRequestParams): MetaTransaction[];
13
+ export declare function saltNonceFromMessage(input: string): string;
13
14
  export {};
package/dist/esm/util.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Network } from "near-ca";
2
- import { concatHex, encodePacked, toHex, isHex, parseTransaction, zeroAddress, } from "viem";
2
+ import { concatHex, encodePacked, toHex, isHex, parseTransaction, zeroAddress, toBytes, keccak256, } from "viem";
3
3
  //
4
4
  export const PLACEHOLDER_SIG = encodePacked(["uint48", "uint48"], [0, 0]);
5
5
  export const packGas = (hi, lo) => encodePacked(["uint128", "uint128"], [BigInt(hi), BigInt(lo)]);
@@ -48,3 +48,10 @@ export function metaTransactionsFromRequest(params) {
48
48
  data: tx.data || "0x",
49
49
  }));
50
50
  }
51
+ export function saltNonceFromMessage(input) {
52
+ // Convert the string to bytes (UTF-8 encoding)
53
+ // Compute the keccak256 hash of the input bytes
54
+ // Convert the resulting hash (which is in hex) to a BigInt
55
+ // Return string for readability and transport.
56
+ return BigInt(keccak256(toBytes(input))).toString();
57
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "near-safe",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "license": "MIT",
5
5
  "description": "An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.",
6
6
  "author": "bh2smith",
@@ -44,7 +44,7 @@
44
44
  "@safe-global/safe-gateway-typescript-sdk": "^3.22.2",
45
45
  "ethers-multisend": "^3.1.0",
46
46
  "near-api-js": "^5.0.0",
47
- "near-ca": "^0.5.7",
47
+ "near-ca": "^0.5.9",
48
48
  "semver": "^7.6.3",
49
49
  "viem": "^2.16.5"
50
50
  },