near-safe 0.7.0 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,4 @@ export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
4
  export * from "./constants";
5
- export { Network, BaseTx, SignRequestData, populateTx } from "near-ca";
5
+ export { Network, BaseTx, SignRequestData, populateTx, NetworkFields, } from "near-ca";
@@ -1,5 +1,5 @@
1
1
  import { Address, Hash, PublicClient, Transport } from "viem";
2
- import { GasPrices, PaymasterData, UnsignedUserOperation, UserOperation, UserOperationReceipt } from "../types";
2
+ import { GasPrices, PaymasterData, SponsorshipPolicyData, UnsignedUserOperation, UserOperation, UserOperationReceipt } from "../types";
3
3
  type SponsorshipPolicy = {
4
4
  sponsorshipPolicyId: string;
5
5
  };
@@ -36,6 +36,7 @@ export declare class Erc4337Bundler {
36
36
  getGasPrice(): Promise<GasPrices>;
37
37
  getUserOpReceipt(userOpHash: Hash): Promise<UserOperationReceipt>;
38
38
  private _getUserOpReceiptInner;
39
+ getSponsorshipPolicies(): Promise<SponsorshipPolicyData[]>;
39
40
  }
40
41
  export declare function stripApiKey(error: unknown): string;
41
42
  export {};
@@ -60,6 +60,18 @@ class Erc4337Bundler {
60
60
  params: [userOpHash],
61
61
  }));
62
62
  }
63
+ // New method to query sponsorship policies
64
+ async getSponsorshipPolicies() {
65
+ const url = `https://api.pimlico.io/v2/account/sponsorship_policies?apikey=${this.apiKey}`;
66
+ const allPolocies = await handleRequest(async () => {
67
+ const response = await fetch(url);
68
+ if (!response.ok) {
69
+ throw new Error(`HTTP error! status: ${response.status}: ${response.statusText}`);
70
+ }
71
+ return response.json();
72
+ });
73
+ return allPolocies.data.filter((p) => p.chain_ids.allowlist.includes(this.chainId));
74
+ }
63
75
  }
64
76
  exports.Erc4337Bundler = Erc4337Bundler;
