Skip to content

Commit e1c0826

Browse files
authored
feat: automatically generate peerIDs for HTTP block providers if they are not passed (#117)
* feat: automatically generate peerIDs for HTTP block providers if they are not passed * docs: clarify synthetic peerid generation explain that auto-generated peerids are placeholders for http providers, not used for cryptographic operations
1 parent 5c96165 commit e1c0826

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following emojis are used to highlight certain changes:
1818
### Changed
1919

2020
- Updated to [Go 1.25](https://go.dev/doc/go1.25)
21+
- When `SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS` are configured but no `SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS` are configured, synthetic PeerIDs will now be autogenerated. These are deterministic placeholders derived from endpoint URLs, used only for routing system compatibility with HTTP-based trustless gateways - no libp2p cryptographic operations are performed with these IDs
2122

2223
### Removed
2324

docs/environment-variables.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ Default: none
9191

9292
### `SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS`
9393

94-
Comma-separated list of [multibase-encoded peerIDs](https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#string-representation) to use in synthetic provider records returned for HTTP providers in `SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS`.
94+
Comma-separated list of [multibase-encoded peerIDs](https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#string-representation) to use in synthetic provider records returned for HTTP providers in `SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS`. The order of PeerIDs must match the order of endpoints.
95+
96+
If no peerIDs are passed but provider endpoints are configured, synthetic PeerIDs will be automatically generated. These are deterministic identifiers derived from SHA256 hashes of the endpoint URLs, used solely for routing system compatibility. Since these providers use HTTP trustless gateway protocol rather than libp2p, the PeerIDs are purely synthetic and never participate in any cryptographic operations or peer authentication.
9597

9698
Default: none
9799

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"crypto/sha256"
45
"errors"
56
"fmt"
67
"log"
@@ -12,6 +13,7 @@ import (
1213
"github.com/ipfs/go-cid"
1314
"github.com/libp2p/go-libp2p/core/peer"
1415
"github.com/multiformats/go-multibase"
16+
"github.com/multiformats/go-multihash"
1517
"github.com/urfave/cli/v2"
1618
)
1719

@@ -175,6 +177,27 @@ func main() {
175177
printIfListConfigured("SOMEGUY_PROVIDER_ENDPOINTS = ", cfg.contentEndpoints)
176178
printIfListConfigured("SOMEGUY_PEER_ENDPOINTS = ", cfg.peerEndpoints)
177179
printIfListConfigured("SOMEGUY_IPNS_ENDPOINTS = ", cfg.ipnsEndpoints)
180+
181+
if len(cfg.blockProviderEndpoints) > 0 && len(cfg.blockProviderPeerIDs) == 0 {
182+
fmt.Printf("SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS is set but SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS were not. PeerIDs will be autogenerated.\n")
183+
// Generate synthetic PeerIDs for HTTP block providers. These are deterministic
184+
// identifiers based on endpoint URLs, used solely for routing system compatibility.
185+
// Since HTTP providers use trustless gateway protocol, these PeerIDs are never
186+
// used for cryptographic operations or libp2p authentication.
187+
for i := 0; i < len(cfg.blockProviderEndpoints); i++ {
188+
digest := sha256.Sum256([]byte(cfg.blockProviderEndpoints[i]))
189+
mh, err := multihash.Encode((digest[:]), multihash.SHA2_256)
190+
if err != nil {
191+
return err
192+
}
193+
p, err := peer.IDFromBytes(mh)
194+
if err != nil {
195+
return err
196+
}
197+
cfg.blockProviderPeerIDs = append(cfg.blockProviderPeerIDs, p.String())
198+
}
199+
}
200+
178201
printIfListConfigured("SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS = ", cfg.blockProviderEndpoints)
179202
printIfListConfigured("SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS = ", cfg.blockProviderPeerIDs)
180203

0 commit comments

Comments
 (0)