Beispiel #1
0
// New returns a new client. In most cases, use mute/serviceguard/client/trivial instead
func New(keyBackends []types.Backend, walletstore WalletStore, walletKey *[ed25519.PrivateKeySize]byte, cacert []byte) (*Client, error) {
	var err error
	c := new(Client)
	c.packetClient, err = packetproto.New(keyBackends)
	if err != nil {
		return nil, err
	}
	err = c.packetClient.Keypool.Load()
	if err != nil {
		return nil, err
	}
	c.walletStore = walletstore
	c.cacert = cacert
	c.walletKey = walletKey
	c.stopChan = make(chan bool)
	pubkey, privkey := splitKey(c.walletKey)
	c.walletRPC = walletrpc.New(pubkey, privkey, c.cacert)
	return c, nil
}
Beispiel #2
0
// getTokenFromWallet gets a single token for usage from Wallet.
func (c *Client) getTokenFromWallet(usage string) (token, params, pubkeyUsed []byte, err error) {
	var tries int
	if !c.IsOnline() {
		c.LastError = ErrOffline
		return nil, nil, nil, ErrOffline
	}
	onlineGroup.Add(1)
	defer onlineGroup.Done()
	pubkey, privkey := splitKey(c.walletKey)
	if c.walletRPC == nil {
		c.walletRPC = walletrpc.New(pubkey, privkey, c.cacert)
	}
	// lookup cached authtoken, set
	c.walletRPC.LastAuthToken, tries = c.walletStore.GetAuthToken()
	if tries > AuthTokenRetry {
		c.walletRPC.LastAuthToken = nil
		tries = 0
	}
	newToken, params, pubkeyUsed, err := c.walletRPC.GetToken(usage)
	if err != nil {
		c.LastError = err
		_, fatal, err := lookupError(err)
		if fatal {
			c.LastError = err
			return nil, nil, nil, ErrFinal
		}
		// cache walletClient.LastAuthToken
		err = c.walletStore.SetAuthToken(c.walletRPC.LastAuthToken, tries+1)
		if err != nil {
			c.LastError = err
			return nil, nil, nil, ErrFatal
		}
		return nil, nil, nil, ErrRetry
	}
	// Reset authtoken cache
	c.walletStore.SetAuthToken(nil, 0)
	return newToken, params, pubkeyUsed, nil
}