65
77
  async function handleRequest(clientMethod) {
@@ -11,7 +11,7 @@ export declare class SafeContractSuite {
11
11
  moduleSetup: Deployment;
12
12
  entryPoint: Deployment;
13
13
  constructor();
14
- addressForSetup(setup: Hex, saltNonce?: string): Promise<Address>;
14
+ addressForSetup(setup: Hex, saltNonce: string): Promise<Address>;
15
15
  getSetup(owners: string[]): Hex;
16
16
  addOwnerData(newOwner: Address): Hex;
17
17
  getOpHash(chainId: number, unsignedUserOp: UserOperation): Promise<Hash>;
@@ -21,7 +21,7 @@ class SafeContractSuite {
21
21
  async addressForSetup(setup, saltNonce) {
22
22
  // bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce));
23
23
  // cf: https://github.com/safe-global/safe-smart-account/blob/499b17ad0191b575fcadc5cb5b8e3faeae5391ae/contracts/proxies/SafeProxyFactory.sol#L58
24
- const salt = (0, viem_1.keccak256)((0, viem_1.encodePacked)(["bytes32", "uint256"], [(0, viem_1.keccak256)(setup), BigInt(saltNonce || "0")]));
24
+ const salt = (0, viem_1.keccak256)((0, viem_1.encodePacked)(["bytes32", "uint256"], [(0, viem_1.keccak256)(setup), BigInt(saltNonce)]));
25
25
  // abi.encodePacked(type(SafeProxy).creationCode, uint256(uint160(_singleton)));
26
26
  // cf: https://github.com/safe-global/safe-smart-account/blob/499b17ad0191b575fcadc5cb5b8e3faeae5391ae/contracts/proxies/SafeProxyFactory.sol#L29
27
27
  const initCode = (0, viem_1.encodePacked)(["bytes", "uint256"], [
@@ -43,18 +43,18 @@ class SafeContractSuite {
43
43
  abi: this.singleton.abi,
44
44
  functionName: "setup",
45
45
  args: [
46
- owners,
47
- 1, // We use sign threshold of 1.
48
- this.moduleSetup.address,
46
+ owners, // _owners
47
+ 1, // _threshold
48
+ this.moduleSetup.address, // to
49
49
  (0, viem_1.encodeFunctionData)({
50
50
  abi: this.moduleSetup.abi,
51
51
  functionName: "enableModules",
52
52
  args: [[this.m4337.address]],
53
- }),
54
- this.m4337.address,
55
- viem_1.zeroAddress,
56
- 0,
57
- viem_1.zeroAddress,
53
+ }), // data
54
+ this.m4337.address, // fallbackHandler
55
+ viem_1.zeroAddress, // paymentToken
56
+ 0, // payment
57
+ viem_1.zeroAddress, // paymentReceiver
58
58
  ],
59
59
  });
60
60
  }
@@ -3,7 +3,7 @@ import { FinalExecutionOutcome } from "near-api-js/lib/providers";
3
3
  import { NearEthAdapter, SignRequestData } from "near-ca";
4
4
  import { Address, Hash, Hex } from "viem";
5
5
  import { SafeContractSuite } from "./lib/safe";
6
- import { DecodedMultisend, EncodedTxData, EvmTransactionData, MetaTransaction, UserOperation, UserOperationReceipt } from "./types";
6
+ import { DecodedMultisend, EncodedTxData, EvmTransactionData, MetaTransaction, SponsorshipPolicyData, UserOperation, UserOperationReceipt } from "./types";
7
7
  export interface NearSafeConfig {
8
8
  accountId: string;
9
9
  mpcContractId: string;
@@ -178,4 +178,5 @@ export declare class NearSafe {
178
178
  payload: number[];
179
179
  hash: Hash;
180
180
  }>;
181
+ policyForChainId(chainId: number): Promise<SponsorshipPolicyData[]>;
181
182
  }
@@ -18,7 +18,8 @@ class NearSafe {
18
18
  * @returns {Promise<NearSafe>} - A promise that resolves to a new `NearSafe` instance.
19
19
  */
20
20
  static async create(config) {
21
- const { pimlicoKey, safeSaltNonce } = config;
21
+ const { pimlicoKey } = config;
22
+ const safeSaltNonce = config.safeSaltNonce || constants_1.DEFAULT_SAFE_SALT_NONCE;
22
23
  // const nearAdapter = await mockAdapter();
23
24
  const nearAdapter = await (0, near_ca_1.setupAdapter)({ ...config });
24
25
  const safePack = new safe_1.SafeContractSuite();
@@ -30,7 +31,7 @@ class NearSafe {
30
31
  MPC EOA: ${nearAdapter.address}
31
32
  Safe: ${safeAddress}
32
33
  `);
33
- return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce || constants_1.DEFAULT_SAFE_SALT_NONCE);
34
+ return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce);
34
35
  }
35
36
  /**
36
37
  * Constructs a new `NearSafe` object with the provided parameters.
@@ -91,7 +92,6 @@ class NearSafe {
91
92
  if (transactions.length === 0) {
92
93
  throw new Error("Empty transaction set!");
93
94
  }
94
- console.log(`Building UserOp on chainId ${chainId} with ${transactions.length} transaction(s)`);
95
95
  const bundler = this.bundlerForChainId(chainId);
96
96
  const [gasFees, nonce, safeDeployed] = await Promise.all([
97
97
  bundler.getGasPrice(),
@@ -329,5 +329,9 @@ class NearSafe {
329
329
  }
330
330
  }
331
331
  }
332
+ async policyForChainId(chainId) {
333
+ const bundler = this.bundlerForChainId(chainId);
334
+ return bundler.getSponsorshipPolicies();
335
+ }
332
336
  }
333
337
  exports.NearSafe = NearSafe;
@@ -246,4 +246,31 @@ export interface EncodedTxData {
246
246
  request: SignArgs;
247
247
  }>;
248
248
  }
249
+ export interface SponsorshipPoliciesResponse {
250
+ has_more: boolean;
251
+ data: SponsorshipPolicyData[];
252
+ }
253
+ export interface SponsorshipPolicyData {
254
+ id: string;
255
+ policy_name: string;
256
+ limits: PolicyLimits;
257
+ start_time: string;
258
+ end_time: string;
259
+ chain_ids: ChainIds;
260
+ policy_status: string;
261
+ created_at: string;
262
+ }
263
+ export interface PolicyLimits {
264
+ global: GlobalLimits;
265
+ }
266
+ export interface GlobalLimits {
267
+ user_operation_spending: SpendingLimit;
268
+ }
269
+ export interface SpendingLimit {
270
+ amount: number;
271
+ currency: string;
272
+ }
273
+ export interface ChainIds {
274
+ allowlist: number[];
275
+ }
249
276
  export {};
@@ -2,4 +2,4 @@ export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
4
  export * from "./constants";
5
- export { Network, BaseTx, SignRequestData, populateTx } from "near-ca";
5
+ export { Network, BaseTx, SignRequestData, populateTx, NetworkFields, } from "near-ca";
package/dist/esm/index.js CHANGED
@@ -2,4 +2,4 @@ export * from "./near-safe";
2
2
  export * from "./types";
3
3
  export * from "./util";
4
4
  export * from "./constants";
5
- export { Network, populateTx } from "near-ca";
5
+ export { Network, populateTx, } from "near-ca";
@@ -1,5 +1,5 @@
1
1
  import { Address, Hash, PublicClient, Transport } from "viem";
2
- import { GasPrices, PaymasterData, UnsignedUserOperation, UserOperation, UserOperationReceipt } from "../types";
2
+ import { GasPrices, PaymasterData, SponsorshipPolicyData, UnsignedUserOperation, UserOperation, UserOperationReceipt } from "../types";
3
3
  type SponsorshipPolicy = {
4
4
  sponsorshipPolicyId: string;
5
5
  };
@@ -36,6 +36,7 @@ export declare class Erc4337Bundler {
36
36
  getGasPrice(): Promise<GasPrices>;
37
37
  getUserOpReceipt(userOpHash: Hash): Promise<UserOperationReceipt>;
38
38
  private _getUserOpReceiptInner;
39
+ getSponsorshipPolicies(): Promise<SponsorshipPolicyData[]>;
39
40
  }
40
41
  export declare function stripApiKey(error: unknown): string;
41
42
  export {};
@@ -60,6 +60,18 @@ export class Erc4337Bundler {
60
60
  params: [userOpHash],
61
61
  }));
62
62
  }
63
+ // New method to query sponsorship policies
64
+ async getSponsorshipPolicies() {
65
+ const url = `https://api.pimlico.io/v2/account/sponsorship_policies?apikey=${this.apiKey}`;
66
+ const allPolocies = await handleRequest(async () => {
67
+ const response = await fetch(url);
68
+ if (!response.ok) {
69
+ throw new Error(`HTTP error! status: ${response.status}: ${response.statusText}`);
70
+ }
71
+ return response.json();
72
+ });
73
+ return allPolocies.data.filter((p) => p.chain_ids.allowlist.includes(this.chainId));
74
+ }
63
75
  }
64
76
  async function handleRequest(clientMethod) {
65
77
  try {
@@ -11,7 +11,7 @@ export declare class SafeContractSuite {
11
11
  moduleSetup: Deployment;
12
12
  entryPoint: Deployment;
13
13
  constructor();
14
- addressForSetup(setup: Hex, saltNonce?: string): Promise<Address>;
14
+ addressForSetup(setup: Hex, saltNonce: string): Promise<Address>;
15
15
  getSetup(owners: string[]): Hex;
16
16
  addOwnerData(newOwner: Address): Hex;
17
17
  getOpHash(chainId: number, unsignedUserOp: UserOperation): Promise<Hash>;
@@ -25,7 +25,7 @@ export class SafeContractSuite {
25
25
  async addressForSetup(setup, saltNonce) {
26
26
  // bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce));
27
27
  // cf: https://github.com/safe-global/safe-smart-account/blob/499b17ad0191b575fcadc5cb5b8e3faeae5391ae/contracts/proxies/SafeProxyFactory.sol#L58
28
- const salt = keccak256(encodePacked(["bytes32", "uint256"], [keccak256(setup), BigInt(saltNonce || "0")]));
28
+ const salt = keccak256(encodePacked(["bytes32", "uint256"], [keccak256(setup), BigInt(saltNonce)]));
29
29
  // abi.encodePacked(type(SafeProxy).creationCode, uint256(uint160(_singleton)));
30
30
  // cf: https://github.com/safe-global/safe-smart-account/blob/499b17ad0191b575fcadc5cb5b8e3faeae5391ae/contracts/proxies/SafeProxyFactory.sol#L29
31
31
  const initCode = encodePacked(["bytes", "uint256"], [
@@ -47,18 +47,18 @@ export class SafeContractSuite {
47
47
  abi: this.singleton.abi,
48
48
  functionName: "setup",
49
49
  args: [
50
- owners,
51
- 1, // We use sign threshold of 1.
52
- this.moduleSetup.address,
50
+ owners, // _owners
51
+ 1, // _threshold
52
+ this.moduleSetup.address, // to
53
53
  encodeFunctionData({
54
54
  abi: this.moduleSetup.abi,
55
55
  functionName: "enableModules",
56
56
  args: [[this.m4337.address]],
57
- }),
58
- this.m4337.address,
59
- zeroAddress,
60
- 0,
61
- zeroAddress,
57
+ }), // data
58
+ this.m4337.address, // fallbackHandler
59
+ zeroAddress, // paymentToken
60
+ 0, // payment
61
+ zeroAddress, // paymentReceiver
62
62
  ],
63
63
  });
64
64
  }
@@ -3,7 +3,7 @@ import { FinalExecutionOutcome } from "near-api-js/lib/providers";
3
3
  import { NearEthAdapter, SignRequestData } from "near-ca";
4
4
  import { Address, Hash, Hex } from "viem";
5
5
  import { SafeContractSuite } from "./lib/safe";
6
- import { DecodedMultisend, EncodedTxData, EvmTransactionData, MetaTransaction, UserOperation, UserOperationReceipt } from "./types";
6
+ import { DecodedMultisend, EncodedTxData, EvmTransactionData, MetaTransaction, SponsorshipPolicyData, UserOperation, UserOperationReceipt } from "./types";
7
7
  export interface NearSafeConfig {
8
8
  accountId: string;
9
9
  mpcContractId: string;
@@ -178,4 +178,5 @@ export declare class NearSafe {
178
178
  payload: number[];
179
179
  hash: Hash;
180
180
  }>;
181
+ policyForChainId(chainId: number): Promise<SponsorshipPolicyData[]>;
181
182
  }
@@ -21,7 +21,8 @@ export class NearSafe {
21
21
  * @returns {Promise<NearSafe>} - A promise that resolves to a new `NearSafe` instance.
22
22
  */
23
23
  static async create(config) {
24
- const { pimlicoKey, safeSaltNonce } = config;
24
+ const { pimlicoKey } = config;
25
+ const safeSaltNonce = config.safeSaltNonce || DEFAULT_SAFE_SALT_NONCE;
25
26
  // const nearAdapter = await mockAdapter();
26
27
  const nearAdapter = await setupAdapter({ ...config });
27
28
  const safePack = new SafeContractSuite();
@@ -33,7 +34,7 @@ export class NearSafe {
33
34
  MPC EOA: ${nearAdapter.address}
34
35
  Safe: ${safeAddress}
35
36
  `);
36
- return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce || DEFAULT_SAFE_SALT_NONCE);
37
+ return new NearSafe(nearAdapter, safePack, pimlicoKey, setup, safeAddress, safeSaltNonce);
37
38
  }
38
39
  /**
39
40
  * Constructs a new `NearSafe` object with the provided parameters.
@@ -94,7 +95,6 @@ export class NearSafe {
94
95
  if (transactions.length === 0) {
95
96
  throw new Error("Empty transaction set!");
96
97
  }
97
- console.log(`Building UserOp on chainId ${chainId} with ${transactions.length} transaction(s)`);
98
98
  const bundler = this.bundlerForChainId(chainId);
99
99
  const [gasFees, nonce, safeDeployed] = await Promise.all([
100
100
  bundler.getGasPrice(),
@@ -332,4 +332,8 @@ export class NearSafe {
332
332
  }
333
333
  }
334
334
  }
335
+ async policyForChainId(chainId) {
336
+ const bundler = this.bundlerForChainId(chainId);
337
+ return bundler.getSponsorshipPolicies();
338
+ }
335
339
  }
@@ -246,4 +246,31 @@ export interface EncodedTxData {
246
246
  request: SignArgs;
247
247
  }>;
248
248
  }
249
+ export interface SponsorshipPoliciesResponse {
250
+ has_more: boolean;
251
+ data: SponsorshipPolicyData[];
252
+ }
253
+ export interface SponsorshipPolicyData {
254
+ id: string;
255
+ policy_name: string;
256
+ limits: PolicyLimits;
257
+ start_time: string;
258
+ end_time: string;
259
+ chain_ids: ChainIds;
260
+ policy_status: string;
261
+ created_at: string;
262
+ }
263
+ export interface PolicyLimits {
264
+ global: GlobalLimits;
265
+ }
266
+ export interface GlobalLimits {
267
+ user_operation_spending: SpendingLimit;
268
+ }
269
+ export interface SpendingLimit {
270
+ amount: number;
271
+ currency: string;
272
+ }
273
+ export interface ChainIds {
274
+ allowlist: number[];
275
+ }
249
276
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "near-safe",
3
- "version": "0.7.0",
3
+ "version": "0.7.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",