From 2a732a4385edbd522042bc97fcfd4be02be5c905 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 6 Nov 2020 10:43:03 +0100 Subject: [PATCH] loop: add initiator string to user agent --- client_test.go | 1 + interface.go | 10 ++++++++++ loopin.go | 2 +- loopin_test.go | 1 + loopout.go | 2 +- server_mock_test.go | 14 ++++++-------- swap_server_client.go | 19 ++++++++++--------- version.go | 15 +++++++++++++-- 8 files changed, 43 insertions(+), 21 deletions(-) diff --git a/client_test.go b/client_test.go index fc840e0..aca47fe 100644 --- a/client_test.go +++ b/client_test.go @@ -34,6 +34,7 @@ var ( MaxPrepayAmount: 100, MaxPrepayRoutingFee: 75000, MaxSwapRoutingFee: 70000, + Initiator: "test", } swapInvoiceDesc = "swap" diff --git a/interface.go b/interface.go index f03d8b2..ed0ee75 100644 --- a/interface.go +++ b/interface.go @@ -81,6 +81,11 @@ type OutRequest struct { // Label contains an optional label for the swap. Label string + + // Initiator is an optional string that identifies what software + // initiated the swap (loop CLI, autolooper, LiT UI and so on) and is + // appended to the user agent string. + Initiator string } // Out contains the full details of a loop out request. This includes things @@ -196,6 +201,11 @@ type LoopInRequest struct { // Label contains an optional label for the swap. Label string + + // Initiator is an optional string that identifies what software + // initiated the swap (loop CLI, autolooper, LiT UI and so on) and is + // appended to the user agent string. + Initiator string } // LoopInTerms are the server terms on which it executes loop in swaps. diff --git a/loopin.go b/loopin.go index 689976c..6877111 100644 --- a/loopin.go +++ b/loopin.go @@ -166,7 +166,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, log.Infof("Initiating swap request at height %v", currentHeight) swapResp, err := cfg.server.NewLoopInSwap(globalCtx, swapHash, request.Amount, senderKey, swapInvoice, probeInvoice, - request.LastHop, + request.LastHop, request.Initiator, ) probeWaitCancel() if err != nil { diff --git a/loopin_test.go b/loopin_test.go index f457842..401a29f 100644 --- a/loopin_test.go +++ b/loopin_test.go @@ -21,6 +21,7 @@ var ( Amount: btcutil.Amount(50000), MaxSwapFee: btcutil.Amount(1000), HtlcConfTarget: 2, + Initiator: "test", } ) diff --git a/loopout.go b/loopout.go index dc10020..b930354 100644 --- a/loopout.go +++ b/loopout.go @@ -114,7 +114,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, // latest swap publication time. swapResp, err := cfg.server.NewLoopOutSwap( globalCtx, swapHash, request.Amount, request.Expiry, - receiverKey, request.SwapPublicationDeadline, + receiverKey, request.SwapPublicationDeadline, request.Initiator, ) if err != nil { return nil, fmt.Errorf("cannot initiate swap: %v", err) diff --git a/server_mock_test.go b/server_mock_test.go index ddc80ff..9aef6ff 100644 --- a/server_mock_test.go +++ b/server_mock_test.go @@ -64,10 +64,9 @@ func newServerMock(lnd *test.LndMockServices) *serverMock { } } -func (s *serverMock) NewLoopOutSwap(ctx context.Context, - swapHash lntypes.Hash, amount btcutil.Amount, expiry int32, - receiverKey [33]byte, _ time.Time) ( - *newLoopOutResponse, error) { +func (s *serverMock) NewLoopOutSwap(_ context.Context, swapHash lntypes.Hash, + amount btcutil.Amount, _ int32, _ [33]byte, _ time.Time, + _ string) (*newLoopOutResponse, error) { _, senderKey := test.CreateKey(100) @@ -138,10 +137,9 @@ func getInvoice(hash lntypes.Hash, amt btcutil.Amount, memo string) (string, err return reqString, nil } -func (s *serverMock) NewLoopInSwap(ctx context.Context, - swapHash lntypes.Hash, amount btcutil.Amount, - senderKey [33]byte, swapInvoice, probeInvoice string, - lastHop *route.Vertex) (*newLoopInResponse, error) { +func (s *serverMock) NewLoopInSwap(_ context.Context, swapHash lntypes.Hash, + amount btcutil.Amount, _ [33]byte, swapInvoice, _ string, + _ *route.Vertex, _ string) (*newLoopInResponse, error) { _, receiverKey := test.CreateKey(101) diff --git a/swap_server_client.go b/swap_server_client.go index 2cf2aaa..7d04d64 100644 --- a/swap_server_client.go +++ b/swap_server_client.go @@ -55,9 +55,8 @@ type swapServerClient interface { NewLoopOutSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, expiry int32, - receiverKey [33]byte, - swapPublicationDeadline time.Time) ( - *newLoopOutResponse, error) + receiverKey [33]byte, swapPublicationDeadline time.Time, + initiator string) (*newLoopOutResponse, error) PushLoopOutPreimage(ctx context.Context, preimage lntypes.Preimage) error @@ -65,7 +64,8 @@ type swapServerClient interface { NewLoopInSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, senderKey [33]byte, swapInvoice, probeInvoice string, - lastHop *route.Vertex) (*newLoopInResponse, error) + lastHop *route.Vertex, initiator string) (*newLoopInResponse, + error) // SubscribeLoopOutUpdates subscribes to loop out server state. SubscribeLoopOutUpdates(ctx context.Context, @@ -220,8 +220,8 @@ func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context, func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, expiry int32, - receiverKey [33]byte, swapPublicationDeadline time.Time) ( - *newLoopOutResponse, error) { + receiverKey [33]byte, swapPublicationDeadline time.Time, + initiator string) (*newLoopOutResponse, error) { rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout) defer rpcCancel() @@ -233,7 +233,7 @@ func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context, SwapPublicationDeadline: swapPublicationDeadline.Unix(), ProtocolVersion: loopdb.CurrentRPCProtocolVersion, Expiry: expiry, - UserAgent: UserAgent(), + UserAgent: UserAgent(initiator), }, ) if err != nil { @@ -276,7 +276,8 @@ func (s *grpcSwapServerClient) PushLoopOutPreimage(ctx context.Context, func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context, swapHash lntypes.Hash, amount btcutil.Amount, senderKey [33]byte, - swapInvoice, probeInvoice string, lastHop *route.Vertex) (*newLoopInResponse, error) { + swapInvoice, probeInvoice string, lastHop *route.Vertex, + initiator string) (*newLoopInResponse, error) { rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout) defer rpcCancel() @@ -288,7 +289,7 @@ func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context, SwapInvoice: swapInvoice, ProtocolVersion: loopdb.CurrentRPCProtocolVersion, ProbeInvoice: probeInvoice, - UserAgent: UserAgent(), + UserAgent: UserAgent(initiator), } if lastHop != nil { req.LastHop = lastHop[:] diff --git a/version.go b/version.go index f2be363..09973a7 100644 --- a/version.go +++ b/version.go @@ -53,10 +53,21 @@ func Version() string { // UserAgent returns the full user agent string that identifies the software // that is submitting swaps to the loop server. -func UserAgent() string { +func UserAgent(initiator string) string { + // We'll only allow "safe" characters in the initiator portion of the + // user agent string and spaces only if surrounded by other characters. + initiatorAlphabet := semanticAlphabet + ". " + cleanInitiator := normalizeVerString( + strings.TrimSpace(initiator), initiatorAlphabet, + ) + if len(cleanInitiator) > 0 { + cleanInitiator = fmt.Sprintf(",initiator=%s", cleanInitiator) + } + // Assemble full string, including the commit hash of current build. return fmt.Sprintf( - "%s/v%s/commit=%s", AgentName, semanticVersion(), Commit, + "%s/v%s/commit=%s%s", AgentName, semanticVersion(), Commit, + cleanInitiator, ) }