Skip to content

Commit e6aa92d

Browse files
authored
Parse ICS tx (keybase#14760)
Parse ICS tx (keybase#14760)
1 parent 0b23844 commit e6aa92d

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

‎go/chat/wallet/parser.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package wallet
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
var txPattern = regexp.MustCompile(
9+
// Must explicitly have a + in front
10+
`\+` +
11+
// Stellar decimal amount
12+
`(\d+\.?\d*|\d*\.?\d+)` +
13+
// Currency code
14+
`([A-Za-z]{2,6})` +
15+
// At sign and username, optional for direct messages
16+
// If not used, must be followed by a non-tx-character or end of string
17+
`(?:(?:@((?:[a-zA-Z0-9]+_?)+))|(?:[^A-Za-z@]|\z))`,
18+
)
19+
20+
var maxAmountLength = 100
21+
var maxUsernameLength = 16
22+
var maxTxsPerMessage = -1
23+
24+
type ChatTxCandidate struct {
25+
Amount string
26+
CurrencyCode string
27+
Username *string
28+
}
29+
30+
func findChatTxCandidates(xs string) []ChatTxCandidate {
31+
// A string that does not appear in the candidate regex so we don't get false positives from concatenations.
32+
replacer := "$"
33+
replaced := replaceQuotedSubstrings(xs, replacer)
34+
35+
rawMatches := txPattern.FindAllStringSubmatch(replaced, maxTxsPerMessage)
36+
matches := make([]ChatTxCandidate, 0, len(rawMatches))
37+
for _, rawMatch := range rawMatches {
38+
amount := rawMatch[1]
39+
currencyCode := rawMatch[2]
40+
username := rawMatch[3]
41+
if len(amount) <= maxAmountLength && len(username) <= maxUsernameLength {
42+
var txUsername *string
43+
if username == "" {
44+
txUsername = nil
45+
} else {
46+
txUsername = &username
47+
}
48+
matches = append(matches, ChatTxCandidate{Amount: amount, CurrencyCode: currencyCode, Username: txUsername})
49+
}
50+
}
51+
return matches
52+
}
53+
54+
var startQuote = ">"
55+
var newline = []rune("\n")
56+
57+
func replaceQuotedSubstrings(xs string, replacer string) string {
58+
xs = regexp.MustCompile("(?s)```.*?```").ReplaceAllString(xs, replacer)
59+
xs = regexp.MustCompile("(?s)`.*?`").ReplaceAllString(xs, replacer)
60+
61+
// Remove all quoted lines. Because we removed all codeblocks
62+
// before, we only need to consider single lines.
63+
var ret []string
64+
for _, line := range strings.Split(xs, string(newline)) {
65+
if !strings.HasPrefix(strings.TrimLeft(line, " "), startQuote) {
66+
ret = append(ret, line)
67+
}
68+
}
69+
return strings.Join(ret, string(newline))
70+
}

‎go/chat/wallet/parser_test.go‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package wallet
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
type candidateTestCase struct {
10+
in string
11+
out []ChatTxCandidate
12+
}
13+
14+
func TestFindCandidates(t *testing.T) {
15+
alfa := "alfa"
16+
bravo := "bravo"
17+
charlie := "charlie"
18+
alfaTx := ChatTxCandidate{Amount: "124.005", CurrencyCode: "XLM", Username: &alfa}
19+
bravoTx := ChatTxCandidate{Amount: ".005", CurrencyCode: "USD", Username: &bravo}
20+
charlieTx := ChatTxCandidate{Amount: "5.", CurrencyCode: "HKD", Username: &charlie}
21+
anonTx := ChatTxCandidate{Amount: "25", CurrencyCode: "eur", Username: nil}
22+
testCases := []candidateTestCase{
23+
candidateTestCase{"+124.005XLM@alfa", []ChatTxCandidate{alfaTx}},
24+
candidateTestCase{" +124.005XLM@alfa ", []ChatTxCandidate{alfaTx}},
25+
candidateTestCase{" +124.005XLM@alfa", []ChatTxCandidate{alfaTx}},
26+
candidateTestCase{"+124.005XLM@alfa ", []ChatTxCandidate{alfaTx}},
27+
candidateTestCase{" `+124.`005XLM@alfa ", []ChatTxCandidate{}},
28+
candidateTestCase{" `+124.005XLM@alfa` ", []ChatTxCandidate{}},
29+
candidateTestCase{" ``` `+124.005XLM@alfa``` ", []ChatTxCandidate{}},
30+
candidateTestCase{" ``` `+124.005XLM@alfa`` ", []ChatTxCandidate{alfaTx}},
31+
candidateTestCase{" ``` `+124.005XLM@alfa`` ", []ChatTxCandidate{alfaTx}},
32+
candidateTestCase{" +124.005XLM@alfa\n+.005USD@bravo \n+5.HKD@charlie", []ChatTxCandidate{alfaTx, bravoTx, charlieTx}},
33+
candidateTestCase{" +124.005XLM@alfa\n-.005USD@bravo \n 5.HKD@charlie", []ChatTxCandidate{alfaTx}},
34+
candidateTestCase{" +124.005X@alfa ", []ChatTxCandidate{}},
35+
candidateTestCase{" +124.005XLM@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ", []ChatTxCandidate{}},
36+
candidateTestCase{" +.XLM@alfa ", []ChatTxCandidate{}},
37+
candidateTestCase{" +XLM@alfa ", []ChatTxCandidate{}},
38+
candidateTestCase{" +XLM+XLM@alfa ", []ChatTxCandidate{}},
39+
candidateTestCase{" +124.005XLM@alfa+.005USD@bravo ", []ChatTxCandidate{alfaTx, bravoTx}},
40+
candidateTestCase{" +124.005XLM@alfa +.005USD@bravo ", []ChatTxCandidate{alfaTx, bravoTx}},
41+
candidateTestCase{"+124.005XLM@alfa +.005USD@bravo", []ChatTxCandidate{alfaTx, bravoTx}},
42+
candidateTestCase{"(+124.005XLM@alfa, +.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
43+
candidateTestCase{"(+124.005XLM@alfa,+.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
44+
candidateTestCase{"(+124.005XLM@alfa$+.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
45+
candidateTestCase{"(+124.005XLM@alfa@+.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
46+
47+
// direct message txs
48+
candidateTestCase{"thanks friend, +25eur!", []ChatTxCandidate{anonTx}},
49+
candidateTestCase{"thanks friend, +25eur for you!", []ChatTxCandidate{anonTx}},
50+
candidateTestCase{"thanks friend,+25eur for you!", []ChatTxCandidate{anonTx}},
51+
candidateTestCase{"thanks friend,+25eur@ for you!", []ChatTxCandidate{}},
52+
candidateTestCase{"thanks friend,+25eur", []ChatTxCandidate{anonTx}},
53+
candidateTestCase{"thanks friend,+25eur\nnewline", []ChatTxCandidate{anonTx}},
54+
candidateTestCase{"thanks friend,+25eur\ttabbed", []ChatTxCandidate{anonTx}},
55+
}
56+
57+
for _, testCase := range testCases {
58+
ret := findChatTxCandidates(testCase.in)
59+
require.Equal(t, testCase.out, ret, testCase.in)
60+
}
61+
62+
require.True(t, true)
63+
}

‎go/protocol/chat1/wallet.go‎

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)