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

devices: add taproot keypath spending support for bitbox02 #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Beerosagos
Copy link
Owner

No description provided.

Copy link

@benma benma left a comment

Choose a reason for hiding this comment

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

GJ! 🎉


if len(psbt_in.tap_internal_key) > 0:
# adding taproot keys to the keypaths to be checked
for pubkey , tup in psbt_in.tap_bip32_paths.items():
Copy link

Choose a reason for hiding this comment

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

formatting, remove space after comma.

Copy link
Owner Author

Choose a reason for hiding this comment

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

done!

if len(psbt_in.tap_internal_key) > 0:
# adding taproot keys to the keypaths to be checked
for pubkey , tup in psbt_in.tap_bip32_paths.items():
keypaths.update({pubkey: tup[1]})
Copy link

@benma benma Jun 14, 2022

Choose a reason for hiding this comment

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

check that tup[0], the leaf hashes, is empty?

for clarity, you could unpack the tup into vars with descriptive names:

for pubkey, (leaf_hashes, key_origin_info) in ...

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is nice! Done, thx :)

#taproot keypath input
psbt_in.tap_key_sig = sig
else:
# ser_sig_der() adds SIGHASH_ALL (only for ECDSA)
Copy link

Choose a reason for hiding this comment

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

sounds like this function adds SIGHASH_ALL only for ECDA and not for Schnorr, but the whole function is not applicable to taproot, as the signature serialization is not a DER-encoding, but the raw 64 r|s bytes. i'd drop "only for ECDSA")

Copy link
Owner Author

Choose a reason for hiding this comment

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

done!

)

# previous tx is needed for all non-Taproot transactions
if len(psbt_in.tap_internal_key) == 0:
Copy link

Choose a reason for hiding this comment

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

the trouble is that we must provide the prevtx if any of the inputs is not taproot.

i think you'll need to have a pass through the inputs beforehand to figure this out.

potentially this function could be useful (but maybe easier to not use it and directly check in the PSBT 🤷 ): https://github.com/digitalbitbox/bitbox02-firmware/blob/987c4ce2c32308f12b31e69ec27bf0185662c42f/py/bitbox02/bitbox02/bitbox02/bitbox02.py#L78

Copy link
Owner Author

Choose a reason for hiding this comment

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

We talked about it, and decided that we could avoid that check, since it is already done by bb02 firmware

raise BadArgumentError(
"Previous transaction missing for input {}".format(input_index)
)

found_pubkey, keypath = find_our_key(psbt_in.hd_keypaths)
keypaths = {}
for pubkey, keypath in psbt_in.hd_keypaths.items():
Copy link

Choose a reason for hiding this comment

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

keypath is better named key_origin_info, which contains the keypath but also the fingerprint.

Copy link
Owner Author

Choose a reason for hiding this comment

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

done!

@@ -650,7 +662,7 @@ def script_config_from_utxo(
# - https://medium.com/shiftcrypto/bitbox-app-firmware-update-6-2020-c70f733a5330
# - https://blog.trezor.io/details-of-firmware-updates-for-trezor-one-version-1-9-1-and-trezor-model-t-version-2-3-1-1eba8f60f2dd.
# - https://github.com/zkSNACKs/WalletWasabi/pull/3822
# The BitBox02 for now requires the prevtx, at least until Taproot activates.
# The BitBox02 requires the prevtx for all non-Taproot utxo.
Copy link

Choose a reason for hiding this comment

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

that is false - it requires all prevtxs if not all of the inputs are taproot

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed!

Comment on lines 683 to 687
# if utxo is Taproot, we don't need prevtx
if prevtx is None and len(psbt_in.tap_internal_key) == 0:
raise BadArgumentError(
"Previous transaction missing for input {}".format(input_index)
)
Copy link

Choose a reason for hiding this comment

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

the condition is not true - we might need prevtx anyway even if it is taproot (if other inputs are non-taproot).

I think you can just delete this whole section and let the bitbox02 handle the error

Copy link
Owner Author

Choose a reason for hiding this comment

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

removed

Comment on lines 689 to 691
key_origin_infos = {}
for pubkey, key_origin_info in psbt_in.hd_keypaths.items():
key_origin_infos.update({pubkey: key_origin_info})
Copy link

Choose a reason for hiding this comment

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

isn't this the same as key_origin_infos = psbt_in.hd_keypaths.copy()

Copy link
Owner Author

Choose a reason for hiding this comment

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

You're right! :D Fixed

Comment on lines 758 to 760
key_origin_infos = {}
for pubkey, key_origin_info in psbt_out.hd_keypaths.items():
key_origin_infos.update({pubkey: key_origin_info})
Copy link

Choose a reason for hiding this comment

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

same

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed!

@benma
Copy link

benma commented Jun 26, 2022

Also please rebase and squash the commits - the older commits still have debug stuff in them

@Beerosagos Beerosagos force-pushed the bb02-tad-addr+sign branch from 2b3743e to 08c6896 Compare June 28, 2022 15:01
@Beerosagos
Copy link
Owner Author

Also please rebase and squash the commits - the older commits still have debug stuff in them

Done! Thanks

achow101 added 3 commits June 29, 2022 16:48
No longer needed, issue was fixed upstream.
No longer needed, taproot fields merged, psbt2 is not needed.
2720633 ci: Remove bitcoind patches (Andrew Chow)
e769e49 ci: Remove coldcard ffilib patch (Andrew Chow)

Pull request description:

  * Coldcard has fixed the ffilib issue upstream so our patch is no longer needed.
  * Bitcoin Core has merged Taproot PSBT fields, so our patch for it is no longer needed.
    * We did not need the PSBTv2 changes in Bitcoin Core for our tests, so these are removed as well.

Top commit has no ACKs.

Tree-SHA512: b5c98744a88479d635ad363599fc5e524f6fd121e4dd3058ba8d522a9dccdb0b10b7a603a2711709ea079f34a07a8d12db3f6680f3eba53180610218882bbede
@benma
Copy link

benma commented Aug 2, 2022

@Beerosagos can add a commit here that bumps the bitbox02 dependency version?

@benma
Copy link

benma commented Aug 2, 2022

@Beerosagos i tested this with taproot, p2wpkh and multisig. Please open the PR upstream :)

@Beerosagos Beerosagos force-pushed the bb02-tad-addr+sign branch 2 times, most recently from 642aa5a to 3df136b Compare August 3, 2022 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants