cosmos-sdk/client/common.go

58 lines
1.8 KiB
Go
Raw Normal View History

2017-09-05 09:01:24 -07:00
package client
import (
"errors"
"github.com/tendermint/light-client/certifiers"
certclient "github.com/tendermint/light-client/certifiers/client"
2017-10-20 11:05:07 -07:00
certerr "github.com/tendermint/light-client/certifiers/errors"
2017-09-05 09:01:24 -07:00
"github.com/tendermint/light-client/certifiers/files"
2017-10-20 11:05:07 -07:00
"github.com/tendermint/light-client/proofs"
2017-09-05 09:01:24 -07:00
rpcclient "github.com/tendermint/tendermint/rpc/client"
)
// GetNode prepares a simple rpc.Client for the given endpoint
func GetNode(url string) rpcclient.Client {
return rpcclient.NewHTTP(url, "/websocket")
}
// GetRPCProvider retuns a certifier compatible data source using
// tendermint RPC
func GetRPCProvider(url string) certifiers.Provider {
2017-10-20 11:05:07 -07:00
return certclient.NewHTTPProvider(url)
2017-09-05 09:01:24 -07:00
}
// GetLocalProvider returns a reference to a file store of headers
// wrapped with an in-memory cache
func GetLocalProvider(dir string) certifiers.Provider {
return certifiers.NewCacheProvider(
certifiers.NewMemStoreProvider(),
files.NewProvider(dir),
)
}
// GetCertifier initializes an inquiring certifier given a fixed chainID
// and a local source of trusted data with at least one seed
func GetCertifier(chainID string, trust certifiers.Provider,
2017-10-20 11:05:07 -07:00
source certifiers.Provider) (*certifiers.Inquiring, error) {
2017-09-05 09:01:24 -07:00
2017-10-23 10:24:22 -07:00
// this gets the most recent verified commit
fc, err := trust.LatestCommit()
2017-10-20 11:05:07 -07:00
if certerr.IsCommitNotFoundErr(err) {
2017-09-05 09:01:24 -07:00
return nil, errors.New("Please run init first to establish a root of trust")
}
if err != nil {
return nil, err
}
2017-10-23 10:24:22 -07:00
cert := certifiers.NewInquiring(chainID, fc, trust, source)
2017-09-05 09:01:24 -07:00
return cert, nil
}
2017-09-05 09:50:57 -07:00
2017-09-05 10:07:34 -07:00
// SecureClient uses a given certifier to wrap an connection to an untrusted
2017-09-05 09:50:57 -07:00
// host and return a cryptographically secure rpc client.
2017-10-20 11:05:07 -07:00
func SecureClient(c rpcclient.Client, cert *certifiers.Inquiring) rpcclient.Client {
return proofs.Wrap(c, cert)
2017-09-05 09:50:57 -07:00
}