You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

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