aiprotocol-sbi 1.2.8 → 1.2.9

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.
@@ -62,7 +62,7 @@ let ConfigService = ConfigService_1 = class ConfigService {
62
62
  requireWallet() {
63
63
  const cfg = this.requireSetup();
64
64
  if (!cfg.wallet_address) {
65
- throw new Error("No wallet configured. Run `aiprotocol-sbi wallet create`.");
65
+ throw new Error("No wallet configured. Run `aiprotocol-sbi setup`.");
66
66
  }
67
67
  return cfg;
68
68
  }
@@ -109,7 +109,7 @@ let SecurityService = class SecurityService {
109
109
  }
110
110
  async checkNoDuplicateEconomy(botId) {
111
111
  const result = await this.backend.checkBotHasEconomy(botId);
112
- if (result.hasEconomy) {
112
+ if (result?._id) {
113
113
  return {
114
114
  allowed: false,
115
115
  reason: "This bot already has an active economy. Only one economy per bot is allowed.",
@@ -124,6 +124,9 @@ let SecurityService = class SecurityService {
124
124
  const rateCheck = await this.checkRateLimit(botId, command);
125
125
  if (!rateCheck.allowed)
126
126
  return rateCheck;
127
+ const economyCheck = await this.checkNoDuplicateEconomy(botId);
128
+ if (!economyCheck.allowed)
129
+ return economyCheck;
127
130
  return { allowed: true };
128
131
  }
129
132
  formatWindow(secs) {
@@ -42,7 +42,7 @@ let WalletConnectCommand = WalletConnectCommand_1 = class WalletConnectCommand {
42
42
  if (opts.list || !botId) {
43
43
  const wallets = this.walletCrypto.listWallets();
44
44
  if (!wallets.length) {
45
- const msg = "No local keystores found. Run `aiprotocol-sbi wallet create` first.";
45
+ const msg = "No local keystores found. Run `aiprotocol-sbi setup` first.";
46
46
  if (opts.json)
47
47
  this.output.jsonError(msg);
48
48
  this.output.error(msg);
@@ -36,7 +36,7 @@ let WalletStatusCommand = WalletStatusCommand_1 = class WalletStatusCommand {
36
36
  const cfg = this.config.requireSetup();
37
37
  const rawAddress = opts.address || cfg.wallet_address;
38
38
  if (!rawAddress) {
39
- const msg = "No wallet address. Run `aiprotocol-sbi wallet create` or pass --address.";
39
+ const msg = "No wallet address. Run `aiprotocol-sbi setup`";
40
40
  if (opts.json)
41
41
  this.output.jsonError(msg);
42
42
  this.output.error(msg);
@@ -40,13 +40,13 @@ let WalletWhoCommand = WalletWhoCommand_1 = class WalletWhoCommand {
40
40
  if (opts.json) {
41
41
  this.output.json(this.output.envelope({
42
42
  registered: false,
43
- message: "No bot identity found. Run `aiprotocol-sbi wallet create` to register.",
43
+ message: "No bot identity found. Run `aiprotocol-sbi setup` to register.",
44
44
  }));
45
45
  }
46
46
  this.output.blank();
47
47
  this.output.warn("No bot identity registered");
48
48
  this.output.blank();
49
- this.output.info("Run `aiprotocol-sbi wallet create` to create and register a wallet.");
49
+ this.output.info("Run `aiprotocol-sbi setup` to create and register a wallet.");
50
50
  return;
51
51
  }
52
52
  const { wallet_address, bot_id, bot_name, command_count, createdAt, last_seen_at, } = existingBot;
@@ -85,7 +85,7 @@ let WalletWhoCommand = WalletWhoCommand_1 = class WalletWhoCommand {
85
85
  keystoreFound: false,
86
86
  botId: bot_id,
87
87
  botName: bot_name,
88
- message: "Keystore missing — re-run `wallet create` to regenerate.",
88
+ message: "Keystore missing — re-run `aiprotocol-sbi setup` to regenerate.",
89
89
  }));
90
90
  }
91
91
  this.output.blank();
@@ -93,7 +93,7 @@ let WalletWhoCommand = WalletWhoCommand_1 = class WalletWhoCommand {
93
93
  this.output.row("Bot ID", bot_id);
94
94
  this.output.row("Bot Name", bot_name);
95
95
  this.output.blank();
96
- this.output.warn("Run `aiprotocol-sbi wallet create` — your stable botId will be reused.");
96
+ this.output.warn("Run `aiprotocol-sbi setup` — your stable botId will be reused.");
97
97
  });
98
98
  });
99
99
  }
@@ -59,7 +59,7 @@ let WalletCryptoService = WalletCryptoService_1 = class WalletCryptoService {
59
59
  async unlockWallet(botId, password) {
60
60
  const keystorePath = this.keystorePath(botId);
61
61
  if (!fs.existsSync(keystorePath)) {
62
- throw new Error(`No keystore found for botId "${botId}". Run wallet create first.`);
62
+ throw new Error(`No keystore found for botId "${botId}". Run aiprotocol-sbi setup first.`);
63
63
  }
64
64
  const raw = fs.readFileSync(keystorePath, "utf-8");
65
65
  return ethers_1.ethers.Wallet.fromEncryptedJson(raw, password);
@@ -2,15 +2,18 @@ import { Command } from "commander";
2
2
  import { WalletCryptoService } from "./modules/wallet/wallet-crypto.service";
3
3
  import { BotIdentityService } from "./modules/wallet/bot-identity.service";
4
4
  import { BackendApiService } from "./modules/backend-api/backend-api.service";
5
+ import { AuditService } from "./modules/security/audit.service";
5
6
  import { OutputService } from "./common/output.service";
6
7
  import { ConfigService } from "./modules/config/config.service";
7
8
  export declare class SetupCommand {
8
9
  private readonly walletCrypto;
9
10
  private readonly botIdentity;
11
+ private readonly audit;
10
12
  private readonly output;
11
13
  private readonly config;
12
14
  private readonly backend;
13
- constructor(walletCrypto: WalletCryptoService, botIdentity: BotIdentityService, output: OutputService, config: ConfigService, backend: BackendApiService);
15
+ private readonly logger;
16
+ constructor(walletCrypto: WalletCryptoService, botIdentity: BotIdentityService, audit: AuditService, output: OutputService, config: ConfigService, backend: BackendApiService);
14
17
  register(program: Command): void;
15
18
  private resetConfig;
16
19
  private handleExistingBot;
@@ -22,5 +25,4 @@ export declare class SetupCommand {
22
25
  private validateBotName;
23
26
  private validateNetwork;
24
27
  private validateFunding;
25
- private handleError;
26
28
  }
@@ -8,21 +8,25 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
+ var SetupCommand_1;
11
12
  Object.defineProperty(exports, "__esModule", { value: true });
12
13
  exports.SetupCommand = void 0;
13
14
  const common_1 = require("@nestjs/common");
14
15
  const wallet_crypto_service_1 = require("./modules/wallet/wallet-crypto.service");
15
16
  const bot_identity_service_1 = require("./modules/wallet/bot-identity.service");
16
17
  const backend_api_service_1 = require("./modules/backend-api/backend-api.service");
18
+ const audit_service_1 = require("./modules/security/audit.service");
17
19
  const output_service_1 = require("./common/output.service");
18
20
  const config_service_1 = require("./modules/config/config.service");
19
- let SetupCommand = class SetupCommand {
20
- constructor(walletCrypto, botIdentity, output, config, backend) {
21
+ let SetupCommand = SetupCommand_1 = class SetupCommand {
22
+ constructor(walletCrypto, botIdentity, audit, output, config, backend) {
21
23
  this.walletCrypto = walletCrypto;
22
24
  this.botIdentity = botIdentity;
25
+ this.audit = audit;
23
26
  this.output = output;
24
27
  this.config = config;
25
28
  this.backend = backend;
29
+ this.logger = new common_1.Logger(SetupCommand_1.name);
26
30
  }
27
31
  register(program) {
28
32
  program
@@ -38,23 +42,36 @@ let SetupCommand = class SetupCommand {
38
42
  .option("--json", "Output JSON")
39
43
  .option("--reset", "Clear config")
40
44
  .action(async (opts) => {
41
- try {
42
- if (opts.reset)
43
- return this.resetConfig(opts);
45
+ const network = opts.network ?? "base";
46
+ const botId = opts.botName && network
47
+ ? this.botIdentity.generateBotId(`${opts.botName}-${network}`)
48
+ : undefined;
49
+ await this.audit.run({
50
+ botId,
51
+ command: "setup",
52
+ args: {
53
+ funding: opts.funding,
54
+ network,
55
+ reset: !!opts.reset,
56
+ },
57
+ json: opts.json,
58
+ }, async () => {
59
+ if (opts.reset) {
60
+ this.resetConfig(opts);
61
+ return;
62
+ }
44
63
  const botName = this.validateBotName(opts.botName);
45
- const network = this.validateNetwork(opts.network);
64
+ const validatedNetwork = this.validateNetwork(network);
46
65
  const funding = this.validateFunding(opts.funding);
47
- const botId = this.botIdentity.generateBotId(`${botName}-${network}`);
48
- const existingBot = await this.backend.getBot(botId);
66
+ const finalBotId = this.botIdentity.generateBotId(`${botName}-${validatedNetwork}`);
67
+ const existingBot = await this.backend.getBot(finalBotId);
49
68
  if (existingBot) {
50
- await this.handleExistingBot(existingBot, botId, network, opts);
69
+ await this.handleExistingBot(existingBot, finalBotId, validatedNetwork, opts);
51
70
  return;
52
71
  }
53
- await this.handleNewBot(botName, botId, network, funding, opts);
54
- }
55
- catch (err) {
56
- this.handleError(err, opts);
57
- }
72
+ await this.handleNewBot(botName, finalBotId, validatedNetwork, funding, opts);
73
+ });
74
+ process.exit(0);
58
75
  });
59
76
  }
60
77
  resetConfig(opts) {
@@ -65,7 +82,6 @@ let SetupCommand = class SetupCommand {
65
82
  else {
66
83
  this.output.success("Config cleared. Run `aiprotocol-sbi setup` to reconfigure.");
67
84
  }
68
- process.exit(0);
69
85
  }
70
86
  async handleExistingBot(existingBot, botId, network, opts) {
71
87
  if (!opts.json) {
@@ -84,20 +100,17 @@ let SetupCommand = class SetupCommand {
84
100
  }
85
101
  const appStatus = await this.backend.getApplicationStatus(botId);
86
102
  if (!appStatus) {
87
- this.output.warn(`Application does not exist or failed to fetch due to an error.`);
88
- this.output.warn(`Please make sure the arguments passed are correct. Reset and restart the command again.`);
89
- process.exit(1);
103
+ throw new Error("Application does not exist or failed to fetch. Reset and try again.");
90
104
  }
91
- if (appStatus?.type === "GRANT") {
92
- await this.handleGrantStatus(existingBot, appStatus, opts);
105
+ if (appStatus.type === "GRANT") {
106
+ await this.handleGrantStatus(appStatus, opts);
93
107
  }
94
- else if (appStatus?.type === "SELF") {
108
+ else {
95
109
  await this.handleSelfStatus(existingBot, appStatus, opts);
96
110
  }
97
- process.exit(0);
98
111
  }
99
- async handleGrantStatus(existingBot, appStatus, opts) {
100
- const status = appStatus?.status ?? "pending";
112
+ async handleGrantStatus(appStatus, opts) {
113
+ const status = appStatus?.status ?? "PENDING";
101
114
  if (!opts.json) {
102
115
  this.output.section("Funding Method: AIP Grant");
103
116
  this.output.row("Application ID", appStatus?.applicationId ?? "—");
@@ -110,34 +123,23 @@ let SetupCommand = class SetupCommand {
110
123
  this.output.error("❌ Grant rejected.");
111
124
  if (appStatus?.reason)
112
125
  this.output.warn(`Reason: ${appStatus.reason}`);
113
- this.output.info("Switch to self-funding: aiprotocol-sbi setup --reset");
114
126
  }
115
127
  else {
116
128
  this.output.info("Grant under review. Response within 48 hours.");
117
- this.output.info("Check status anytime: aiprotocol-sbi grant status");
118
129
  }
119
130
  this.output.blank();
120
131
  }
121
132
  }
122
133
  async handleSelfStatus(existingBot, appStatus, opts) {
123
- const token = "ALI";
124
- const requiredAmount = "500 ALI";
125
134
  if (!opts.json) {
126
135
  this.output.section("Funding Method: Self-Funded");
127
136
  this.output.info("You need to send 500 ALI to launch an economy.");
128
- this.output.info("Send payment to your bot's address:");
129
- this.output.divider();
130
137
  this.output.row("Wallet Address", existingBot.wallet_address);
131
- this.output.row("Payment Token", token);
132
- this.output.row("Required Amount", requiredAmount);
133
- this.output.blank();
134
- this.output.info("Approve these funds using 'Approve Script'");
135
138
  if (appStatus?.status === "SUCCESS") {
136
139
  this.output.success("✅ Payment confirmed! Your agent is funded.");
137
- this.output.info("aiprotocol-sbi economy launch");
138
140
  }
139
141
  else {
140
- this.output.info("Verify payment: aiprotocol-sbi payment verify");
142
+ this.output.info("Verify payment using: aiprotocol-sbi payment verify");
141
143
  }
142
144
  this.output.blank();
143
145
  }
@@ -162,9 +164,11 @@ let SetupCommand = class SetupCommand {
162
164
  network,
163
165
  });
164
166
  if (funding === "GRANT") {
165
- return this.handleGrantFlow(botId, botName, wallet, network, opts);
167
+ await this.handleGrantFlow(botId, botName, wallet, network, opts);
168
+ }
169
+ else {
170
+ await this.handleSelfFlow(botId, wallet, network, opts);
166
171
  }
167
- return this.handleSelfFlow(botId, botName, wallet, network, opts);
168
172
  }
169
173
  async handleGrantFlow(botId, botName, wallet, network, opts) {
170
174
  if (!opts.applicantName ||
@@ -173,7 +177,6 @@ let SetupCommand = class SetupCommand {
173
177
  !opts.links) {
174
178
  throw new Error("GRANT requires --applicant-name, --applicant-email, --purpose --links");
175
179
  }
176
- this.output.section("AIP Grant Application");
177
180
  const result = await this.backend.submitGrantApplication({
178
181
  bot_id: botId,
179
182
  name: opts.applicantName,
@@ -189,33 +192,21 @@ let SetupCommand = class SetupCommand {
189
192
  this.output.row("Bot ID", botId);
190
193
  this.output.row("Bot Name", botName);
191
194
  this.output.row("Wallet Address", wallet.address);
192
- this.output.row("Private Key", wallet.privateKey);
193
195
  this.output.info("Wait 48 hours for approval.");
194
- this.output.info("Check status anytime: aiprotocol-sbi grant status");
195
- process.exit(0);
196
196
  }
197
- async handleSelfFlow(botId, botName, wallet, network, opts) {
198
- const token = "ALI";
199
- if (!["ALI", "USDC"].includes(token)) {
200
- throw new Error("SELF requires --payment-token ALI or USDC");
201
- }
202
- const requiredAmount = token === "ALI" ? "500" : "10";
197
+ async handleSelfFlow(botId, wallet, network, opts) {
203
198
  await this.backend.submitApplication({
204
199
  bot_id: botId,
205
200
  wallet_address: wallet.address,
206
- expected_token: token,
207
- amount: requiredAmount,
201
+ expected_token: "ALI",
202
+ amount: "500",
208
203
  network,
209
204
  type: "SELF",
210
205
  });
211
206
  this.output.warn("⚠️ You need to send 500 ALI to launch an economy.");
212
- this.output.warn("Send payment to your bot's address:");
213
207
  this.output.row("Wallet", wallet.address);
214
208
  this.output.row("Network", network);
215
- this.output.row("Required Amount", requiredAmount);
216
- this.output.info("After sending, approve these funds using 'Approve Script'");
217
209
  this.output.info("Verify payment using: aiprotocol-sbi payment verify");
218
- process.exit(0);
219
210
  }
220
211
  validateBotName(name) {
221
212
  if (!name || name.trim().length < 2) {
@@ -236,22 +227,13 @@ let SetupCommand = class SetupCommand {
236
227
  }
237
228
  return f;
238
229
  }
239
- handleError(err, opts) {
240
- const message = err?.message || String(err);
241
- if (opts.json) {
242
- this.output.json({ success: false, error: message });
243
- }
244
- else {
245
- this.output.error(`Setup failed: ${message}`);
246
- }
247
- process.exit(1);
248
- }
249
230
  };
250
231
  exports.SetupCommand = SetupCommand;
251
- exports.SetupCommand = SetupCommand = __decorate([
232
+ exports.SetupCommand = SetupCommand = SetupCommand_1 = __decorate([
252
233
  (0, common_1.Injectable)(),
253
234
  __metadata("design:paramtypes", [wallet_crypto_service_1.WalletCryptoService,
254
235
  bot_identity_service_1.BotIdentityService,
236
+ audit_service_1.AuditService,
255
237
  output_service_1.OutputService,
256
238
  config_service_1.ConfigService,
257
239
  backend_api_service_1.BackendApiService])