Skip to content

Commit 2a732a4

Browse files
committed
loop: add initiator string to user agent
1 parent e6e533a commit 2a732a4

File tree

8 files changed

+43
-21
lines changed

8 files changed

+43
-21
lines changed

client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
MaxPrepayAmount: 100,
3535
MaxPrepayRoutingFee: 75000,
3636
MaxSwapRoutingFee: 70000,
37+
Initiator: "test",
3738
}
3839

3940
swapInvoiceDesc = "swap"

interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ type OutRequest struct {
8181

8282
// Label contains an optional label for the swap.
8383
Label string
84+
85+
// Initiator is an optional string that identifies what software
86+
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
87+
// appended to the user agent string.
88+
Initiator string
8489
}
8590

8691
// Out contains the full details of a loop out request. This includes things
@@ -196,6 +201,11 @@ type LoopInRequest struct {
196201

197202
// Label contains an optional label for the swap.
198203
Label string
204+
205+
// Initiator is an optional string that identifies what software
206+
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
207+
// appended to the user agent string.
208+
Initiator string
199209
}
200210

201211
// LoopInTerms are the server terms on which it executes loop in swaps.

loopin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
166166
log.Infof("Initiating swap request at height %v", currentHeight)
167167
swapResp, err := cfg.server.NewLoopInSwap(globalCtx, swapHash,
168168
request.Amount, senderKey, swapInvoice, probeInvoice,
169-
request.LastHop,
169+
request.LastHop, request.Initiator,
170170
)
171171
probeWaitCancel()
172172
if err != nil {

loopin_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
Amount: btcutil.Amount(50000),
2222
MaxSwapFee: btcutil.Amount(1000),
2323
HtlcConfTarget: 2,
24+
Initiator: "test",
2425
}
2526
)
2627

loopout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
114114
// latest swap publication time.
115115
swapResp, err := cfg.server.NewLoopOutSwap(
116116
globalCtx, swapHash, request.Amount, request.Expiry,
117-
receiverKey, request.SwapPublicationDeadline,
117+
receiverKey, request.SwapPublicationDeadline, request.Initiator,
118118
)
119119
if err != nil {
120120
return nil, fmt.Errorf("cannot initiate swap: %v", err)

server_mock_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ func newServerMock(lnd *test.LndMockServices) *serverMock {
6464
}
6565
}
6666

