ipfs-node 0.1.6__py3-none-any.whl
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.
- ipfs_node/__init__.py +4 -0
- ipfs_node/ipfs_files.py +233 -0
- ipfs_node/ipfs_node.py +230 -0
- ipfs_node/ipfs_peers.py +71 -0
- ipfs_node/ipfs_pubsub.py +474 -0
- ipfs_node/ipfs_tunnels.py +263 -0
- ipfs_node/libkubo/libkubo_linux_x86_64.h +237 -0
- ipfs_node/libkubo/libkubo_linux_x86_64.so +0 -0
- ipfs_node/utils/__init__.py +3 -0
- ipfs_node/utils/cid_utils.py +55 -0
- ipfs_node/utils/peer_utils.py +62 -0
- ipfs_node-0.1.6.dist-info/LICENSE +21 -0
- ipfs_node-0.1.6.dist-info/METADATA +134 -0
- ipfs_node-0.1.6.dist-info/RECORD +18 -0
- ipfs_node-0.1.6.dist-info/WHEEL +5 -0
- ipfs_node-0.1.6.dist-info/top_level.txt +2 -0
- libkubo/__init__.py +1 -0
- libkubo/libkubo_loader.py +45 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
import json
|
|
3
|
+
from typing import List, Dict, Optional, Tuple, Any, Union
|
|
4
|
+
from ipfs_tk_generics.tunnels import SenderTunnel, ListenerTunnel, TunnelsList, BaseTunnels
|
|
5
|
+
from libkubo import libkubo, c_str, from_c_str, ffi, c_bool
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class NodeTunnels(BaseTunnels):
|
|
12
|
+
"""
|
|
13
|
+
Provides P2P stream mounting functionality for IPFS nodes.
|
|
14
|
+
|
|
15
|
+
Stream mounting allows you to expose local TCP services to the libp2p network
|
|
16
|
+
and connect to remote TCP services exposed by other nodes.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, node):
|
|
20
|
+
self._node = node
|
|
21
|
+
self._repo_path = self._node._repo_path
|
|
22
|
+
BaseTunnels.__init__(self)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _enable_p2p(self) -> bool:
|
|
26
|
+
"""Enable p2p functionality in the IPFS configuration."""
|
|
27
|
+
|
|
28
|
+
repo_path = c_str(self._repo_path.encode('utf-8'))
|
|
29
|
+
result = libkubo.P2PEnable(repo_path)
|
|
30
|
+
|
|
31
|
+
if result <= 0:
|
|
32
|
+
print(f"Warning: Could not enable p2p functionality ({result})")
|
|
33
|
+
return False
|
|
34
|
+
return True
|
|
35
|
+
|
|
36
|
+
def open_sender(self, name: str, listen_addr: int | str, target_peer_id: str):
|
|
37
|
+
"""
|
|
38
|
+
Forward local connections to a remote peer.
|
|
39
|
+
|
|
40
|
+
This creates a new listener that forwards connections to the specified
|
|
41
|
+
peer over the libp2p network.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
name: The protocol name to use for the forwarding.
|
|
45
|
+
listen_addr: The local address to listen on (e.g. "127.0.0.1:8080").
|
|
46
|
+
target_peer_id: The peer ID to forward connections to.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
bool: True if the forwarding was set up successfully, False otherwise.
|
|
50
|
+
"""
|
|
51
|
+
# if not target_peer_id.startswith("/p2p/"):
|
|
52
|
+
# target_peer_id = f"/p2p/{target_peer_id}"
|
|
53
|
+
result = libkubo.P2PForward(
|
|
54
|
+
c_str(self._repo_path.encode('utf-8')),
|
|
55
|
+
c_str(name.encode('utf-8')),
|
|
56
|
+
c_str(self._port_to_addr(listen_addr).encode('utf-8')),
|
|
57
|
+
c_str(target_peer_id.encode('utf-8'))
|
|
58
|
+
)
|
|
59
|
+
if result > 0:
|
|
60
|
+
return
|
|
61
|
+
if result == -2:
|
|
62
|
+
print(f"Can't create sender {name} {listen_addr} {target_peer_id}")
|
|
63
|
+
raise Exception("IpfsNode.tunnels.open_sender: failed to open sender: bind: address already in use")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
raise Exception("IpfsNode.tunnels.open_sender: failed to open sender")
|
|
67
|
+
|
|
68
|
+
def open_listener(self, name: str, target_addr: int | str):
|
|
69
|
+
"""
|
|
70
|
+
Listen for libp2p connections and forward them to a local TCP service.
|
|
71
|
+
|
|
72
|
+
This exposes a local TCP service to the libp2p network.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
name: The protocol name to use for the listener.
|
|
76
|
+
target_addr: The local address to forward connections to (e.g. "127.0.0.1:8080").
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
bool: True if the listener was set up successfully, False otherwise.
|
|
80
|
+
"""
|
|
81
|
+
result = libkubo.P2PListen(
|
|
82
|
+
c_str(self._repo_path.encode('utf-8')),
|
|
83
|
+
c_str(name.encode('utf-8')),
|
|
84
|
+
c_str(self._port_to_addr(target_addr)))
|
|
85
|
+
if result > 0:
|
|
86
|
+
return
|
|
87
|
+
if result == -2:
|
|
88
|
+
print(f"Can't open listener {name} {target_addr}")
|
|
89
|
+
raise Exception("IpfsNode.tunnels.open_sender: failed to open listener")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
raise Exception("IpfsNode.tunnels.open_sender: failed to open listener")
|
|
93
|
+
|
|
94
|
+
def close_sender(self, name: str = None, port: int = None, peer_id: str = None) -> int:
|
|
95
|
+
"""
|
|
96
|
+
Close a specific TCP forwarding connection, optionally filtered by protocol, port, or peer ID.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
name: Optional protocol filter
|
|
100
|
+
port: Optional port filter
|
|
101
|
+
peer_id: Optional peer ID filter
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
int: Number of forwarding connections closed
|
|
105
|
+
"""
|
|
106
|
+
if peer_id and not peer_id.startswith("/p2p/"):
|
|
107
|
+
peer_id = f"/p2p/{peer_id}"
|
|
108
|
+
return self.close_tcp_connections(name, port, peer_id, senders=True, listeners=False)
|
|
109
|
+
|
|
110
|
+
def close_listener(self, name: str = None, port: int = None) -> int:
|
|
111
|
+
"""
|
|
112
|
+
Close a specific TCP listening connection, optionally filtered by protocol name or port.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
name: Optional protocol name filter
|
|
116
|
+
port: Optional port filter
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
int: Number of listening connections closed
|
|
120
|
+
"""
|
|
121
|
+
return self.close_tcp_connections(name, target_addr=port, listeners=True, senders=False)
|
|
122
|
+
|
|
123
|
+
def close_streams(self, name: str, port: int | None = None, target_peer_id: str = "") -> bool:
|
|
124
|
+
"""
|
|
125
|
+
Close a P2P listener or stream.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
name: The protocol name of the listener or stream to close.
|
|
129
|
+
listen_addr: For streams, the local address that the stream listens on.
|
|
130
|
+
target_peer_id: For streams, the peer ID that the stream connects to.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
bool: True if the listener or stream was closed successfully, False otherwise.
|
|
134
|
+
"""
|
|
135
|
+
result = libkubo.P2PClose(
|
|
136
|
+
c_str(self._repo_path.encode('utf-8')),
|
|
137
|
+
c_str(name.encode('utf-8')),
|
|
138
|
+
c_str(self._port_to_addr(port)) if port else c_str(""),
|
|
139
|
+
c_str(target_peer_id.encode('utf-8'))
|
|
140
|
+
)
|
|
141
|
+
return result > 0
|
|
142
|
+
|
|
143
|
+
def close_tcp_connections(
|
|
144
|
+
self,
|
|
145
|
+
name: str = "", listen_addr: str | int | None = None, target_addr: str | int | None = None, all: bool = False,
|
|
146
|
+
listeners: bool = True, senders: bool = True
|
|
147
|
+
) -> int:
|
|
148
|
+
"""
|
|
149
|
+
Close specific TCP p2p connections, optionally filtered by protocol name, port, or peer ID.
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
name: Optional protocol name filter
|
|
153
|
+
port: Optional port filter
|
|
154
|
+
peer_id: Optional peer ID filter
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
int: Number of connections closed
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
repo_path = c_str(self._repo_path.encode('utf-8'))
|
|
161
|
+
|
|
162
|
+
result = libkubo.P2PClose(
|
|
163
|
+
repo_path, c_str(name),
|
|
164
|
+
c_str(self._port_to_addr(listen_addr)),
|
|
165
|
+
c_str(self._port_to_addr(target_addr)),
|
|
166
|
+
c_bool(all), c_bool(listeners), c_bool(senders)
|
|
167
|
+
)
|
|
168
|
+
return result
|
|
169
|
+
|
|
170
|
+
def close_all_senders(self) -> int:
|
|
171
|
+
"""
|
|
172
|
+
Close all TCP forwarding connections.
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
int: Number of forwarding connections closed
|
|
176
|
+
"""
|
|
177
|
+
return self.close_tcp_connections(all=True, listeners=False, senders=True)
|
|
178
|
+
|
|
179
|
+
def close_all_listeners(self) -> int:
|
|
180
|
+
"""
|
|
181
|
+
Close all TCP listening connections.
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
int: Number of listening connections closed
|
|
185
|
+
"""
|
|
186
|
+
return self.close_tcp_connections(all=True, listeners=True, senders=False)
|
|
187
|
+
def close_all(self) -> int:
|
|
188
|
+
"""
|
|
189
|
+
Close all TCP connections.
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
int: Number of listening connections closed
|
|
193
|
+
"""
|
|
194
|
+
return self.close_tcp_connections(all=True, listeners=True, senders=True)
|
|
195
|
+
|
|
196
|
+
def get_tunnels(self) ->TunnelsList:
|
|
197
|
+
"""
|
|
198
|
+
List all active P2P tunnels.
|
|
199
|
+
"""
|
|
200
|
+
result_ptr = libkubo.P2PListListeners(
|
|
201
|
+
c_str(self._repo_path.encode('utf-8'))
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
if not result_ptr:
|
|
205
|
+
return [], []
|
|
206
|
+
|
|
207
|
+
# Convert the C string to a Python string and release memory
|
|
208
|
+
result_str = from_c_str(result_ptr)
|
|
209
|
+
# libkubo.free(result_ptr)
|
|
210
|
+
|
|
211
|
+
if not result_str:
|
|
212
|
+
return [], []
|
|
213
|
+
|
|
214
|
+
# Parse the JSON
|
|
215
|
+
try:
|
|
216
|
+
result = json.loads(result_str)
|
|
217
|
+
except json.JSONDecodeError:
|
|
218
|
+
return [], []
|
|
219
|
+
|
|
220
|
+
# Add local listeners
|
|
221
|
+
listeners = []
|
|
222
|
+
for item in result.get('Listens', []):
|
|
223
|
+
listener = ListenerTunnel(
|
|
224
|
+
name=item.get('Protocol', ''),
|
|
225
|
+
# listen_address=item.get('ListenAddress', ''),
|
|
226
|
+
target_address=item.get('TargetAddress', '')
|
|
227
|
+
)
|
|
228
|
+
listeners.append(listener)
|
|
229
|
+
|
|
230
|
+
# Add remote listeners
|
|
231
|
+
forwarders = []
|
|
232
|
+
for item in result.get('Forwards', []):
|
|
233
|
+
listener = SenderTunnel(
|
|
234
|
+
name=item.get('Protocol', ''),
|
|
235
|
+
listen_address=item.get('ListenAddress', ''),
|
|
236
|
+
target_address=item.get('TargetAddress', '')
|
|
237
|
+
)
|
|
238
|
+
forwarders.append(listener)
|
|
239
|
+
|
|
240
|
+
# # Extract active streams
|
|
241
|
+
# streams = []
|
|
242
|
+
# for item in result.get('Streams', []):
|
|
243
|
+
# stream = P2PStream(
|
|
244
|
+
# name=item.get('Protocol', ''),
|
|
245
|
+
# origin_address=item.get('LocalAddr', ''),
|
|
246
|
+
# target_address=item.get('RemoteAddr', '')
|
|
247
|
+
# )
|
|
248
|
+
# streams.append(stream)
|
|
249
|
+
|
|
250
|
+
return TunnelsList(senders=forwarders, listeners=listeners)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _port_to_addr(self, addr: int | str | None) -> str:
|
|
254
|
+
if not addr:
|
|
255
|
+
return ""
|
|
256
|
+
if isinstance(addr, int):
|
|
257
|
+
return f"/ip4/{self._node._ipfs_host_ip()}/tcp/{addr}"
|
|
258
|
+
else:
|
|
259
|
+
return addr
|
|
260
|
+
def terminate(self):
|
|
261
|
+
pass
|
|
262
|
+
def __del__(self):
|
|
263
|
+
self.terminate()
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/* Code generated by cmd/cgo; DO NOT EDIT. */
|
|
2
|
+
|
|
3
|
+
/* package github.com/emendir/py_ipfs_node/libkubo */
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
#line 1 "cgo-builtin-export-prolog"
|
|
7
|
+
|
|
8
|
+
#include <stddef.h>
|
|
9
|
+
|
|
10
|
+
#ifndef GO_CGO_EXPORT_PROLOGUE_H
|
|
11
|
+
#define GO_CGO_EXPORT_PROLOGUE_H
|
|
12
|
+
|
|
13
|
+
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
|
14
|
+
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
/* Start of preamble from import "C" comments. */
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
#line 3 "files.go"
|
|
23
|
+
#include <stdlib.h>
|
|
24
|
+
#include <stdbool.h>
|
|
25
|
+
|
|
26
|
+
#line 1 "cgo-generated-wrapper"
|
|
27
|
+
|
|
28
|
+
#line 3 "p2p.go"
|
|
29
|
+
|
|
30
|
+
#include <stdlib.h>
|
|
31
|
+
#include <stdbool.h>
|
|
32
|
+
|
|
33
|
+
#line 1 "cgo-generated-wrapper"
|
|
34
|
+
|
|
35
|
+
#line 3 "peers.go"
|
|
36
|
+
#include <stdlib.h>
|
|
37
|
+
|
|
38
|
+
#line 1 "cgo-generated-wrapper"
|
|
39
|
+
|
|
40
|
+
#line 3 "pubsub.go"
|
|
41
|
+
#include <stdlib.h>
|
|
42
|
+
|
|
43
|
+
#line 1 "cgo-generated-wrapper"
|
|
44
|
+
|
|
45
|
+
#line 3 "repo.go"
|
|
46
|
+
#include <stdlib.h>
|
|
47
|
+
|
|
48
|
+
#line 1 "cgo-generated-wrapper"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/* End of preamble from import "C" comments. */
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/* Start of boilerplate cgo prologue. */
|
|
55
|
+
#line 1 "cgo-gcc-export-header-prolog"
|
|
56
|
+
|
|
57
|
+
#ifndef GO_CGO_PROLOGUE_H
|
|
58
|
+
#define GO_CGO_PROLOGUE_H
|
|
59
|
+
|
|
60
|
+
typedef signed char GoInt8;
|
|
61
|
+
typedef unsigned char GoUint8;
|
|
62
|
+
typedef short GoInt16;
|
|
63
|
+
typedef unsigned short GoUint16;
|
|
64
|
+
typedef int GoInt32;
|
|
65
|
+
typedef unsigned int GoUint32;
|
|
66
|
+
typedef long long GoInt64;
|
|
67
|
+
typedef unsigned long long GoUint64;
|
|
68
|
+
typedef GoInt64 GoInt;
|
|
69
|
+
typedef GoUint64 GoUint;
|
|
70
|
+
typedef size_t GoUintptr;
|
|
71
|
+
typedef float GoFloat32;
|
|
72
|
+
typedef double GoFloat64;
|
|
73
|
+
#ifdef _MSC_VER
|
|
74
|
+
#include <complex.h>
|
|
75
|
+
typedef _Fcomplex GoComplex64;
|
|
76
|
+
typedef _Dcomplex GoComplex128;
|
|
77
|
+
#else
|
|
78
|
+
typedef float _Complex GoComplex64;
|
|
79
|
+
typedef double _Complex GoComplex128;
|
|
80
|
+
#endif
|
|
81
|
+
|
|
82
|
+
/*
|
|
83
|
+
static assertion to make sure the file is being used on architecture
|
|
84
|
+
at least with matching size of GoInt.
|
|
85
|
+
*/
|
|
86
|
+
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
|
|
87
|
+
|
|
88
|
+
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
|
89
|
+
typedef _GoString_ GoString;
|
|
90
|
+
#endif
|
|
91
|
+
typedef void *GoMap;
|
|
92
|
+
typedef void *GoChan;
|
|
93
|
+
typedef struct { void *t; void *v; } GoInterface;
|
|
94
|
+
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
|
95
|
+
|
|
96
|
+
#endif
|
|
97
|
+
|
|
98
|
+
/* End of boilerplate cgo prologue. */
|
|
99
|
+
|
|
100
|
+
#ifdef __cplusplus
|
|
101
|
+
extern "C" {
|
|
102
|
+
#endif
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
// AddFile adds a file to IPFS
|
|
106
|
+
//
|
|
107
|
+
extern char* AddFile(char* repoPath, char* filePath, _Bool onlyHash);
|
|
108
|
+
|
|
109
|
+
// FreeString is a no-op for now - we'll let Go's garbage collection handle the memory
|
|
110
|
+
//
|
|
111
|
+
extern void FreeString(char* str);
|
|
112
|
+
|
|
113
|
+
// Download retrieves a file or directory from IPFS
|
|
114
|
+
//
|
|
115
|
+
extern int Download(char* repoPath, char* cidStr, char* destPath);
|
|
116
|
+
|
|
117
|
+
// PinCID pins a CID to the IPFS node
|
|
118
|
+
//
|
|
119
|
+
extern int PinCID(char* repoPath, char* cidStr);
|
|
120
|
+
|
|
121
|
+
// UnpinCID unpins a CID from the IPFS node
|
|
122
|
+
//
|
|
123
|
+
extern int UnpinCID(char* repoPath, char* cidStr);
|
|
124
|
+
|
|
125
|
+
// ListPins returns a list of pinned CIDs
|
|
126
|
+
//
|
|
127
|
+
extern char* ListPins(char* repoPath);
|
|
128
|
+
|
|
129
|
+
// RemoveCID removes a pinned CID from IPFS (alias for UnpinCID for clarity)
|
|
130
|
+
//
|
|
131
|
+
extern int RemoveCID(char* repoPath, char* cidStr);
|
|
132
|
+
|
|
133
|
+
// P2PForward creates a libp2p stream mounting forwarding connection
|
|
134
|
+
//
|
|
135
|
+
extern int P2PForward(char* repoPath, char* proto, char* listenAddr, char* targetPeerID);
|
|
136
|
+
|
|
137
|
+
// P2PListen creates a libp2p service that listens for connections on the given protocol
|
|
138
|
+
//
|
|
139
|
+
extern int P2PListen(char* repoPath, char* proto, char* targetAddr);
|
|
140
|
+
|
|
141
|
+
// P2PClose closes p2p listener or stream
|
|
142
|
+
//
|
|
143
|
+
extern int P2PClose(char* repoPath, char* proto, char* listenAddr, char* targetAddr, _Bool _all, _Bool listeners, _Bool forwarders);
|
|
144
|
+
|
|
145
|
+
// P2PListListeners lists active p2p listeners
|
|
146
|
+
//
|
|
147
|
+
extern char* P2PListListeners(char* repoPath);
|
|
148
|
+
|
|
149
|
+
// P2PEnable ensures p2p functionality is enabled in the config
|
|
150
|
+
//
|
|
151
|
+
extern int P2PEnable(char* repoPath);
|
|
152
|
+
|
|
153
|
+
// P2PListForwards lists active p2p forwarding connections
|
|
154
|
+
//
|
|
155
|
+
extern char* P2PListForwards(char* repoPath);
|
|
156
|
+
|
|
157
|
+
// P2PCloseAllListeners closes all p2p listeners
|
|
158
|
+
//
|
|
159
|
+
extern int P2PCloseAllListeners(char* repoPath);
|
|
160
|
+
|
|
161
|
+
// P2PCloseAllForwards closes all p2p forwards
|
|
162
|
+
//
|
|
163
|
+
extern int P2PCloseAllForwards(char* repoPath);
|
|
164
|
+
|
|
165
|
+
// ConnectToPeer connects to a peer
|
|
166
|
+
//
|
|
167
|
+
extern int ConnectToPeer(char* repoPath, char* peerAddr);
|
|
168
|
+
|
|
169
|
+
// ListPeers connects to a peer
|
|
170
|
+
//
|
|
171
|
+
extern char* ListPeers(char* repoPath);
|
|
172
|
+
|
|
173
|
+
// ListPeers connects to a peer
|
|
174
|
+
//
|
|
175
|
+
extern char* ListPeersIDs(char* repoPath);
|
|
176
|
+
|
|
177
|
+
// FindPeer connects to a peer
|
|
178
|
+
//
|
|
179
|
+
extern char* FindPeer(char* repoPath, char* peerAddr, int timeOut);
|
|
180
|
+
|
|
181
|
+
// PubSubListTopics lists the topics the node is subscribed to
|
|
182
|
+
//
|
|
183
|
+
extern char* PubSubListTopics(char* repoPath);
|
|
184
|
+
|
|
185
|
+
// PubSubPublish publishes a message to a topic
|
|
186
|
+
//
|
|
187
|
+
extern int PubSubPublish(char* repoPath, char* topic, void* data, int dataLen);
|
|
188
|
+
|
|
189
|
+
// PubSubSubscribe subscribes to a topic
|
|
190
|
+
//
|
|
191
|
+
extern long long int PubSubSubscribe(char* repoPath, char* topic);
|
|
192
|
+
|
|
193
|
+
// PubSubNextMessage gets the next message from a subscription
|
|
194
|
+
//
|
|
195
|
+
extern char* PubSubNextMessage(long long int subID);
|
|
196
|
+
|
|
197
|
+
// PubSubUnsubscribe unsubscribes from a topic
|
|
198
|
+
//
|
|
199
|
+
extern int PubSubUnsubscribe(long long int subID);
|
|
200
|
+
|
|
201
|
+
// PubSubPeers lists peers participating in a topic
|
|
202
|
+
//
|
|
203
|
+
extern char* PubSubPeers(char* repoPath, char* topic);
|
|
204
|
+
|
|
205
|
+
// PubSubCloseRepoSubscriptions closes all active pubsub subscriptions for a specific repository
|
|
206
|
+
//
|
|
207
|
+
extern int PubSubCloseRepoSubscriptions(char* repoPath);
|
|
208
|
+
|
|
209
|
+
// PubSubCloseAllSubscriptions closes all active pubsub subscriptions across all repositories
|
|
210
|
+
//
|
|
211
|
+
extern int PubSubCloseAllSubscriptions();
|
|
212
|
+
|
|
213
|
+
// CreateRepo initializes a new IPFS repository
|
|
214
|
+
//
|
|
215
|
+
extern int CreateRepo(char* repoPath);
|
|
216
|
+
extern int RunNode(char* repoPath);
|
|
217
|
+
|
|
218
|
+
// PubSubEnable enables pubsub on an IPFS node configuration
|
|
219
|
+
//
|
|
220
|
+
extern int PubSubEnable(char* repoPath);
|
|
221
|
+
extern char* TestGetString();
|
|
222
|
+
|
|
223
|
+
// GetNodeID gets the ID of the IPFS node
|
|
224
|
+
//
|
|
225
|
+
extern char* GetNodeID(char* repoPath);
|
|
226
|
+
|
|
227
|
+
// GetNodeMultiAddrs gets the ID of the IPFS node
|
|
228
|
+
//
|
|
229
|
+
extern char* GetNodeMultiAddrs(char* repoPath);
|
|
230
|
+
|
|
231
|
+
// CleanupNode explicitly releases a node by path
|
|
232
|
+
//
|
|
233
|
+
extern int CleanupNode(char* repoPath);
|
|
234
|
+
|
|
235
|
+
#ifdef __cplusplus
|
|
236
|
+
}
|
|
237
|
+
#endif
|
|
Binary file
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Utilities for working with IPFS Content Identifiers (CIDs).
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
# Regular expression for matching CID v0 (Qm...)
|
|
8
|
+
CID_V0_REGEX = re.compile(r"^Qm[1-9A-Za-z]{44}$")
|
|
9
|
+
|
|
10
|
+
# Regular expression for matching CID v1
|
|
11
|
+
CID_V1_REGEX = re.compile(r"^ba[a-zA-Z2-7]{57}$")
|
|
12
|
+
|
|
13
|
+
def is_valid_cid(cid: str) -> bool:
|
|
14
|
+
"""
|
|
15
|
+
Check if a string is a valid IPFS CID.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
cid: The Content Identifier to check.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
bool: True if the CID is valid, False otherwise.
|
|
22
|
+
"""
|
|
23
|
+
if not cid or not isinstance(cid, str):
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
# Check for CID v0 (starts with "Qm" and is 46 characters long)
|
|
27
|
+
if CID_V0_REGEX.match(cid):
|
|
28
|
+
return True
|
|
29
|
+
|
|
30
|
+
# Check for CID v1 (starts with "ba" and is 59 characters long)
|
|
31
|
+
if CID_V1_REGEX.match(cid):
|
|
32
|
+
return True
|
|
33
|
+
|
|
34
|
+
# Could add more sophisticated validation here
|
|
35
|
+
return False
|
|
36
|
+
|
|
37
|
+
def format_cid_link(cid: str, gateway: str = "https://ipfs.io/ipfs/") -> str:
|
|
38
|
+
"""
|
|
39
|
+
Format a CID as a link through an IPFS gateway.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
cid: The Content Identifier.
|
|
43
|
+
gateway: The IPFS gateway URL. Defaults to the public ipfs.io gateway.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
str: The gateway URL for the CID.
|
|
47
|
+
"""
|
|
48
|
+
if not is_valid_cid(cid):
|
|
49
|
+
raise ValueError(f"Invalid CID: {cid}")
|
|
50
|
+
|
|
51
|
+
# Ensure the gateway URL ends with a slash
|
|
52
|
+
if not gateway.endswith("/"):
|
|
53
|
+
gateway += "/"
|
|
54
|
+
|
|
55
|
+
return f"{gateway}{cid}"
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Utilities for working with IPFS peer addresses and connections.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
# Regular expression for matching multiaddress format
|
|
8
|
+
MULTIADDR_REGEX = re.compile(r"^(/[^/]+)+$")
|
|
9
|
+
|
|
10
|
+
# Regular expression for matching peer ID format (with multiaddress)
|
|
11
|
+
PEER_ID_REGEX = re.compile(r"/p2p/([a-zA-Z0-9]+)$")
|
|
12
|
+
|
|
13
|
+
def is_valid_multiaddr(addr: str) -> bool:
|
|
14
|
+
"""
|
|
15
|
+
Check if a string is a valid IPFS multiaddress.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
addr: The multiaddress to check.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
bool: True if the multiaddress is valid, False otherwise.
|
|
22
|
+
"""
|
|
23
|
+
if not addr or not isinstance(addr, str):
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
# Basic check for multiaddress format
|
|
27
|
+
return bool(MULTIADDR_REGEX.match(addr))
|
|
28
|
+
|
|
29
|
+
def extract_peer_id(multiaddr: str) -> str:
|
|
30
|
+
"""
|
|
31
|
+
Extract the peer ID from a multiaddress.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
multiaddr: The multiaddress containing a peer ID.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
str: The peer ID, or an empty string if not found.
|
|
38
|
+
"""
|
|
39
|
+
if not is_valid_multiaddr(multiaddr):
|
|
40
|
+
return ""
|
|
41
|
+
|
|
42
|
+
# Find the peer ID component
|
|
43
|
+
match = PEER_ID_REGEX.search(multiaddr)
|
|
44
|
+
if match:
|
|
45
|
+
return match.group(1)
|
|
46
|
+
|
|
47
|
+
return ""
|
|
48
|
+
|
|
49
|
+
def get_bootstrap_peers() -> list:
|
|
50
|
+
"""
|
|
51
|
+
Get a list of default IPFS bootstrap peers.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
list: A list of multiaddresses for the default bootstrap peers.
|
|
55
|
+
"""
|
|
56
|
+
return [
|
|
57
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
|
58
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
|
|
59
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
|
|
60
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
|
|
61
|
+
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
|
62
|
+
]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|