@mysten-incubation/suipay-buyer 0.0.0-stage → 0.3.0-dev.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
@@ -1,3 +1,198 @@
1
- # Temporary Holding Version
1
+ # @mysten-incubation/suipay-buyer
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Typed buyer SDK for SuiPay. Pay a 402 from a delegated spend-account grant,
4
+ or manage the same `/v1` control plane the console and MCP already use.
5
+
6
+ V1 allowance settlement is removed from the HTTP wire (ADR-0026). This package
7
+ leads with `createPayer` over a spend-account grant.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @mysten-incubation/suipay-buyer
13
+ ```
14
+
15
+ Do not publish or depend on `@cmdoss/suipay-core`. The wire types and 402
16
+ codec are bundled into this package.
17
+
18
+ ## Quickstart — pay
19
+
20
+ ```ts
21
+ import { createPayer } from '@mysten-incubation/suipay-buyer';
22
+ import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
23
+
24
+ const keypair = Ed25519Keypair.fromSecretKey(/* 32-byte seed */);
25
+
26
+ // Runs in Node. A page on another site cannot use this call.
27
+ const payer = await createPayer({
28
+ gatewayUrl: 'https://gateway.example',
29
+ keypair,
30
+ });
31
+ if (!payer.ok) throw new Error(payer.error.detail);
32
+ const result = await payer.value.pay('https://gateway.example/paid/weather', {
33
+ intent: { maxAmount: '1000000', asset: '0x2::sui::SUI' },
34
+ });
35
+ ```
36
+
37
+ ## Errors — branch on `code`
38
+
39
+ Every failed or ambiguous `PayResult` carries the same fields as a gateway
40
+ problem document:
41
+
42
+ | Field | Meaning |
43
+ | --- | --- |
44
+ | `code` | Registered error code, e.g. `account.funds_insufficient`. **Branch on this.** An unknown code from a newer gateway is kept verbatim; treat it as `contact_support`. |
45
+ | `paymentState` | `charged`, `unknown` or `not_charged`; always agrees with `paid` (`true`, `'unknown'`, `false`). |
46
+ | `retryable` | `true` only when a retry cannot charge twice. |
47
+ | `nextAction` | `retry`, `retry_later`, `fix_request`, `sign_in`, `approve_access`, `add_funds`, `reconcile`, `contact_support` or `none`. |
48
+ | `detail` | Developer text. Never parse it. |
49
+
50
+ `status` keeps its meaning: `failed` only when nothing was charged,
51
+ `ambiguous` when money moved or may have. Mistakes caught before anything is
52
+ attempted are thrown as `SuiPayError`, with the same fields. The package
53
+ exports `SuiPayError`, `toErrorFields`, and the `ErrorCode`, `PaymentState`
54
+ and `NextAction` types, so a `switch` over `ErrorCode` can be exhaustive.
55
+
56
+ This is the docs site's `handle-errors` sample
57
+ (`apps/docs/src/snippets/handle-errors.ts`), which the docs build type-checks
58
+ against this package:
59
+
60
+ ```ts
61
+ import { SuiPayError, toErrorFields, type PayResult } from '@mysten-incubation/suipay-buyer';
62
+
63
+ function nextStep(result: PayResult): string {
64
+ if (result.status === 'ok') return 'done';
65
+ // Money first: never pay again once money moved or may have.
66
+ if (result.paymentState !== 'not_charged') {
67
+ return `reconcile ${result.paymentId ?? result.txDigest}`;
68
+ }
69
+ switch (result.code) {
70
+ case 'account.funds_insufficient':
71
+ return 'ask the owner to add funds';
72
+ case 'account.limit_exceeded':
73
+ return 'ask the owner to raise the per-payment limit';
74
+ case 'account.authority_stale':
75
+ return 'ask the owner to approve access again';
76
+ default:
77
+ return result.retryable ? 'retry' : (result.nextAction ?? 'contact_support');
78
+ }
79
+ }
80
+
81
+ try {
82
+ // ... createPayer / pay
83
+ } catch (err) {
84
+ const fields = err instanceof SuiPayError ? err : toErrorFields(err);
85
+ console.log(fields.code, fields.paymentState, fields.nextAction);
86
+ }
87
+ ```
88
+
89
+ The control-plane client (`createBuyer`) returns the same fields on
90
+ `result.error` (a `SuiPayError`), with `retryable` derived from the code rather
91
+ than always `false`.
92
+
93
+ Every code has a docs page at `<docs>/errors/<category>/<reason>`.
94
+
95
+ ## Quickstart — connection card
96
+
97
+ The console Connection card is an `AgentConnectionManifest`. Parse it, then
98
+ pay with the same barrel — do not import private `src/agent-sdk/`.
99
+
100
+ ```ts
101
+ import {
102
+ parseAgentManifest,
103
+ manifestToSession,
104
+ createReader,
105
+ personalMessageSigner,
106
+ type AgentConnectionManifest,
107
+ } from '@mysten-incubation/suipay-buyer';
108
+ import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
109
+
110
+ const parsed = parseAgentManifest(card as AgentConnectionManifest);
111
+ if (!parsed.ok) throw new Error(parsed.error.detail); // parsed.error.code is a registry code
112
+ const keypair = Ed25519Keypair.fromSecretKey(/* 32-byte seed */);
113
+ const session = manifestToSession({ manifest: parsed.value, signer: keypair });
114
+ if (!session.ok) throw new Error(session.error.detail);
115
+
116
+ const reads = createReader({
117
+ gatewayUrl: 'https://gateway.example',
118
+ context: {
119
+ network: session.value.session.network,
120
+ packageId: session.value.session.packageId,
121
+ grantObjectId: session.value.session.authority.grantObjectId,
122
+ delegateAddress: session.value.session.authority.delegateAddress,
123
+ },
124
+ signPersonalMessage: personalMessageSigner(keypair),
125
+ });
126
+ await reads.balance();
127
+ ```
128
+
129
+ ## Quickstart — control plane
130
+
131
+ `createBuyer` wraps `/v1` only. It loads no `node:sqlite` and needs
132
+ no native addons.
133
+
134
+ ```ts
135
+ import { createBuyer } from '@mysten-incubation/suipay-buyer';
136
+
137
+ const client = createBuyer({
138
+ baseUrl: 'https://gateway.example',
139
+ getAuthorization: () => `Bearer ${sessionToken}`,
140
+ ownerAddress: '0x…',
141
+ packageId: '0x…', // Pinned SuiPay Move package ID
142
+ signer: {
143
+ address: '0x…',
144
+ async signTransaction({ bytes }) {
145
+ /* wallet or keypair over verified sponsored BCS bytes */
146
+ return { signature: '…' };
147
+ },
148
+ },
149
+ });
150
+
151
+ const accounts = await client.accounts.list();
152
+ const created = await client.agents.createAndActivate({
153
+ agentAddress: '0x…',
154
+ name: 'my-agent',
155
+ assetScopes: [
156
+ {
157
+ asset: 'sui',
158
+ totalBudgetAtomic: '1000000000',
159
+ maxPerPaymentAtomic: '10000000',
160
+ },
161
+ ],
162
+ idempotencyKey: 'grant-1',
163
+ });
164
+ ```
165
+
166
+ Transactions returned by the gateway for owner signing are verified client-side
167
+ against the pinned `packageId` and strict per-operation templates before passing
168
+ bytes to the owner signer (ADR-0065). If transaction bytes do not strictly match
169
+ the expected operation, signing is refused and a `TransactionVerificationError`
170
+ is thrown.
171
+
172
+ `createAndActivate` and the pool / policy / grant action helpers run the
173
+ prepare → sponsor → sign → execute → finalize round-trip. After execute
174
+ is sent, a lost confirmation is `chain.tx_unconfirmed` (money state
175
+ `unknown`, next action `reconcile`) and is **not** retried.
176
+
177
+ ## Entries
178
+
179
+ | Import | What it is |
180
+ | --- | --- |
181
+ | `@mysten-incubation/suipay-buyer` | Delegated pay, `createPayer`, typed `/v1` client |
182
+ | `@mysten-incubation/suipay-mcp` | MCP client |
183
+ | Seller template | Copy `apps/seller-*`; no published seller SDK |
184
+
185
+ A management-only import of the main entry needs no native addons.
186
+
187
+ ## Build (this repo)
188
+
189
+ ```bash
190
+ pnpm --filter @mysten-incubation/suipay-buyer test
191
+ pnpm --filter @mysten-incubation/suipay-buyer build
192
+ ```
193
+
194
+ Publishing is the `release-npm` workflow: merge to `dev` publishes
195
+ `@mysten-incubation/suipay-buyer@<version>-dev.N` with dist-tag `dev` (OIDC, pack gate,
196
+ provenance). `latest` is a gated dispatch from `main` (`dry_run` default
197
+ true). Do not `npm publish` from a laptop. See
198
+ [`docs/guide/npm-release.md`](../../docs/guide/npm-release.md).