@stellar-snaps/sdk 0.2.0 → 0.3.1
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 +138 -1
- package/dist/index.d.mts +613 -64
- package/dist/index.d.ts +613 -64
- package/dist/index.js +722 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +681 -61
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -14
package/README.md
CHANGED
|
@@ -14,6 +14,22 @@ npm install @stellar-snaps/sdk
|
|
|
14
14
|
pnpm add @stellar-snaps/sdk
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
+
**Peer dependencies** (install if you use transaction building or Freighter):
|
|
18
|
+
`@stellar/freighter-api`, `@stellar/stellar-sdk`
|
|
19
|
+
|
|
20
|
+
## How it works
|
|
21
|
+
|
|
22
|
+
- **Shareable Snap API** (`createSnap`, `getSnap`, `listSnaps`, `deleteSnap`)
|
|
23
|
+
Calls the Stellar Snaps web app at `https://stellar-snaps.vercel.app` by default. Snaps are stored there; you get back shareable URLs (e.g. `https://stellar-snaps.vercel.app/s/abc123`). Works in **Node.js** and **browser** (CORS is enabled on the API). Use `baseUrl` to point at your own deployment.
|
|
24
|
+
|
|
25
|
+
- **SEP-0007 URIs** (`createPaymentSnap`, `createTransactionSnap`, `parseSnapUri`)
|
|
26
|
+
Pure logic, no network. Build or parse `web+stellar:...` payment/transaction links.
|
|
27
|
+
|
|
28
|
+
- **Transactions & Freighter** (`buildPaymentTransaction`, `signWithFreighter`, `submitTransaction`, etc.)
|
|
29
|
+
Run in a **browser** with the [Freighter](https://www.freighter.app/) extension installed. Uses `@stellar/freighter-api` and `@stellar/stellar-sdk`.
|
|
30
|
+
|
|
31
|
+
So: install the package, use the Shareable Snap API from any environment; use Freighter-related APIs only in a browser with Freighter installed.
|
|
32
|
+
|
|
17
33
|
## Quick Start
|
|
18
34
|
|
|
19
35
|
### 1. Create a Payment Link
|
|
@@ -79,7 +95,46 @@ if (result.successful) {
|
|
|
79
95
|
}
|
|
80
96
|
```
|
|
81
97
|
|
|
82
|
-
### 4.
|
|
98
|
+
### 4. Create Shareable Snaps (Web App API)
|
|
99
|
+
|
|
100
|
+
Create, fetch, list, and delete database-backed snaps—same behavior as the web dashboard. Uses `https://stellar-snaps.vercel.app` by default.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { createSnap, getSnap, listSnaps, deleteSnap } from '@stellar-snaps/sdk';
|
|
104
|
+
|
|
105
|
+
// Create a snap (stores on server, returns shareable URL)
|
|
106
|
+
const { url, snap } = await createSnap({
|
|
107
|
+
creator: 'GCNZMNUTQ5UMQ5QL0FUAW3CUADWB...',
|
|
108
|
+
title: 'Buy me a coffee',
|
|
109
|
+
destination: 'GCNZMNUTQ5UMQ5QL0FUAW3CUADWB...',
|
|
110
|
+
amount: '5',
|
|
111
|
+
network: 'testnet',
|
|
112
|
+
});
|
|
113
|
+
console.log(url);
|
|
114
|
+
// => https://stellar-snaps.vercel.app/s/u8oYHLhl
|
|
115
|
+
|
|
116
|
+
// Fetch a snap by ID
|
|
117
|
+
const fetchedSnap = await getSnap('u8oYHLhl');
|
|
118
|
+
|
|
119
|
+
// List all snaps for a creator
|
|
120
|
+
const snaps = await listSnaps('GCNZMNUTQ5UMQ5QL0FUAW3CUADWB...');
|
|
121
|
+
|
|
122
|
+
// Delete a snap (requires creator address)
|
|
123
|
+
await deleteSnap('u8oYHLhl', 'GCNZMNUTQ5UMQ5QL0FUAW3CUADWB...');
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Use `baseUrl` to point at your own Stellar Snaps instance:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const { url } = await createSnap({
|
|
130
|
+
creator: 'G...',
|
|
131
|
+
title: 'Test',
|
|
132
|
+
destination: 'G...',
|
|
133
|
+
baseUrl: 'https://your-stellar-snaps.vercel.app',
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### 5. Host Your Own Snap Endpoints
|
|
83
138
|
|
|
84
139
|
Create a discovery file to enable the browser extension to find your snaps:
|
|
85
140
|
|
|
@@ -99,6 +154,77 @@ const discovery = createDiscoveryFile({
|
|
|
99
154
|
|
|
100
155
|
## API Reference
|
|
101
156
|
|
|
157
|
+
### Shareable Snap API (Web App)
|
|
158
|
+
|
|
159
|
+
These functions mirror the Stellar Snaps web app API. Use them to create and manage snaps that are stored on a server and get shareable URLs.
|
|
160
|
+
|
|
161
|
+
#### `createSnap(options)`
|
|
162
|
+
|
|
163
|
+
Creates a snap on the server and returns the shareable URL.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
interface CreateSnapOptions {
|
|
167
|
+
creator: string; // Creator's Stellar address (required)
|
|
168
|
+
title: string; // Display title (required)
|
|
169
|
+
destination: string; // Payment destination (required)
|
|
170
|
+
description?: string;
|
|
171
|
+
amount?: string; // Omit for open amount
|
|
172
|
+
assetCode?: string; // Default: 'XLM'
|
|
173
|
+
assetIssuer?: string; // Required for non-XLM
|
|
174
|
+
memo?: string;
|
|
175
|
+
memoType?: MemoType;
|
|
176
|
+
network?: 'public' | 'testnet'; // Default: 'testnet'
|
|
177
|
+
imageUrl?: string;
|
|
178
|
+
baseUrl?: string; // Default: 'https://stellar-snaps.vercel.app'
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface CreateSnapResult {
|
|
182
|
+
id: string;
|
|
183
|
+
url: string; // Full shareable URL
|
|
184
|
+
snap: Snap;
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### `getSnap(id, options?)`
|
|
189
|
+
|
|
190
|
+
Fetches a snap by ID. Uses `GET /api/snap/[id]`.
|
|
191
|
+
|
|
192
|
+
- **Throws** `SnapNotFoundError` if the snap does not exist.
|
|
193
|
+
- **Throws** `SnapApiError` on other API errors.
|
|
194
|
+
|
|
195
|
+
#### `listSnaps(creator, options?)`
|
|
196
|
+
|
|
197
|
+
Lists all snaps for a creator. Uses `GET /api/snaps?creator=...`.
|
|
198
|
+
|
|
199
|
+
#### `deleteSnap(id, creator, options?)`
|
|
200
|
+
|
|
201
|
+
Deletes a snap. Requires the creator's address (ownership). Uses `DELETE /api/snaps?id=...&creator=...`.
|
|
202
|
+
|
|
203
|
+
- **Throws** `SnapNotFoundError` if the snap does not exist.
|
|
204
|
+
- **Throws** `SnapUnauthorizedError` if the creator does not own the snap.
|
|
205
|
+
- **Throws** `SnapApiError` on other API errors.
|
|
206
|
+
|
|
207
|
+
#### `Snap` type
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
interface Snap {
|
|
211
|
+
id: string;
|
|
212
|
+
creator?: string; // Omitted in GET /api/snap/[id] response
|
|
213
|
+
title: string;
|
|
214
|
+
description?: string | null;
|
|
215
|
+
destination: string;
|
|
216
|
+
amount?: string | null;
|
|
217
|
+
assetCode: string;
|
|
218
|
+
assetIssuer?: string | null;
|
|
219
|
+
memo?: string | null;
|
|
220
|
+
memoType?: string;
|
|
221
|
+
network: string;
|
|
222
|
+
imageUrl?: string | null;
|
|
223
|
+
createdAt?: string;
|
|
224
|
+
updatedAt?: string;
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
102
228
|
### SEP-0007 URI Functions
|
|
103
229
|
|
|
104
230
|
#### `createPaymentSnap(options)`
|
|
@@ -260,6 +386,9 @@ All errors extend `StellarSnapError` with a `code` property for programmatic han
|
|
|
260
386
|
- `InvalidAmountError` - Invalid amount
|
|
261
387
|
- `InvalidAssetError` - Invalid asset configuration
|
|
262
388
|
- `InvalidUriError` - Invalid SEP-0007 URI
|
|
389
|
+
- `SnapNotFoundError` - Snap not found (404 from API)
|
|
390
|
+
- `SnapUnauthorizedError` - Not authorized to perform action (e.g. delete another user's snap)
|
|
391
|
+
- `SnapApiError` - Stellar Snaps API returned an error (includes optional `statusCode`)
|
|
263
392
|
|
|
264
393
|
```typescript
|
|
265
394
|
try {
|
|
@@ -269,6 +398,14 @@ try {
|
|
|
269
398
|
console.log(error.code); // 'INVALID_ADDRESS'
|
|
270
399
|
}
|
|
271
400
|
}
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
const snap = await getSnap('missing-id');
|
|
404
|
+
} catch (error) {
|
|
405
|
+
if (error instanceof SnapNotFoundError) {
|
|
406
|
+
console.log(error.code); // 'SNAP_NOT_FOUND'
|
|
407
|
+
}
|
|
408
|
+
}
|
|
272
409
|
```
|
|
273
410
|
|
|
274
411
|
### Constants
|