-
Notifications
You must be signed in to change notification settings - Fork 128
staticaddr: validate server address parameters #1137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "github.com/btcsuite/btcd/btcec/v2" | ||
| "github.com/btcsuite/btcd/btcec/v2/schnorr" | ||
| "github.com/btcsuite/btcd/btcutil" | ||
| "github.com/btcsuite/btcd/wire" | ||
| "github.com/lightninglabs/loop/loopdb" | ||
| "github.com/lightninglabs/loop/staticaddr/script" | ||
| "github.com/lightninglabs/loop/swap" | ||
|
|
@@ -93,8 +94,9 @@ func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context, | |
|
|
||
| args := m.Called(ctx, in, opts) | ||
|
|
||
| return args.Get(0).(*swapserverrpc.ServerNewAddressResponse), | ||
| args.Error(1) | ||
| resp, _ := args.Get(0).(*swapserverrpc.ServerNewAddressResponse) | ||
|
|
||
| return resp, args.Error(1) | ||
| } | ||
|
|
||
| // TestManager tests the static address manager generates the corerct static | ||
|
|
@@ -128,6 +130,100 @@ func TestManager(t *testing.T) { | |
| require.EqualValues(t, defaultExpiry, expiry) | ||
| } | ||
|
|
||
| // TestNewAddressValidatesServerResponse tests that the untrusted | ||
| // ServerNewAddress response is validated before the address script is created. | ||
| func TestNewAddressValidatesServerResponse(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| resp *swapserverrpc.ServerNewAddressResponse | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "nil response", | ||
| expected: "missing server new address response", | ||
| }, | ||
| { | ||
| name: "nil params", | ||
| resp: &swapserverrpc.ServerNewAddressResponse{}, | ||
| expected: "missing server address parameters", | ||
| }, | ||
|
hieblmi marked this conversation as resolved.
|
||
| { | ||
| name: "missing server key", | ||
| resp: &swapserverrpc.ServerNewAddressResponse{ | ||
| Params: &swapserverrpc.ServerAddressParameters{ | ||
| Expiry: defaultExpiry, | ||
| }, | ||
| }, | ||
| expected: "missing server public key", | ||
| }, | ||
| { | ||
| name: "uncompressed server key", | ||
| resp: &swapserverrpc.ServerNewAddressResponse{ | ||
| Params: &swapserverrpc.ServerAddressParameters{ | ||
| ServerKey: []byte{0x04}, | ||
| Expiry: defaultExpiry, | ||
| }, | ||
| }, | ||
| expected: "server public key is not a compressed", | ||
| }, | ||
| { | ||
| name: "zero expiry", | ||
| resp: newServerNewAddressResponse(0), | ||
| expected: "static address CSV expiry must be non-zero", | ||
| }, | ||
| { | ||
| name: "seconds flag", | ||
| resp: newServerNewAddressResponse( | ||
| wire.SequenceLockTimeIsSeconds | 1, | ||
| ), | ||
| expected: "static address expiry does not fit into CSV", | ||
| }, | ||
| { | ||
| name: "disabled flag", | ||
| resp: newServerNewAddressResponse( | ||
| wire.SequenceLockTimeDisabled | 1, | ||
| ), | ||
| expected: "static address expiry does not fit into CSV", | ||
| }, | ||
| { | ||
| name: "reserved flag", | ||
| resp: newServerNewAddressResponse( | ||
| wire.SequenceLockTimeMask + 1, | ||
| ), | ||
| expected: "static address expiry does not fit into CSV", | ||
| }, | ||
| { | ||
| name: "too large", | ||
| resp: newServerNewAddressResponse( | ||
| maxStaticAddressCSVExpiry + 1, | ||
| ), | ||
| expected: "exceeds maximum", | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| testContext := NewAddressManagerTestContextWithResponse( | ||
| t, test.resp, | ||
| ) | ||
|
|
||
| _, _, err := testContext.manager.NewAddress(t.Context()) | ||
| require.ErrorContains(t, err, test.expected) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestNewAddressAcceptsMaxCSVExpiry tests the upper valid CSV boundary. | ||
| func TestNewAddressAcceptsMaxCSVExpiry(t *testing.T) { | ||
| testContext := NewAddressManagerTestContextWithResponse( | ||
| t, newServerNewAddressResponse(maxStaticAddressCSVExpiry), | ||
| ) | ||
|
|
||
| _, expiry, err := testContext.manager.NewAddress(t.Context()) | ||
| require.NoError(t, err) | ||
| require.EqualValues(t, maxStaticAddressCSVExpiry, expiry) | ||
| } | ||
|
|
||
| // GenerateExpectedTaprootAddress generates the expected taproot address that | ||
| // the predefined parameters are supposed to generate. | ||
| func GenerateExpectedTaprootAddress(t *ManagerTestContext) ( | ||
|
|
@@ -170,6 +266,16 @@ type ManagerTestContext struct { | |
| // NewAddressManagerTestContext creates a new test context for the static | ||
| // address manager. | ||
| func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext { | ||
| return NewAddressManagerTestContextWithResponse( | ||
| t, newServerNewAddressResponse(defaultExpiry), | ||
| ) | ||
| } | ||
|
|
||
| // NewAddressManagerTestContextWithResponse creates a new test context with a | ||
| // custom ServerNewAddress response. | ||
| func NewAddressManagerTestContextWithResponse(t *testing.T, | ||
| resp *swapserverrpc.ServerNewAddressResponse) *ManagerTestContext { | ||
|
|
||
| ctxb, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
|
|
@@ -184,14 +290,7 @@ func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext { | |
|
|
||
| mockStaticAddressClient.On( | ||
| "ServerNewAddress", mock.Anything, mock.Anything, mock.Anything, | ||
| ).Return( | ||
| &swapserverrpc.ServerNewAddressResponse{ | ||
| Params: &swapserverrpc.ServerAddressParameters{ | ||
| ServerKey: defaultServerPubkeyBytes, | ||
| Expiry: defaultExpiry, | ||
| }, | ||
| }, nil, | ||
| ) | ||
| ).Return(resp, nil) | ||
|
|
||
| cfg := &ManagerConfig{ | ||
| Store: store, | ||
|
|
@@ -215,3 +314,14 @@ func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext { | |
| mockStaticAddressClient: mockStaticAddressClient, | ||
| } | ||
| } | ||
|
|
||
| // newServerNewAddressResponse returns a valid server response with the given | ||
| // CSV expiry. | ||
| func newServerNewAddressResponse(expiry uint32) *swapserverrpc.ServerNewAddressResponse { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. godoc is missing
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| return &swapserverrpc.ServerNewAddressResponse{ | ||
| Params: &swapserverrpc.ServerAddressParameters{ | ||
| ServerKey: defaultServerPubkeyBytes, | ||
| Expiry: expiry, | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.