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