Skip to content

Commit

Permalink
stakepooldclient: add DefaultAccountPubKey and WalletsHavePubkey methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Jul 31, 2019
1 parent 0ddd235 commit fb2a7ac
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion stakepooldclient/stakepooldclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"golang.org/x/net/context"
)

var requiredStakepooldAPI = semver{major: 5, minor: 0, patch: 0}
var requiredStakepooldAPI = semver{major: 6, minor: 0, patch: 0}

type StakepooldManager struct {
grpcConnections []*grpc.ClientConn
Expand Down Expand Up @@ -498,3 +498,69 @@ func (s *StakepooldManager) RPCStatus() []string {

return stakepooldPageInfo
}

// comaprePubkeys runs the gRPC command WalletMasterPubKeys on all
// stakepoold instances, performs fn on all key, value pairs of received
// maps, and returns whether any are true. Returns an error if any
// WalletMasterPubKeys commands fail.
func (s *StakepooldManager) comparePubKeys(fn func(string, string) bool) (bool, error) {
for i, conn := range s.grpcConnections {
client := pb.NewStakepooldServiceClient(conn)
resp, err := client.WalletMasterPubKeys(context.Background(), &pb.WalletMasterPubKeysRequest{})
if err != nil {
return false, fmt.Errorf("GetMasterPubkeys gRPC failed on stakepoold instance %d: %v", i, err)
}
for _, v := range resp.MasterPubKeys {
if fn(v.Account, v.PubKey) {
return true, nil
}
}
}
return false, nil
}

// WalletsHavePubKey checks whether pubkey exists on any stakepoold
// wallets. Returns an error if comparePubKeys fails.
func (s *StakepooldManager) WalletsHavePubKey(pubkey string) (bool, error) {
hasPubkeyFn := func(_ string, v string) bool {
return v == pubkey
}
hasPubKey, err := s.comparePubKeys(hasPubkeyFn)
if err != nil {
return false, err
}
return hasPubKey, nil
}

// DefaultAccountPubKey gets the master extended public key for the
// "default" account. Returns an error if pubkeys for the "default"
// account are not the same accross all stakepoold wallets or
// comparePubKeys fails.
func (s *StakepooldManager) DefaultAccountPubKey() (string, error) {
var pubkey string
keysDontMatchFn := func(k string, v string) bool {
log.Debugf("found master extended pubkey %s for account \"%s\"", v, k)
// Ignore other accounts.
if k != "default" {
return false
}
// The first pass sets pubkey.
if pubkey == "" {
pubkey = v
return false
}
// All other passes must equal.
if pubkey != v {
return true
}
return false
}
mismatch, err := s.comparePubKeys(keysDontMatchFn)
if err != nil {
return "", err
}
if mismatch {
return "", errors.New("conflicting extended master public keys were found for the \"default\" account")
}
return pubkey, nil
}

0 comments on commit fb2a7ac

Please sign in to comment.