Skip to content
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

Add unit test for method ExternalKey.PubKey #1284

Merged
merged 2 commits into from
Jan 16, 2025
Merged

Conversation

ffranr
Copy link
Contributor

@ffranr ffranr commented Jan 9, 2025

This PR introduces a comprehensive unit test for the PubKey method of the ExternalKey struct. The test covers various scenarios, including valid and invalid derivation paths, ensuring the method's correctness and error handling.


Notes for Reviewers

This PR depends on #1272 . This PR should be merged after #1272 has been merged. I will keep in draft until #1272 is merged.

@ffranr ffranr self-assigned this Jan 9, 2025
@ffranr
Copy link
Contributor Author

ffranr commented Jan 9, 2025

Need a BIP86 xpub and master fingerprint. I don't think it's possible with https://iancoleman.io/bip39/ ?

@ffranr
Copy link
Contributor Author

ffranr commented Jan 9, 2025

We can get a real BIP86 xpub set from the chantools itest:

  chantools_harness.go:108: Chantools derivekey output: 2025-01-09 14:02:16.974 [INF] CHAN: chantools version v0.13.5 commit 
        
        Path:				m/86'/1'/0'
        Network: 			regtest
        Master Fingerprint:             279eeee3
        Public key: 			03b33a2a53dcf579282b3dd2d20981b6a707f8e1ec4983e5896dba5f4283277d39
        Extended public key (xpub): 	tpubDDfTBtwwqxXuCej7pKYfbXeCW3inAtv1cw4knmvYTTHkw3NoKaeCNH5XdY6n6fnBPc1gWEgeurfmBVzJLfBB1hGU64LsHFzJv4ASqaHyALH
        Address: 			bcrt1qmlsx2ntq3wk7lkmeyfhkp0wf5fgp49x5zpcnrn
        Legacy address: 		n1vho7tbggnYU3mCQgZHXzndh1RFawWDRo
        Taproot address:                bcrt1p07mls9jl9cmlg76q55x2e075r2j8uwykq9m9tlj45t5h7ggpjeks0tg49a
        Private key (WIF): 		n/a
        Extended private key (xprv):	n/a

But we need for mainnet also. Not just regtest.

@dstadulis dstadulis added this to the v0.6 milestone Jan 9, 2025
asset/asset_test.go Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jan 9, 2025

Pull Request Test Coverage Report for Build 12809434712

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 26 unchanged lines in 5 files lost coverage.
  • Overall coverage increased (+0.04%) to 40.762%

Files with Coverage Reduction New Missed Lines %
tapgarden/planter.go 2 71.02%
tapchannel/aux_leaf_signer.go 2 43.08%
tapdb/universe.go 4 80.91%
tapdb/multiverse.go 6 68.21%
universe/interface.go 12 51.95%
Totals Coverage Status
Change from base Build 12808880884: 0.04%
Covered Lines: 26553
Relevant Lines: 65142

💛 - Coveralls

@ffranr ffranr marked this pull request as ready for review January 10, 2025 12:23
@ffranr ffranr marked this pull request as draft January 10, 2025 12:26
@ffranr
Copy link
Contributor Author

ffranr commented Jan 10, 2025

The base branch has not been merged yet. To avoid merging this PR prematurely, I will keep it in draft status. However, @ziggie1984, it is ready for review.

@ffranr
Copy link
Contributor Author

ffranr commented Jan 10, 2025

Note also this comment from starius (from #1286):

Added more derivation paths and corresponding public keys, check in the test that the values returned by the method match to expected values. Also added tpub (testnet xpub).
To run "chantools derivekey" with xpubs, use PR lightninglabs/chantools#179

@ffranr ffranr requested a review from bhandras January 10, 2025 12:39
@ffranr ffranr force-pushed the ExternalKey-PubKey-unit-test branch from c425baf to 94d8eba Compare January 10, 2025 13:04
Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

testCases := []struct {
name string
externalKey ExternalKey
expectError bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: seems like there's no need for this bool, we can just expect an error if the expectedError is not empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the expectError bool flag we can specify that we expect an error without having to specify the error message. So I see it as optionality that we might as well keep. Let me know if you feel strongly about this and I can change as you suggest.

asset/asset_test.go Outdated Show resolved Hide resolved
t.Run(tc.name, func(tt *testing.T) {
pubKey, err := tc.externalKey.PubKey()

if tc.expectError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if tx.expectedError != ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice not to have to specify the error message IMO, #1284 (comment)

asset/asset_test.go Outdated Show resolved Hide resolved
@Roasbeef Roasbeef removed the request for review from ziggie1984 January 10, 2025 15:45
Copy link

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tho I would say this change is so low level it almost feels it should be put into its own package, at least the taproot key generation tests.

@ffranr ffranr force-pushed the ExternalKey-PubKey-unit-test branch from 94d8eba to f5d4327 Compare January 14, 2025 17:37
@ffranr ffranr requested a review from GeorgeTsagk January 14, 2025 17:49
Copy link
Member

@GeorgeTsagk GeorgeTsagk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm ✔️
-- pending comments by @bhandras

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tho I would say this change is so low level it almost feels it should be put into its own package

Would agree with this. This PR should be to the btcutil repo. It adds more tests to routines defined in the hdkeychain package.

@Roasbeef Roasbeef marked this pull request as ready for review January 14, 2025 19:14
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🍐

@ffranr ffranr marked this pull request as draft January 14, 2025 20:24
@ffranr
Copy link
Contributor Author

ffranr commented Jan 14, 2025

Moved to draft to avoid accidental merging before cold-group-key branch is merged.

@ffranr
Copy link
Contributor Author

ffranr commented Jan 14, 2025

Needs rebase.

@guggero
Copy link
Member

guggero commented Jan 15, 2025

@ffranr shouldn't we just merge this, since it goes into the cold-group-key branch? Then we can still review it there and address comments? Two more commits shouldn't make that big of a difference :)

@ffranr
Copy link
Contributor Author

ffranr commented Jan 15, 2025

@ffranr shouldn't we just merge this, since it goes into the cold-group-key branch? Then we can still review it there and address comments? Two more commits shouldn't make that big of a difference :)

At this point, cold-group-key branch has two approvals. Very close to a merge. I would prefer not to change it in anyway unless I have to.

@guggero guggero changed the base branch from cold-group-key to main January 16, 2025 12:29
@guggero
Copy link
Member

guggero commented Jan 16, 2025

Changed the base branch to main, needs a rebase.

ffranr and others added 2 commits January 16, 2025 12:56
This commit introduces a comprehensive unit test for the `PubKey` method
of the `ExternalKey` struct. The test covers various scenarios,
including valid and invalid derivation paths, ensuring the method's
correctness and error handling.
Added more derivation paths and corresponding public keys, check in the test
that the values returned by the method match to expected values. Also added
tpub (testnet xpub).

To run "chantools derivekey" with xpubs, use PR
lightninglabs/chantools#179
@ffranr ffranr force-pushed the ExternalKey-PubKey-unit-test branch from f5d4327 to cba1ca7 Compare January 16, 2025 13:00
@ffranr ffranr marked this pull request as ready for review January 16, 2025 13:00
@ffranr ffranr enabled auto-merge January 16, 2025 13:01
@ffranr ffranr added this pull request to the merge queue Jan 16, 2025
Merged via the queue into main with commit 2e343d7 Jan 16, 2025
18 checks passed
@guggero guggero deleted the ExternalKey-PubKey-unit-test branch January 16, 2025 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

9 participants