github.com/devwanda/aphelion-staking@v0.33.9/lite2/setup.go (about)

     1  package lite
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/devwanda/aphelion-staking/lite2/provider"
     7  	"github.com/devwanda/aphelion-staking/lite2/provider/http"
     8  	"github.com/devwanda/aphelion-staking/lite2/store"
     9  )
    10  
    11  // NewHTTPClient initiates an instance of a lite client using HTTP addresses
    12  // for both the primary provider and witnesses of the lite client. A trusted
    13  // header and hash must be passed to initialize the client.
    14  //
    15  // See all Option(s) for the additional configuration.
    16  // See NewClient.
    17  func NewHTTPClient(
    18  	chainID string,
    19  	trustOptions TrustOptions,
    20  	primaryAddress string,
    21  	witnessesAddresses []string,
    22  	trustedStore store.Store,
    23  	options ...Option) (*Client, error) {
    24  
    25  	providers, err := providersFromAddresses(append(witnessesAddresses, primaryAddress), chainID)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	return NewClient(
    31  		chainID,
    32  		trustOptions,
    33  		providers[len(providers)-1],
    34  		providers[:len(providers)-1],
    35  		trustedStore,
    36  		options...)
    37  }
    38  
    39  // NewHTTPClientFromTrustedStore initiates an instance of a lite client using
    40  // HTTP addresses for both the primary provider and witnesses and uses a
    41  // trusted store as the root of trust.
    42  //
    43  // See all Option(s) for the additional configuration.
    44  // See NewClientFromTrustedStore.
    45  func NewHTTPClientFromTrustedStore(
    46  	chainID string,
    47  	trustingPeriod time.Duration,
    48  	primaryAddress string,
    49  	witnessesAddresses []string,
    50  	trustedStore store.Store,
    51  	options ...Option) (*Client, error) {
    52  
    53  	providers, err := providersFromAddresses(append(witnessesAddresses, primaryAddress), chainID)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	return NewClientFromTrustedStore(
    59  		chainID,
    60  		trustingPeriod,
    61  		providers[len(providers)-1],
    62  		providers[:len(providers)-1],
    63  		trustedStore,
    64  		options...)
    65  }
    66  
    67  func providersFromAddresses(addrs []string, chainID string) ([]provider.Provider, error) {
    68  	providers := make([]provider.Provider, len(addrs))
    69  	for idx, address := range addrs {
    70  		p, err := http.New(chainID, address)
    71  		if err != nil {
    72  			return nil, err
    73  		}
    74  		providers[idx] = p
    75  	}
    76  	return providers, nil
    77  }