-
Notifications
You must be signed in to change notification settings - Fork 223
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
Implement BIP-143-like sighash #1598
Conversation
Codecov Report
@@ Coverage Diff @@
## v0.10.0-dev #1598 +/- ##
===============================================
- Coverage 60.01% 59.98% -0.03%
===============================================
Files 515 516 +1
Lines 20454 20494 +40
===============================================
+ Hits 12275 12293 +18
- Misses 6229 6247 +18
- Partials 1950 1954 +4
Continue to review full report at Codecov.
|
1a4847d
to
db5dc05
Compare
address.EncodeAddress(): key, | ||
}), mkGetScript(map[string][]byte{ | ||
scriptAddr.EncodeAddress(): scriptPubKey.Script, | ||
})); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reformat the above five lines for readability's sake.
const ( | ||
SigHashAll SigHashType = 0x1 | ||
SigHashNone SigHashType = 0x2 | ||
SigHashSingle SigHashType = 0x3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'll be a tiny bit more clear that this is a bit field if you write it as 0x01.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could even go full literal: 0b00000001
which makes it easier to see the bits and masks with the naked eye (ie SigHashMask = 0x1f
becomes: 0b00011111
)
} | ||
hashWriter := hashes.NewTransactionSigningHashWriter() | ||
hashTxOut(hashWriter, tx.Outputs[inputIndex]) | ||
return hashWriter.Finalize() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should update reusedValues.outputsHash here and only return later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this outputsHash isn't cached, because if it's single then each input will have its own outputsHash, you can't cache them between inputs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
domain/consensus/utils/consensushashing/calculate_signature_hash.go
Outdated
Show resolved
Hide resolved
domain/consensus/utils/consensushashing/calculate_signature_hash.go
Outdated
Show resolved
Hide resolved
if hashType.isSigHashNone() { | ||
return externalapi.NewZeroHash() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a meaningful difference between this and the bitcoin approach.
the difference is how do we handle unknown sighashes, in bitcoin unknown hash types will be all zeros here, and in this code SigHashAll
will be the default.
I'm not sure which is better and what kinds of validation happen to the sighash before this function (maybe only valid known sighashes can enter this function)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for getSequencesHash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a validation in the beggining of CalculateSignatureHash that makes sure that the SigHashType passed is a standard one, so we are not going to have a default (it will error out if you pass 0)
domain/consensus/utils/consensushashing/calculate_signature_hash_test.go
Outdated
Show resolved
Hide resolved
}), mkGetScript(nil), nil); err != nil { | ||
if err := signAndCheck(msg, tx, i, scriptPubKey, hashType, mkGetKey(map[string]*secp256k1.SchnorrKeyPair{ | ||
address.EncodeAddress(): key, | ||
}), mkGetScript(nil)); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reformat these three lines as well.
SigHashAnyOneCanPay SigHashType = 0x80 | ||
SigHashAll SigHashType = 0b00000001 | ||
SigHashNone SigHashType = 0b00000010 | ||
SigHashSingle SigHashType = 0b00000100 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's 4, it should be 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is deliberate.
@stasatdaglabs asked me to do this so that SigHashType is a true bitfield.
LMK if you disagree
func (sht SigHashType) isStandardSigHashType() bool { | ||
switch sht { | ||
case SigHashAll, SigHashNone, SigHashSingle, | ||
SigHashAll | SigHashAnyOneCanPay, SigHashNone | SigHashAnyOneCanPay, SigHashSingle | SigHashAnyOneCanPay: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure this is good and correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As sure as one can be.
Many things inside the code and tests of CalculateSignatureHash assume the above is correct.
If it is not - we should fix both it and the code.
hashType: 0x04, | ||
hashType: 0b00000011, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you meant 0b00000100
?
…o 1341-bip-143-sighash
This PR resolves #1341
Complete spec: https://github.com/kaspanet/docs/blob/main/Specs/BIP143-like%20SigHashes.md