Skip to content

Add xkey matching with PSBT::Input::bip32_derivation - #2

Open
rajarshimaitra wants to merge 1 commit into
afilini:masterfrom
rajarshimaitra:psbt-match-xpubs
Open

Add xkey matching with PSBT::Input::bip32_derivation#2
rajarshimaitra wants to merge 1 commit into
afilini:masterfrom
rajarshimaitra:psbt-match-xpubs

Conversation

@rajarshimaitra

Copy link
Copy Markdown
Contributor

Proposing a small update in the psbt-xpub exercise.

It occurred to me that while iterating via Descriptor::for_each_key(), we should also ensure that we are inserting the right keys into the global key map. If we happen to have a descriptor of many miniscript branches, and we need only one/or some of them to be included for that specific transaction, we should only add the related xpubs in the global map.

Iterating over all the keys will insert all of them, even if they aren't used in the final witness stack.

I made the check by doing a match() of the xpub in descriptor and thge PSBT input's bip32_derivation keymap. But this require secp context. So has extra overburden.. Is there any better way to do this??

@afilini afilini left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Just looking at the technical side of it, I've added a few comments for things I think you could improve.

More broadly though, I'm not convinced by these changes: in general I think we should always add to the PSBT as much information as possible. If, let's say, the creator of the PSBT didn't know about one xpub I don't think that's a good reason for not adding it, if we know it. It could be an argument for also adding the corresponding bip32 path, but that's out of scope for this exercise.

Comment thread psbt-xpub/src/answer.rs
};

psbt.xpub.insert(xpub.xkey, origin);
// Check if this key matches with the PBBT-input's bip32 keymap.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

typo: PSBT

Comment thread psbt-xpub/src/answer.rs
// We need this to ensure we are not adding unrelated keys that might
// be present in the descriptor, into the PSBT global keymap.
psbt.inputs.iter().for_each(|input| {
input.bip32_derivation.iter().for_each(|(_, keysource)| {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

To support taproot descriptors you should also look at tap_key_origins.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice idea.. will add..

Comment thread psbt-xpub/src/answer.rs
// Check if this key matches with the PBBT-input's bip32 keymap.
// We need this to ensure we are not adding unrelated keys that might
// be present in the descriptor, into the PSBT global keymap.
psbt.inputs.iter().for_each(|input| {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I would rewrite this as two nested for loops, so that you can break out of both (using a label) once you insert the key. Otherwise you are just potentially wasting iterations doing nothing because you've already found and added the key.

This should also let you remove the clone() on the origin when inserting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup that makes more sense than nested iterators..

@rajarshimaitra

rajarshimaitra commented Jun 14, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @afilini for the look.. I have few more questions..

If, let's say, the creator of the PSBT didn't know about one xpub I don't think that's a good reason for not adding it, if we know it. It could be an argument for also adding the corresponding bip32 path, but that's out of scope for this exercise.

My thought behind this was, if we have a minscript with multiple execution branch, and we only used one branch to satisfy the witness script, this logic will then push all the keys associated with the full descriptor into the global map.. And for taproot cases, no. of execution branches can be very large too.. So makes sense to have some kinda gate over there, and not just push all the keys we can find from a descriptor??

Future proposal:
It feels many other PSBT operations can be added in these exercise book.. That would be really helpful.. A lot is going on inside creation-signing-finializing, and if we can demo them like this in parts would be a good resource for devs.. Let me know if I can be of any help for that.. If we have a list I can try to pitch in short exercises like this..

@afilini

afilini commented Jun 15, 2022

Copy link
Copy Markdown
Owner

Yes, what you described also makes sense, but it really depends on what one is trying to do specifically.

Ideally we would have options to tweak this, maybe we are the only "updater" and so we have to add everything ourselves, maybe we are just finishing up the work of somebody else.

My guess is that here we should just try to keep it simple, we are not building a full wallet after all

@rajarshimaitra

rajarshimaitra commented Jun 16, 2022

Copy link
Copy Markdown
Contributor Author

Yes that makes sense.. There would be many other logic like that to handle and its out of scope for the exercise.. But thought this one could be mentioned as a generic thing to do, especially in Taproot kinda env..

Maybe we can just add a note in this exercise like "Something you might wanna keep in mind while using for_each_key" of descriptors"?? Its actually less of a problem if you do it over a single minscript key.. But then you have to know which minscript you wanna work with among all the execution paths..

Any more ideas you have on how we can expand the PSBT exercise?? Seems very useful for wallet developers.. If you have any mental lists let me know.. I would try to sketch out some exercises from them..

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.

2 participants