-
Notifications
You must be signed in to change notification settings - Fork 47
lndclient: attach key locator when any component is non-zero #269
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
+220
−12
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| package lndclient | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/btcsuite/btcd/btcec/v2" | ||
| "github.com/btcsuite/btcd/wire" | ||
| "github.com/lightningnetwork/lnd/keychain" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // newTestPubKey returns a fresh public key for use in descriptor | ||
| // marshalling tests. | ||
| func newTestPubKey(t *testing.T) *btcec.PublicKey { | ||
| t.Helper() | ||
|
|
||
| priv, err := btcec.NewPrivateKey() | ||
| require.NoError(t, err) | ||
|
|
||
| return priv.PubKey() | ||
| } | ||
|
|
||
| // TestMarshallSignDescriptorsFullLocator pins the contract of the full | ||
| // descriptor marshaller used by SignOutputRawKeyLocator: the key locator | ||
| // is attached whenever either the family or the index is non-zero, and | ||
| // only the all-zero KeyLocator (the struct's zero value, indistinguishable | ||
| // from "unset") is omitted. | ||
| // | ||
| // The family-6 / index-0 case is the regression guard. A prior version of | ||
| // the helper required both components to be non-zero and therefore | ||
| // silently dropped the locator for LND's node identity key slot. Signers | ||
| // that must re-derive the key from its locator — restored-from-seed | ||
| // wallets, or family-pinned identity keys — could not resolve the key. | ||
| func TestMarshallSignDescriptorsFullLocator(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| pubKey := newTestPubKey(t) | ||
| output := &wire.TxOut{PkScript: []byte{0x51}, Value: 1000} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| family keychain.KeyFamily | ||
| index uint32 | ||
| omitPubKey bool | ||
| expectLocator bool | ||
| }{ | ||
| { | ||
| name: "all-zero locator is omitted", | ||
| family: 0, | ||
| index: 0, | ||
| expectLocator: false, | ||
| }, | ||
| { | ||
| name: "family-only locator is attached", | ||
| family: keychain.KeyFamilyNodeKey, | ||
| index: 0, | ||
| expectLocator: true, | ||
| }, | ||
| { | ||
| name: "index-only locator is attached", | ||
| family: 0, | ||
| index: 5, | ||
| expectLocator: true, | ||
| }, | ||
| { | ||
| name: "fully populated locator is attached", | ||
| family: keychain.KeyFamilyNodeKey, | ||
| index: 5, | ||
| expectLocator: true, | ||
| }, | ||
| { | ||
| name: "family-only locator is attached with " + | ||
| "nil pubkey", | ||
| family: keychain.KeyFamilyNodeKey, | ||
| index: 0, | ||
| omitPubKey: true, | ||
| expectLocator: true, | ||
| }, | ||
| { | ||
| name: "index-only locator is attached with " + | ||
| "nil pubkey", | ||
| family: 0, | ||
| index: 5, | ||
| omitPubKey: true, | ||
| expectLocator: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| keyDesc := keychain.KeyDescriptor{ | ||
| KeyLocator: keychain.KeyLocator{ | ||
| Family: tc.family, | ||
| Index: tc.index, | ||
| }, | ||
| } | ||
| if !tc.omitPubKey { | ||
| keyDesc.PubKey = pubKey | ||
| } | ||
|
|
||
| descs := []*SignDescriptor{{ | ||
| KeyDesc: keyDesc, | ||
| Output: output, | ||
| }} | ||
|
|
||
| rpcDescs := marshallSignDescriptors(descs, true) | ||
| require.Len(t, rpcDescs, 1) | ||
|
|
||
| rpcKeyDesc := rpcDescs[0].KeyDesc | ||
| require.NotNil(t, rpcKeyDesc) | ||
|
|
||
| if tc.omitPubKey { | ||
| require.Empty(t, rpcKeyDesc.RawKeyBytes) | ||
| } else { | ||
| require.Equal(t, | ||
| pubKey.SerializeCompressed(), | ||
| rpcKeyDesc.RawKeyBytes, | ||
| ) | ||
| } | ||
|
|
||
| if !tc.expectLocator { | ||
| require.Nil(t, rpcKeyDesc.KeyLoc) | ||
| return | ||
| } | ||
|
|
||
| require.NotNil(t, rpcKeyDesc.KeyLoc) | ||
| require.Equal(t, | ||
| int32(tc.family), | ||
| rpcKeyDesc.KeyLoc.KeyFamily, | ||
| ) | ||
| require.Equal(t, | ||
| int32(tc.index), | ||
| rpcKeyDesc.KeyLoc.KeyIndex, | ||
| ) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestMarshallSignDescriptorsPartialOnlyOneOf verifies that the partial | ||
| // descriptor marshaller used by the legacy SignOutputRaw populates either | ||
| // the public key or the locator, but never both. Applications like Loop | ||
| // have adjusted themselves to this behavior, which is why the fix for the | ||
| // locator-drop bug lives in the full descriptor path instead. | ||
| func TestMarshallSignDescriptorsPartialOnlyOneOf(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| pubKey := newTestPubKey(t) | ||
| output := &wire.TxOut{PkScript: []byte{0x51}, Value: 1000} | ||
|
|
||
| t.Run("pubkey present drops locator", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| descs := []*SignDescriptor{{ | ||
| KeyDesc: keychain.KeyDescriptor{ | ||
| PubKey: pubKey, | ||
| KeyLocator: keychain.KeyLocator{ | ||
| Family: keychain.KeyFamilyNodeKey, | ||
| Index: 5, | ||
| }, | ||
| }, | ||
| Output: output, | ||
| }} | ||
|
|
||
| rpcDescs := marshallSignDescriptors(descs, false) | ||
| require.Len(t, rpcDescs, 1) | ||
|
|
||
| require.Equal(t, | ||
| pubKey.SerializeCompressed(), | ||
| rpcDescs[0].KeyDesc.RawKeyBytes, | ||
| ) | ||
| require.Nil(t, rpcDescs[0].KeyDesc.KeyLoc) | ||
| }) | ||
|
|
||
| t.Run("pubkey absent uses locator", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| descs := []*SignDescriptor{{ | ||
| KeyDesc: keychain.KeyDescriptor{ | ||
| KeyLocator: keychain.KeyLocator{ | ||
| Family: keychain.KeyFamilyNodeKey, | ||
| Index: 5, | ||
| }, | ||
| }, | ||
| Output: output, | ||
| }} | ||
|
|
||
| rpcDescs := marshallSignDescriptors(descs, false) | ||
| require.Len(t, rpcDescs, 1) | ||
|
|
||
| require.Empty(t, rpcDescs[0].KeyDesc.RawKeyBytes) | ||
| require.NotNil(t, rpcDescs[0].KeyDesc.KeyLoc) | ||
| require.Equal(t, | ||
| int32(keychain.KeyFamilyNodeKey), | ||
| rpcDescs[0].KeyDesc.KeyLoc.KeyFamily, | ||
| ) | ||
| require.Equal(t, | ||
| int32(5), | ||
| rpcDescs[0].KeyDesc.KeyLoc.KeyIndex, | ||
| ) | ||
| }) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose to add two more tests where
KeyDesc.PubKeyis nil and eitherfamily!=0orindex!=0. I.e. replicate "family-only locator is attached" and "index-only locator is attached" for theKeyDesc.PubKey=nilcase.