/
stellar_common.go
111 lines (105 loc) · 3.43 KB
/
stellar_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package client
import (
"fmt"
"os"
"strings"
"github.com/keybase/client/go/terminalescaper"
isatty "github.com/mattn/go-isatty"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/stellar1"
)
func printPayment(g *libkb.GlobalContext, p stellar1.PaymentCLILocal, verbose bool, dui libkb.DumbOutputUI) {
lineUnescaped := func(format string, args ...interface{}) {
dui.PrintfUnescaped(format+"\n", args...)
}
line := func(format string, args ...interface{}) {
dui.Printf(format+"\n", args...)
}
timeStr := p.Time.Time().Format("2006/01/02 15:04")
lineUnescaped("%v", ColorString(g, "bold", timeStr))
amount := fmt.Sprintf("%v XLM", libkb.StellarSimplifyAmount(p.Amount))
if !p.Asset.IsNativeXLM() {
amount = fmt.Sprintf("%v %v/%v", p.Amount, p.Asset.Code, p.Asset.Issuer)
}
if p.DisplayAmount != nil && p.DisplayCurrency != nil && len(*p.DisplayAmount) > 0 && len(*p.DisplayAmount) > 0 {
amount = fmt.Sprintf("%v %v (%v)", *p.DisplayAmount, *p.DisplayCurrency, amount)
}
lineUnescaped("%v", ColorString(g, "green", amount))
// Show sender and recipient. Prefer keybase form, fall back to stellar abbreviations.
var showedAbbreviation bool
var from string
switch {
case p.FromUsername != nil:
from = *p.FromUsername
default:
from = p.FromStellar.LossyAbbreviation()
showedAbbreviation = true
}
var to string
switch {
case p.ToUsername != nil && p.ToAssertion != nil && (*p.ToUsername != *p.ToAssertion):
to = fmt.Sprintf("%s (%q)", *p.ToUsername, *p.ToAssertion)
case p.ToUsername != nil:
to = *p.ToUsername
case p.ToStellar != nil:
to = p.ToStellar.LossyAbbreviation()
showedAbbreviation = true
case p.ToAssertion != nil:
to = fmt.Sprintf("%q", *p.ToAssertion)
default:
// This should never happen
lineUnescaped("%v", ColorString(g, "red", "missing recipient info"))
}
line("%v -> %v", from, to)
// If an abbreviation was shown, show the full addresses
if showedAbbreviation || verbose {
line("From: %v", p.FromStellar.String())
if p.ToStellar != nil {
line("To: %v", p.ToStellar.String())
} else {
lineUnescaped("To: %v", ColorString(g, "yellow", "unclaimed"))
}
}
if g.Env.GetDisplayRawUntrustedOutput() || !isatty.IsTerminal(os.Stdout.Fd()) {
if len(p.Note) > 0 {
lineUnescaped("Note: %v", ColorString(g, "yellow", printPaymentFilterNote(p.Note)))
}
if len(p.NoteErr) > 0 {
lineUnescaped("Note Error: %v", ColorString(g, "red", p.NoteErr))
}
} else {
if len(p.Note) > 0 {
lineUnescaped("Note: %v", ColorString(g, "yellow", printPaymentFilterNote(terminalescaper.Clean(p.Note))))
}
if len(p.NoteErr) > 0 {
lineUnescaped("Note Error: %v", ColorString(g, "red", terminalescaper.Clean(p.NoteErr)))
}
}
if verbose {
line("Transaction Hash: %v", p.TxID)
}
switch {
case p.Status == "":
case cicmp(p.Status, "completed"):
default:
color := "red"
if cicmp(p.Status, "claimable") {
color = "yellow"
}
lineUnescaped("Status: %v", ColorString(g, color, p.Status))
lineUnescaped(" %v", ColorString(g, color, p.StatusDetail))
}
}
// printPaymentFilterNote: Pare down the note so that it's less likely to contain tricks.
// Such as newlines and fake transactions.
// Shows only the first line.
func printPaymentFilterNote(note string) string {
lines := strings.Split(strings.TrimSpace(note), "\n")
if len(lines) < 1 {
return ""
}
return strings.TrimSpace(lines[0])
}
func cicmp(a, b string) bool {
return strings.ToLower(a) == strings.ToLower(b)
}