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

Add SegWit support #15

Open
ilyavf opened this issue Aug 7, 2018 · 1 comment
Open

Add SegWit support #15

ilyavf opened this issue Aug 7, 2018 · 1 comment
Assignees

Comments

@ilyavf
Copy link
Owner

ilyavf commented Aug 7, 2018

Add an option for building SegWit transactions.

Sources:

Details:

    // P2PKH output script
    DUP HASH160 ab68025513c3dbd2f7b92a94e0581f5d50f654e7 EQUALVERIFY CHECKSIG
    // P2WPKH output script (includes: a witness version 0)
    0 ab68025513c3dbd2f7b92a94e0581f5d50f654e7
    // To spend the Segregated Witness output:
    [...]
    "vin" : [{
        "txid": "0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2",
        "vout": 0,
        "scriptSig": ""
    }]
    [...]
    "witness": "<Bob’s witness data>"
    [...]
  • Pay-to-Witness-Script-Hash (P2WSH)
    The second type of witness program corresponds to a Pay-to-Script-Hash (P2SH) script.
    Examples:
    // 1. P2SH
    // Includes 20-byte RIPEMD160(SHA256(script)) hash:
    HASH160 54c557e07dde5bb6cb791c7a540e0a4796f5e97e EQUAL

    // P2SH output being spent:
    "vout": 0,
     	 "scriptSig": “<SigA> <SigB> <2 PubA PubB PubC PubD PubE 5 CHECKMULTISIG>”,
    ]

    // 2. P2WSH output script
    // Includes a witness version (0) and 32-byte SHA256 hash of the redeem script.
    0 a9b7b38d972cabc7961dbfbcb841ad4508d133c47ba87457b4a0e8aae86dbb89

    // P2WSH output being spent with separate witness data
    "vout": 0,
     	 "scriptSig": “”,
    ]
    [...]
    "witness": "<SigA> <SigB> <2 PubA PubB PubC PubD PubE 5 CHECKMULTISIG>"
  • Wallet construction of P2WPKH
    • It is extremely important to note that P2WPKH should only be created by the payee (recipient) and not converted by the sender from a known public key, P2PKH script, or address. Sender does not know if the recipient has SegWit support.
    • P2WPKH outputs must be constructed from the hash of a compressed public key.

Transaction:

  • If all txins in a transaction are not associated with any witness data, the transaction MUST be serialized in the original transaction format.
  • Under segwit, each transaction will have 2 IDs.
@ilyavf ilyavf self-assigned this Aug 7, 2018
@ilyavf
Copy link
Owner Author

ilyavf commented Aug 16, 2018

API:

  • add option to choose what type of script should be built:
    • P2WPKH (pure SegWit);
    • P2SH (embed SegWit for older nodes).
  • distinguish between SegWit addresses (Bech32) and Base58Check.
// buildTx :: (Tx, Options) -> Buffer
const tx = {
  vin: [{
      txid: '2d7a9f0534ddac231ef1978bda388791c32321f7e14e18e7df3bbed261615f54',
      index: 1,
      script: '',   // ?
      segwit: ''    // ?
  }],
  vout: [{
    value: 100000000,
    address: 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx'
    segwit: 'P2WPKH'     // << define the output script type here.
  }, { ... }]
}
buildTx(tx)

Cases:

  1. VIN: regular UTXO; VOUT: P2SH-P2WPKH; regular transaction structure.

Steps:

  1. Create P2SH-P2WPKH address

ilyavf added a commit that referenced this issue Aug 20, 2018
Upgraded to bitcoinjs-lib v4. Related to #15
ilyavf added a commit that referenced this issue Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant