Skip to main content
deleted 79 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
  1. Segregated witness outputs (only implement this after miners have accepted segwit, or you may lose money)

    There are two ways of signaling that a scriptPubKey should be interpreted as a segregated witness program:

    • Native witness program

         <version byte> <2 to 40 bytes of witness data>
      

      The version byte must be equal to or less than 16.

    • P2SH wrapped witness program

         OP_HASH160 <20 bytes of script hash> OP_EQUAL
      

      The scriptSig must have exactly one item. That item is interpreted as a witness program, as above.

     

    Once you extract the witness program, you must match it to one of the witness program types:

    • P2WPKH

         0 <20 bytes of public key hash>
      

      This is hashed identically to P2PKH.

    • P2WSH

         0 <32 bytes of witness script hash>
      

      This is pretty similar to P2SH, except that the script hash is SHA256, not HASH160.

    • Other version 0 scripts, not 20 or 32 bytes in length, are nobody-can-spend, so ignore them.

     

    Note that any witness program format can be combined with any witness program. For example, you can have P2SH wrapping P2WSH, like this:

    scriptPubKey:

     OP_HASH160 <20 byte hash of scriptSig element> OP_EQUAL
    

    scriptSig:

     <34 byte serialized witness program>
    

    unserialized witness program:

     0 <32 byte hash of first witness item>
    

    Then, the first witness item is unpacked and interpreted as the actual program.

    More information here.

  2. m-of-n bare multisig

  1. Segregated witness outputs (only implement this after miners have accepted segwit, or you may lose money)

    There are two ways of signaling that a scriptPubKey should be interpreted as a segregated witness program:

    • Native witness program

         <version byte> <2 to 40 bytes of witness data>
      

      The version byte must be equal to or less than 16.

    • P2SH wrapped witness program

         OP_HASH160 <20 bytes of script hash> OP_EQUAL
      

      The scriptSig must have exactly one item. That item is interpreted as a witness program, as above.

     

    Once you extract the witness program, you must match it to one of the witness program types:

    • P2WPKH

         0 <20 bytes of public key hash>
      

      This is hashed identically to P2PKH.

    • P2WSH

         0 <32 bytes of witness script hash>
      

      This is pretty similar to P2SH, except that the script hash is SHA256, not HASH160.

    • Other version 0 scripts, not 20 or 32 bytes in length, are nobody-can-spend, so ignore them.

     

    Note that any witness program format can be combined with any witness program. For example, you can have P2SH wrapping P2WSH, like this:

    scriptPubKey:

     OP_HASH160 <20 byte hash of scriptSig element> OP_EQUAL
    

    scriptSig:

     <34 byte serialized witness program>
    

    unserialized witness program:

     0 <32 byte hash of first witness item>
    

    Then, the first witness item is unpacked and interpreted as the actual program.

    More information here.

  2. m-of-n bare multisig

  1. Segregated witness outputs

    There are two ways of signaling that a scriptPubKey should be interpreted as a segregated witness program:

    • Native witness program

         <version byte> <2 to 40 bytes of witness data>
      

      The version byte must be equal to or less than 16.

    • P2SH wrapped witness program

         OP_HASH160 <20 bytes of script hash> OP_EQUAL
      

      The scriptSig must have exactly one item. That item is interpreted as a witness program, as above.

     

    Once you extract the witness program, you must match it to one of the witness program types:

    • P2WPKH

         0 <20 bytes of public key hash>
      

      This is hashed identically to P2PKH.

    • P2WSH

         0 <32 bytes of witness script hash>
      

      This is pretty similar to P2SH, except that the script hash is SHA256, not HASH160.

    • Other version 0 scripts, not 20 or 32 bytes in length, are nobody-can-spend, so ignore them.

     

    Note that any witness program format can be combined with any witness program. For example, you can have P2SH wrapping P2WSH, like this:

    scriptPubKey:

     OP_HASH160 <20 byte hash of scriptSig element> OP_EQUAL
    

    scriptSig:

     <34 byte serialized witness program>
    

    unserialized witness program:

     0 <32 byte hash of first witness item>
    

    Then, the first witness item is unpacked and interpreted as the actual program.

    More information here.

  2. m-of-n bare multisig