67-
func (s *serverMock) NewLoopOutSwap(ctx context.Context,
68-
swapHash lntypes.Hash, amount btcutil.Amount, expiry int32,
69-
receiverKey [33]byte, _ time.Time) (
70-
*newLoopOutResponse, error) {
67+
func (s *serverMock) NewLoopOutSwap(_ context.Context, swapHash lntypes.Hash,
68+
amount btcutil.Amount, _ int32, _ [33]byte, _ time.Time,
69+
_ string) (*newLoopOutResponse, error) {
7170

7271
_, senderKey := test.CreateKey(100)
7372

@@ -138,10 +137,9 @@ func getInvoice(hash lntypes.Hash, amt btcutil.Amount, memo string) (string, err
138137
return reqString, nil
139138
}
140139

141-
func (s *serverMock) NewLoopInSwap(ctx context.Context,
142-
swapHash lntypes.Hash, amount btcutil.Amount,
143-
senderKey [33]byte, swapInvoice, probeInvoice string,
144-
lastHop *route.Vertex) (*newLoopInResponse, error) {
140+
func (s *serverMock) NewLoopInSwap(_ context.Context, swapHash lntypes.Hash,
141+
amount btcutil.Amount, _ [33]byte, swapInvoice, _ string,
142+
_ *route.Vertex, _ string) (*newLoopInResponse, error) {
145143

146144
_, receiverKey := test.CreateKey(101)
147145

swap_server_client.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ type swapServerClient interface {
5555

5656
NewLoopOutSwap(ctx context.Context,
5757
swapHash lntypes.Hash, amount btcutil.Amount, expiry int32,
58-
receiverKey [33]byte,
59-
swapPublicationDeadline time.Time) (
60-
*newLoopOutResponse, error)
58+
receiverKey [33]byte, swapPublicationDeadline time.Time,
59+
initiator string) (*newLoopOutResponse, error)
6160

6261
PushLoopOutPreimage(ctx context.Context,
6362
preimage lntypes.Preimage) error
6463

6564
NewLoopInSwap(ctx context.Context,
6665
swapHash lntypes.Hash, amount btcutil.Amount,
6766
senderKey [33]byte, swapInvoice, probeInvoice string,
68-
lastHop *route.Vertex) (*newLoopInResponse, error)
67+
lastHop *route.Vertex, initiator string) (*newLoopInResponse,
68+
error)
6969

7070
// SubscribeLoopOutUpdates subscribes to loop out server state.
7171
SubscribeLoopOutUpdates(ctx context.Context,
@@ -220,8 +220,8 @@ func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
220220

221221
func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context,
222222
swapHash lntypes.Hash, amount btcutil.Amount, expiry int32,
223-
receiverKey [33]byte, swapPublicationDeadline time.Time) (
224-
*newLoopOutResponse, error) {
223+
receiverKey [33]byte, swapPublicationDeadline time.Time,
224+
initiator string) (*newLoopOutResponse, error) {
225225

226226
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
227227
defer rpcCancel()
@@ -233,7 +233,7 @@ func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context,
233233
SwapPublicationDeadline: swapPublicationDeadline.Unix(),
234234
ProtocolVersion: loopdb.CurrentRPCProtocolVersion,
235235
Expiry: expiry,
236-
UserAgent: UserAgent(),
236+
UserAgent: UserAgent(initiator),
237237
},
238238
)
239239
if err != nil {
@@ -276,7 +276,8 @@ func (s *grpcSwapServerClient) PushLoopOutPreimage(ctx context.Context,
276276

277277
func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context,
278278
swapHash lntypes.Hash, amount btcutil.Amount, senderKey [33]byte,
279-
swapInvoice, probeInvoice string, lastHop *route.Vertex) (*newLoopInResponse, error) {
279+
swapInvoice, probeInvoice string, lastHop *route.Vertex,
280+
initiator string) (*newLoopInResponse, error) {
280281

281282
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
282283
defer rpcCancel()
@@ -288,7 +289,7 @@ func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context,
288289
SwapInvoice: swapInvoice,
289290
ProtocolVersion: loopdb.CurrentRPCProtocolVersion,
290291
ProbeInvoice: probeInvoice,
291-
UserAgent: UserAgent(),
292+
UserAgent: UserAgent(initiator),
292293
}
293294
if lastHop != nil {
294295
req.LastHop = lastHop[:]

version.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ func Version() string {
5353

5454
// UserAgent returns the full user agent string that identifies the software
5555
// that is submitting swaps to the loop server.
56-
func UserAgent() string {
56+
func UserAgent(initiator string) string {
57+
// We'll only allow "safe" characters in the initiator portion of the
58+
// user agent string and spaces only if surrounded by other characters.
59+
initiatorAlphabet := semanticAlphabet + ". "
60+
cleanInitiator := normalizeVerString(
61+
strings.TrimSpace(initiator), initiatorAlphabet,
62+
)
63+
if len(cleanInitiator) > 0 {
64+
cleanInitiator = fmt.Sprintf(",initiator=%s", cleanInitiator)
65+
}
66+
5767
// Assemble full string, including the commit hash of current build.
5868
return fmt.Sprintf(
59-
"%s/v%s/commit=%s", AgentName, semanticVersion(), Commit,
69+
"%s/v%s/commit=%s%s", AgentName, semanticVersion(), Commit,
70+
cleanInitiator,
6071
)
6172
}
6273

0 commit comments

Comments
 (0)