github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lncore/peers.go (about)

     1  package lncore
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mit-dci/lit/bech32"
     7  )
     8  
     9  // LnAddr is just a bech32-encoded pubkey.
    10  // TODO Move this to another package so it's more obviously not *just* IO-related.
    11  type LnAddr string
    12  
    13  // ParseLnAddr will verify that the string passed is a valid LN address, as in
    14  // ln1pmclh89haeswrw0unf8awuyqeu4t2uell58nea.
    15  func ParseLnAddr(m string) (LnAddr, error) {
    16  
    17  	prefix, raw, err := bech32.Decode(m)
    18  
    19  	// Check it's valid bech32.
    20  	if err != nil {
    21  		return "", err
    22  	}
    23  
    24  	// Check it has the right prefix.
    25  	if prefix != "ln" {
    26  		return "", fmt.Errorf("prefix is not 'ln'")
    27  	}
    28  
    29  	// Check the length of the content bytes is right.
    30  	if len(raw) > 20 {
    31  		return "", fmt.Errorf("address too long to be pubkey")
    32  	}
    33  
    34  	return LnAddr(m), nil // should be the only place we cast to this type
    35  
    36  }
    37  
    38  // ToString returns the LnAddr as a string.  Right now it just unwraps it but it
    39  // might do something more eventually.
    40  func (lnaddr LnAddr) ToString() string {
    41  	return string(lnaddr)
    42  }
    43  
    44  // LitPeerStorage is storage for peer data.
    45  type LitPeerStorage interface {
    46  	GetPeerAddrs() ([]LnAddr, error)
    47  	GetPeerInfo(addr LnAddr) (*PeerInfo, error)
    48  	GetPeerInfos() (map[LnAddr]PeerInfo, error)
    49  	AddPeer(addr LnAddr, pi PeerInfo) error
    50  	UpdatePeer(addr LnAddr, pi *PeerInfo) error
    51  	DeletePeer(addr LnAddr) error
    52  
    53  	// TEMP Until we figure this all out.
    54  	GetUniquePeerIdx() (uint32, error)
    55  }
    56  
    57  // PeerInfo .
    58  type PeerInfo struct {
    59  	LnAddr   *LnAddr `json:"lnaddr"`
    60  	Nickname *string `json:"name"`
    61  	NetAddr  *string `json:"netaddr"` // ip address, port, I guess
    62  
    63  	// TEMP This is again, for adapting to the old system.
    64  	PeerIdx uint32 `json:"hint_peeridx"`
    65  }