@ftptech/canton-agent-wallet 0.10.0 → 1.1.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.
@@ -1 +1 @@
1
- {"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../src/withdraw.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAc,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,uDAAuD;IACvD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,+EAA+E;IAC/E,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC;;yEAEqE;IACrE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;sBAMkB;IAClB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB;;6BAEyB;IACzB,KAAK,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,wBAAsB,QAAQ,CAC5B,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CA8B/C"}
1
+ {"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../src/withdraw.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAc,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAMhD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,uDAAuD;IACvD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,+EAA+E;IAC/E,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC;;yEAEqE;IACrE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;;sBAMkB;IAClB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB;;6BAEyB;IACzB,KAAK,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,wBAAsB,QAAQ,CAC5B,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAsF/C"}
package/dist/withdraw.js CHANGED
@@ -8,6 +8,8 @@ import { loadWallet } from "./store.js";
8
8
  import { RelayClient } from "./relay-client.js";
9
9
  import { transfer } from "./tx.js";
10
10
  import { resolveHashBinding } from "./hash-binding.js";
11
+ import { resolveTrustedRegistryParties } from "./registry-parties.js";
12
+ import { sumLedgerDecimals, compareLedgerDecimals } from "@ftptech/x402-canton-core";
11
13
  import { resolveTrustedDsoParty } from "./trusted-dso.js";
