MultiAddress support

This commit is contained in:
Tanguy 2023-10-11 11:35:00 +02:00
parent 75871817ee
commit 6ab779d30a
No known key found for this signature in database
GPG Key ID: 7DD8EC6B6CE6C45E
3 changed files with 57 additions and 5 deletions

View File

@ -297,6 +297,33 @@ proc dnsVB(vb: var VBuffer): bool =
if s.find('/') == -1:
result = true
proc certHashStB(s: string, vb: var VBuffer): bool =
## CertHash address stringToBuffer() implementation.
var data = MultiBase.decode(s).valueOr:
return false
var mh: MultiHash
if MultiHash.decode(data, mh).isOk:
vb.writeSeq(data)
result = true
proc certHashBtS(vb: var VBuffer, s: var string): bool =
## CertHash address bufferToString() implementation.
var address = newSeq[byte]()
if vb.readSeq(address) > 0:
var mh: MultiHash
if MultiHash.decode(address, mh).isOk:
s = MultiBase.encode("base64", address).valueOr:
return false
result = true
proc certHashVB(vb: var VBuffer): bool =
## CertHash address validateBuffer() implementation.
var address = newSeq[byte]()
if vb.readSeq(address) > 0:
var mh: MultiHash
if MultiHash.decode(address, mh).isOk:
result = true
proc mapEq*(codec: string): MaPattern =
## ``Equal`` operator for pattern
result.operator = Eq
@ -358,6 +385,11 @@ const
bufferToString: dnsBtS,
validateBuffer: dnsVB
)
TranscoderCertHash* = Transcoder(
stringToBuffer: certHashStB,
bufferToString: certHashBtS,
validateBuffer: certHashVB
)
ProtocolsList = [
MAProtocol(
mcodec: multiCodec("ip4"), kind: Fixed, size: 4,
@ -458,7 +490,17 @@ const
),
MAProtocol(
mcodec: multiCodec("p2p-webrtc-direct"), kind: Marker, size: 0
)
),
MAProtocol(
mcodec: multiCodec("webrtc"), kind: Marker, size: 0
),
MAProtocol(
mcodec: multiCodec("webrtc-direct"), kind: Marker, size: 0
),
MAProtocol(
mcodec: multiCodec("certhash"), kind: Length, size: 0,
coder: TranscoderCertHash
),
]
DNSANY* = mapEq("dns")
@ -489,6 +531,7 @@ const
WebSockets_DNS* = mapOr(WS_DNS, WSS_DNS)
WebSockets_IP* = mapOr(WS_IP, WSS_IP)
WebSockets* = mapOr(WS, WSS)
WebRtcDirect2* = mapAnd(UDP, mapEq("webrtc-direct"), mapEq("certhash"))
Onion3* = mapEq("onion3")
TcpOnion3* = mapAnd(TCP, Onion3)
@ -512,7 +555,7 @@ const
mapAnd(DNS, mapEq("https"))
)
WebRTCDirect* = mapOr(
WebRTCDirect* {.deprecated.} = mapOr(
mapAnd(HTTP, mapEq("p2p-webrtc-direct")),
mapAnd(HTTPS, mapEq("p2p-webrtc-direct"))
)

View File

@ -193,11 +193,14 @@ const MultiCodecList = [
("https", 0x01BB),
("tls", 0x01C0),
("quic", 0x01CC),
("certhash", 0x01D2),
("ws", 0x01DD),
("wss", 0x01DE),
("p2p-websocket-star", 0x01DF), # not in multicodec list
("p2p-webrtc-star", 0x0113), # not in multicodec list
("p2p-webrtc-direct", 0x0114), # not in multicodec list
("webrtc-direct", 0x0118),
("webrtc", 0x0119),
("onion", 0x01BC),
("onion3", 0x01BD),
("p2p-circuit", 0x0122),

View File

@ -147,7 +147,9 @@ const
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/p2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/tcp/9090/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"
"/ip4/127.0.0.1/tcp/9090/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/udp/1234/webrtc-direct",
"/ip4/127.0.0.1/udp/1234/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
]
RustSuccessExpects = [
@ -177,7 +179,9 @@ const
"047F000001A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B0604D2",
"29200108A07AC542013AC986FFFE317095061F40DD03A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
"9302047F000001062382DD03A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
"047F000001062382A202A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B"
"047F000001062382A202A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
"047F000001910204D29802",
"047F000001910204D29802D203221220C3AB8FF13720E8AD9047DD39466B3C8974E592C2FA383D4A3960714CAEF0C4F2"
]
RustFailureVectors = [
@ -211,7 +215,9 @@ const
"/ip4/127.0.0.1/tcp",
"/ip4/127.0.0.1/ipfs",
"/ip4/127.0.0.1/ipfs/tcp",
"/p2p-circuit/50"
"/p2p-circuit/50",
"/ip4/127.0.0.1/udp/1234/webrtc-direct/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp",
"/ip4/127.0.0.1/udp/1234/webrtc-direct/certhash"
]
PathVectors = [