@vordium/sdk 0.3.0 → 0.4.0

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/README.md CHANGED
@@ -133,7 +133,7 @@ writes of this computer whose outcome is not known yet.
133
133
  ## Agents
134
134
 
135
135
  ```ts
136
- await me.registerAgent({ agent: agentAddress, name: 'Night Desk', mandate: 'market making on ETH', expiresMs })
136
+ await me.registerAgent({ agent: agentAddress, name: 'Night Desk', mandate: 'market making on ETH', expiresMs }) // a revoked agent throws AgentWasRevoked: use a new key
137
137
  await me.setAgentPolicy(agentAddress, policy) // refused before signing if the chain would refuse it
138
138
  await me.setAgentMarketLimit(agentAddress, { pair: 1, allowed: true, maxOrder18, maxPosition18, maxLeverageBps: 20000 })
139
139
  await me.createSession(agentAddress, endMs) // the agent trades with its own key for this account
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LOWERCASE_ADDRESS_OPS = exports.SUBMIT_FIELDS = exports.DAY_MS = exports.MAX_LEVERAGE_BPS = exports.ORACLE_BAND_MAX_BPS = exports.ORACLE_BAND_MIN_BPS = exports.CAPS_ALL = exports.CAPABILITY_BITS = exports.RESERVED_WORDS_DEFAULT = exports.NAME_MAX_BYTES = exports.NAME_MIN_BYTES = void 0;
3
+ exports.LOWERCASE_ADDRESS_OPS = exports.SUBMIT_FIELDS = exports.AgentWasRevoked = exports.DAY_MS = exports.MAX_LEVERAGE_BPS = exports.ORACLE_BAND_MAX_BPS = exports.ORACLE_BAND_MIN_BPS = exports.CAPS_ALL = exports.CAPABILITY_BITS = exports.RESERVED_WORDS_DEFAULT = exports.NAME_MAX_BYTES = exports.NAME_MIN_BYTES = void 0;
4
4
  exports.normalizeName = normalizeName;
5
5
  exports.nameSkeleton = nameSkeleton;
6
6
  exports.nameRefusalStep = nameRefusalStep;
7
7
  exports.requireName = requireName;
8
8
  exports.policyProblems = policyProblems;
9
9
  exports.loosens = loosens;
10
+ exports.agentRecordRead = agentRecordRead;
11
+ exports.agentWasRevoked = agentWasRevoked;
10
12
  exports.submitBody = submitBody;
11
13
  exports.policyValue = policyValue;
12
14
  exports.policyFromRead = policyFromRead;
@@ -143,6 +145,31 @@ function loosens(prev, next) {
143
145
  out.push('changes the active hours');
144
146
  return out;
145
147
  }
148
+ // ── A revoked agent is never registered again ─────────────────────────────────────────────────────────────────────
149
+ /** The agent's record shows it was revoked before: it is never registered again. Make a new key for a new agent. */
150
+ class AgentWasRevoked extends errors_js_1.VordiumError {
151
+ agent;
152
+ constructor(agent) {
153
+ super('agent_revoked_before', `Agent ${agent} was revoked before. A revoked agent is never registered again: make a new key for a new agent. Nothing was signed.`);
154
+ this.name = 'AgentWasRevoked';
155
+ this.agent = agent;
156
+ }
157
+ }
158
+ exports.AgentWasRevoked = AgentWasRevoked;
159
+ /** True when `rec` is an agent read the network answered: `found` true (an agent record) or false (never added). Any
160
+ * other answer counts as not read, and nothing is signed on it. */
161
+ function agentRecordRead(rec) {
162
+ return !!rec && typeof rec === 'object' && !Array.isArray(rec) && typeof rec.found === 'boolean';
163
+ }
164
+ /** True when the agent's record (`GET /agents/{owner}/{agent}`) shows it was ever revoked: `revoked_at_ms` or
165
+ * `revoked_height` set, `revoked` true, or `status` "revoked". The network's `found: false` is never revoked. */
166
+ function agentWasRevoked(rec) {
167
+ if (!agentRecordRead(rec) || rec.found === false)
168
+ return false;
169
+ const set = (v) => v !== undefined && v !== null && v !== 0 && v !== '0' && v !== '' && v !== false;
170
+ return set(rec.revoked_at_ms) || set(rec.revoked_height) || rec.revoked === true
171
+ || (typeof rec.status === 'string' && rec.status.toLowerCase().includes('revoked'));
172
+ }
146
173
  /** Per operation: [struct name, wire name, kind], in the order the node reads them. */
147
174
  exports.SUBMIT_FIELDS = {
148
175
  RegisterAgentV2: [['owner', 'owner', 'address'], ['agent', 'agent', 'address'], ['name', 'name', 'string'], ['mandate', 'mandate', 'string'], ['expiresMs', 'expires_ms', 'u64'], ['nonce', 'nonce', 'u64']],
@@ -850,11 +850,23 @@ class Account {
850
850
  const c = (0, ethers_1.getAddress)(a);
851
851
  return agents_js_1.LOWERCASE_ADDRESS_OPS.has(op) ? c.toLowerCase() : c;
852
852
  }
853
- /** Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. */
853
+ /**
854
+ * Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. The
855
+ * agent's record is read first: an agent that was ever revoked throws {@link AgentWasRevoked} (make a new key), a
856
+ * live agent may be registered again (renew / extend), and a record that cannot be read throws
857
+ * {@link NotAvailable}. Nothing is signed in either refusal.
858
+ */
854
859
  async registerAgent(a, waitMs = 30000) {
855
860
  const signer = this.needOwner('Registering an agent');
856
861
  const op = 'RegisterAgentV2';
857
- const value = { owner: this.addr(this.owner, op), agent: this.addr(a.agent, op), name: (0, agents_js_1.requireName)(a.name), mandate: a.mandate, expiresMs: a.expiresMs, nonce: await this.fresh('account') };
862
+ const agent = this.addr(a.agent, op);
863
+ const name = (0, agents_js_1.requireName)(a.name);
864
+ const held = await this.client.nodeReads().ok(`/agents/${this.owner}/${agent}`);
865
+ if (!(0, agents_js_1.agentRecordRead)(held))
866
+ throw new errors_js_1.NotAvailable("The agent's record could not be read, so it cannot be checked. Nothing was signed; try again.");
867
+ if ((0, agents_js_1.agentWasRevoked)(held))
868
+ throw new agents_js_1.AgentWasRevoked(agent);
869
+ const value = { owner: this.addr(this.owner, op), agent, name, mandate: a.mandate, expiresMs: a.expiresMs, nonce: await this.fresh('account') };
858
870
  return this.submitAgentOp(signer, op, value, (rec) => !!rec && rec.status === 'active' && rec.name === value.name, value.agent, waitMs);
859
871
  }
860
872
  /**
@@ -1,3 +1,4 @@
1
+ import { VordiumError } from './errors.js';
1
2
  /**
2
3
  * Agents: a key that trades for an account within the limits its owner signs. The operations in force:
3
4
  * RegisterAgentV2 (a name is required), SetAgentName, SetAgentPolicyV1, SetAgentMarketLimit, ClearAgentLock and
@@ -57,6 +58,19 @@ export interface PolicyV1 {
57
58
  export declare function policyProblems(p: PolicyV1, nowMs: number): string[];
58
59
  /** What in `next` would LOOSEN `prev`; [] = a tightening or no change (and always [] with nothing stored). */
59
60
  export declare function loosens(prev: PolicyV1 | null, next: PolicyV1): string[];
61
+ /** The agent's record shows it was revoked before: it is never registered again. Make a new key for a new agent. */
62
+ export declare class AgentWasRevoked extends VordiumError {
63
+ readonly agent: string;
64
+ constructor(agent: string);
65
+ }
66
+ /** True when `rec` is an agent read the network answered: `found` true (an agent record) or false (never added). Any
67
+ * other answer counts as not read, and nothing is signed on it. */
68
+ export declare function agentRecordRead(rec: unknown): rec is Record<string, unknown> & {
69
+ found: boolean;
70
+ };
71
+ /** True when the agent's record (`GET /agents/{owner}/{agent}`) shows it was ever revoked: `revoked_at_ms` or
72
+ * `revoked_height` set, `revoked` true, or `status` "revoked". The network's `found: false` is never revoked. */
73
+ export declare function agentWasRevoked(rec: unknown): boolean;
60
74
  export type AgentOp = 'RegisterAgentV2' | 'SetAgentName' | 'SetAgentPolicyV1' | 'SetAgentMarketLimit' | 'ClearAgentLock' | 'RevokeAgent';
61
75
  type Kind = 'address' | 'string' | 'bool' | 'u8' | 'u32' | 'u64' | 'u128';
62
76
  /** Per operation: [struct name, wire name, kind], in the order the node reads them. */
@@ -1,4 +1,4 @@
1
- import { InvalidRequest } from './errors.js';
1
+ import { InvalidRequest, VordiumError } from './errors.js';
2
2
  /**
3
3
  * Agents: a key that trades for an account within the limits its owner signs. The operations in force:
4
4
  * RegisterAgentV2 (a name is required), SetAgentName, SetAgentPolicyV1, SetAgentMarketLimit, ClearAgentLock and
@@ -131,6 +131,30 @@ export function loosens(prev, next) {
131
131
  out.push('changes the active hours');
132
132
  return out;
133
133
  }
134
+ // ── A revoked agent is never registered again ─────────────────────────────────────────────────────────────────────
135
+ /** The agent's record shows it was revoked before: it is never registered again. Make a new key for a new agent. */
136
+ export class AgentWasRevoked extends VordiumError {
137
+ agent;
138
+ constructor(agent) {
139
+ super('agent_revoked_before', `Agent ${agent} was revoked before. A revoked agent is never registered again: make a new key for a new agent. Nothing was signed.`);
140
+ this.name = 'AgentWasRevoked';
141
+ this.agent = agent;
142
+ }
143
+ }
144
+ /** True when `rec` is an agent read the network answered: `found` true (an agent record) or false (never added). Any
145
+ * other answer counts as not read, and nothing is signed on it. */
146
+ export function agentRecordRead(rec) {
147
+ return !!rec && typeof rec === 'object' && !Array.isArray(rec) && typeof rec.found === 'boolean';
148
+ }
149
+ /** True when the agent's record (`GET /agents/{owner}/{agent}`) shows it was ever revoked: `revoked_at_ms` or
150
+ * `revoked_height` set, `revoked` true, or `status` "revoked". The network's `found: false` is never revoked. */
151
+ export function agentWasRevoked(rec) {
152
+ if (!agentRecordRead(rec) || rec.found === false)
153
+ return false;
154
+ const set = (v) => v !== undefined && v !== null && v !== 0 && v !== '0' && v !== '' && v !== false;
155
+ return set(rec.revoked_at_ms) || set(rec.revoked_height) || rec.revoked === true
156
+ || (typeof rec.status === 'string' && rec.status.toLowerCase().includes('revoked'));
157
+ }
134
158
  /** Per operation: [struct name, wire name, kind], in the order the node reads them. */
135
159
  export const SUBMIT_FIELDS = {
136
160
  RegisterAgentV2: [['owner', 'owner', 'address'], ['agent', 'agent', 'address'], ['name', 'name', 'string'], ['mandate', 'mandate', 'string'], ['expiresMs', 'expires_ms', 'u64'], ['nonce', 'nonce', 'u64']],
@@ -355,7 +355,12 @@ export declare class Account {
355
355
  setMarginMode(pairId: number, mode: MarginMode, waitMs?: number): Promise<Result>;
356
356
  private submitAgentOp;
357
357
  private addr;
358
- /** Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. */
358
+ /**
359
+ * Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. The
360
+ * agent's record is read first: an agent that was ever revoked throws {@link AgentWasRevoked} (make a new key), a
361
+ * live agent may be registered again (renew / extend), and a record that cannot be read throws
362
+ * {@link NotAvailable}. Nothing is signed in either refusal.
363
+ */
359
364
  registerAgent(a: {
360
365
  agent: string;
361
366
  name: string;
@@ -1,8 +1,8 @@
1
1
  import { getAddress } from 'ethers';
2
2
  import { MODE_CODE, cancelTwapBody, marginModeBody, planTwap, readTwapRules, sessionCreateBody, sessionEnd, sessionRevokeBody, twapBody, } from './account.js';
3
- import { LOWERCASE_ADDRESS_OPS, loosens, policyFromRead, policyProblems, policyValue, requireName, submitBody, } from './agents.js';
3
+ import { AgentWasRevoked, LOWERCASE_ADDRESS_OPS, agentRecordRead, agentWasRevoked, loosens, policyFromRead, policyProblems, policyValue, requireName, submitBody, } from './agents.js';
4
4
  import { domain } from './domains.js';
5
- import { InvalidRequest, NetworkMismatch, VordiumError } from './errors.js';
5
+ import { InvalidRequest, NetworkMismatch, NotAvailable, VordiumError } from './errors.js';
6
6
  import { fetchTransport } from './http.js';
7
7
  import { connectNetwork, nodeIdentity, refreshRecord, signingPin } from './network.js';
8
8
  import { PinMismatch, pinDifferences, pinRefusal } from './pins.js';
@@ -842,11 +842,23 @@ export class Account {
842
842
  const c = getAddress(a);
843
843
  return LOWERCASE_ADDRESS_OPS.has(op) ? c.toLowerCase() : c;
844
844
  }
845
- /** Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. */
845
+ /**
846
+ * Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. The
847
+ * agent's record is read first: an agent that was ever revoked throws {@link AgentWasRevoked} (make a new key), a
848
+ * live agent may be registered again (renew / extend), and a record that cannot be read throws
849
+ * {@link NotAvailable}. Nothing is signed in either refusal.
850
+ */
846
851
  async registerAgent(a, waitMs = 30000) {
847
852
  const signer = this.needOwner('Registering an agent');
848
853
  const op = 'RegisterAgentV2';
849
- const value = { owner: this.addr(this.owner, op), agent: this.addr(a.agent, op), name: requireName(a.name), mandate: a.mandate, expiresMs: a.expiresMs, nonce: await this.fresh('account') };
854
+ const agent = this.addr(a.agent, op);
855
+ const name = requireName(a.name);
856
+ const held = await this.client.nodeReads().ok(`/agents/${this.owner}/${agent}`);
857
+ if (!agentRecordRead(held))
858
+ throw new NotAvailable("The agent's record could not be read, so it cannot be checked. Nothing was signed; try again.");
859
+ if (agentWasRevoked(held))
860
+ throw new AgentWasRevoked(agent);
861
+ const value = { owner: this.addr(this.owner, op), agent, name, mandate: a.mandate, expiresMs: a.expiresMs, nonce: await this.fresh('account') };
850
862
  return this.submitAgentOp(signer, op, value, (rec) => !!rec && rec.status === 'active' && rec.name === value.name, value.agent, waitMs);
851
863
  }
852
864
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vordium/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "TypeScript and JavaScript SDK for Vordium Chain and the Vordex perpetuals exchange: signing, orders, session keys, agents, TWAP, margin mode and live feeds.",
5
5
  "license": "MIT",
6
6
  "author": "Vordium <dev@vordium.com>",