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

WIP implementation of BIP322 #140

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

wip-abramson
Copy link

This P.R. adds basic support for bip322 signatures following a similar API to the current open P.R. in Bitcoin core (See bitcoin/bitcoin#24058)

There are a few areas that still need some work:

  • BIP0322 Legacy signatures not yet supported (@jimmysong I believe this also needs adding to the library?, specifically the ability for compact encoding of a signature and public key - if so I am happy to have a go adding this in this P.R.)
  • Potentially add custom error handling (e.g. return a MessageVerificationResult similar to bitcoin core impl)
  • Proof of Funds is not yet supported
  • MultiSig BIP322 not yet supported

Open to suggestions for how to restructure this to align with the existing library.

Additionally, would be interested in thoughts around this thread - https://github.com/bitcoin/bitcoin/pull/24058/files#r872561862. I was expecting to be able to reproduce the same signature as provided in the bip. But ended up producing a different, valid signature for the same address, message pair. Does this indicate that there is a mismatch between how RFC6979 is implemented between buidl-python and bitcoin core?

Finally, I want to acknowledge Digital Contract Design and @rxgrant for funding my work on this P.R. to date.

Any time you have to review this much appreciated, thanks!

Copy link
Collaborator

@jimmysong jimmysong left a comment

Choose a reason for hiding this comment

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

Good start to the thing. I left some comments. Thanks for the contribution @wip-abramson !

buidl/message.py Outdated Show resolved Hide resolved
buidl/message.py Outdated
return True


def sign_message(format: MessageSignatureFormat, private_key: PrivateKey, address: str, message: str):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nothing else in the library uses Type hints like this. I would prefer you specify in the documentation.
Also, please make clear what you are getting back when you call this function. What sort of signature is it? How do I verify it? What's the signature encoded in?

Copy link
Author

Choose a reason for hiding this comment

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

I have removed type hints and added documentation in comments above the function

buidl/message.py Outdated

# return base64_encode(signature.der())

def sign_message_bip322(format: MessageSignatureFormat, private_key: PrivateKey, address: str, message: str):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally in sign message functions, there should be some sort of sanity check about whether it validates. If it doesn't then we can throw an error.

Copy link
Author

Choose a reason for hiding this comment

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

Isn't that what is implemented on line 195 - 197? Or did you have something else in mind

buidl/message.py Outdated

def sign_message_bip322(format: MessageSignatureFormat, private_key: PrivateKey, address: str, message: str):

assert(format != MessageSignatureFormat.LEGACY)
Copy link
Collaborator

Choose a reason for hiding this comment

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

A helpful error message would be nice.

Copy link
Author

Choose a reason for hiding this comment

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

Added the following error message to the assert

BIP 322 Signatures must be represented in either the Simple of Full Formats

tx_outputs = [tx_output]
network="mainnet"
# is to_sign always a segwit?
segwit=True
Copy link
Author

Choose a reason for hiding this comment

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

I have run into some issues with the current hard coded segwit=True that is set of the to_sign transaction. Currently, I am attempting to create a bip322 p2sh multisig.

@jimmysong is there a recommended way for me to test whether this to_sign transation should have it's segwit flag set to True or not.

It is to do with the scriptPubKey on the tx_out of the to_sign transaction right?

@wip-abramson
Copy link
Author

I'd also like to propose either:

  • Tracking in the Tx object if a Tx is a bip322 Tx through a bool flag similar to segwit.
  • Or, adding a is_bip322 flag to both the Tx verify() and the PSBT final_tx() fns

This is because currently these fail for bip322 Txs because no fee is specified (None is required)

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