Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Segwit serialization support and nested p2sh signing #50

Merged
merged 23 commits into from
Mar 11, 2018

Conversation

braydonf
Copy link
Contributor

@braydonf braydonf commented Mar 12, 2016

Adds initial segwit support, including:

  • New transaction serialization that includes witness data
  • Nested p2sh segwit transaction creation, signing and verification (Ability to use segwit with p2sh multisig addresses.)
  • Verification of segwit transaction scripts
    • pay-to-witness-public-key-hash
    • pay-to-witness-script-hash
    • pay-to-nested-p2sh-witness

Closes: #49
Closes: #42

return writer.toBuffer();
};

MultiSigScriptHashInput.prototype.getSatoshisBuffer = function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe move this to input? It helps keep pay-to-script-hash/witness-scripthash handling more general

@bityogi
Copy link

bityogi commented Mar 24, 2016

+1. I'll be following this one closely. Thanks for working on this.

@rubensayshi
Copy link
Contributor

@braydonf what's the status on this PR and if it's not finished what is left to do and how can I help?

@braydonf
Copy link
Contributor Author

braydonf commented Jul 5, 2016

Rebased on latest master.

@braydonf
Copy link
Contributor Author

braydonf commented Jul 5, 2016

@rubensayshi from what I remember, I think these need to be finished (for the initial merge):

  • Adding coverage by pulling test cases from: https://github.com/bitcoin/bitcoin/blob/master/src/test/transaction_tests.cpp#L422-L612 (nested witness p2sh is only tested currently). There could be additional changes necessary to get the tests to pass.
  • Cleanup and refactoring
  • Remove segnet network and test with testnet, and update for any changes that have happened since segnet3
  • Code, API and backwards compatibility review. For example witness serialization should likely be enabled if the transaction is segwit for example by default. I think all transactions will be serialized with witness data currently.
  • Updating documentation and include examples

Edit: Added this list to a task list in the PR. If there anything else please make a note.

