@stellar-snaps/sdk 0.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.
- package/README.md +295 -0
- package/dist/index.d.mts +521 -0
- package/dist/index.d.ts +521 -0
- package/dist/index.js +505 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +461 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# @stellar-snaps/sdk
|
|
2
|
+
|
|
3
|
+
A complete SDK for building on Stellar Snaps technology. Create shareable payment links, build transactions, integrate wallets, and host your own snap endpoints.
|
|
4
|
+
|
|
5
|
+
## What is Stellar Snaps?
|
|
6
|
+
|
|
7
|
+
Stellar Snaps lets you create shareable payment links for the Stellar blockchain - like Venmo/PayPal request links, but for crypto. Share links on Twitter, Discord, or your website, and recipients can pay with one click using their Freighter wallet.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @stellar-snaps/sdk
|
|
13
|
+
# or
|
|
14
|
+
pnpm add @stellar-snaps/sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
### 1. Create a Payment Link
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createPaymentSnap } from '@stellar-snaps/sdk';
|
|
23
|
+
|
|
24
|
+
const { uri } = createPaymentSnap({
|
|
25
|
+
destination: 'GDQP2KPQGKIHYJGXNUIYOMHARUARCA7DJT5FO2FFOOUJ3DTJE4QRK764',
|
|
26
|
+
amount: '10',
|
|
27
|
+
assetCode: 'USDC',
|
|
28
|
+
assetIssuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN',
|
|
29
|
+
memo: 'Coffee payment',
|
|
30
|
+
network: 'public',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(uri);
|
|
34
|
+
// => web+stellar:pay?destination=GDQP2K...&amount=10&asset_code=USDC&...
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Parse a Payment Link
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { parseSnapUri } from '@stellar-snaps/sdk';
|
|
41
|
+
|
|
42
|
+
const parsed = parseSnapUri('web+stellar:pay?destination=GDQP2K...&amount=10');
|
|
43
|
+
|
|
44
|
+
console.log(parsed);
|
|
45
|
+
// => { type: 'pay', destination: 'GDQP2K...', amount: '10', ... }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 3. Build and Sign a Transaction
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import {
|
|
52
|
+
connectFreighter,
|
|
53
|
+
buildPaymentTransaction,
|
|
54
|
+
signWithFreighter,
|
|
55
|
+
submitTransaction,
|
|
56
|
+
} from '@stellar-snaps/sdk';
|
|
57
|
+
|
|
58
|
+
// Connect to user's wallet
|
|
59
|
+
const { publicKey, network } = await connectFreighter();
|
|
60
|
+
|
|
61
|
+
// Build the transaction
|
|
62
|
+
const xdr = buildPaymentTransaction({
|
|
63
|
+
source: publicKey,
|
|
64
|
+
sequence: '123456789', // Get from Horizon API
|
|
65
|
+
destination: 'GBZX...',
|
|
66
|
+
amount: '10',
|
|
67
|
+
asset: { code: 'XLM' },
|
|
68
|
+
network: 'public',
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Sign with Freighter (opens popup)
|
|
72
|
+
const signedXdr = await signWithFreighter(xdr, 'public');
|
|
73
|
+
|
|
74
|
+
// Submit to Stellar network
|
|
75
|
+
const result = await submitTransaction(signedXdr, 'public');
|
|
76
|
+
|
|
77
|
+
if (result.successful) {
|
|
78
|
+
console.log(`Success! Hash: ${result.hash}`);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 4. Host Your Own Snap Endpoints
|
|
83
|
+
|
|
84
|
+
Create a discovery file to enable the browser extension to find your snaps:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { createDiscoveryFile } from '@stellar-snaps/sdk';
|
|
88
|
+
|
|
89
|
+
const discovery = createDiscoveryFile({
|
|
90
|
+
name: 'My Payment App',
|
|
91
|
+
description: 'Accept Stellar payments',
|
|
92
|
+
rules: [
|
|
93
|
+
{ pathPattern: '/pay/*', apiPath: '/api/snap/$1' },
|
|
94
|
+
],
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Save as: public/.well-known/stellar-snap.json
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## API Reference
|
|
101
|
+
|
|
102
|
+
### SEP-0007 URI Functions
|
|
103
|
+
|
|
104
|
+
#### `createPaymentSnap(options)`
|
|
105
|
+
|
|
106
|
+
Creates a SEP-0007 payment URI.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
interface PaymentSnapOptions {
|
|
110
|
+
destination: string; // Recipient's Stellar address
|
|
111
|
+
amount?: string; // Amount to send
|
|
112
|
+
assetCode?: string; // Asset code (default: XLM)
|
|
113
|
+
assetIssuer?: string; // Asset issuer (required for non-XLM)
|
|
114
|
+
memo?: string; // Transaction memo
|
|
115
|
+
memoType?: MemoType; // Memo type (default: MEMO_TEXT)
|
|
116
|
+
message?: string; // Human-readable message (max 300 chars)
|
|
117
|
+
network?: 'public' | 'testnet';
|
|
118
|
+
callback?: string; // Callback URL after signing
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### `createTransactionSnap(options)`
|
|
123
|
+
|
|
124
|
+
Creates a SEP-0007 transaction URI for arbitrary transactions.
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
interface TransactionSnapOptions {
|
|
128
|
+
xdr: string; // Transaction XDR
|
|
129
|
+
network?: 'public' | 'testnet';
|
|
130
|
+
message?: string; // Human-readable message
|
|
131
|
+
callback?: string; // Callback URL
|
|
132
|
+
pubkey?: string; // Required signer's public key
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### `parseSnapUri(uri)`
|
|
137
|
+
|
|
138
|
+
Parses a SEP-0007 URI back into an object.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
const parsed = parseSnapUri('web+stellar:pay?destination=G...');
|
|
142
|
+
// => { type: 'pay', destination: 'G...', ... }
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Validation Functions
|
|
146
|
+
|
|
147
|
+
#### `isValidStellarAddress(address)`
|
|
148
|
+
|
|
149
|
+
Returns `true` if the address is a valid Stellar public key (56 chars, starts with 'G').
|
|
150
|
+
|
|
151
|
+
#### `isValidAssetCode(code)`
|
|
152
|
+
|
|
153
|
+
Returns `true` if the code is a valid asset code (1-12 alphanumeric chars).
|
|
154
|
+
|
|
155
|
+
#### `isValidAmount(amount)`
|
|
156
|
+
|
|
157
|
+
Returns `true` if the amount is a positive number string.
|
|
158
|
+
|
|
159
|
+
### Transaction Building
|
|
160
|
+
|
|
161
|
+
#### `buildPaymentTransaction(options)`
|
|
162
|
+
|
|
163
|
+
Builds a Stellar payment transaction and returns the XDR.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
interface BuildPaymentOptions {
|
|
167
|
+
source: string; // Payer's address
|
|
168
|
+
sequence: string; // Account sequence number
|
|
169
|
+
destination: string; // Recipient's address
|
|
170
|
+
amount: string; // Amount to send
|
|
171
|
+
asset?: {
|
|
172
|
+
code: string;
|
|
173
|
+
issuer?: string;
|
|
174
|
+
};
|
|
175
|
+
memo?: {
|
|
176
|
+
type: MemoType;
|
|
177
|
+
value: string;
|
|
178
|
+
};
|
|
179
|
+
network: 'public' | 'testnet';
|
|
180
|
+
timeout?: number; // Default: 180 seconds
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### `createAsset(code, issuer?)`
|
|
185
|
+
|
|
186
|
+
Creates a Stellar Asset object.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const xlm = createAsset('XLM');
|
|
190
|
+
const usdc = createAsset('USDC', 'GA5ZS...');
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Freighter Wallet Integration
|
|
194
|
+
|
|
195
|
+
#### `connectFreighter()`
|
|
196
|
+
|
|
197
|
+
Connects to Freighter wallet and returns the user's public key and network.
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
const { publicKey, network } = await connectFreighter();
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### `isFreighterConnected()`
|
|
204
|
+
|
|
205
|
+
Returns `true` if Freighter is installed and the user has granted access.
|
|
206
|
+
|
|
207
|
+
#### `getFreighterNetwork()`
|
|
208
|
+
|
|
209
|
+
Returns the current network from Freighter ('public' or 'testnet').
|
|
210
|
+
|
|
211
|
+
#### `signWithFreighter(xdr, network)`
|
|
212
|
+
|
|
213
|
+
Signs a transaction XDR using Freighter. Opens a popup for user approval.
|
|
214
|
+
|
|
215
|
+
#### `submitTransaction(signedXdr, network)`
|
|
216
|
+
|
|
217
|
+
Submits a signed transaction to the Stellar network.
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
const result = await submitTransaction(signedXdr, 'public');
|
|
221
|
+
// => { hash: 'abc...', successful: true, ledger: 12345 }
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Discovery Files
|
|
225
|
+
|
|
226
|
+
#### `createDiscoveryFile(options)`
|
|
227
|
+
|
|
228
|
+
Creates a discovery file object for hosting at `/.well-known/stellar-snap.json`.
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
interface CreateDiscoveryFileOptions {
|
|
232
|
+
name: string;
|
|
233
|
+
description?: string;
|
|
234
|
+
icon?: string;
|
|
235
|
+
rules: Array<{
|
|
236
|
+
pathPattern: string; // URL pattern, e.g., "/pay/*"
|
|
237
|
+
apiPath: string; // API path, e.g., "/api/snap/$1"
|
|
238
|
+
}>;
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### `validateDiscoveryFile(file)`
|
|
243
|
+
|
|
244
|
+
Type guard that validates if an object is a valid discovery file.
|
|
245
|
+
|
|
246
|
+
#### `matchUrlToRule(pathname, rules)`
|
|
247
|
+
|
|
248
|
+
Matches a URL path against rules and returns the API path.
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
matchUrlToRule('/pay/abc123', rules);
|
|
252
|
+
// => '/api/snap/abc123'
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Error Classes
|
|
256
|
+
|
|
257
|
+
All errors extend `StellarSnapError` with a `code` property for programmatic handling:
|
|
258
|
+
|
|
259
|
+
- `InvalidAddressError` - Invalid Stellar address
|
|
260
|
+
- `InvalidAmountError` - Invalid amount
|
|
261
|
+
- `InvalidAssetError` - Invalid asset configuration
|
|
262
|
+
- `InvalidUriError` - Invalid SEP-0007 URI
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
try {
|
|
266
|
+
createPaymentSnap({ destination: 'invalid' });
|
|
267
|
+
} catch (error) {
|
|
268
|
+
if (error instanceof InvalidAddressError) {
|
|
269
|
+
console.log(error.code); // 'INVALID_ADDRESS'
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Constants
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
import { NETWORK_PASSPHRASES, HORIZON_URLS } from '@stellar-snaps/sdk';
|
|
278
|
+
|
|
279
|
+
NETWORK_PASSPHRASES.public; // 'Public Global Stellar Network ; September 2015'
|
|
280
|
+
NETWORK_PASSPHRASES.testnet; // 'Test SDF Network ; September 2015'
|
|
281
|
+
|
|
282
|
+
HORIZON_URLS.public; // 'https://horizon.stellar.org'
|
|
283
|
+
HORIZON_URLS.testnet; // 'https://horizon-testnet.stellar.org'
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Types
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
type Network = 'public' | 'testnet';
|
|
290
|
+
type MemoType = 'MEMO_TEXT' | 'MEMO_ID' | 'MEMO_HASH' | 'MEMO_RETURN';
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
MIT
|