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

feat: add witness signature for segwit tx #119

Merged
merged 2 commits into from
Nov 28, 2022
Merged

Conversation

gitferry
Copy link
Collaborator

@gitferry gitferry commented Nov 25, 2022

This PR is to add witness signature for constructing a SegWit transaction. Basically, the submitter distinguishes the type of the utxo's address. If it's a SegWit type, then the transaction needs a signature script in the witness field to unlock the utxo, leaving the Signature field empty. For detailed explanation of scripting SegWit transactions, see here

@gitferry gitferry marked this pull request as ready for review November 25, 2022 03:47
return false, nil
case *btcutil.AddressWitnessPubKeyHash:
return true, nil
case *btcutil.AddressWitnessScriptHash:
Copy link
Collaborator

Choose a reason for hiding this comment

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

just curious what are ScriptHash and PubKeyHash?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Basically, with the former, you pay the funds to a customized script, which allows customized locking/unlocking mechanisms. With the latter, you pay the funds to a public key. Anyone with the corresponding private key can unlock the script. For detailed explanation, See here.

@gusin13 gusin13 self-requested a review November 25, 2022 04:51
@gitferry gitferry changed the title add witnessfor segwit tx feat: add witness signature for segwit tx Nov 25, 2022
Copy link
Member

@vitsalis vitsalis left a comment

Choose a reason for hiding this comment

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

looks good!


func isSegWit(addr btcutil.Address) (bool, error) {
switch addr.(type) {
case *btcutil.AddressPubKeyHash:
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use case lists here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, sure. Didn't realize we can use it. Thanks!


// add signature/witness depending on the type of the previous address
// if not segwit, add signature; otherwise, add witness
ok, err := isSegWit(utxo.Addr)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ok, err := isSegWit(utxo.Addr)
segwit, err := isSegWit(utxo.Addr)

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