Skip to content

Commit 5854b76

Browse files
refactor(xrpl): pass wallet as value in authorize channel + unify tests
1 parent 43948b5 commit 5854b76

File tree

2 files changed

+48
-69
lines changed

2 files changed

+48
-69
lines changed

xrpl/wallet/authorize_channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// amount is the amount to redeem, expressed in drops.
1515
//
1616
// Returns the signature or an error if the signature cannot be created.
17-
func AuthorizeChannel(channelID, amount string, wallet *Wallet) (string, error) {
17+
func AuthorizeChannel(channelID, amount string, wallet Wallet) (string, error) {
1818
encodedData, err := binarycodec.EncodeForSigningClaim(map[string]any{
1919
"Channel": channelID,
2020
"Amount": amount,

xrpl/wallet/authorize_channel_test.go

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,119 @@ import (
77
)
88

99
func TestAuthorizeChannel(t *testing.T) {
10-
// secp256k1 wallet
10+
// SECP256K1
1111
secpWallet, err := FromSeed("snGHNrPbHrdUcszeuDEigMdC1Lyyd", "")
1212
require.NoError(t, err)
1313

14-
// ed25519 wallet
14+
// ED25519
1515
edWallet, err := FromSeed("sEdSuqBPSQaood2DmNYVkwWTn1oQTj2", "")
1616
require.NoError(t, err)
1717

18-
channelID := "5DB01B7FFED6B67E6B0414DED11E051D2EE2B7619CE0EAA6286D67A3A4D5BDB3"
19-
amount := "1000000"
18+
validChannelID := "5DB01B7FFED6B67E6B0414DED11E051D2EE2B7619CE0EAA6286D67A3A4D5BDB3"
19+
validAmount := "1000000"
2020

2121
tc := []struct {
2222
name string
23-
wallet *Wallet
23+
wallet Wallet
2424
channelID string
2525
amount string
2626
expectedSig string
27+
expectError bool
2728
}{
2829
{
2930
name: "pass - succeeds with secp256k1 seed",
30-
wallet: &secpWallet,
31-
channelID: channelID,
32-
amount: amount,
31+
wallet: secpWallet,
32+
channelID: validChannelID,
33+
amount: validAmount,
3334
expectedSig: "304402204E7052F33DDAFAAA55C9F5B132A5E50EE95B2CF68C0902F61DFE77299BC893740220353640B951DCD24371C16868B3F91B78D38B6F3FD1E826413CDF891FA8250AAC",
35+
expectError: false,
3436
},
3537
{
3638
name: "pass - succeeds with ed25519 seed",
37-
wallet: &edWallet,
38-
channelID: channelID,
39-
amount: amount,
39+
wallet: edWallet,
40+
channelID: validChannelID,
41+
amount: validAmount,
4042
expectedSig: "7E1C217A3E4B3C107B7A356E665088B4FBA6464C48C58267BEF64975E3375EA338AE22E6714E3F5E734AE33E6B97AAD59058E1E196C1F92346FC1498D0674404",
43+
expectError: false,
4144
},
42-
{
43-
name: "fail - fails with invalid channel ID format",
44-
wallet: &secpWallet,
45-
channelID: "invalid-id",
46-
amount: amount,
47-
expectedSig: "",
48-
},
49-
{
50-
name: "fail - fails with invalid amount format",
51-
wallet: &secpWallet,
52-
channelID: channelID,
53-
amount: "invalid-amount",
54-
expectedSig: "",
55-
},
56-
}
57-
58-
for _, tt := range tc {
59-
t.Run(tt.name, func(t *testing.T) {
60-
signature, err := AuthorizeChannel(tt.channelID, tt.amount, tt.wallet)
61-
if err == nil && tt.expectedSig != "" {
62-
require.NoError(t, err)
63-
require.Equal(t, tt.expectedSig, signature)
64-
} else {
65-
require.Error(t, err)
66-
require.Empty(t, signature)
67-
}
68-
})
69-
}
70-
}
71-
72-
func TestAuthorizeChannel_EdgeCases(t *testing.T) {
73-
// secp256k1 wallet
74-
secpWallet, err := FromSeed("snGHNrPbHrdUcszeuDEigMdC1Lyyd", "")
75-
require.NoError(t, err)
76-
77-
// ed25519 wallet
78-
edWallet, err := FromSeed("sEdSuqBPSQaood2DmNYVkwWTn1oQTj2", "")
79-
require.NoError(t, err)
80-
81-
validChannelID := "5DB01B7FFED6B67E6B0414DED11E051D2EE2B7619CE0EAA6286D67A3A4D5BDB3"
82-
validAmount := "1000000"
83-
84-
tc := []struct {
85-
name string
86-
wallet *Wallet
87-
channelID string
88-
amount string
89-
expectError bool
90-
}{
9145
{
9246
name: "pass - different amounts with secp256k1",
93-
wallet: &secpWallet,
47+
wallet: secpWallet,
9448
channelID: validChannelID,
9549
amount: "5000000",
50+
expectedSig: "304402202DF006FDE665C8A15628991A946629DDD08F7677E75C54619A96E9872BCC615F02206689262B5F102992346E5D84CA4EC73E947906073E4B2873DCDBEE54AFE948C3",
9651
expectError: false,
9752
},
9853
{
9954
name: "pass - different amounts with ed25519",
100-
wallet: &edWallet,
55+
wallet: edWallet,
10156
channelID: validChannelID,
10257
amount: "5000000",
58+
expectedSig: "AEEFCF001061F4E0368805B8A56D116EA8B9E4879A69C5B56A5B7E0F6ABD63E63341D56247192104012BC6AAEA71B1C97E466F47DA0736EFAD462481B165FB0E",
10359
expectError: false,
10460
},
10561
{
10662
name: "pass - zero amount",
107-
wallet: &secpWallet,
63+
wallet: secpWallet,
10864
channelID: validChannelID,
10965
amount: "0",
66+
expectedSig: "3044022069888D92E1F4104FAD7BA66D8DA69278E579FE6EDAF32E87ACF481A6383C4AEB02204E0286429FF9842724627A08EAFD1A8356A6B36994DA6F386D2363C5D3AAFE7C",
11067
expectError: false,
11168
},
69+
{
70+
name: "fail - fails with invalid channel ID format",
71+
wallet: secpWallet,
72+
channelID: "invalid-id",
73+
amount: validAmount,
74+
expectedSig: "",
75+
expectError: true,
76+
},
77+
{
78+
name: "fail - fails with invalid amount format",
79+
wallet: secpWallet,
80+
channelID: validChannelID,
81+
amount: "invalid-amount",
82+
expectedSig: "",
83+
expectError: true,
84+
},
11285
{
11386
name: "fail - empty channel ID",
114-
wallet: &secpWallet,
87+
wallet: secpWallet,
11588
channelID: "",
11689
amount: validAmount,
90+
expectedSig: "",
11791
expectError: true,
11892
},
11993
{
12094
name: "fail - empty amount",
121-
wallet: &secpWallet,
95+
wallet: secpWallet,
12296
channelID: validChannelID,
12397
amount: "",
98+
expectedSig: "",
12499
expectError: true,
125100
},
126101
{
127102
name: "fail - channel ID too short",
128-
wallet: &secpWallet,
103+
wallet: secpWallet,
129104
channelID: "5DB01B7FFED6B67E6B0414DED11E051D2EE2B7619CE0EAA6286D67A3A4D5BDB",
130105
amount: validAmount,
106+
expectedSig: "",
131107
expectError: true,
132108
},
133109
{
134110
name: "fail - channel ID too long",
135-
wallet: &secpWallet,
111+
wallet: secpWallet,
136112
channelID: "5DB01B7FFED6B67E6B0414DED11E051D2EE2B7619CE0EAA6286D67A3A4D5BDB3A",
137113
amount: validAmount,
114+
expectedSig: "",
138115
expectError: true,
139116
},
140117
{
141118
name: "fail - channel ID with invalid hex characters",
142-
wallet: &secpWallet,
119+
wallet: secpWallet,
143120
channelID: "5DB01B7FFED6B67E6B0414DED11E051D2EE2B7619CE0EAA6286D67A3A4D5BDBG",
144121
amount: validAmount,
122+
expectedSig: "",
145123
expectError: true,
146124
},
147125
}
@@ -155,6 +133,7 @@ func TestAuthorizeChannel_EdgeCases(t *testing.T) {
155133
} else {
156134
require.NoError(t, err)
157135
require.NotEmpty(t, signature)
136+
require.Equal(t, tt.expectedSig, signature)
158137
}
159138
})
160139
}

0 commit comments

Comments
 (0)