near-safe
Version:
An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.
76 lines (75 loc) • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pimlico = void 0;
exports.stripApiKey = stripApiKey;
const viem_1 = require("viem");
class Pimlico {
constructor(apiKey) {
this.apiKey = apiKey;
}
bundlerUrl(chainId) {
return `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${this.apiKey}`;
}
// New method to query sponsorship policies
async getSponsorshipPolicies() {
const url = `https://api.pimlico.io/v2/account/sponsorship_policies?apikey=${this.apiKey}`;
const allPolicies = await this.handleRequest(async () => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}: ${response.statusText}`);
}
return response.json();
});
return allPolicies.data;
}
async getSponsorshipPolicyByName(name) {
const allPolicies = await this.getSponsorshipPolicies();
const result = allPolicies.filter((t) => t.policy_name === name);
if (result.length === 0) {
throw new Error(`No policy found with policy_name=${name}: try ${allPolicies.map((t) => t.policy_name)}`);
}
else if (result.length > 1) {
throw new Error(`Multiple Policies with same policy_name=${name}: ${JSON.stringify(result)}`);
}
return result[0];
}
async getSponsorshipPolicyById(id) {
const allPolicies = await this.getSponsorshipPolicies();
const result = allPolicies.filter((t) => t.id === id);
if (result.length === 0) {
throw new Error(`No policy found with id=${id}: try ${allPolicies.map((t) => t.id)}`);
}
// We assume that ids are unique so that result.length > 1 need not be handled.
return result[0];
}
async handleRequest(clientMethod) {
try {
return await clientMethod();
}
catch (error) {
const message = stripApiKey(error);
if (error instanceof viem_1.HttpRequestError) {
if (error.status === 401) {
throw new Error("Unauthorized request. Please check your Pimlico API key.");
}
else {
throw new Error(`Pimlico: ${message}`);
}
}
else if (error instanceof viem_1.RpcError) {
throw new Error(`Failed to send user op with: ${message}`);
}
throw new Error(`Bundler Request: ${message}`);
}
}
}
exports.Pimlico = Pimlico;
function stripApiKey(error) {
const message = error instanceof Error ? error.message : String(error);
return message.replace(/(apikey=)[^\s&]+/, "$1***");
// Could also do this with slicing.
// const keyStart = message.indexOf("apikey=") + 7;
// // If no apikey in the message, return it as is.
// if (keyStart === -1) return message;
// return `${message.slice(0, keyStart)}***${message.slice(keyStart + 36)}`;
}