@stellar-snaps/sdk 0.3.0 → 0.3.2
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 +168 -1
- package/dist/index.d.mts +95 -52
- package/dist/index.d.ts +95 -52
- package/dist/index.js +165 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +161 -61
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -14
package/dist/index.js
CHANGED
|
@@ -23,8 +23,62 @@ function _interopNamespace(e) {
|
|
|
23
23
|
|
|
24
24
|
var StellarSdk__namespace = /*#__PURE__*/_interopNamespace(StellarSdk);
|
|
25
25
|
|
|
26
|
+
// src/errors.ts
|
|
27
|
+
var StellarSnapError = class extends Error {
|
|
28
|
+
constructor(message, code) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.name = "StellarSnapError";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var InvalidAddressError = class extends StellarSnapError {
|
|
35
|
+
constructor(address) {
|
|
36
|
+
super(`Invalid Stellar address: ${address}`, "INVALID_ADDRESS");
|
|
37
|
+
this.name = "InvalidAddressError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var InvalidAmountError = class extends StellarSnapError {
|
|
41
|
+
constructor(amount) {
|
|
42
|
+
super(`Invalid amount: ${amount}. Must be a positive number.`, "INVALID_AMOUNT");
|
|
43
|
+
this.name = "InvalidAmountError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var InvalidAssetError = class extends StellarSnapError {
|
|
47
|
+
constructor(message) {
|
|
48
|
+
super(message, "INVALID_ASSET");
|
|
49
|
+
this.name = "InvalidAssetError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var InvalidUriError = class extends StellarSnapError {
|
|
53
|
+
constructor(message) {
|
|
54
|
+
super(message, "INVALID_URI");
|
|
55
|
+
this.name = "InvalidUriError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var SnapNotFoundError = class extends StellarSnapError {
|
|
59
|
+
constructor(snapId) {
|
|
60
|
+
super(`Snap not found: ${snapId}`, "SNAP_NOT_FOUND");
|
|
61
|
+
this.name = "SnapNotFoundError";
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var SnapUnauthorizedError = class extends StellarSnapError {
|
|
65
|
+
constructor(message = "Unauthorized") {
|
|
66
|
+
super(message, "SNAP_UNAUTHORIZED");
|
|
67
|
+
this.name = "SnapUnauthorizedError";
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var SnapApiError = class extends StellarSnapError {
|
|
71
|
+
constructor(message, statusCode) {
|
|
72
|
+
super(message, "SNAP_API_ERROR");
|
|
73
|
+
this.statusCode = statusCode;
|
|
74
|
+
this.name = "SnapApiError";
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
26
78
|
// src/create-snap.ts
|
|
79
|
+
var DEFAULT_BASE_URL = "https://stellar-snaps.vercel.app";
|
|
27
80
|
async function createSnap(options) {
|
|
81
|
+
const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
28
82
|
const {
|
|
29
83
|
creator,
|
|
30
84
|
title,
|
|
@@ -36,20 +90,19 @@ async function createSnap(options) {
|
|
|
36
90
|
memo,
|
|
37
91
|
memoType = "MEMO_TEXT",
|
|
38
92
|
network = "testnet",
|
|
39
|
-
imageUrl
|
|
40
|
-
serverUrl = "https://stellarsnaps.com"
|
|
93
|
+
imageUrl
|
|
41
94
|
} = options;
|
|
42
95
|
if (!creator || creator.length !== 56 || !creator.startsWith("G")) {
|
|
43
|
-
throw new
|
|
96
|
+
throw new InvalidAddressError(creator || "");
|
|
44
97
|
}
|
|
45
98
|
if (!title || title.trim().length === 0) {
|
|
46
|
-
throw new
|
|
99
|
+
throw new SnapApiError("Title is required");
|
|
47
100
|
}
|
|
48
101
|
if (!destination || destination.length !== 56 || !destination.startsWith("G")) {
|
|
49
|
-
throw new
|
|
102
|
+
throw new InvalidAddressError(destination || "");
|
|
50
103
|
}
|
|
51
104
|
if (assetCode !== "XLM" && !assetIssuer) {
|
|
52
|
-
throw new
|
|
105
|
+
throw new SnapApiError("Asset issuer is required for non-XLM assets");
|
|
53
106
|
}
|
|
54
107
|
const body = {
|
|
55
108
|
creator,
|
|
@@ -64,53 +117,133 @@ async function createSnap(options) {
|
|
|
64
117
|
network,
|
|
65
118
|
imageUrl
|
|
66
119
|
};
|
|
67
|
-
const response = await fetch(`${
|
|
120
|
+
const response = await fetch(`${baseUrl}/api/snaps`, {
|
|
68
121
|
method: "POST",
|
|
69
|
-
headers: {
|
|
70
|
-
"Content-Type": "application/json"
|
|
71
|
-
},
|
|
122
|
+
headers: { "Content-Type": "application/json" },
|
|
72
123
|
body: JSON.stringify(body)
|
|
73
124
|
});
|
|
74
125
|
if (!response.ok) {
|
|
75
|
-
const
|
|
76
|
-
|
|
126
|
+
const data = await response.json().catch(() => ({ error: "Unknown error" }));
|
|
127
|
+
const message = data.error || `Failed to create snap: ${response.status}`;
|
|
128
|
+
throw new SnapApiError(message, response.status);
|
|
77
129
|
}
|
|
78
130
|
const snap = await response.json();
|
|
79
131
|
return {
|
|
80
132
|
id: snap.id,
|
|
81
|
-
url: `${
|
|
133
|
+
url: `${baseUrl}/s/${snap.id}`,
|
|
82
134
|
snap
|
|
83
135
|
};
|
|
84
136
|
}
|
|
85
137
|
async function getSnap(id, options = {}) {
|
|
86
|
-
const
|
|
87
|
-
const response = await fetch(`${
|
|
138
|
+
const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
139
|
+
const response = await fetch(`${baseUrl}/api/snap/${id}`);
|
|
88
140
|
if (response.status === 404) {
|
|
89
|
-
|
|
141
|
+
throw new SnapNotFoundError(id);
|
|
90
142
|
}
|
|
91
143
|
if (!response.ok) {
|
|
92
|
-
|
|
144
|
+
const data = await response.json().catch(() => ({}));
|
|
145
|
+
const message = data.error || `Failed to fetch snap: ${response.status}`;
|
|
146
|
+
throw new SnapApiError(message, response.status);
|
|
93
147
|
}
|
|
94
148
|
return response.json();
|
|
95
149
|
}
|
|
96
150
|
async function listSnaps(creator, options = {}) {
|
|
97
|
-
const
|
|
98
|
-
const response = await fetch(
|
|
151
|
+
const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
152
|
+
const response = await fetch(
|
|
153
|
+
`${baseUrl}/api/snaps?creator=${encodeURIComponent(creator)}`
|
|
154
|
+
);
|
|
99
155
|
if (!response.ok) {
|
|
100
|
-
|
|
156
|
+
const data = await response.json().catch(() => ({}));
|
|
157
|
+
const message = data.error || `Failed to list snaps: ${response.status}`;
|
|
158
|
+
throw new SnapApiError(message, response.status);
|
|
101
159
|
}
|
|
102
160
|
return response.json();
|
|
103
161
|
}
|
|
104
|
-
async function deleteSnap(id, options = {}) {
|
|
105
|
-
const
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
162
|
+
async function deleteSnap(id, creator, options = {}) {
|
|
163
|
+
const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
164
|
+
const url = `${baseUrl}/api/snaps?id=${encodeURIComponent(id)}&creator=${encodeURIComponent(creator)}`;
|
|
165
|
+
const response = await fetch(url, { method: "DELETE" });
|
|
166
|
+
if (response.status === 404) {
|
|
167
|
+
throw new SnapNotFoundError(id);
|
|
168
|
+
}
|
|
169
|
+
if (response.status === 403) {
|
|
170
|
+
throw new SnapUnauthorizedError("You do not have permission to delete this snap");
|
|
171
|
+
}
|
|
172
|
+
if (!response.ok) {
|
|
173
|
+
const data = await response.json().catch(() => ({}));
|
|
174
|
+
const message = data.error || `Failed to delete snap: ${response.status}`;
|
|
175
|
+
throw new SnapApiError(message, response.status);
|
|
111
176
|
}
|
|
112
177
|
}
|
|
113
178
|
|
|
179
|
+
// src/snap-modal.ts
|
|
180
|
+
var MODAL_ID = "stellar-snap-modal";
|
|
181
|
+
var IFRAME_ID = "stellar-snap-modal-iframe";
|
|
182
|
+
function isBrowser() {
|
|
183
|
+
return typeof document !== "undefined" && typeof window !== "undefined";
|
|
184
|
+
}
|
|
185
|
+
function openSnapModal(snapUrl, options = {}) {
|
|
186
|
+
if (!isBrowser()) return;
|
|
187
|
+
closeSnapModal();
|
|
188
|
+
const { width = 420, height = 560, onClose } = options;
|
|
189
|
+
const overlay = document.createElement("div");
|
|
190
|
+
overlay.id = MODAL_ID;
|
|
191
|
+
overlay.setAttribute("role", "dialog");
|
|
192
|
+
overlay.setAttribute("aria-modal", "true");
|
|
193
|
+
overlay.setAttribute("aria-label", "Stellar Snap payment");
|
|
194
|
+
const styles = {
|
|
195
|
+
position: "fixed",
|
|
196
|
+
inset: "0",
|
|
197
|
+
zIndex: "2147483647",
|
|
198
|
+
display: "flex",
|
|
199
|
+
alignItems: "center",
|
|
200
|
+
justifyContent: "center",
|
|
201
|
+
backgroundColor: "rgba(0, 0, 0, 0.6)",
|
|
202
|
+
padding: "20px",
|
|
203
|
+
boxSizing: "border-box"
|
|
204
|
+
};
|
|
205
|
+
Object.assign(overlay.style, styles);
|
|
206
|
+
const container = document.createElement("div");
|
|
207
|
+
container.style.position = "relative";
|
|
208
|
+
container.style.width = `${width}px`;
|
|
209
|
+
container.style.maxWidth = "100%";
|
|
210
|
+
container.style.height = `${height}px`;
|
|
211
|
+
container.style.maxHeight = "90vh";
|
|
212
|
+
container.style.backgroundColor = "rgb(23, 23, 23)";
|
|
213
|
+
container.style.borderRadius = "16px";
|
|
214
|
+
container.style.boxShadow = "0 25px 50px -12px rgba(0, 0, 0, 0.5)";
|
|
215
|
+
container.style.overflow = "hidden";
|
|
216
|
+
const closeBtn = document.createElement("button");
|
|
217
|
+
closeBtn.type = "button";
|
|
218
|
+
closeBtn.setAttribute("aria-label", "Close");
|
|
219
|
+
closeBtn.textContent = "\xD7";
|
|
220
|
+
closeBtn.style.cssText = "position:absolute;top:12px;right:12px;z-index:10;width:32px;height:32px;border:none;border-radius:8px;background:rgba(255,255,255,0.1);color:#e5e5e5;font-size:24px;line-height:1;cursor:pointer;display:flex;align-items:center;justify-content:center;";
|
|
221
|
+
closeBtn.onclick = () => {
|
|
222
|
+
closeSnapModal();
|
|
223
|
+
onClose?.();
|
|
224
|
+
};
|
|
225
|
+
const iframe = document.createElement("iframe");
|
|
226
|
+
iframe.id = IFRAME_ID;
|
|
227
|
+
iframe.src = snapUrl;
|
|
228
|
+
iframe.title = "Stellar Snap payment";
|
|
229
|
+
iframe.style.cssText = "width:100%;height:100%;border:none;border-radius:16px;";
|
|
230
|
+
container.appendChild(closeBtn);
|
|
231
|
+
container.appendChild(iframe);
|
|
232
|
+
overlay.appendChild(container);
|
|
233
|
+
overlay.addEventListener("click", (e) => {
|
|
234
|
+
if (e.target === overlay) {
|
|
235
|
+
closeSnapModal();
|
|
236
|
+
onClose?.();
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
document.body.appendChild(overlay);
|
|
240
|
+
}
|
|
241
|
+
function closeSnapModal() {
|
|
242
|
+
if (!isBrowser()) return;
|
|
243
|
+
const el = document.getElementById(MODAL_ID);
|
|
244
|
+
if (el) el.remove();
|
|
245
|
+
}
|
|
246
|
+
|
|
114
247
|
// src/snap-id.ts
|
|
115
248
|
var ALPHABET = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
116
249
|
function generateSnapId(length = 8) {
|
|
@@ -317,39 +450,6 @@ var HORIZON_URLS = {
|
|
|
317
450
|
testnet: "https://horizon-testnet.stellar.org"
|
|
318
451
|
};
|
|
319
452
|
|
|
320
|
-
// src/errors.ts
|
|
321
|
-
var StellarSnapError = class extends Error {
|
|
322
|
-
constructor(message, code) {
|
|
323
|
-
super(message);
|
|
324
|
-
this.code = code;
|
|
325
|
-
this.name = "StellarSnapError";
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
var InvalidAddressError = class extends StellarSnapError {
|
|
329
|
-
constructor(address) {
|
|
330
|
-
super(`Invalid Stellar address: ${address}`, "INVALID_ADDRESS");
|
|
331
|
-
this.name = "InvalidAddressError";
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
var InvalidAmountError = class extends StellarSnapError {
|
|
335
|
-
constructor(amount) {
|
|
336
|
-
super(`Invalid amount: ${amount}. Must be a positive number.`, "INVALID_AMOUNT");
|
|
337
|
-
this.name = "InvalidAmountError";
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
var InvalidAssetError = class extends StellarSnapError {
|
|
341
|
-
constructor(message) {
|
|
342
|
-
super(message, "INVALID_ASSET");
|
|
343
|
-
this.name = "InvalidAssetError";
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
var InvalidUriError = class extends StellarSnapError {
|
|
347
|
-
constructor(message) {
|
|
348
|
-
super(message, "INVALID_URI");
|
|
349
|
-
this.name = "InvalidUriError";
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
|
|
353
453
|
// src/create-transaction-snap.ts
|
|
354
454
|
function createTransactionSnap(options) {
|
|
355
455
|
const { xdr, network = "public", message, callback, pubkey } = options;
|
|
@@ -1166,10 +1266,14 @@ exports.NETWORK_PASSPHRASES = NETWORK_PASSPHRASES2;
|
|
|
1166
1266
|
exports.POSTGRES_SCHEMA = POSTGRES_SCHEMA;
|
|
1167
1267
|
exports.SHORTENER_DOMAINS = SHORTENER_DOMAINS;
|
|
1168
1268
|
exports.SQLITE_SCHEMA = SQLITE_SCHEMA;
|
|
1269
|
+
exports.SnapApiError = SnapApiError;
|
|
1270
|
+
exports.SnapNotFoundError = SnapNotFoundError;
|
|
1271
|
+
exports.SnapUnauthorizedError = SnapUnauthorizedError;
|
|
1169
1272
|
exports.StellarSnapError = StellarSnapError;
|
|
1170
1273
|
exports.addDomain = addDomain;
|
|
1171
1274
|
exports.buildPaymentTransaction = buildPaymentTransaction;
|
|
1172
1275
|
exports.buildUrl = buildUrl;
|
|
1276
|
+
exports.closeSnapModal = closeSnapModal;
|
|
1173
1277
|
exports.connectFreighter = connectFreighter;
|
|
1174
1278
|
exports.createAsset = createAsset;
|
|
1175
1279
|
exports.createDiscoveryFile = createDiscoveryFile;
|
|
@@ -1207,6 +1311,7 @@ exports.isValidStellarAddress = isValidStellarAddress;
|
|
|
1207
1311
|
exports.listSnaps = listSnaps;
|
|
1208
1312
|
exports.matchUrlToRule = matchUrlToRule;
|
|
1209
1313
|
exports.metaTagsToHtml = metaTagsToHtml;
|
|
1314
|
+
exports.openSnapModal = openSnapModal;
|
|
1210
1315
|
exports.parseAddress = parseAddress;
|
|
1211
1316
|
exports.parseQueryParams = parseQueryParams;
|
|
1212
1317
|
exports.parseSnapUri = parseSnapUri;
|