@x1scroll/agent-sdk 1.2.1 → 1.2.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +59 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x1scroll/agent-sdk",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Agent identity and on-chain memory protocol for X1 blockchain",
5
5
  "license": "BSL-1.1",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -326,25 +326,26 @@ class AgentClient {
326
326
  * X1 RPC endpoint (default: https://rpc.x1.xyz).
327
327
  * Use https://rpc.x1scroll.io for our dedicated node.
328
328
  */
329
- constructor({ wallet = null, rpcUrl = DEFAULT_RPC_URL } = {}) {
330
- // ── resolve wallet ──
331
- if (wallet === null || wallet === undefined) {
329
+ constructor({ wallet = null, keypair = null, rpcUrl = DEFAULT_RPC_URL } = {}) {
330
+ // ── resolve wallet — accept both `wallet` and `keypair` param names ──
331
+ const resolvedWallet = wallet || keypair;
332
+ if (resolvedWallet === null || resolvedWallet === undefined) {
332
333
  this.keypair = null;
333
334
  this.walletAddress = null;
334
- } else if (wallet instanceof Keypair) {
335
- this.keypair = wallet;
336
- this.walletAddress = wallet.publicKey.toBase58();
337
- } else if (typeof wallet === 'string') {
338
- const secretKey = bs58decode(wallet);
335
+ } else if (resolvedWallet instanceof Keypair) {
336
+ this.keypair = resolvedWallet;
337
+ this.walletAddress = resolvedWallet.publicKey.toBase58();
338
+ } else if (typeof resolvedWallet === 'string') {
339
+ const secretKey = bs58decode(resolvedWallet);
339
340
  this.keypair = Keypair.fromSecretKey(secretKey);
340
341
  this.walletAddress = this.keypair.publicKey.toBase58();
341
- } else if (wallet && wallet.secretKey && wallet.publicKey) {
342
+ } else if (resolvedWallet && resolvedWallet.secretKey && resolvedWallet.publicKey) {
342
343
  // Keypair-like object
343
- this.keypair = wallet;
344
- this.walletAddress = wallet.publicKey.toBase58();
344
+ this.keypair = resolvedWallet;
345
+ this.walletAddress = resolvedWallet.publicKey.toBase58();
345
346
  } else {
346
347
  throw new AgentSDKError(
347
- 'wallet must be a Keypair, a base58 secret key string, or null',
348
+ 'wallet (or keypair) must be a Keypair, a base58 secret key string, or null',
348
349
  'INVALID_WALLET'
349
350
  );
350
351
  }
@@ -992,8 +993,24 @@ class AgentClient {
992
993
  * @param {Buffer|null} [parentHash] 32-byte parent decision hash; zeros for root
993
994
  * @returns {Promise<{ sig: string, decisionHash: string, pda: string }>}
994
995
  */
995
- async decisionWrite(agentKeypair, branchLabel, cid, outcome, confidence, parentHash = null) {
996
+ async decisionWrite(agentKeypair, branchLabelOrMessage, cidOrOpts, outcome, confidence, parentHash = null) {
996
997
  assertIsSigner(agentKeypair, 'agentKeypair');
998
+
999
+ // ── Simple overload: decisionWrite(keypair, type, message) ──
1000
+ // Allows: client.decisionWrite(kp, 'trade', 'bought XNT at 0.34')
1001
+ // Internally maps to branchLabel=type, cid=sha256(message), outcome=1, confidence=9000
1002
+ let branchLabel, cid;
1003
+ if (typeof cidOrOpts === 'string' && cidOrOpts.length < 64 && !cidOrOpts.startsWith('Qm') && outcome === undefined) {
1004
+ branchLabel = branchLabelOrMessage;
1005
+ // Hash the message string into a pseudo-CID for on-chain storage
1006
+ cid = 'msg:' + crypto.createHash('sha256').update(cidOrOpts).digest('hex').slice(0, 44);
1007
+ outcome = 1; // executed
1008
+ confidence = 9000; // 90%
1009
+ } else {
1010
+ branchLabel = branchLabelOrMessage;
1011
+ cid = cidOrOpts;
1012
+ }
1013
+
997
1014
  validateString(branchLabel, 'branchLabel', 64);
998
1015
  validateString(cid, 'cid', 64);
999
1016
 
@@ -1001,10 +1018,17 @@ class AgentClient {
1001
1018
  await this._assertSufficientBalance(agentKeypair, 1_000_000, 'decision_write (0.001 XNT)');
1002
1019
 
1003
1020
  if (typeof outcome !== 'number' || ![0, 1, 2].includes(outcome)) {
1004
- throw new AgentSDKError('outcome must be 0 (pending), 1 (executed), or 2 (rejected)', 'INVALID_INPUT');
1021
+ throw new AgentSDKError(
1022
+ `outcome must be 0 (pending), 1 (executed), or 2 (rejected) — got: ${JSON.stringify(outcome)}. ` +
1023
+ 'Tip: use the simple form decisionWrite(keypair, type, message) to skip outcome/confidence.',
1024
+ 'INVALID_INPUT'
1025
+ );
1005
1026
  }
1006
1027
  if (typeof confidence !== 'number' || confidence < 0 || confidence > 10000) {
1007
- throw new AgentSDKError('confidence must be 0-10000 basis points', 'INVALID_INPUT');
1028
+ throw new AgentSDKError(
1029
+ `confidence must be 0-10000 basis points — got: ${JSON.stringify(confidence)}`,
1030
+ 'INVALID_INPUT'
1031
+ );
1008
1032
  }
1009
1033
 
1010
1034
  // Derive decision_hash: sha256(JSON.stringify({cid, branchLabel, timestamp}))
@@ -1179,8 +1203,17 @@ class AgentClient {
1179
1203
  throw new AgentSDKError('limit must be a positive integer', 'INVALID_INPUT');
1180
1204
  }
1181
1205
 
1182
- const agentPdaPubkey = new PublicKey(agentPda);
1183
- const connection = this._getConnection();
1206
+ let agentPdaPubkey;
1207
+ try {
1208
+ agentPdaPubkey = new PublicKey(agentPda);
1209
+ } catch (err) {
1210
+ throw new AgentSDKError(
1211
+ `contextGet: invalid agentPda — expected a base58 public key, got: ${JSON.stringify(agentPda)}`,
1212
+ 'INVALID_INPUT', err
1213
+ );
1214
+ }
1215
+
1216
+ const connection = this._getConnection();
1184
1217
 
1185
1218
  // Fetch recent signatures for this address
1186
1219
  const sigs = await connection.getSignaturesForAddress(agentPdaPubkey, { limit });
@@ -1222,8 +1255,15 @@ class AgentClient {
1222
1255
  });
1223
1256
  break; // one entry per tx
1224
1257
  }
1225
- } catch (_) {
1226
- // skip transactions we can't parse
1258
+ } catch (err) {
1259
+ // Log parse failures but don't block — one bad tx shouldn't kill the whole query
1260
+ entries.push({
1261
+ slot: sigInfo.slot,
1262
+ sig: sigInfo.signature,
1263
+ instruction: 'ParseError',
1264
+ error: err.message || String(err),
1265
+ blockTime: sigInfo.blockTime || null,
1266
+ });
1227
1267
  }
1228
1268
  }));
1229
1269