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

Transaction: Witness serialization support default only for some func… #701

Merged
merged 9 commits into from
Nov 14, 2016

Conversation

dcousens
Copy link
Contributor

@dcousens dcousens commented Nov 14, 2016

…tions

  • Add [originally failing] tests to ensure TXID is encoded without SegWit

@dcousens dcousens added this to the 3.0.0 milestone Nov 14, 2016
@dcousens dcousens self-assigned this Nov 14, 2016
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.toBuffer(buffer, 0)
txTmp.__toBuffer(buffer, 0, false)
Copy link
Contributor Author

@dcousens dcousens Nov 14, 2016

Choose a reason for hiding this comment

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

@afk11 this is my understanding, hashForSignature (the original) should always sign the legacy serialisation of a transaction.

Copy link
Contributor

Choose a reason for hiding this comment

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

For now that makes sense! There might be room for a convenience function that takes (scriptCode, sigVersion, sigHashType, [amount]) and returns the appropriate hash? It's not very long!

@@ -395,7 +399,11 @@ Transaction.prototype.getId = function () {
}

Transaction.prototype.toBuffer = function (buffer, initialOffset) {
if (!buffer) buffer = new Buffer(this.byteLength())
return this.__toBuffer(buffer, initialOffset, true)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note the public API does not change

@@ -386,7 +390,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
}

Transaction.prototype.getHash = function () {
return bcrypto.hash256(this.toBuffer())
return bcrypto.hash256(this.__toBuffer(undefined, undefined, false))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has to use an internal function such that getHash is always returning the legacy TXID of the transaction, not WTXID

@dcousens
Copy link
Contributor Author

dcousens commented Nov 14, 2016

@afk11 the only hard parts of any of this: naming things, and finding test fixtures

@afk11
Copy link
Contributor

afk11 commented Nov 14, 2016

I'll dig up the wtxid tomorrow - the witness fixtures are on testnet!

@dcousens
Copy link
Contributor Author

Rebased and now adds failing fixtures before adding a fix commit.

For example, 16abf3e was failing , but fixed with 2683391, as setWitness was not accepting valid arguments.

7a54826 was failing due to the default serialization being to always serialize witnesses if available.
Fixed by 44e386e.

"raw": {
"version": 1,
"ins": [
{
"hash": "97990032af6e5c1741021c017cdd86b081019de4189ee995486941e10af4ae93",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bitcoin core has ambiguity between hash and an id.
For testing purposes, we always call the hash the raw hex string which would have been returned by sha256, and the id is reversed.

This amends the test fixtures to reflect that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Doh, I should have double checked that.

@@ -311,13 +269,12 @@
"id": "d4ebb5e09c06dd375f265b51eb7f1a14d12deba71329500e2a00a905fdfdc27d",
"hash": "7dc2fdfd05a9002a0e502913a7eb2dd1141a7feb515b265f37dd069ce0b5ebd4",
"hex": "0100000001a30e865fa60f6c25a8b218bb5a6b9acc7cf3f1db2f2e3a7114b51af5d6ae811f000000006c473044022026d2b56b6cb0269bf4e80dd655b9e917019e2ccef57f4b858d03bb45a2da59d9022010519a7f327f03e7c9613e0694f929544af29d3682e7ec8f19147e7a86651ecd012321038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2bacffffffff0160ea0000000000001976a914851a33a5ef0d4279bd5854949174e2c65b1d450088ac00000000",
"hasWitness": false,
"witnessHex": "",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no need having both

@@ -95,7 +94,7 @@ describe('Transaction', function () {
it('exports ' + f.description + ' (' + f.id + ')', function () {
var actual = fromRaw(f.raw)

assert.strictEqual(actual.toHex(), f.hex, actual.toHex())
assert.strictEqual(actual.toHex(), f.whex || f.hex, actual.toHex())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can import both, but since this exports from the raw data, we have to test against whex first.


assert.strictEqual(actual.toHex(), f.hex, actual.toHex())
var actual = fromRaw(f.raw, true)
assert.strictEqual(actual.toHex(), f.hex)
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

might as well test both, because we can

@dcousens
Copy link
Contributor Author

@afk11 we can put getWitnessHash and getWitnessId in another PR.
They are not limiting this one in any way, and not really related either.

@dcousens
Copy link
Contributor Author

hasWitnesses being added to the public API should be discussed in #703 first.

This PR purely focuses on fixing the getId and import/export ambiguity issues.

@dcousens
Copy link
Contributor Author

dcousens commented Nov 14, 2016

Good to merge! Waiting on tests

@dcousens dcousens added the bug label Nov 14, 2016
@dcousens dcousens merged commit 66ad980 into master Nov 14, 2016
@dcousens dcousens deleted the witnessid branch November 14, 2016 04:05
@dcousens
Copy link
Contributor Author

@afk11 nothing controversial here AFAIK, so I've merged to ease the rebase hell. But please still review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants