make addrComponentForCert a pure function

This commit is contained in:
Marten Seemann 2022-09-07 10:14:22 +03:00
parent c783884fb1
commit ef657d6d7c
2 changed files with 14 additions and 15 deletions

View File

@ -11,7 +11,6 @@ import (
"github.com/benbjohnson/clock"
ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
)
@ -147,12 +146,12 @@ func (m *certManager) Verify(hashes []multihash.DecodedMultihash) error {
}
func (m *certManager) cacheAddrComponent() error {
addr, err := m.addrComponentForCert(m.currentConfig.sha256[:])
addr, err := addrComponentForCert(m.currentConfig.sha256[:])
if err != nil {
return err
}
if m.nextConfig != nil {
comp, err := m.addrComponentForCert(m.nextConfig.sha256[:])
comp, err := addrComponentForCert(m.nextConfig.sha256[:])
if err != nil {
return err
}
@ -162,18 +161,6 @@ func (m *certManager) cacheAddrComponent() error {
return nil
}
func (m *certManager) addrComponentForCert(hash []byte) (ma.Multiaddr, error) {
mh, err := multihash.Encode(hash, multihash.SHA2_256)
if err != nil {
return nil, err
}
certStr, err := multibase.Encode(multibase.Base58BTC, mh)
if err != nil {
return nil, err
}
return ma.NewComponent(ma.ProtocolWithCode(ma.P_CERTHASH).Name, certStr)
}
func (m *certManager) Close() error {
m.ctxCancel()
m.refCount.Wait()

View File

@ -66,3 +66,15 @@ func extractCertHashes(addr ma.Multiaddr) ([]multihash.DecodedMultihash, error)
}
return certHashes, nil
}
func addrComponentForCert(hash []byte) (ma.Multiaddr, error) {
mh, err := multihash.Encode(hash, multihash.SHA2_256)
if err != nil {
return nil, err
}
certStr, err := multibase.Encode(multibase.Base58BTC, mh)
if err != nil {
return nil, err
}
return ma.NewComponent(ma.ProtocolWithCode(ma.P_CERTHASH).Name, certStr)
}