12
14
  export async function withdraw(opts) {
13
15
  const wallet = opts.wallet ?? loadWallet();
@@ -15,16 +17,60 @@ export async function withdraw(opts) {
15
17
  throw new Error("no wallet — run `create` first");
16
18
  const relay = opts.relay ??
17
19
  new RelayClient({ relayUrl: wallet.relayUrl, apiKey: opts.apiKey });
20
+ // A registry token needs BOTH halves: the registrar (admin) and the id. One
21
+ // without the other cannot name an instrument, so it is refused here rather
22
+ // than silently falling back to Canton Coin — sending the wrong asset to an
23
+ // external address is the one mistake a withdraw must never make.
24
+ const isRegistry = opts.instrumentId !== undefined;
25
+ if (isRegistry && !opts.instrumentAdmin) {
26
+ throw new Error(`withdraw: instrumentId ${JSON.stringify(opts.instrumentId)} needs instrumentAdmin ` +
27
+ `(the registrar party) alongside it`);
28
+ }
29
+ if (isRegistry && opts.instrumentId === "Amulet") {
30
+ throw new Error("withdraw: Amulet is Canton Coin — omit instrumentId to withdraw CC");
31
+ }
18
32
  // Read the balance once: it's both the default (full) amount and the
19
33
  // pre-flight check so an over-balance / zero withdraw fails FAST with a clear
20
34
  // message instead of a cryptic relay 502 (prepare can't fund) downstream.
21
- const bal = await relay.balance(wallet.party);
22
- const amount = opts.amount ?? bal.cc;
23
- if (!(Number(amount) > 0)) {
35
+ //
36
+ // For a registry token the balance comes from the HoldingV1 read, and only
37
+ // UNLOCKED holdings count: a locked holding is owned but not spendable, and
38
+ // offering it as an input would fail at the ledger after the user committed
39
+ // to the amount.
40
+ let available;
41
+ let inputCids;
42
+ let unit;
43
+ if (isRegistry) {
44
+ const h = await relay.holdings(wallet.party, {
45
+ admin: opts.instrumentAdmin,
46
+ id: opts.instrumentId,
47
+ });
48
+ const ins = h.instruments[0];
49
+ const spendable = (ins?.holdings ?? []).filter((x) => !x.locked);
50
+ // Exact sum: a float total can land an atomic ABOVE what the wallet holds,
51
+ // and that number is both the transfer amount and the pre-flight guard.
52
+ available = sumLedgerDecimals(spendable.map((x) => x.amount));
53
+ inputCids = spendable.map((x) => x.cid);
54
+ unit = opts.instrumentId;
55
+ }
56
+ else {
57
+ const bal = await relay.balance(wallet.party);
58
+ available = bal.cc;
59
+ unit = "CC";
60
+ }
61
+ const amount = opts.amount ?? available;
62
+ let positive = false;
63
+ try {
64
+ positive = compareLedgerDecimals(amount, "0") > 0;
65
+ }
66
+ catch {
67
+ positive = false;
68
+ }
69
+ if (!positive) {
24
70
  throw new Error(`withdraw amount must be a positive number (got ${JSON.stringify(amount)})`);
25
71
  }
26
- if (Number(amount) > Number(bal.cc)) {
27
- throw new Error(`insufficient funds: wallet balance is ${bal.cc} CC but ${amount} CC was requested`);
72
+ if (compareLedgerDecimals(amount, available) > 0) {
73
+ throw new Error(`insufficient funds: wallet balance is ${available} ${unit} but ${amount} ${unit} was requested`);
28
74
  }
29
75
  const instrumentAdmin = opts.instrumentAdmin ?? resolveTrustedDsoParty(process.env, wallet.network);
30
76
  const updateId = await transfer(relay, wallet, {
@@ -32,6 +78,14 @@ export async function withdraw(opts) {
32
78
  amount,
33
79
  hashBinding: opts.hashBinding ?? resolveHashBinding(),
34
80
  ...(instrumentAdmin !== undefined ? { expectInstrumentAdmin: instrumentAdmin } : {}),
81
+ ...(isRegistry
82
+ ? {
83
+ registryInstrument: true,
84
+ expectInstrumentId: opts.instrumentId,
85
+ inputHoldingCids: inputCids,
86
+ trustedRegistryParties: resolveTrustedRegistryParties(opts.instrumentAdmin, process.env),
87
+ }
88
+ : {}),
35
89
  });
36
90
  return { updateId, amount };
37
91
  }
@@ -1 +1 @@
1
- {"version":3,"file":"withdraw.js","sourceRoot":"","sources":["../src/withdraw.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,UAAU,EAAoB,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AA4B1D,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAkB;IAElB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,UAAU,EAAE,CAAC;IAC3C,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IAC/D,MAAM,KAAK,GACT,IAAI,CAAC,KAAK;QACV,IAAI,WAAW,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACtE,qEAAqE;IACrE,8EAA8E;IAC9E,0EAA0E;IAC1E,MAAM,GAAG,G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AI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAC5E,CAAC;IACJ,CAAC;IACD,IAAI,qBAAqB,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,KAAK,CACb,yCAAyC,SAAS,IAAI,IAAI,QAAQ,MAAM,IAAI,IAAI,gBAAgB,CACjG,CAAC;IACJ,CAAC;IACD,MAAM,eAAe,GACnB,IAAI,CAAC,eAAe,IAAI,sBAAsB,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;IAC9E,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE;QAC7C,QAAQ,EAAE,IAAI,CAAC,EAAE;QACjB,MAAM;QACN,WAAW,EAAE,IAAI,CAAC,WAAW,IAAI,kBAAkB,EAAE;QACrD,GAAG,CAAC,eAAe,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,qBAAqB,EAAE,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACpF,GAAG,CAAC,UAAU;YACZ,CAAC,CAAC;gBACE,kBAAkB,EAAE,IAAI;gBACxB,kBAAkB,EAAE,IAAI,CAAC,YAAa;gBACtC,gBAAgB,EAAE,SAAU;gBAC5B,sBAAsB,EAAE,6BAA6B,CACnD,IAAI,CAAC,eAAgB,EACrB,OAAO,CAAC,GAAG,CACZ;aACF;YACH,CAAC,CAAC,EAAE,CAAC;KACR,CAAC,CAAC;IACH,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;AAC9B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ftptech/canton-agent-wallet",
3
3
  "private": false,
4
- "version": "0.10.0",
4
+ "version": "1.1.0",
5
5
  "description": "Self-custody Canton wallet + x402 autopay for AI agents. One CLI: create, pay, balance, withdraw, export, import. Backs the canton-x402-agent skill.",
6
6
  "license": "Apache-2.0",
7
7
  "author": "FTP team",
@@ -19,9 +19,9 @@
19
19
  "@canton-network/core-tx-visualizer": "~1.4.0",
20
20
  "@canton-network/core-ledger-proto": "~1.4.0",
21
21
  "undici": "^7.1.0",
22
- "@ftptech/x402-canton-ledger": "^0.3.0",
23
- "@ftptech/x402-canton-core": "^0.8.0",
24
- "@ftptech/x402-canton-client": "^0.8.0"
22
+ "@ftptech/x402-canton-ledger": "^0.4.0",
23
+ "@ftptech/x402-canton-core": "^1.1.0",
24
+ "@ftptech/x402-canton-client": "^1.0.0"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public"