* @return {Address}
*/
Address.createMultisig = function(publicKeys, threshold, network) {
Address.createMultisig = function(publicKeys, threshold, network, nestedWitness) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would Address.createP2SH be better here, allowing arbitrary scripts to be passed in? Technically, multisig outputs don't have an address, but P2SH outputs do

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Address.payingTo is for allowing arbitrary scripts to be passed in. Address.createMultisig is for creating multisig p2sh addresses, as bare multisig doesn't have an address, as you mention.

@afk11
Copy link
Contributor

afk11 commented Jul 5, 2016

Adding coverage by pulling test cases from: https://github.com/bitcoin/bitcoin/blob/master/src/test/transaction_tests.cpp#L422-L612 (nested witness p2sh is only tested currently). There could be additional changes necessary to get the tests to pass.

FWIW: https://github.com/bitcoin/bitcoin/blob/master/src/test/script_tests.cpp#L857 this might also be useful if you don't test against it already. It's mainly for verification purposes, but may turn up bugs outside of this PR.

When working on signing, it's best to define your standard types (p2pk/p2pkh/multisig) and be able to deal with them anywhere P2SH, P2WSH comes in, since both of these can be used in tandem. An example of things maybe going awry is this file: https://github.com/bitpay/bitcore-lib/blob/master/lib/transaction/input/multisigscripthash.js & the multisig.js that was added after. P2SH & P2WSH require general modifications to a plain script-in-txout-script style transaction. AFAICT Bitcoin Core does this as well.

I would submit a patch, but it's not a trivial refactoring.. just my two cents worth! See serializeSimpleSig / serializeSignatures https://github.com/Bit-Wasp/bitcoin-php/blob/master/src/Transaction/Factory/InputSigner.php#L460-544. The if branches for P2SH, then witness, are repeated anywhere in that file that deals with possible combinations of P2SH | P2WSH.

@braydonf
Copy link
Contributor Author

braydonf commented Jul 6, 2016

@braydonf braydonf changed the base branch from master to 1.0.0 September 30, 2016 15:29
@braydonf braydonf modified the milestones: 1.0.0, 1.1.0 Sep 30, 2016
@braydonf braydonf changed the title WIP: Segwit nested p2sh signing Segwit nested p2sh signing Sep 30, 2016
@braydonf braydonf changed the title Segwit nested p2sh signing Segwit serialization support and nested p2sh signing Oct 4, 2016
@btcdrak
Copy link

btcdrak commented Oct 5, 2016

@braydonf
Copy link
Contributor Author

braydonf commented Oct 5, 2016

@gabegattis gabegattis mentioned this pull request Oct 18, 2016
@afk11
Copy link
Contributor

afk11 commented Nov 2, 2016

bump

@afk11
Copy link
Contributor

afk11 commented Dec 22, 2016

What's the status of this PR?

@braydonf
Copy link
Contributor Author

It's ready for testing, and can go on 0.13 or 1.0.0 branches.

@afk11
Copy link
Contributor

afk11 commented Dec 22, 2016

I guess I mean, how are things going supporting [P2PK, P2PKH, multisig] in any representation of [P2SH, P2WSH, P2SH|P2WSH]? Currently there's only a buildWitnessMultisigOutFromScript

@braydonf
Copy link
Contributor Author

braydonf commented Dec 22, 2016

The scripts for pay-to-witness-public-key-hash and pay-to-witness-script-hash can be built and verified, there are tests for this case. However I don't think there are convenience methods for them. It's related to the deferred BIP142 https://github.com/bitcoin/bips/blob/master/bip-0142.mediawiki

So I think adding native segwit convenience methods can be made with another PR, while this one can bring in what is necessary to support the transaction serialization and script verification.

@afk11
Copy link
Contributor

afk11 commented Feb 7, 2017

Ready to go?

@braydonf braydonf changed the base branch from 1.0.0 to master February 19, 2017 17:59
braydonf and others added 9 commits February 19, 2017 13:29
Using this code in the test to get test data:
```
string TxHexStr(CTransaction tx)
{
    CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
    ssTx << tx;
    std::string ssTxStr = ssTx.str();
    return HexStr(ssTxStr);
}

std::cout << "scriptPubkey1: " + HexStr(scriptPubkey1) + "\n";
std::cout << "output1: " + TxHexStr(output1) + "\n";
```

From commit bitcoin core 0.13.0 commit: a402396dce64c42ea73535b7dde4a9164d430438
In the file: src/test/transaction_tests.cpp#L493
From commit bitcoin core 0.13.0 commit: a402396dce64c42ea73535b7dde4a9164d430438
In the file: src/test/transaction_tests.cpp

- pay-to-compressed publickey (v0)
- p2sh witness pay-to-compressed pubkey (v0)
- witness 2-of-2 multisig
- p2sh witness 2-of-2 multisig
@afk11
Copy link
Contributor

afk11 commented May 2, 2017

What is the ETA for this to be merged? Does it currently sign all the previously supported script types with P2SH and P2SH|P2WSH?

@gabegattis
Copy link
Contributor

@afk11 I would not expect to see any additional work on this PR until segwit activation appears imminent.

@sheerlox
Copy link

Any news ?

@afk11
Copy link
Contributor

afk11 commented Jul 18, 2017

Per an email to the Segwit 2x mailing list, parts of the bitcore project are being discontinued (called it #119) in favor of bcoin.

It's genuinely frustrating I, and certainly @braydonf, put effort into adding segwit support for this library 19 months ago (with others offering to help), for it to be discontinued when people have been testing other implementations for as long.

I guess that's what you get for using a business backed bitcoin implementation - and I will hesitate before I do so again (looking at you bcoin)

@h0jeZvgoxFepBQ2C
Copy link

Is there still progress or do I have to move away from copay/bitcore?

@afk11
Copy link
Contributor

afk11 commented Aug 17, 2017

@lichtamberg looks like it. They're suggesting everyone upgrades to btc1 https://blog.bitpay.com/bitcore-segwit-activation/

@h0jeZvgoxFepBQ2C
Copy link

Lol, I would never use this shit BTC1.. And I'm shocked that bitpay is distributing malware now: https://twitter.com/larrybitcoin/status/898188496015425536

Bitpay was a really nice company, but within the last months they went fully retard...

@braydonf
Copy link
Contributor Author

braydonf commented Aug 18, 2017

FYI:

Satoshi Labs, @Runn1ng, has been maintaining a branch that supports segwit for quite some time now:

Also Bitcore patches for indexing were applied to 0.13 Bitcoin Core a year ago, which also supports segwit (I made sure that these were applied to support segwit when activated):

Running bitcore with insight-api and etc. is ready to go with segwit with this PR and those branches.

@afk11
Copy link
Contributor

afk11 commented Aug 18, 2017

@braydonf

Bitcore-node is really not the problem, bitcore-lib is. Bitcore-lib / this repository does not have the BIP143, BIP141 code, unless it's in this pull request (which is also incomplete). So users of this library can't sign segwit transactions, or create segwit addresses, etc.

@bobquest33
Copy link

Why this https://blog.bitpay.com/bitcore-segwit-activation/ was posted, what is the stand of bitpay and bitcore will it only support segwit2x. Has the orginal segwit implemented or not. Please clarify.

@karelbilek
Copy link

@afk11 if you care only about backend for insight/bitcore, this is enough, you don't need the code for creating segwit addresses.

Also you don't need that for copay either, it doesn't use segwit currently.

So if you don't plan to also update copay to use segwit addresses for user's addresses something like this is enough.

@braydonf
Copy link
Contributor Author

An update on this:

There is a small downside to using a nested witness script; the transaction is actually larger than direct P2WPKH or P2PKH, so it is less disk and bandwidth efficient. However it may still mean less fees, especially for the case of a large number of signers. It was unclear what the address would be for segwit at the time this was implemented.

Now with BIP173 and bech32, this is a better alternative to using a nested witness script. And there is a push to, at the minimum, support sending to bech32 addresses in more wallets: https://whensegwit.com/

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

Successfully merging this pull request may close these issues.