@lumenwipe/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 +102 -0
- package/dist/index.cjs +209 -0
- package/dist/index.d.cts +577 -0
- package/dist/index.d.ts +577 -0
- package/dist/index.js +178 -0
- package/package.json +59 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var LumenWipeApiError = class extends Error {
|
|
3
|
+
constructor(status, body) {
|
|
4
|
+
super(`LumenWipe API error ${status}`);
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.body = body;
|
|
7
|
+
this.name = "LumenWipeApiError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var LumenWipeTimeoutError = class extends Error {
|
|
11
|
+
constructor(timeoutMs) {
|
|
12
|
+
super(`LumenWipe API request timed out after ${timeoutMs}ms`);
|
|
13
|
+
this.timeoutMs = timeoutMs;
|
|
14
|
+
this.name = "LumenWipeTimeoutError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/http.ts
|
|
19
|
+
var HttpTransport = class {
|
|
20
|
+
constructor(baseUrl, apiKey, timeout, doFetch) {
|
|
21
|
+
this.baseUrl = baseUrl;
|
|
22
|
+
this.apiKey = apiKey;
|
|
23
|
+
this.timeout = timeout;
|
|
24
|
+
this.doFetch = doFetch;
|
|
25
|
+
}
|
|
26
|
+
async request(method, path, body) {
|
|
27
|
+
const headers = { Authorization: `Bearer ${this.apiKey}` };
|
|
28
|
+
if (body !== void 0) headers["Content-Type"] = "application/json";
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const bounded = Number.isFinite(this.timeout) && this.timeout > 0;
|
|
31
|
+
const timer = bounded ? setTimeout(() => controller.abort(), this.timeout) : void 0;
|
|
32
|
+
try {
|
|
33
|
+
const res = await this.doFetch(`${this.baseUrl}${path}`, {
|
|
34
|
+
method,
|
|
35
|
+
headers,
|
|
36
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0,
|
|
37
|
+
signal: controller.signal
|
|
38
|
+
});
|
|
39
|
+
const text = await res.text();
|
|
40
|
+
let parsed = void 0;
|
|
41
|
+
if (text) {
|
|
42
|
+
try {
|
|
43
|
+
parsed = JSON.parse(text);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
parsed = text;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (!res.ok) throw new LumenWipeApiError(res.status, parsed);
|
|
49
|
+
return parsed;
|
|
50
|
+
} catch (e) {
|
|
51
|
+
if (controller.signal.aborted) throw new LumenWipeTimeoutError(this.timeout);
|
|
52
|
+
throw e;
|
|
53
|
+
} finally {
|
|
54
|
+
if (timer) clearTimeout(timer);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/client.ts
|
|
60
|
+
var LumenWipeClient = class {
|
|
61
|
+
constructor(options) {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
const resolved = (_a = options.fetch) != null ? _a : globalThis.fetch;
|
|
64
|
+
if (!resolved) {
|
|
65
|
+
throw new Error("No fetch implementation available; pass one via options.fetch.");
|
|
66
|
+
}
|
|
67
|
+
this.http = new HttpTransport(
|
|
68
|
+
options.baseUrl.replace(/\/+$/, ""),
|
|
69
|
+
options.apiKey,
|
|
70
|
+
(_b = options.timeout) != null ? _b : 3e4,
|
|
71
|
+
resolved
|
|
72
|
+
);
|
|
73
|
+
this.defaultNetwork = (_c = options.network) != null ? _c : "testnet";
|
|
74
|
+
}
|
|
75
|
+
health() {
|
|
76
|
+
return this.http.request("GET", "/health");
|
|
77
|
+
}
|
|
78
|
+
getAccount(address, network = this.defaultNetwork) {
|
|
79
|
+
return this.http.request(
|
|
80
|
+
"GET",
|
|
81
|
+
`/${network}/account/${encodeURIComponent(address)}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
getPaths(params, network = this.defaultNetwork) {
|
|
85
|
+
const query = new URLSearchParams({ fromAsset: params.fromAsset, amount: params.amount });
|
|
86
|
+
return this.http.request("GET", `/${network}/paths?${query.toString()}`);
|
|
87
|
+
}
|
|
88
|
+
closePlan(body, network = this.defaultNetwork) {
|
|
89
|
+
return this.http.request("POST", `/v1/${network}/close/plan`, body);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Builds the next unsigned transaction(s) for a close. A close can span several
|
|
93
|
+
* transactions (a fused close, or separate claim / cleanup / mediator-merge steps),
|
|
94
|
+
* so the response's `remaining.requiresAnotherCall` says whether more follow: sign and
|
|
95
|
+
* submit the returned transactions in `order`, wait for confirmation, then call this
|
|
96
|
+
* again until `requiresAnotherCall` is false.
|
|
97
|
+
*/
|
|
98
|
+
closeTransactions(body, network = this.defaultNetwork) {
|
|
99
|
+
return this.http.request(
|
|
100
|
+
"POST",
|
|
101
|
+
`/v1/${network}/close/transactions`,
|
|
102
|
+
body
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
submit(signedXdr, network = this.defaultNetwork) {
|
|
106
|
+
return this.http.request("POST", `/v1/${network}/submit`, { signedXdr });
|
|
107
|
+
}
|
|
108
|
+
mediatorCheck(address, network = this.defaultNetwork) {
|
|
109
|
+
return this.http.request(
|
|
110
|
+
"GET",
|
|
111
|
+
`/${network}/mediator/check/${encodeURIComponent(address)}`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
mediatorSign(transaction, network = this.defaultNetwork) {
|
|
115
|
+
return this.http.request("POST", `/${network}/mediator/sign`, {
|
|
116
|
+
transaction
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/close-engine.ts
|
|
122
|
+
var InsufficientSignatureWeightError = class extends Error {
|
|
123
|
+
constructor(pending) {
|
|
124
|
+
super(
|
|
125
|
+
`Transaction ${pending.tx.id} needs signing weight ${pending.requiredWeight} but only has ${pending.accumulatedWeight}.`
|
|
126
|
+
);
|
|
127
|
+
this.pending = pending;
|
|
128
|
+
this.name = "InsufficientSignatureWeightError";
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
async function runClose(deps, resume) {
|
|
132
|
+
var _a, _b;
|
|
133
|
+
const maxRounds = (_a = deps.maxRounds) != null ? _a : 25;
|
|
134
|
+
if (resume) {
|
|
135
|
+
await signOrThrow(deps, resume.tx, resume.xdr, resume.queue, resume.requiresAnotherCall);
|
|
136
|
+
await processTxs(deps, resume.queue, resume.requiresAnotherCall);
|
|
137
|
+
if (!resume.requiresAnotherCall) return;
|
|
138
|
+
}
|
|
139
|
+
for (let round = 0; round < maxRounds; round++) {
|
|
140
|
+
(_b = deps.onProgress) == null ? void 0 : _b.call(deps, "Preparing transactions\u2026");
|
|
141
|
+
const batch = await deps.getTransactions();
|
|
142
|
+
const txs = [...batch.transactions].sort((a, b) => a.order - b.order);
|
|
143
|
+
await processTxs(deps, txs, batch.remaining.requiresAnotherCall);
|
|
144
|
+
if (!batch.remaining.requiresAnotherCall) return;
|
|
145
|
+
}
|
|
146
|
+
throw new Error("The close did not converge after the maximum number of rounds.");
|
|
147
|
+
}
|
|
148
|
+
async function processTxs(deps, txs, requiresAnotherCall) {
|
|
149
|
+
for (let i = 0; i < txs.length; i++) {
|
|
150
|
+
const tx = txs[i];
|
|
151
|
+
await deps.verify(tx);
|
|
152
|
+
await signOrThrow(deps, tx, tx.xdr, txs.slice(i + 1), requiresAnotherCall);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function signOrThrow(deps, tx, xdr, queue, requiresAnotherCall) {
|
|
156
|
+
var _a;
|
|
157
|
+
const required = deps.requiredWeight(tx);
|
|
158
|
+
const { xdr: signedXdr, weight } = await deps.sign(tx, xdr);
|
|
159
|
+
if (weight < required) {
|
|
160
|
+
throw new InsufficientSignatureWeightError({
|
|
161
|
+
tx,
|
|
162
|
+
xdr: signedXdr,
|
|
163
|
+
requiredWeight: required,
|
|
164
|
+
accumulatedWeight: weight,
|
|
165
|
+
queue,
|
|
166
|
+
requiresAnotherCall
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const hash = await deps.submit(tx, signedXdr);
|
|
170
|
+
(_a = deps.onConfirmed) == null ? void 0 : _a.call(deps, tx, hash);
|
|
171
|
+
}
|
|
172
|
+
export {
|
|
173
|
+
InsufficientSignatureWeightError,
|
|
174
|
+
LumenWipeApiError,
|
|
175
|
+
LumenWipeClient,
|
|
176
|
+
LumenWipeTimeoutError,
|
|
177
|
+
runClose
|
|
178
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lumenwipe/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Typed fetch client for the LumenWipe API - non-custodial Stellar account closing.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.cts",
|
|
18
|
+
"default": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"provenance": true
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/LumenWipe/lumenwipe.git",
|
|
33
|
+
"directory": "packages/sdk"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/LumenWipe/lumenwipe/tree/main/packages/sdk#readme",
|
|
36
|
+
"bugs": "https://github.com/LumenWipe/lumenwipe/issues",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"stellar",
|
|
39
|
+
"sdk",
|
|
40
|
+
"wallet",
|
|
41
|
+
"account-merge"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "bun run --filter '@lumenwipe/types' build && tsup src/index.ts --format esm,cjs --clean && tsc -p tsconfig.build.json && api-extractor run --local && cp dist/index.d.ts dist/index.d.cts",
|
|
45
|
+
"type-check": "tsc --noEmit && tsc -p tests/tsconfig.json --noEmit",
|
|
46
|
+
"lint": "eslint src",
|
|
47
|
+
"test": "bun test tests"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@microsoft/api-extractor": "^7.58.12",
|
|
51
|
+
"@types/bun": "^1.3.14",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
53
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
54
|
+
"eslint": "^8.57.1",
|
|
55
|
+
"tsup": "^8.3.0",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"@lumenwipe/types": "workspace:*"
|
|
58
|
+
}
|
|
59
|
+
}
|