@mysten-incubation/sui-agent-pass 0.1.32 → 0.1.34

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.
package/dist/chain.js CHANGED
@@ -523,8 +523,11 @@ export async function readAllowance(client, id, coinType) {
523
523
  if (normalizeStructTag(object.type) !== normalizeStructTag(allowanceType(coinType)) ||
524
524
  object.owner.$kind !== 'Shared')
525
525
  throw new Error('Unexpected allowance type or ownership');
526
- const now = await chainTime(client);
527
- const a = allowanceBcs.parse(object.content), s = a.settings, r = s.rate_limit?.Windowed;
526
+ return allowanceView(object.content, coinType, await chainTime(client));
527
+ }
528
+ /** An allowance's fields from its content, with `now` for the rate-limit window. */
529
+ function allowanceView(content, coinType, now) {
530
+ const a = allowanceBcs.parse(content), s = a.settings, r = s.rate_limit?.Windowed;
528
531
  let rateLimit = null;
529
532
  if (r) {
530
533
  const periodMs = r.window.PeriodicMs ?? null;
@@ -572,43 +575,46 @@ export async function readAllowanceCapTarget(client, id) {
572
575
  */
573
576
  export async function discoverAllowances(client, d, pass) {
574
577
  const app = normalizeStructTag(appType(d)), owner = normalizeSuiAddress(pass.owner), agent = normalizeSuiAddress(pass.agent);
575
- const caps = [];
578
+ // The caps come with their content, so each page names its allowances outright; the
579
+ // allowances are then one batched read. Three round trips in all, whatever the wallet holds.
580
+ const capOf = new Map();
576
581
  let cursor = null;
577
582
  do {
578
583
  const page = await client.listOwnedObjects({
579
584
  owner,
580
585
  cursor,
581
586
  limit: 50,
587
+ type: '0x2::allowance::AllowanceCap',
588
+ include: { content: true },
582
589
  });
583
- for (const o of page.objects) {
584
- const tag = parseStructTag(o.type);
585
- if (normalizeSuiAddress(tag.address) === normalizeSuiAddress('0x2') &&
586
- tag.module === 'allowance' &&
587
- tag.name === 'AllowanceCap')
588
- caps.push(o.objectId);
589
- }
590
+ for (const o of page.objects)
591
+ capOf.set(normalizeSuiAddress(allowanceCapBcs.parse(o.content).allowance), o.objectId);
590
592
  cursor = page.hasNextPage ? page.cursor : null;
591
- } while (cursor && caps.length < 1000);
593
+ } while (cursor && capOf.size < 1000);
592
594
  const allowances = {};
593
595
  let gas = null;
594
- await Promise.all(caps.map(async (cap) => {
595
- const target = await readAllowanceCapTarget(client, cap).catch(() => null);
596
- if (!target)
597
- return;
598
- const { object } = await client
599
- .getObject({ objectId: target })
600
- .catch(() => ({ object: null }));
601
- const coin = object ? allowanceCoin(object.type) : null;
596
+ if (!capOf.size)
597
+ return { allowances, gas };
598
+ const [{ objects }, now] = await Promise.all([
599
+ client.getObjects({ objectIds: [...capOf.keys()], include: { content: true } }),
600
+ chainTime(client),
601
+ ]);
602
+ for (const object of objects) {
603
+ // A revoked allowance is deleted while its cap may linger; that entry is an error here.
604
+ if (!('objectId' in object))
605
+ continue;
606
+ const coin = allowanceCoin(object.type);
602
607
  if (!coin)
603
- return;
604
- const view = await readAllowance(client, target, coin);
605
- if (!view || view.funder !== owner || view.spender !== agent)
606
- return;
608
+ continue;
609
+ const view = allowanceView(object.content, coin, now);
610
+ if (view.funder !== owner || view.spender !== agent)
611
+ continue;
612
+ const target = normalizeSuiAddress(object.objectId), cap = capOf.get(target);
607
613
  if (view.app === app)
608
614
  allowances[coin] = { allowance: target, revokeCap: cap };
609
615
  else if (view.app === null && view.name === AGENT_GAS_NAME && coin === SUI)
610
616
  gas = { allowance: target, revokeCap: cap };
611
- }));
617
+ }
612
618
  return { allowances, gas };
613
619
  }
614
620
  /** The pass-bound allowances of an active session, by coin; null where one no longer exists. */
package/dist/explorer.js CHANGED
@@ -122,12 +122,9 @@ export async function findPass(client, d, passId) {
122
122
  * pass-bound ones carry the `PASS` app and the owner as funder, the gas allowance its fixed
123
123
  * name. Null once the owner has revoked, because the allowances no longer exist.
124
124
  */
125
- export async function discoverActive(client, d, pass) {
126
- const control = await readControl(client, d, pass.id);
127
- const { allowances, gas } = await discoverAllowances(client, d, {
128
- owner: pass.owner,
129
- agent: control.agent,
130
- });
125
+ export async function discoverActive(client, d, pass, control) {
126
+ const { agent } = control ?? (await readControl(client, d, pass.id));
127
+ const { allowances, gas } = await discoverAllowances(client, d, { owner: pass.owner, agent });
131
128
  if (!gas || Object.keys(allowances).length === 0)
132
129
  return null;
133
130
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysten-incubation/sui-agent-pass",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "type": "module",
5
5
  "description": "AI builds transactions. A session signer signs. Your pass enforces the rules.",
6
6
  "repository": {