Skip to content

Commit 0f1dde0

Browse files
author
tim
committed
Add multiaddr support
1 parent ab2a1d4 commit 0f1dde0

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Tests
5454

5555
.. code-block:: bash
5656
57-
$ pip install --process-dependency-links -e .[dev]
57+
$ pip install -e .[dev]
5858
$ py.test -v
5959
6060

ipld/ipld.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from copy import deepcopy
2-
from multihash import digest
32

43
from cbor import dumps, loads, Tag
4+
from multiaddr import Multiaddr
5+
from multihash import digest
56

67

78
# NOTE: jbenet plans to register tag 258:
@@ -19,8 +20,13 @@ def transform(di):
1920
di[k] = transform(v)
2021

2122
if LINK_SYMBOL in di:
22-
# TODO: Support: https://github.com/jbenet/js-multiaddr
2323
link = di.pop(LINK_SYMBOL)
24+
25+
try:
26+
link = Multiaddr(link).to_bytes()
27+
except ValueError:
28+
pass
29+
2430
if di:
2531
raise KeyError('Links must not have siblings')
2632
return Tag(LINK_TAG, link)
@@ -35,15 +41,22 @@ def unmarshal(cbor_data):
3541
json_data = loads(cbor_data)
3642

3743
def transform(di):
38-
# NOTE: https://github.com/ipfs/js-ipld/blob/master/src/cbor.js#L81
39-
# Am I right in assuming that this is not supported in IPLD
40-
# anymore, as links cannot have properties themselves?
4144
for k, v in di.items():
42-
# NOTE: Dear Python-experts: Is this safe?
4345
if isinstance(v, Tag):
44-
di[k] = {
45-
LINK_SYMBOL: v.value
46-
}
46+
link = Multiaddr(bytes_addr=v.value)
47+
48+
try:
49+
# the __str__ method of Multiaddr could fail
50+
# if wrong values are passed to it (for example,
51+
# any value that is not a bytes list)
52+
link = str(link)
53+
except:
54+
link = v.value
55+
finally:
56+
di[k] = {
57+
LINK_SYMBOL: link
58+
}
59+
4760
elif isinstance(v, dict):
4861
di[k] = transform(v)
4962
return di

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def find_version(*file_paths):
6464
'cbor==1.0.0',
6565
'base58==0.2.2',
6666
'pymultihash==0.8.2',
67+
'multiaddr==0.0.2',
6768
],
6869
setup_requires=['pytest-runner'],
6970
tests_require=tests_require,

tests/test_ipld.py

+51
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from cbor import dumps, Tag
44
from ipld import LINK_TAG, marshal, multihash, unmarshal
5+
from multiaddr import Multiaddr
56

67

78
def test_transform_dict_to_cbor():
@@ -151,6 +152,56 @@ def test_transform_cbor_with_nested_link_to_dict():
151152
assert unmarshal(src) == expected
152153

153154

155+
def test_transform_dict_to_cbor_with_multiaddr():
156+
addr1 = Multiaddr('/ip4/127.0.0.1/udp/1234')
157+
addr2 = Multiaddr('/ipfs/Qmafmh1Cw3H1bwdYpaaj5AbCW4LkYyUWaM7Nykpn5NZoYL')
158+
159+
src = {
160+
'data': 'hello world',
161+
'size': 11,
162+
'l1': {
163+
'/': str(addr1),
164+
},
165+
'l2': {
166+
'/': str(addr2),
167+
}
168+
}
169+
170+
expected = {
171+
'data': 'hello world',
172+
'size': 11,
173+
'l1': Tag(LINK_TAG, addr1.to_bytes()),
174+
'l2': Tag(LINK_TAG, addr2.to_bytes()),
175+
}
176+
177+
assert marshal(src) == dumps(expected, sort_keys=True)
178+
179+
180+
def test_transform_cbor_to_dict_with_multiaddr():
181+
addr1 = Multiaddr('/ip4/127.0.0.1/udp/1234')
182+
addr2 = Multiaddr('/ipfs/Qmafmh1Cw3H1bwdYpaaj5AbCW4LkYyUWaM7Nykpn5NZoYL')
183+
184+
src = dumps({
185+
'data': 'hello world',
186+
'size': 11,
187+
'l1': Tag(LINK_TAG, addr1.to_bytes()),
188+
'l2': Tag(LINK_TAG, addr2.to_bytes()),
189+
}, sort_keys=True)
190+
191+
expected = {
192+
'data': 'hello world',
193+
'size': 11,
194+
'l1': {
195+
'/': str(addr1),
196+
},
197+
'l2': {
198+
'/': str(addr2),
199+
}
200+
}
201+
202+
assert unmarshal(src) == expected
203+
204+
154205
def test_multihashing_cbor():
155206
src = dumps({
156207
'name': 'hello.txt',

0 commit comments

Comments
 (0)