added 1769 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132

This is now mostly unused, except by miners, but there are still unspent outputs in the form:

  1. m-of-n bare multisig

    Segregated witness outputs (only implement this after miners have accepted segwit, or you may lose money)

    There are two ways of signaling that a scriptPubKey should be interpreted as a segregated witness program:

    • Native witness program

         <version byte> <2 to 40 bytes of witness data>
      

      The version byte must be equal to or less than 16.

    • P2SH wrapped witness program

         OP_HASH160 <20 bytes of script hash> OP_EQUAL
      

      The scriptSig must have exactly one item. That item is interpreted as a witness program, as above.

     

    Once you extract the witness program, you must match it to one of the witness program types:

    • P2WPKH

         0 <20 bytes of public key hash>
      

      This is hashed identically to P2PKH.

    • P2WSH

         0 <32 bytes of witness script hash>
      

      This is pretty similar to P2SH, except that the script hash is SHA256, not HASH160.

    • Other version 0 scripts, not 20 or 32 bytes in length, are nobody-can-spend, so ignore them.

     

    Note that any witness program format can be combined with any witness program. For example, you can have P2SH wrapping P2WSH, like this:

    scriptPubKey:

     OP_HASH160 <20 byte hash of scriptSig element> OP_EQUAL
    

    scriptSig:

     <34 byte serialized witness program>
    

    unserialized witness program:

     0 <32 byte hash of first witness item>
    

    Then, the first witness item is unpacked and interpreted as the actual program.

    More information here.

  2. m-of-n bare multisig

  1. Data-carrying, output
  1. Data-carrying output

This is now mostly unused, but there are still unspent outputs in the form:

  1. m-of-n bare multisig
  1. Data-carrying, output

This is now mostly unused, except by miners, but there are still unspent outputs in the form:

  1. Segregated witness outputs (only implement this after miners have accepted segwit, or you may lose money)

    There are two ways of signaling that a scriptPubKey should be interpreted as a segregated witness program:

    • Native witness program

         <version byte> <2 to 40 bytes of witness data>
      

      The version byte must be equal to or less than 16.

    • P2SH wrapped witness program

         OP_HASH160 <20 bytes of script hash> OP_EQUAL
      

      The scriptSig must have exactly one item. That item is interpreted as a witness program, as above.

     

    Once you extract the witness program, you must match it to one of the witness program types:

    • P2WPKH

         0 <20 bytes of public key hash>
      

      This is hashed identically to P2PKH.

    • P2WSH

         0 <32 bytes of witness script hash>
      

      This is pretty similar to P2SH, except that the script hash is SHA256, not HASH160.

    • Other version 0 scripts, not 20 or 32 bytes in length, are nobody-can-spend, so ignore them.

     

    Note that any witness program format can be combined with any witness program. For example, you can have P2SH wrapping P2WSH, like this:

    scriptPubKey:

     OP_HASH160 <20 byte hash of scriptSig element> OP_EQUAL
    

    scriptSig:

     <34 byte serialized witness program>
    

    unserialized witness program:

     0 <32 byte hash of first witness item>
    

    Then, the first witness item is unpacked and interpreted as the actual program.

    More information here.

  2. m-of-n bare multisig

  1. Data-carrying output
added 446 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132

These are the scriptPubKey's that are standard, arranged in order of popularity:

  1. P2PKH (Pay to public key hash)

     OP_DUP OP_HASH160 <20 bytes of public key hash> OP_EQUALVERIFY OP_CHECKSIG
    
  2. P2SH (Pay to script hash)

     OP_HASH160 <20 bytes of script hash> OP_EQUAL
    
  3. P2PK (Pay to public key)

This is now mostly unused, but there are still unspent outputs in the form:

    <33 or 65 bytes of public key> OP_CHECKSIG
  1. m-of-n bare multisig

Also pretty rare.

    <m> [n <public key>s] <n> OP_CHECKMULTISIG
  1. Data-carrying, output

Only one of these is allowed per transaction. These outputs can never be spent, so you probably don't need to detect them.

    OP_RETURN <less than 80 bytes of data>

