@gethashd/bytecave-browser 1.0.58 → 1.0.60
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/dist/{chunk-NLZDUMYM.js → chunk-LMK44RJF.js} +34 -396
- package/dist/client.d.ts +0 -17
- package/dist/index.cjs +34 -396
- package/dist/index.js +1 -1
- package/dist/react/index.cjs +3 -22
- package/dist/react/index.js +1 -1
- package/dist/types.d.ts +0 -4
- package/package.json +1 -1
- package/src/client.ts +35 -301
- package/src/p2p-protocols.ts +6 -27
- package/src/types.ts +0 -4
- package/dist/storage-webtransport.d.ts +0 -47
- package/src/storage-webtransport.ts +0 -201
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WebTransport Storage Client
|
|
3
|
-
*
|
|
4
|
-
* Provides direct browser-to-node storage over WebTransport (HTTP/3)
|
|
5
|
-
* Used as fallback when WebSocket relay is unavailable
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
// Logger removed - using console.log for browser compatibility
|
|
9
|
-
|
|
10
|
-
export interface WebTransportStorageRequest {
|
|
11
|
-
data: Uint8Array;
|
|
12
|
-
contentType: string;
|
|
13
|
-
hashIdToken?: number;
|
|
14
|
-
authorization?: {
|
|
15
|
-
signature: string;
|
|
16
|
-
address: string;
|
|
17
|
-
timestamp: number;
|
|
18
|
-
nonce: string;
|
|
19
|
-
appId: string;
|
|
20
|
-
contentHash: string;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface WebTransportStorageResponse {
|
|
25
|
-
success: boolean;
|
|
26
|
-
cid?: string;
|
|
27
|
-
error?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export class StorageWebTransportClient {
|
|
31
|
-
private nodeMultiaddr: string;
|
|
32
|
-
|
|
33
|
-
constructor(nodeMultiaddr: string) {
|
|
34
|
-
this.nodeMultiaddr = nodeMultiaddr;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Store data via WebTransport direct to node
|
|
39
|
-
*/
|
|
40
|
-
async store(request: WebTransportStorageRequest): Promise<WebTransportStorageResponse> {
|
|
41
|
-
try {
|
|
42
|
-
// Check if WebTransport is supported
|
|
43
|
-
if (typeof WebTransport === 'undefined') {
|
|
44
|
-
return {
|
|
45
|
-
success: false,
|
|
46
|
-
error: 'WebTransport not supported in this browser'
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Parse multiaddr to get WebTransport URL
|
|
51
|
-
// Format: /ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/...
|
|
52
|
-
const url = this.multiaddrToWebTransportUrl(this.nodeMultiaddr);
|
|
53
|
-
if (!url) {
|
|
54
|
-
return {
|
|
55
|
-
success: false,
|
|
56
|
-
error: 'Invalid WebTransport multiaddr'
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
console.log('[WebTransport] Connecting to node:', url);
|
|
61
|
-
|
|
62
|
-
// Create WebTransport connection
|
|
63
|
-
// For localhost development with mkcert, the certificate is trusted by the system CA
|
|
64
|
-
// For production with proper SSL, no serverCertificateHashes needed
|
|
65
|
-
const transport = new WebTransport(url);
|
|
66
|
-
await transport.ready;
|
|
67
|
-
|
|
68
|
-
console.log('[WebTransport] Connected, opening bidirectional stream');
|
|
69
|
-
|
|
70
|
-
// Open bidirectional stream for storage protocol
|
|
71
|
-
const stream = await transport.createBidirectionalStream();
|
|
72
|
-
const writer = stream.writable.getWriter();
|
|
73
|
-
const reader = stream.readable.getReader();
|
|
74
|
-
|
|
75
|
-
// Send storage request
|
|
76
|
-
const requestData = {
|
|
77
|
-
type: 'storage-request',
|
|
78
|
-
data: Array.from(request.data), // Convert Uint8Array to array for JSON
|
|
79
|
-
contentType: request.contentType,
|
|
80
|
-
hashIdToken: request.hashIdToken,
|
|
81
|
-
authorization: request.authorization
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const requestJson = JSON.stringify(requestData);
|
|
85
|
-
const requestBytes = new TextEncoder().encode(requestJson);
|
|
86
|
-
|
|
87
|
-
await writer.write(requestBytes);
|
|
88
|
-
await writer.close();
|
|
89
|
-
|
|
90
|
-
console.log('[WebTransport] Request sent, waiting for response');
|
|
91
|
-
|
|
92
|
-
// Read response
|
|
93
|
-
const { value, done } = await reader.read();
|
|
94
|
-
if (done || !value) {
|
|
95
|
-
return {
|
|
96
|
-
success: false,
|
|
97
|
-
error: 'No response from node'
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const responseJson = new TextDecoder().decode(value);
|
|
102
|
-
const response = JSON.parse(responseJson);
|
|
103
|
-
|
|
104
|
-
console.log('[WebTransport] Response received:', response);
|
|
105
|
-
|
|
106
|
-
// Close transport
|
|
107
|
-
await transport.close();
|
|
108
|
-
|
|
109
|
-
return response;
|
|
110
|
-
|
|
111
|
-
} catch (error: any) {
|
|
112
|
-
console.error('[WebTransport] Storage failed:', error);
|
|
113
|
-
return {
|
|
114
|
-
success: false,
|
|
115
|
-
error: error.message || 'WebTransport storage failed'
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Convert libp2p multiaddr to WebTransport URL
|
|
122
|
-
*/
|
|
123
|
-
private multiaddrToWebTransportUrl(multiaddr: string): string | null {
|
|
124
|
-
try {
|
|
125
|
-
// Parse multiaddr format: /ip4/127.0.0.1/udp/4001/quic-v1/webtransport
|
|
126
|
-
const parts = multiaddr.split('/').filter(p => p);
|
|
127
|
-
|
|
128
|
-
let ip = '';
|
|
129
|
-
let port = '';
|
|
130
|
-
let hasQuic = false;
|
|
131
|
-
let hasWebTransport = false;
|
|
132
|
-
|
|
133
|
-
for (let i = 0; i < parts.length; i++) {
|
|
134
|
-
if (parts[i] === 'ip4' && i + 1 < parts.length) {
|
|
135
|
-
ip = parts[i + 1];
|
|
136
|
-
} else if (parts[i] === 'udp' && i + 1 < parts.length) {
|
|
137
|
-
port = parts[i + 1];
|
|
138
|
-
} else if (parts[i] === 'quic-v1') {
|
|
139
|
-
hasQuic = true;
|
|
140
|
-
} else if (parts[i] === 'webtransport') {
|
|
141
|
-
hasWebTransport = true;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!ip || !port || !hasQuic || !hasWebTransport) {
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// WebTransport URL format: https://host:port/storage
|
|
150
|
-
// Use 'localhost' instead of '127.0.0.1' for better certificate compatibility
|
|
151
|
-
const host = ip === '127.0.0.1' ? 'localhost' : ip;
|
|
152
|
-
return `https://${host}:${port}/storage`;
|
|
153
|
-
|
|
154
|
-
} catch (error) {
|
|
155
|
-
console.error('[WebTransport] Failed to parse multiaddr:', error);
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Extract certificate hash from multiaddr
|
|
162
|
-
* Format: /ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEi...
|
|
163
|
-
*/
|
|
164
|
-
private extractCertHash(multiaddr: string): string | null {
|
|
165
|
-
try {
|
|
166
|
-
const parts = multiaddr.split('/').filter(p => p);
|
|
167
|
-
const certhashIndex = parts.indexOf('certhash');
|
|
168
|
-
if (certhashIndex !== -1 && certhashIndex + 1 < parts.length) {
|
|
169
|
-
return parts[certhashIndex + 1];
|
|
170
|
-
}
|
|
171
|
-
return null;
|
|
172
|
-
} catch (error) {
|
|
173
|
-
console.error('[WebTransport] Failed to extract cert hash:', error);
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Convert libp2p multihash cert hash to SHA-256 ArrayBuffer for WebTransport
|
|
180
|
-
* The certhash in multiaddr format is: uEi<hex-hash>
|
|
181
|
-
* where 'u' = base32 multibase, 'Ei' = multihash prefix for SHA-256
|
|
182
|
-
*/
|
|
183
|
-
private async certHashToSHA256(multihash: string): Promise<ArrayBuffer> {
|
|
184
|
-
try {
|
|
185
|
-
// Remove 'uEi' prefix to get the raw hex hash
|
|
186
|
-
// Format: uEi733dc9ebf43a04ad3bc692f104cf6ccc228a062fcd7aa43fc370f9c3c67e3bfc
|
|
187
|
-
let hexHash = multihash.startsWith('uEi') ? multihash.slice(3) : multihash;
|
|
188
|
-
|
|
189
|
-
// The hash after uEi is already in hex format, just convert to bytes
|
|
190
|
-
const bytes = new Uint8Array(hexHash.match(/.{1,2}/g)!.map(byte => parseInt(byte, 16)));
|
|
191
|
-
|
|
192
|
-
console.log('[WebTransport] Decoded certificate hash:', hexHash);
|
|
193
|
-
console.log('[WebTransport] Hash bytes length:', bytes.length);
|
|
194
|
-
|
|
195
|
-
return bytes.buffer;
|
|
196
|
-
} catch (error) {
|
|
197
|
-
console.error('[WebTransport] Failed to convert cert hash:', error);
|
|
198
|
-
throw new Error('Failed to decode certificate hash');
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|