github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/stellar/stellarcommon/common.go (about)

     1  package stellarcommon
     2  
     3  import (
     4  	"encoding/base64"
     5  	"errors"
     6  	"strconv"
     7  
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/keybase1"
    10  	"github.com/keybase/stellarnet"
    11  )
    12  
    13  type RecipientInput string
    14  
    15  type Recipient struct {
    16  	Input RecipientInput
    17  	// These 5 fields are nullable.
    18  	User      *User
    19  	Assertion *keybase1.SocialAssertion
    20  	// Recipient may not have a stellar wallet ready to receive
    21  	AccountID *stellarnet.AddressStr // User entered G... OR target has receiving address
    22  
    23  	// federation address lookups can return a memo
    24  	PublicMemo     *string
    25  	PublicMemoType *string
    26  }
    27  
    28  func (r Recipient) HasMemo() bool {
    29  	return r.PublicMemo != nil && r.PublicMemoType != nil
    30  }
    31  
    32  func (r Recipient) Memo() (*stellarnet.Memo, error) {
    33  	if !r.HasMemo() {
    34  		return nil, nil
    35  	}
    36  
    37  	switch *r.PublicMemoType {
    38  	case "text":
    39  		return stellarnet.NewMemoText(*r.PublicMemo), nil
    40  	case "hash":
    41  		data, err := base64.StdEncoding.DecodeString(*r.PublicMemo)
    42  		if err != nil {
    43  			return nil, errors.New("invalid federation memo hash")
    44  		}
    45  		if len(data) != 32 {
    46  			return nil, errors.New("invalid federation memo hash")
    47  		}
    48  		var hash stellarnet.MemoHash
    49  		copy(hash[:], data)
    50  		return stellarnet.NewMemoHash(hash), nil
    51  	case "id":
    52  		id, err := strconv.ParseUint(*r.PublicMemo, 10, 64)
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  		return stellarnet.NewMemoID(id), nil
    57  	default:
    58  		return nil, errors.New("invalid federation memo type")
    59  	}
    60  
    61  }
    62  
    63  type User struct {
    64  	UV       keybase1.UserVersion
    65  	Username libkb.NormalizedUsername
    66  }