Should I implement more sophisticated script parser which can detect "non-standard" scripts as well?

No. It's a waste of development time, when nobody uses transactions like that. (If they did, those transaction types would be added to the list of standard transactions. :)) 

It's also going to be very difficult to implement correctly. If you don't do it correctly, then you'll think you have coins when you really don't. For example, theimagine someone sends you a transaction with a scriptPubKey like this:

OP_DUP OP_HASH160 <20 bytes of public key hash> OP_2DROP OP_CHECKSIG

.. Note that this is like a P2PKH output, except that OP_EQUALVERIFY has been replaced by OP_2DROP.is spendable That means that the script checks that the signature is correct, but doesn't check that it's signed by the right key.

That means that anybody can spend it, not just you. But does your script parser know that?

(NoteCorrectly detecting edge cases like these make writing a program that OP_EQUALVERIFY has been replaced by OP_2DROP in this examplecan understand nonstandard outputs more trouble than it's worth.)

These are the scriptPubKey's that are standard, arranged in order of popularity:

  1. P2PKH (Pay to public key hash)

     OP_DUP OP_HASH160 <20 bytes of public key hash> OP_EQUALVERIFY OP_CHECKSIG
    
  2. P2SH (Pay to script hash)

     OP_HASH160 <20 bytes of script hash> OP_EQUAL
    
  3. P2PK (Pay to public key)

This is now mostly unused, but there are still unspent outputs in the form:

    <33 or 65 bytes of public key> OP_CHECKSIG
  1. m-of-n bare multisig

Also pretty rare.

    <m> [n <public key>s] <n> OP_CHECKMULTISIG

Should I implement more sophisticated script parser which can detect "non-standard" scripts as well?

No. It's a waste of development time, when nobody uses transactions like that. (If they did, those transaction types would be added to the list of standard transactions. :)) It's also going to be very difficult to implement correctly. If you don't do it correctly, then you'll think you have coins when you really don't. For example, the transaction:

OP_DUP OP_HASH160 <20 bytes of public key hash> OP_2DROP OP_CHECKSIG

...is spendable by anybody, not just you. But does your script parser know that?

(Note that OP_EQUALVERIFY has been replaced by OP_2DROP in this example.)

These are the scriptPubKey's that are standard, arranged in order of popularity:

  1. P2PKH (Pay to public key hash)

     OP_DUP OP_HASH160 <20 bytes of public key hash> OP_EQUALVERIFY OP_CHECKSIG
    
  2. P2SH (Pay to script hash)

     OP_HASH160 <20 bytes of script hash> OP_EQUAL
    
  3. P2PK (Pay to public key)

This is now mostly unused, but there are still unspent outputs in the form:

    <33 or 65 bytes of public key> OP_CHECKSIG
  1. m-of-n bare multisig

Also pretty rare.

    <m> [n <public key>s] <n> OP_CHECKMULTISIG
  1. Data-carrying, output

Only one of these is allowed per transaction. These outputs can never be spent, so you probably don't need to detect them.

    OP_RETURN <less than 80 bytes of data>

Should I implement more sophisticated script parser which can detect "non-standard" scripts as well?

No. It's a waste of development time, when nobody uses transactions like that. (If they did, those transaction types would be added to the list of standard transactions. :)) 

It's also going to be very difficult to implement correctly. If you don't do it correctly, then you'll think you have coins when you really don't. For example, imagine someone sends you a transaction with a scriptPubKey like this:

OP_DUP OP_HASH160 <20 bytes of public key hash> OP_2DROP OP_CHECKSIG

Note that this is like a P2PKH output, except that OP_EQUALVERIFY has been replaced by OP_2DROP. That means that the script checks that the signature is correct, but doesn't check that it's signed by the right key.

That means that anybody can spend it, not just you. Correctly detecting edge cases like these make writing a program that can understand nonstandard outputs more trouble than it's worth.

added 95 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading
added 77 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading
added 33 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading
added 167 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading
added 2 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading
added 14 characters in body
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading
Source Link
Nick ODell
  • 29.5k
  • 11
  • 73
  • 132
Loading