blossom-client-sdk 3.0.1 → 4.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/lib/actions/delete.d.ts +4 -5
- package/lib/actions/delete.js +18 -6
- package/lib/actions/download.d.ts +4 -5
- package/lib/actions/download.js +18 -5
- package/lib/actions/list.d.ts +4 -5
- package/lib/actions/list.js +22 -5
- package/lib/actions/media.js +18 -5
- package/lib/actions/mirror.d.ts +4 -5
- package/lib/actions/mirror.js +19 -6
- package/lib/actions/multi-server.js +15 -4
- package/lib/actions/upload.d.ts +4 -5
- package/lib/actions/upload.js +19 -6
- package/lib/auth.d.ts +4 -1
- package/lib/auth.js +18 -8
- package/lib/helpers/blob.d.ts +2 -0
- package/lib/helpers/blob.js +8 -5
- package/lib/helpers/cashu.js +14 -0
- package/lib/helpers/index.d.ts +1 -1
- package/lib/helpers/index.js +1 -1
- package/lib/helpers/signal.js +1 -1
- package/lib/image.js +25 -11
- package/lib/types.d.ts +20 -4
- package/package.json +18 -17
- package/src/actions/delete.ts +96 -0
- package/src/actions/download.ts +95 -0
- package/src/actions/index.ts +7 -0
- package/src/actions/list.ts +95 -0
- package/src/actions/media.ts +113 -0
- package/src/actions/mirror.ts +110 -0
- package/src/actions/multi-server.ts +218 -0
- package/src/actions/upload.ts +140 -0
- package/src/auth.ts +145 -0
- package/src/client.ts +221 -0
- package/src/const.ts +1 -0
- package/src/error.ts +23 -0
- package/src/helpers/blob.ts +62 -0
- package/src/helpers/cashu.ts +21 -0
- package/src/helpers/fetch.ts +16 -0
- package/src/helpers/index.ts +5 -0
- package/src/helpers/signal.ts +20 -0
- package/src/helpers/url.ts +16 -0
- package/src/image.ts +122 -0
- package/src/index.ts +9 -0
- package/src/nostr.ts +17 -0
- package/src/types.ts +63 -0
- package/lib/helpers/cachu.js +0 -6
- /package/lib/helpers/{cachu.d.ts → cashu.d.ts} +0 -0
package/lib/actions/delete.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type DeleteOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
7
|
-
/** Override
|
|
8
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
9
8
|
/** Request timeout */
|
|
10
9
|
timeout?: number;
|
|
11
10
|
/**
|
|
@@ -14,7 +13,7 @@ export type DeleteOptions<S extends ServerType> = {
|
|
|
14
13
|
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
15
14
|
* @param request the payment request
|
|
16
15
|
*/
|
|
17
|
-
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<
|
|
16
|
+
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
18
17
|
/**
|
|
19
18
|
* A method used to request a signed auth event for a server and sha256
|
|
20
19
|
* @param server the server requesting the auth
|
package/lib/actions/delete.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import HTTPError from "../error.js";
|
|
2
1
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import
|
|
2
|
+
import HTTPError from "../error.js";
|
|
4
3
|
import { fetchWithTimeout } from "../helpers/fetch.js";
|
|
5
4
|
/** Deletes a blob to a server */
|
|
6
5
|
export async function deleteBlob(server, hash, opts) {
|
|
7
6
|
const url = new URL("/" + hash, server);
|
|
8
7
|
const headers = {};
|
|
9
|
-
// attach the
|
|
10
|
-
if (opts?.auth)
|
|
11
|
-
|
|
8
|
+
// attach the authorization if its already set
|
|
9
|
+
if (opts?.auth) {
|
|
10
|
+
if (typeof opts.auth === "boolean") {
|
|
11
|
+
if (!opts.onAuth)
|
|
12
|
+
throw new Error("Missing onAuth handler");
|
|
13
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, hash));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
12
19
|
let res = await fetchWithTimeout(url, {
|
|
13
20
|
method: "DELETE",
|
|
14
21
|
headers,
|
|
@@ -18,7 +25,11 @@ export async function deleteBlob(server, hash, opts) {
|
|
|
18
25
|
// handle auth and payment
|
|
19
26
|
switch (res.status) {
|
|
20
27
|
case 401: {
|
|
21
|
-
|
|
28
|
+
// throw an error if authorization is required but disabled
|
|
29
|
+
if (opts?.auth === false)
|
|
30
|
+
throw new Error("Authorization disabled");
|
|
31
|
+
// request authorization for this request
|
|
32
|
+
const auth = await opts?.onAuth?.(server, hash);
|
|
22
33
|
if (!auth)
|
|
23
34
|
throw new Error("Missing auth handler");
|
|
24
35
|
// Try delete with auth
|
|
@@ -34,6 +45,7 @@ export async function deleteBlob(server, hash, opts) {
|
|
|
34
45
|
if (!opts?.onPayment)
|
|
35
46
|
throw new Error("Missing payment handler");
|
|
36
47
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
48
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
37
49
|
const request = getPaymentRequestFromHeaders(res.headers);
|
|
38
50
|
const token = await opts.onPayment(server, hash, request);
|
|
39
51
|
const payment = getEncodedToken(token);
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type DownloadOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
7
|
-
/** Override
|
|
8
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
9
8
|
/** Request timeout */
|
|
10
9
|
timeout?: number;
|
|
11
10
|
/**
|
|
@@ -14,7 +13,7 @@ export type DownloadOptions<S extends ServerType> = {
|
|
|
14
13
|
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
15
14
|
* @param request the payment request
|
|
16
15
|
*/
|
|
17
|
-
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<
|
|
16
|
+
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
18
17
|
/**
|
|
19
18
|
* A method used to request a signed auth event for a server and sha256
|
|
20
19
|
* @param server the server requesting the auth
|
package/lib/actions/download.js
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import HTTPError from "../error.js";
|
|
2
2
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import { fetchWithTimeout
|
|
3
|
+
import { fetchWithTimeout } from "../helpers/index.js";
|
|
4
4
|
/** Downloads a blob from a server and returns the Response */
|
|
5
5
|
export async function downloadBlob(server, hash, opts) {
|
|
6
6
|
const url = new URL("/" + hash, server);
|
|
7
7
|
const headers = {};
|
|
8
|
-
// attach the
|
|
9
|
-
if (opts?.auth)
|
|
10
|
-
|
|
8
|
+
// attach the authorization if its already set
|
|
9
|
+
if (opts?.auth) {
|
|
10
|
+
if (typeof opts.auth === "boolean") {
|
|
11
|
+
if (!opts.onAuth)
|
|
12
|
+
throw new Error("Missing onAuth handler");
|
|
13
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, hash));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
11
19
|
let download = await fetchWithTimeout(url, {
|
|
12
20
|
headers,
|
|
13
21
|
signal: opts?.signal,
|
|
@@ -16,7 +24,11 @@ export async function downloadBlob(server, hash, opts) {
|
|
|
16
24
|
// handle auth and payment
|
|
17
25
|
switch (download.status) {
|
|
18
26
|
case 401: {
|
|
19
|
-
|
|
27
|
+
// throw an error if auth is requested and disabled
|
|
28
|
+
if (opts?.auth === false)
|
|
29
|
+
throw new Error("Authorization disabled");
|
|
30
|
+
// Request authorization for this request
|
|
31
|
+
const auth = await opts?.onAuth?.(server, hash);
|
|
20
32
|
if (!auth)
|
|
21
33
|
throw new Error("Missing auth handler");
|
|
22
34
|
// Try download with auth
|
|
@@ -31,6 +43,7 @@ export async function downloadBlob(server, hash, opts) {
|
|
|
31
43
|
if (!opts?.onPayment)
|
|
32
44
|
throw new Error("Missing payment handler");
|
|
33
45
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
46
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
34
47
|
const request = getPaymentRequestFromHeaders(download.headers);
|
|
35
48
|
const token = await opts.onPayment(server, hash, request);
|
|
36
49
|
const payment = getEncodedToken(token);
|
package/lib/actions/list.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { BlobDescriptor, PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type ListOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
7
|
-
/** Override
|
|
8
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
9
8
|
/** Request timeout */
|
|
10
9
|
timeout?: number;
|
|
11
10
|
since?: number;
|
|
@@ -15,7 +14,7 @@ export type ListOptions<S extends ServerType> = {
|
|
|
15
14
|
* @param server the server requiring payment
|
|
16
15
|
* @param request the payment request
|
|
17
16
|
*/
|
|
18
|
-
onPayment?: (server: S, request: PaymentRequest) => Promise<
|
|
17
|
+
onPayment?: (server: S, request: PaymentRequest) => Promise<PaymentToken>;
|
|
19
18
|
/**
|
|
20
19
|
* A method used to request a signed auth event for a server
|
|
21
20
|
* @param server the server requesting the auth
|
package/lib/actions/list.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import HTTPError from "../error.js";
|
|
2
2
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import { fetchWithTimeout
|
|
3
|
+
import { fetchWithTimeout } from "../helpers/index.js";
|
|
4
4
|
/** Mirrors a blob to a server */
|
|
5
5
|
export async function listBlobs(server, pubkey, opts) {
|
|
6
6
|
const url = new URL(`/list/` + pubkey, server);
|
|
@@ -8,16 +8,32 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
8
8
|
url.searchParams.append("since", String(opts.since));
|
|
9
9
|
if (opts?.until)
|
|
10
10
|
url.searchParams.append("until", String(opts.until));
|
|
11
|
-
|
|
11
|
+
// attach the auth if its already set
|
|
12
|
+
const headers = {};
|
|
13
|
+
// attach the authorization if its already set
|
|
14
|
+
if (opts?.auth) {
|
|
15
|
+
if (typeof opts.auth === "boolean") {
|
|
16
|
+
if (!opts.onAuth)
|
|
17
|
+
throw new Error("Missing onAuth handler");
|
|
18
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let list = await fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
12
25
|
// handle auth and payments
|
|
13
26
|
switch (list.status) {
|
|
14
27
|
case 401: {
|
|
15
|
-
|
|
28
|
+
// throw an error if auth is requested and disabled
|
|
29
|
+
if (opts?.auth === false)
|
|
30
|
+
throw new Error("Authorization disabled");
|
|
31
|
+
const auth = await opts?.onAuth?.(server);
|
|
16
32
|
if (!auth)
|
|
17
33
|
throw new Error("Missing auth handler");
|
|
18
34
|
// Try list with auth
|
|
19
35
|
list = await fetchWithTimeout(url, {
|
|
20
|
-
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
36
|
+
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
21
37
|
signal: opts?.signal,
|
|
22
38
|
timeout: opts?.timeout,
|
|
23
39
|
});
|
|
@@ -27,12 +43,13 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
27
43
|
if (!opts?.onPayment)
|
|
28
44
|
throw new Error("Missing payment handler");
|
|
29
45
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
46
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
30
47
|
const request = getPaymentRequestFromHeaders(list.headers);
|
|
31
48
|
const token = await opts.onPayment(server, request);
|
|
32
49
|
const payment = getEncodedToken(token);
|
|
33
50
|
// Try list with payment
|
|
34
51
|
list = await fetchWithTimeout(url, {
|
|
35
|
-
headers: { "X-Cashu": payment },
|
|
52
|
+
headers: { ...headers, "X-Cashu": payment },
|
|
36
53
|
signal: opts?.signal,
|
|
37
54
|
timeout: opts?.timeout,
|
|
38
55
|
});
|
package/lib/actions/media.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getBlobSha256, getBlobSize, getBlobType
|
|
1
|
+
import { getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
|
|
2
2
|
import HTTPError from "../error.js";
|
|
3
3
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
4
4
|
/** Error thrown when /media endpoint is not present on a server */
|
|
@@ -11,9 +11,17 @@ export async function uploadMedia(server, blob, opts) {
|
|
|
11
11
|
const headers = {
|
|
12
12
|
"X-SHA-256": sha256,
|
|
13
13
|
};
|
|
14
|
-
// attach the
|
|
15
|
-
if (opts?.auth)
|
|
16
|
-
|
|
14
|
+
// attach the authorization if its already set
|
|
15
|
+
if (opts?.auth) {
|
|
16
|
+
if (typeof opts.auth === "boolean") {
|
|
17
|
+
if (!opts.onAuth)
|
|
18
|
+
throw new Error("Missing onAuth handler");
|
|
19
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, sha256, "media", blob));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
17
25
|
// build check headers
|
|
18
26
|
const checkHeaders = {
|
|
19
27
|
...headers,
|
|
@@ -36,7 +44,11 @@ export async function uploadMedia(server, blob, opts) {
|
|
|
36
44
|
// handle auth and payment
|
|
37
45
|
switch (firstTry.status) {
|
|
38
46
|
case 401: {
|
|
39
|
-
|
|
47
|
+
// throw an error if auth is requested and disabled
|
|
48
|
+
if (opts?.auth === false)
|
|
49
|
+
throw new Error("Authorization disabled");
|
|
50
|
+
// Request authorization event for this upload
|
|
51
|
+
const auth = await opts?.onAuth?.(server, sha256, "media", blob);
|
|
40
52
|
if (!auth)
|
|
41
53
|
throw new Error("Missing auth handler");
|
|
42
54
|
// Try upload with auth
|
|
@@ -52,6 +64,7 @@ export async function uploadMedia(server, blob, opts) {
|
|
|
52
64
|
if (!opts?.onPayment)
|
|
53
65
|
throw new Error("Missing payment handler");
|
|
54
66
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
67
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
55
68
|
const request = getPaymentRequestFromHeaders(firstTry.headers);
|
|
56
69
|
const token = await opts.onPayment(server, sha256, blob, request);
|
|
57
70
|
const payment = getEncodedToken(token);
|
package/lib/actions/mirror.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { BlobDescriptor,
|
|
2
|
+
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type MirrorOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
7
|
-
/** Override
|
|
8
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
9
8
|
/** Request timeout */
|
|
10
9
|
timeout?: number;
|
|
11
10
|
/**
|
|
@@ -15,7 +14,7 @@ export type MirrorOptions<S extends ServerType> = {
|
|
|
15
14
|
* @param blob the original blob
|
|
16
15
|
* @param request the payment request
|
|
17
16
|
*/
|
|
18
|
-
onPayment?: (server: S, sha256: string, blob: BlobDescriptor, request: PaymentRequest) => Promise<
|
|
17
|
+
onPayment?: (server: S, sha256: string, blob: BlobDescriptor, request: PaymentRequest) => Promise<PaymentToken>;
|
|
19
18
|
/**
|
|
20
19
|
* A method used to request a signed auth event for a server and sha256
|
|
21
20
|
* @param server the server requesting the auth
|
package/lib/actions/mirror.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import HTTPError from "../error.js";
|
|
2
1
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import
|
|
2
|
+
import HTTPError from "../error.js";
|
|
3
|
+
import { fetchWithTimeout } from "../helpers/index.js";
|
|
4
4
|
/** Mirrors a blob to a server */
|
|
5
5
|
export async function mirrorBlob(server, blob, opts) {
|
|
6
6
|
const url = new URL("/mirror", server);
|
|
@@ -11,9 +11,17 @@ export async function mirrorBlob(server, blob, opts) {
|
|
|
11
11
|
};
|
|
12
12
|
if (blob.type)
|
|
13
13
|
headers["X-Content-Type"] = blob.type;
|
|
14
|
-
// attach the
|
|
15
|
-
if (opts?.auth)
|
|
16
|
-
|
|
14
|
+
// attach the authorization if its already set
|
|
15
|
+
if (opts?.auth) {
|
|
16
|
+
if (typeof opts.auth === "boolean") {
|
|
17
|
+
if (!opts.onAuth)
|
|
18
|
+
throw new Error("Missing onAuth handler");
|
|
19
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, blob.sha256, blob));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
17
25
|
const body = JSON.stringify({ url: blob.url });
|
|
18
26
|
let mirror = await fetchWithTimeout(url, {
|
|
19
27
|
method: "PUT",
|
|
@@ -24,7 +32,11 @@ export async function mirrorBlob(server, blob, opts) {
|
|
|
24
32
|
});
|
|
25
33
|
switch (mirror.status) {
|
|
26
34
|
case 401: {
|
|
27
|
-
|
|
35
|
+
// throw an error if auth is requested and disabled
|
|
36
|
+
if (opts?.auth === false)
|
|
37
|
+
throw new Error("Authorization disabled");
|
|
38
|
+
// Request authorization event for this mirror
|
|
39
|
+
const auth = await opts?.onAuth?.(server, blob.sha256, blob);
|
|
28
40
|
if (!auth)
|
|
29
41
|
throw new Error("Missing auth handler");
|
|
30
42
|
// Try mirror with auth
|
|
@@ -41,6 +53,7 @@ export async function mirrorBlob(server, blob, opts) {
|
|
|
41
53
|
if (!opts?.onPayment)
|
|
42
54
|
throw new Error("Missing payment handler");
|
|
43
55
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
56
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
44
57
|
const request = getPaymentRequestFromHeaders(mirror.headers);
|
|
45
58
|
const token = await opts.onPayment(server, blob.sha256, blob, request);
|
|
46
59
|
const payment = getEncodedToken(token);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { doseAuthMatchBlob } from "../auth.js";
|
|
2
2
|
import { getBlobSha256 } from "../helpers/index.js";
|
|
3
3
|
import { MediaEndpointMissingError, uploadMedia } from "./media.js";
|
|
4
4
|
import { mirrorBlob } from "./mirror.js";
|
|
@@ -41,11 +41,11 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
41
41
|
let initialUpload;
|
|
42
42
|
const results = new Map();
|
|
43
43
|
// reuse auth events
|
|
44
|
-
let authEvents = options.auth ? [options.auth] : [];
|
|
44
|
+
let authEvents = typeof options.auth === "object" ? [options.auth] : [];
|
|
45
45
|
const handleAuthRequest = async (server, sha256, type) => {
|
|
46
46
|
// check if any existing auth events match
|
|
47
47
|
for (const auth of authEvents) {
|
|
48
|
-
if (await
|
|
48
|
+
if (await doseAuthMatchBlob(auth, server, sha256, type))
|
|
49
49
|
return auth;
|
|
50
50
|
}
|
|
51
51
|
// create a new auth event
|
|
@@ -74,6 +74,10 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
74
74
|
options.onStart?.(server, sha256, blob);
|
|
75
75
|
// attempt to upload media
|
|
76
76
|
initialUpload = await uploadMedia(server, blob, {
|
|
77
|
+
// pass through abort signal
|
|
78
|
+
signal: options.signal,
|
|
79
|
+
// pass through authorization flag
|
|
80
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
77
81
|
onAuth: handleAuthRequest,
|
|
78
82
|
onPayment: handlePaymentRequest,
|
|
79
83
|
});
|
|
@@ -113,7 +117,10 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
113
117
|
try {
|
|
114
118
|
options.onStart?.(server, initialUpload.sha256, blob);
|
|
115
119
|
metadata = await mirrorBlob(server, initialUpload, {
|
|
120
|
+
// pass through abort signal
|
|
116
121
|
signal: options.signal,
|
|
122
|
+
// pass through authorization flag
|
|
123
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
117
124
|
onAuth: (server, sha256) => handleAuthRequest(server, sha256, "upload"),
|
|
118
125
|
onPayment: handlePaymentRequest,
|
|
119
126
|
timeout: options.mirrorTimeout,
|
|
@@ -125,11 +132,15 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
125
132
|
continue;
|
|
126
133
|
}
|
|
127
134
|
}
|
|
128
|
-
// attempt to upload
|
|
135
|
+
// attempt to upload if first upload or mirror failed
|
|
129
136
|
if (!metadata) {
|
|
130
137
|
const sha256 = await getBlobSha256(blob);
|
|
131
138
|
options.onStart?.(server, sha256, blob);
|
|
132
139
|
metadata = await uploadBlob(server, blob, {
|
|
140
|
+
// pass through abort signal
|
|
141
|
+
signal: options.signal,
|
|
142
|
+
// pass through authorization flag
|
|
143
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
133
144
|
onAuth: handleAuthRequest,
|
|
134
145
|
onPayment: handlePaymentRequest,
|
|
135
146
|
timeout: options.mirrorTimeout,
|
package/lib/actions/upload.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType, UploadType } from "../client.js";
|
|
3
|
-
import { BlobDescriptor, PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
7
|
-
/** Override
|
|
8
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
9
8
|
/** Request timeout */
|
|
10
9
|
timeout?: number;
|
|
11
10
|
/**
|
|
@@ -15,7 +14,7 @@ export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
|
15
14
|
* @param blob the original blob
|
|
16
15
|
* @param request the payment request
|
|
17
16
|
*/
|
|
18
|
-
onPayment?: (server: S, sha256: string, blob: B, request: PaymentRequest) => Promise<
|
|
17
|
+
onPayment?: (server: S, sha256: string, blob: B, request: PaymentRequest) => Promise<PaymentToken>;
|
|
19
18
|
/**
|
|
20
19
|
* A method used to request a signed auth event for a server and sha256
|
|
21
20
|
* @param server the server requesting the auth
|
package/lib/actions/upload.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType, getPaymentRequestFromHeaders, } from "../helpers/index.js";
|
|
2
|
-
import HTTPError from "../error.js";
|
|
3
1
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
2
|
+
import HTTPError from "../error.js";
|
|
3
|
+
import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
|
|
4
4
|
/** Upload a blob to a server, handles payment and auth */
|
|
5
5
|
export async function uploadBlob(server, blob, opts) {
|
|
6
6
|
const url = new URL("/upload", server);
|
|
@@ -8,9 +8,17 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
8
8
|
const headers = {
|
|
9
9
|
"X-SHA-256": sha256,
|
|
10
10
|
};
|
|
11
|
-
// attach the
|
|
12
|
-
if (opts?.auth)
|
|
13
|
-
|
|
11
|
+
// attach the authorization if its already set
|
|
12
|
+
if (opts?.auth) {
|
|
13
|
+
if (typeof opts.auth === "boolean") {
|
|
14
|
+
if (!opts.onAuth)
|
|
15
|
+
throw new Error("Missing onAuth handler");
|
|
16
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, sha256, "upload", blob));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
14
22
|
// build check headers
|
|
15
23
|
const checkHeaders = {
|
|
16
24
|
...headers,
|
|
@@ -39,7 +47,11 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
39
47
|
// handle auth and payment
|
|
40
48
|
switch (firstTry.status) {
|
|
41
49
|
case 401: {
|
|
42
|
-
|
|
50
|
+
// throw an error if auth is requested and disabled
|
|
51
|
+
if (opts?.auth === false)
|
|
52
|
+
throw new Error("Authorization disabled");
|
|
53
|
+
// Request authorization event for this upload
|
|
54
|
+
const auth = await opts?.onAuth?.(server, sha256, "upload", blob);
|
|
43
55
|
if (!auth)
|
|
44
56
|
throw new Error("Missing auth handler");
|
|
45
57
|
// Try upload with auth
|
|
@@ -56,6 +68,7 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
56
68
|
if (!opts?.onPayment)
|
|
57
69
|
throw new Error("Missing payment handler");
|
|
58
70
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
71
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
59
72
|
const request = getPaymentRequestFromHeaders(firstTry.headers);
|
|
60
73
|
const token = await opts.onPayment(server, sha256, blob, request);
|
|
61
74
|
const payment = getEncodedToken(token);
|
package/lib/auth.d.ts
CHANGED
|
@@ -2,9 +2,12 @@ import { ServerType, UploadType } from "./client.js";
|
|
|
2
2
|
import { SignedEvent, Signer } from "./types.js";
|
|
3
3
|
export declare const now: () => number;
|
|
4
4
|
export declare const oneHour: () => number;
|
|
5
|
+
/** Encodes an auth event into a nostr authorization header */
|
|
5
6
|
export declare function encodeAuthorizationHeader(event: SignedEvent): string;
|
|
6
7
|
/** Checks if an auth event matches a server / blob upload */
|
|
7
|
-
export declare function
|
|
8
|
+
export declare function doseAuthMatchBlob(auth: SignedEvent, server: ServerType, blob: string | UploadType, type?: string): Promise<boolean>;
|
|
9
|
+
/** @deprecated Use `doseAuthMatchBlob` instead */
|
|
10
|
+
export declare const doseAuthMatchUpload: typeof doseAuthMatchBlob;
|
|
8
11
|
export type AuthType = "upload" | "list" | "delete" | "get" | "media";
|
|
9
12
|
export type AuthEventOptions = {
|
|
10
13
|
blobs?: string | string[] | UploadType | UploadType[];
|
package/lib/auth.js
CHANGED
|
@@ -3,13 +3,14 @@ import { getBlobSha256, isSha256 } from "./helpers/blob.js";
|
|
|
3
3
|
import { areServersEqual } from "./helpers/url.js";
|
|
4
4
|
export const now = () => Math.floor(new Date().valueOf() / 1000);
|
|
5
5
|
export const oneHour = () => now() + 60 * 60;
|
|
6
|
+
/** Encodes an auth event into a nostr authorization header */
|
|
6
7
|
export function encodeAuthorizationHeader(event) {
|
|
7
8
|
return "Nostr " + btoa(JSON.stringify(event));
|
|
8
9
|
}
|
|
9
10
|
/** Checks if an auth event matches a server / blob upload */
|
|
10
|
-
export async function
|
|
11
|
-
const
|
|
12
|
-
if (
|
|
11
|
+
export async function doseAuthMatchBlob(auth, server, blob, type = "upload") {
|
|
12
|
+
const authType = auth.tags.find((t) => t[0] === "t")?.[1];
|
|
13
|
+
if (authType !== type)
|
|
13
14
|
return false;
|
|
14
15
|
const sha256 = typeof blob === "string" ? blob : await getBlobSha256(blob);
|
|
15
16
|
for (const tag of auth.tags) {
|
|
@@ -26,6 +27,8 @@ export async function doseAuthMatchUpload(auth, server, blob) {
|
|
|
26
27
|
}
|
|
27
28
|
return false;
|
|
28
29
|
}
|
|
30
|
+
/** @deprecated Use `doseAuthMatchBlob` instead */
|
|
31
|
+
export const doseAuthMatchUpload = doseAuthMatchBlob;
|
|
29
32
|
async function normalizeToHash(blob) {
|
|
30
33
|
return typeof blob === "string" ? blob : getBlobSha256(blob);
|
|
31
34
|
}
|
|
@@ -43,17 +46,24 @@ export async function createAuthEvent(signer, type, options) {
|
|
|
43
46
|
};
|
|
44
47
|
// add blob tags
|
|
45
48
|
if (options?.blobs) {
|
|
46
|
-
if (Array.isArray(options.blobs))
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
if (Array.isArray(options.blobs)) {
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
for (const blob of options.blobs) {
|
|
52
|
+
const hash = await normalizeToHash(blob);
|
|
53
|
+
if (!seen.has(hash)) {
|
|
54
|
+
draft.tags.push(["x", hash]);
|
|
55
|
+
seen.add(hash);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
49
59
|
else
|
|
50
60
|
draft.tags.push(["x", await normalizeToHash(options.blobs)]);
|
|
51
61
|
}
|
|
52
62
|
// add server tags
|
|
53
63
|
if (options?.servers) {
|
|
54
64
|
if (Array.isArray(options.servers))
|
|
55
|
-
for (const
|
|
56
|
-
draft.tags.push(["server",
|
|
65
|
+
for (const server of new Set(options.servers))
|
|
66
|
+
draft.tags.push(["server", server]);
|
|
57
67
|
else
|
|
58
68
|
draft.tags.push(["server", options.servers]);
|
|
59
69
|
}
|
package/lib/helpers/blob.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { UploadType } from "../client.js";
|
|
2
|
+
/** Checks if a string is a 64 length hex string */
|
|
2
3
|
export declare function isSha256(str: string): RegExpMatchArray | null;
|
|
4
|
+
/** A symbol use to store the sha256 hash of a blob on a Blob instance */
|
|
3
5
|
export declare const BlobHashSymbol: unique symbol;
|
|
4
6
|
/** gets or calculates the sha2456 of a Blob */
|
|
5
7
|
export declare function getBlobSha256(blob: UploadType): string | Promise<string>;
|
package/lib/helpers/blob.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { bytesToHex } from "@noble/hashes/utils";
|
|
2
|
+
/** Checks if a string is a 64 length hex string */
|
|
2
3
|
export function isSha256(str) {
|
|
3
4
|
return str.match(/^[0-9a-f]{64}$/);
|
|
4
5
|
}
|
|
6
|
+
/** A symbol use to store the sha256 hash of a blob on a Blob instance */
|
|
5
7
|
export const BlobHashSymbol = Symbol.for("sha256");
|
|
6
8
|
/** gets or calculates the sha2456 of a Blob */
|
|
7
9
|
export function getBlobSha256(blob) {
|
|
@@ -15,7 +17,8 @@ export function getBlobSha256(blob) {
|
|
|
15
17
|
/** Calculates the sha2456 of a Blob */
|
|
16
18
|
export async function computeBlobSha256(blob) {
|
|
17
19
|
let buffer;
|
|
18
|
-
|
|
20
|
+
// NOTE: use typeof File !== "undefined" to check if File is supported (required to support node 18)
|
|
21
|
+
if ((typeof File !== "undefined" && blob instanceof File) || blob instanceof Blob) {
|
|
19
22
|
buffer = await blob.arrayBuffer();
|
|
20
23
|
}
|
|
21
24
|
else {
|
|
@@ -23,19 +26,19 @@ export async function computeBlobSha256(blob) {
|
|
|
23
26
|
buffer = blob;
|
|
24
27
|
}
|
|
25
28
|
let hash;
|
|
26
|
-
if (crypto.subtle) {
|
|
29
|
+
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
27
30
|
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
|
|
28
31
|
hash = new Uint8Array(hashBuffer);
|
|
29
32
|
}
|
|
30
33
|
else {
|
|
31
|
-
const { sha256 } = await import("@noble/hashes/
|
|
34
|
+
const { sha256 } = await import("@noble/hashes/sha2");
|
|
32
35
|
hash = sha256.create().update(new Uint8Array(buffer)).digest();
|
|
33
36
|
}
|
|
34
37
|
return bytesToHex(hash);
|
|
35
38
|
}
|
|
36
39
|
/** Returns the size of the blob in bytes */
|
|
37
40
|
export function getBlobSize(blob) {
|
|
38
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
41
|
+
if ((typeof File !== "undefined" && blob instanceof File) || blob instanceof Blob) {
|
|
39
42
|
return blob.size;
|
|
40
43
|
}
|
|
41
44
|
else {
|
|
@@ -45,7 +48,7 @@ export function getBlobSize(blob) {
|
|
|
45
48
|
}
|
|
46
49
|
/** Returns the mime type of the blob */
|
|
47
50
|
export function getBlobType(blob) {
|
|
48
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
51
|
+
if ((typeof File !== "undefined" && blob instanceof File) || blob instanceof Blob) {
|
|
49
52
|
return blob.type;
|
|
50
53
|
}
|
|
51
54
|
return undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { decodePaymentRequest } from "@cashu/cashu-ts";
|
|
2
|
+
export function getPaymentRequestFromHeaders(headers, quite = false) {
|
|
3
|
+
const header = headers.get("X-Cashu");
|
|
4
|
+
if (!header) {
|
|
5
|
+
if (!quite)
|
|
6
|
+
throw new Error("Missing cashu header");
|
|
7
|
+
else
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const request = decodePaymentRequest(header);
|
|
11
|
+
// Clear the transport, since NUT-23 is only in-band payments
|
|
12
|
+
request.transport = [];
|
|
13
|
+
return request;
|
|
14
|
+
}
|
package/lib/helpers/index.d.ts
CHANGED