blossom-client-sdk 5.0.0-2 → 5.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "5.0.0-2",
3
+ "version": "5.0.0",
4
4
  "description": "Client SDK for talking to blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -1,5 +0,0 @@
1
- import { PaymentRequest } from "../types.js";
2
- /** Extracts a cashu-ts PaymentRequest from Headers */
3
- export declare function getPaymentRequestFromHeaders(headers: Headers): PaymentRequest;
4
- export declare function getPaymentRequestFromHeaders(headers: Headers, quite: false): PaymentRequest;
5
- export declare function getPaymentRequestFromHeaders(headers: Headers, quite: true): PaymentRequest | undefined;
@@ -1,14 +0,0 @@
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.d.ts DELETED
@@ -1,20 +0,0 @@
1
- import { type UploadType } from "./client.js";
2
- import { PaymentRequest } from "./types.js";
3
- export declare function isSha256(str: string): RegExpMatchArray | null;
4
- /** returns the last sha256 in a URL */
5
- export declare function getHashFromURL(url: string | URL): string | null;
6
- export declare const BlobHashSymbol: unique symbol;
7
- /** gets or calculates the sha2456 of a Blob */
8
- export declare function getBlobSha256(blob: UploadType): string | Promise<string>;
9
- /** Calculates the sha2456 of a Blob */
10
- export declare function computeBlobSha256(blob: UploadType): Promise<string>;
11
- /** Returns the size of the blob in bytes */
12
- export declare function getBlobSize(blob: UploadType): number;
13
- /** Returns the mime type of the blob */
14
- export declare function getBlobType(blob: UploadType): string | undefined;
15
- /** Check if two servers are the same */
16
- export declare function areServersEqual(a: string | URL, b: string | URL): boolean;
17
- /** Extracts a cashu-ts PaymentRequest from Headers */
18
- export declare function getPaymentRequestFromHeaders(headers: Headers): PaymentRequest;
19
- export declare function getPaymentRequestFromHeaders(headers: Headers, quite: false): PaymentRequest;
20
- export declare function getPaymentRequestFromHeaders(headers: Headers, quite: true): PaymentRequest | undefined;
package/lib/helpers.js DELETED
@@ -1,73 +0,0 @@
1
- import { bytesToHex } from "@noble/hashes/utils";
2
- export function isSha256(str) {
3
- return str.match(/^[0-9a-f]{64}$/);
4
- }
5
- /** returns the last sha256 in a URL */
6
- export function getHashFromURL(url) {
7
- if (typeof url === "string")
8
- url = new URL(url);
9
- const hashes = Array.from(url.pathname.matchAll(/[0-9a-f]{64}/gi));
10
- if (hashes.length > 0)
11
- return hashes[hashes.length - 1][0];
12
- return null;
13
- }
14
- export const BlobHashSymbol = Symbol.for("sha256");
15
- /** gets or calculates the sha2456 of a Blob */
16
- export function getBlobSha256(blob) {
17
- if (Reflect.has(blob, BlobHashSymbol))
18
- return Reflect.get(blob, BlobHashSymbol);
19
- return computeBlobSha256(blob).then((hash) => {
20
- Reflect.set(blob, BlobHashSymbol, hash);
21
- return hash;
22
- });
23
- }
24
- /** Calculates the sha2456 of a Blob */
25
- export async function computeBlobSha256(blob) {
26
- let buffer;
27
- if (blob instanceof File || blob instanceof Blob) {
28
- buffer = await blob.arrayBuffer();
29
- }
30
- else {
31
- // nodejs Buffer
32
- buffer = blob;
33
- }
34
- let hash;
35
- if (crypto.subtle) {
36
- const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
37
- hash = new Uint8Array(hashBuffer);
38
- }
39
- else {
40
- const { sha256 } = await import("@noble/hashes/sha256");
41
- hash = sha256.create().update(new Uint8Array(buffer)).digest();
42
- }
43
- return bytesToHex(hash);
44
- }
45
- /** Returns the size of the blob in bytes */
46
- export function getBlobSize(blob) {
47
- if (blob instanceof File || blob instanceof Blob) {
48
- return blob.size;
49
- }
50
- else {
51
- // nodejs Buffer
52
- return blob.length;
53
- }
54
- }
55
- /** Returns the mime type of the blob */
56
- export function getBlobType(blob) {
57
- if (blob instanceof File || blob instanceof Blob) {
58
- return blob.type;
59
- }
60
- return undefined;
61
- }
62
- /** Check if two servers are the same */
63
- export function areServersEqual(a, b) {
64
- const hostnameA = a instanceof URL ? a.hostname : new URL(a).hostname;
65
- const hostnameB = b instanceof URL ? b.hostname : new URL(b).hostname;
66
- return hostnameA === hostnameB;
67
- }
68
- export function getPaymentRequestFromHeaders(headers, quite = false) {
69
- const header = headers.get("X-Cashu");
70
- if (!header && !quite)
71
- throw new Error("Missing cashu header");
72
- return header ? JSON.parse(atob(header)) : undefined;
73
- }
package/lib/upload.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { BlobDescriptor, ServerType, SignedEvent, Signer, UploadType } from "./client.js";
2
- /**
3
- * Creates an AsyncGenerator that can be used to upload a blob to multiple servers
4
- * @param servers A Set or Array of servers to upload to
5
- * @param file The blob to be uploaded
6
- * @param signer An async function used for signing nostr events
7
- * @returns The BlobDescriptor if successful
8
- */
9
- export declare function multiServerUpload(servers: Iterable<ServerType>, file: UploadType, signer: Signer, auth?: SignedEvent): AsyncGenerator<{
10
- blob: BlobDescriptor;
11
- progress: number;
12
- server: ServerType;
13
- }, BlobDescriptor | undefined, unknown>;
package/lib/upload.js DELETED
@@ -1,37 +0,0 @@
1
- import { BlossomClient } from "./client.js";
2
- /**
3
- * Creates an AsyncGenerator that can be used to upload a blob to multiple servers
4
- * @param servers A Set or Array of servers to upload to
5
- * @param file The blob to be uploaded
6
- * @param signer An async function used for signing nostr events
7
- * @returns The BlobDescriptor if successful
8
- */
9
- export async function* multiServerUpload(servers, file, signer, auth) {
10
- auth = auth || (await BlossomClient.getUploadAuth(file, signer));
11
- let blob = undefined;
12
- let total = Array.from(servers).length;
13
- let uploaded = 0;
14
- for (const server of servers) {
15
- if (blob) {
16
- // mirror blob to server
17
- try {
18
- let b = await BlossomClient.mirrorBlob(server, blob.url, auth);
19
- uploaded++;
20
- yield { blob: b, progress: uploaded / total, server };
21
- }
22
- catch (error) {
23
- // not all servers support mirroring, attempt to upload
24
- let b = await BlossomClient.uploadBlob(server, file, auth);
25
- uploaded++;
26
- yield { blob: b, progress: uploaded / total, server };
27
- }
28
- }
29
- else {
30
- // attempt initial upload
31
- blob = await BlossomClient.uploadBlob(server, file, auth);
32
- uploaded++;
33
- yield { blob, progress: uploaded / total, server };
34
- }
35
- }
36
- return blob;
37
- }