<p>wow, cool question, that's not very easy. Especially as there are different Segwit tx thinkable (native P2WPKH, P2SH-P2WPKH, Native P2WSH, P2SH-P2WSH or one with Bech32 address). Then there is the point of adapting all the length parameters (for signatures and scripts). I will give it a try with a standard P2WPKH. So first I'd decode your provided tx, I get this:</p>

<pre><code>VERSION
 01000000

TX_IN COUNT [var_int]: hex=03, decimal=3
 TX_IN[0] OutPoint hash 9D62373BF1838B4E4F497836DB0D8EDEFFBCAD64B1474904BCDA8DC37937C0B0
 TX_IN[0] OutPoint index hex=01000000, reversed=00000001, decimal=1
 TX_IN[0] Script Length hex=6B, decimal=107
 TX_IN[0] Script Sig 
  483045022100F3C0F555CD39198CAF77F6756256801FC57BCABE4892601B9DEF52259698F40A0220230262841BFF151
EB617F7D4CDDA6BF06D6FA791230C152E47F68BCDD0E6F64A01210204B3506D8903CA601C97A4ABAB6548E91004C535A5
A45E21299A494B146859CA 
 TX_IN[0] Sequence (uint32_t) FFFFFFFF

 TX_IN[1] OutPoint hash B3CA9199969BD474BA0A609A1C01C3B6C2EB97885FC1DCAD2CD1704BE5EA0E06
 TX_IN[1] OutPoint index hex=00000000, reversed=00000000, decimal=0
 TX_IN[1] Script Length hex=6B, decimal=107
 TX_IN[1] Script Sig
  4830450221009DBEB64DDD4646E1118503C87FEAA95C531B5178C6E543BE782F6ECB05E8FBE602203945570F8DC56C145617F0283FA4032E0B7895A9CF2A81C7B65665B8AC608CB00121026602A5DC59C30F485B2C457AC8E2F617E27B10A1D2AE76F3231F9B01DFF08964 
 TX_IN[1] Sequence FFFFFFFF

 TX_IN[2] OutPoint hash 3E90870CB2B89307514498D010EE1A1F724EE578859B8F118902DB08A45B717F
 TX_IN[2] OutPoint index hex=01000000, reversed=00000001, decimal=1
 TX_IN[2] Script Length hex=6B, decimal=107
 TX_IN[2] Script Sig 
  48304502210095E300886EC4DF78E39D6D0CF5E5E531DED8F42F00E5730C371D8951867DAD5B02203C55F9403F6C2AAC444213161A93A86661E7367D4007BE74FFB3981387CC1C790121023F0AADFEB71A4964C5087FEC8B052C8236051DAE838D4747543330C6B266CE6E 
 TX_IN[2] Sequence FFFFFFFF

TX_OUT COUNT, hex=02, decimal=2
 TX_OUT[0] Value hex=D578110000000000, dec=1145045, bitcoin=0.01145045
 TX_OUT[0] PK_Script Length hex=19, dec=25
 TX_OUT[0] pk_script 76A914342AB422C9E3EF285EFE9882AE54269ED9713DD688AC
 TX_OUT[1] Value hex=20A1070000000000, dec=500000, bitcoin=0.00500000
 TX_OUT[1] PK_Script Length hex=19, dec=25
 TX_OUT[1] pk_script 76A91488D924F51033B74A895863A5FB57FD545529DF7D88AC

 LOCK_TIME 00000000
</code></pre>

<p>Now I make a P2WPKH tx out of it. In general, the segwit  transaction is composed this way: </p>

<blockquote>
  <p>[nVersion][marker][flag][txins][txouts][witness][nLockTime]</p>
</blockquote>

<pre><code>VERSION
 01000000

SEGWIT (BIP141): this is a segwit tx, marker=00
       (BIP141): flag=01

TX_IN COUNT [var_int]: hex=03, decimal=3
  TX_IN[0] 9D62373BF1838B4E4F497836DB0D8EDEFFBCAD64B1474904BCDA8DC37937C0B0
  TX_IN[0] hex=01000000, reversed=00000001, decimal=1
  TX_IN[0] Script Length hex=00, decimal=0
  TX_IN[0] Sequence (uint32_t) FFFFFFFF

  TX_IN[1] B3CA9199969BD474BA0A609A1C01C3B6C2EB97885FC1DCAD2CD1704BE5EA0E06
  TX_IN[1] hex=00000000, reversed=00000000, decimal=0
  TX_IN[1] Script Length hex=00, decimal=0
  TX_IN[1] Sequence (uint32_t) FFFFFFFF

  TX_IN[2] 3E90870CB2B89307514498D010EE1A1F724EE578859B8F118902DB08A45B717F
  TX_IN[2] hex=01000000, reversed=00000001, decimal=1
  TX_IN[2] Script Length hex=00, decimal=0
  TX_IN[2] Sequence (uint32_t) FFFFFFFF

TX_OUT COUNT, hex=02, decimal=2
  TX_OUT[0] Value hex=D578110000000000, dec=1145045, bitcoin=0.01145045
  TX_OUT[0] PK_Script Length hex=19, dec=25
  TX_OUT[0] pk_script 76A914342AB422C9E3EF285EFE9882AE54269ED9713DD688AC
 TX_OUT[1]
  TX_OUT[1] Value hex=20A1070000000000, dec=500000, bitcoin=0.00500000
  TX_OUT[1] PK_Script Length hex=19, dec=25
  TX_OUT[1] pk_script 76A91488D924F51033B74A895863A5FB57FD545529DF7D88AC

WITNESS TXIN[0] stack elements: hex=02, decimal=2
 WITNESS[0] data length (var_int), hex=48, decimal=72, data(uchar[]): 
  3045022100F3C0F555CD39198CAF77F6756256801FC57BCABE4892601B9DEF52259698F4
  0A0220230262841BFF151EB617F7D4CDDA6BF06D6FA791230C152E47F68BCDD0E6F64A01
 WITNESS[1] data length (var_int), hex=21, decimal=33, data(uchar[]):
  0204B3506D8903CA601C97A4ABAB6548E91004C535A5A45E21299A494B146859CA

WITNESS TXIN[1] stack elements: hex=02, decimal=2
 WITNESS[0] data length (var_int), hex=48, decimal=72, data(uchar[]):
  30450221009DBEB64DDD4646E1118503C87FEAA95C531B5178C6E543BE782F6ECB05E8FB
  E602203945570F8DC56C145617F0283FA4032E0B7895A9CF2A81C7B65665B8AC608CB001
 WITNESS[1] data length (var_int), hex=21, decimal=33, data(uchar[]):
  026602A5DC59C30F485B2C457AC8E2F617E27B10A1D2AE76F3231F9B01DFF08964

WITNESS TXIN[2] stack elements: hex=02, decimal=2
 WITNESS[0] data length (var_int), hex=48, decimal=72, data(uchar[]):
  304502210095E300886EC4DF78E39D6D0CF5E5E531DED8F42F00E5730C371D8951867DAD
  5B02203C55F9403F6C2AAC444213161A93A86661E7367D4007BE74FFB3981387CC1C7901
 WITNESS[1] data length (var_int), hex=21, decimal=33, data(uchar[]):
  023F0AADFEB71A4964C5087FEC8B052C8236051DAE838D4747543330C6B266CE6E

 LOCK_TIME
00000000
</code></pre>

<p>So I assume the segwit pattern can clearly be recognized, see that the signature length is given as "0", and at the end appears per input the witness data. More to it can be found in the <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">corresponding BIPs</a>, and of course in <a href="https://bitcoincore.org/en/segwit_wallet_dev/" rel="noreferrer">the wiki</a>. </p>

<p>and the serialized hex would be this:</p>

<blockquote>
  <p>01000000<strong>0001</strong>03b0c03779c38ddabc044947b164adbcffde8e0ddb3678494f4e8b83f13b37629d0100000000ffffffff060eeae54b70d12caddcc15f8897ebc2b6c3011c9a600aba74d49b969991cab30000000000ffffffff7f715ba408db0289118f9b8578e54e721f1aee10d09844510793b8b20c87903e0100000000ffffffff02d5781100000000001976a914342ab422c9e3ef285efe9882ae54269ed9713dd688ac20a10700000000001976a91488d924f51033b74a895863a5fb57fd545529df7d88ac<strong>02</strong>483045022100f3c0f555cd39198caf77f6756256801fc57bcabe4892601b9def52259698f40a0220230262841bff151eb617f7d4cdda6bf06d6fa791230c152e47f68bcdd0e6f64a01210204b3506d8903ca601c97a4abab6548e91004c535a5a45e21299a494b146859ca024830450221009dbeb64ddd4646e1118503c87feaa95c531b5178c6e543be782f6ecb05e8fbe602203945570f8dc56c145617f0283fa4032e0b7895a9cf2a81c7b65665b8ac608cb00121026602a5dc59c30f485b2c457ac8e2f617e27b10a1d2ae76f3231f9b01dff089640248304502210095e300886ec4df78e39d6d0cf5e5e531ded8f42f00e5730c371d8951867dad5b02203c55f9403f6c2aac444213161a93a86661e7367d4007be74ffb3981387cc1c790121023f0aadfeb71a4964c5087fec8b052c8236051dae838d4747543330c6b266ce6e00000000</p>
</blockquote>

CC BY-SA 3.0
2018-03-02T19:44:33.773Z
2018-03-02T19:44:33.773Z
pebwindkraft
<p>How would this raw bitcoin transaction appear if it was a raw segwit transaction?</p>

<p><a href="https://blockchain.info/tx/ff8766ec873ff55cc0ac17dee7b379a4efa2a5c83dabdd9a30287c7761ad55d5" rel="noreferrer">https://blockchain.info/tx/ff8766ec873ff55cc0ac17dee7b379a4efa2a5c83dabdd9a30287c7761ad55d5</a></p>

<p>This TX Raw is:</p>

<blockquote>
  <p>0100000003b0c03779c38ddabc044947b164adbcffde8e0ddb3678494f4e8b83f13b37629d010000006b483045022100f3c0f555cd39198caf77f6756256801fc57bcabe4892601b9def52259698f40a0220230262841bff151eb617f7d4cdda6bf06d6fa791230c152e47f68bcdd0e6f64a01210204b3506d8903ca601c97a4abab6548e91004c535a5a45e21299a494b146859caffffffff060eeae54b70d12caddcc15f8897ebc2b6c3011c9a600aba74d49b969991cab3000000006b4830450221009dbeb64ddd4646e1118503c87feaa95c531b5178c6e543be782f6ecb05e8fbe602203945570f8dc56c145617f0283fa4032e0b7895a9cf2a81c7b65665b8ac608cb00121026602a5dc59c30f485b2c457ac8e2f617e27b10a1d2ae76f3231f9b01dff08964ffffffff7f715ba408db0289118f9b8578e54e721f1aee10d09844510793b8b20c87903e010000006b48304502210095e300886ec4df78e39d6d0cf5e5e531ded8f42f00e5730c371d8951867dad5b02203c55f9403f6c2aac444213161a93a86661e7367d4007be74ffb3981387cc1c790121023f0aadfeb71a4964c5087fec8b052c8236051dae838d4747543330c6b266ce6effffffff02d5781100000000001976a914342ab422c9e3ef285efe9882ae54269ed9713dd688ac20a10700000000001976a91488d924f51033b74a895863a5fb57fd545529df7d88ac00000000</p>
</blockquote>

<p>(Yes, I understand the hash would be different and therefore the S value would be different.  I'm more concerned about how the transaction is structured.)</p>

<transactions><segregated-witness><raw-transaction>
How would this normal raw transaction appear if it was a raw segwit transaction?
Mine
# How would this normal raw transaction appear if it was a raw segwit transaction?

(Asked by: username_0 on 2018-03-02)

How would this raw bitcoin transaction appear if it was a raw segwit transaction?

<https://blockchain.info/tx/ff8766ec873ff55cc0ac17dee7b379a4efa2a5c83dabdd9a30287c7761ad55d5>

This TX Raw is:

> 0100000003b0c03779c38ddabc044947b164adbcffde8e0ddb3678494f4e8b83f13b37629d010000006b483045022100f3c0f555cd39198caf77f6756256801fc57bcabe4892601b9def52259698f40a0220230262841bff151eb617f7d4cdda6bf06d6fa791230c152e47f68bcdd0e6f64a01210204b3506d8903ca601c97a4abab6548e91004c535a5a45e21299a494b146859caffffffff060eeae54b70d12caddcc15f8897ebc2b6c3011c9a600aba74d49b969991cab3000000006b4830450221009dbeb64ddd4646e1118503c87feaa95c531b5178c6e543be782f6ecb05e8fbe602203945570f8dc56c145617f0283fa4032e0b7895a9cf2a81c7b65665b8ac608cb00121026602a5dc59c30f485b2c457ac8e2f617e27b10a1d2ae76f3231f9b01dff08964ffffffff7f715ba408db0289118f9b8578e54e721f1aee10d09844510793b8b20c87903e010000006b48304502210095e300886ec4df78e39d6d0cf5e5e531ded8f42f00e5730c371d8951867dad5b02203c55f9403f6c2aac444213161a93a86661e7367d4007be74ffb3981387cc1c790121023f0aadfeb71a4964c5087fec8b052c8236051dae838d4747543330c6b266ce6effffffff02d5781100000000001976a914342ab422c9e3ef285efe9882ae54269ed9713dd688ac20a10700000000001976a91488d924f51033b74a895863a5fb57fd545529df7d88ac00000000

(Yes, I understand the hash would be different and therefore the S value would be different. I'm more concerned about how the transaction is structured.)

**Tags:** transactions, segregated-witness, raw-transaction


---

## 1 answers

### Answer by username_1 on 2018-03-02. Score: 6

wow, cool question, that's not very easy. Especially as there are different Segwit tx thinkable (native P2WPKH, P2SH-P2WPKH, Native P2WSH, P2SH-P2WSH or one with Bech32 address). Then there is the point of adapting all the length parameters (for signatures and scripts). I will give it a try with a standard P2WPKH. So first I'd decode your provided tx, I get this:

```
VERSION
 01000000

TX_IN COUNT [var_int]: hex=03, decimal=3
 TX_IN[0] OutPoint hash 9D62373BF1838B4E4F497836DB0D8EDEFFBCAD64B1474904BCDA8DC37937C0B0
 TX_IN[0] OutPoint index hex=01000000, reversed=00000001, decimal=1
 TX_IN[0] Script Length hex=6B, decimal=107
 TX_IN[0] Script Sig 
  483045022100F3C0F555CD39198CAF77F6756256801FC57BCABE4892601B9DEF52259698F40A0220230262841BFF151
EB617F7D4CDDA6BF06D6FA791230C152E47F68BCDD0E6F64A01210204B3506D8903CA601C97A4ABAB6548E91004C535A5
A45E21299A494B146859CA 
 TX_IN[0] Sequence (uint32_t) FFFFFFFF

 TX_IN[1] OutPoint hash B3CA9199969BD474BA0A609A1C01C3B6C2EB97885FC1DCAD2CD1704BE5EA0E06
 TX_IN[1] OutPoint index hex=00000000, reversed=00000000, decimal=0
 TX_IN[1] Script Length hex=6B, decimal=107
 TX_IN[1] Script Sig
  4830450221009DBEB64DDD4646E1118503C87FEAA95C531B5178C6E543BE782F6ECB05E8FBE602203945570F8DC56C145617F0283FA4032E0B7895A9CF2A81C7B65665B8AC608CB00121026602A5DC59C30F485B2C457AC8E2F617E27B10A1D2AE76F3231F9B01DFF08964 
 TX_IN[1] Sequence FFFFFFFF

 TX_IN[2] OutPoint hash 3E90870CB2B89307514498D010EE1A1F724EE578859B8F118902DB08A45B717F
 TX_IN[2] OutPoint index hex=01000000, reversed=00000001, decimal=1
 TX_IN[2] Script Length hex=6B, decimal=107
 TX_IN[2] Script Sig 
  48304502210095E300886EC4DF78E39D6D0CF5E5E531DED8F42F00E5730C371D8951867DAD5B02203C55F9403F6C2AAC444213161A93A86661E7367D4007BE74FFB3981387CC1C790121023F0AADFEB71A4964C5087FEC8B052C8236051DAE838D4747543330C6B266CE6E 
 TX_IN[2] Sequence FFFFFFFF

TX_OUT COUNT, hex=02, decimal=2
 TX_OUT[0] Value hex=D578110000000000, dec=1145045, bitcoin=0.01145045
 TX_OUT[0] PK_Script Length hex=19, dec=25
 TX_OUT[0] pk_script 76A914342AB422C9E3EF285EFE9882AE54269ED9713DD688AC
 TX_OUT[1] Value hex=20A1070000000000, dec=500000, bitcoin=0.00500000
 TX_OUT[1] PK_Script Length hex=19, dec=25
 TX_OUT[1] pk_script 76A91488D924F51033B74A895863A5FB57FD545529DF7D88AC

 LOCK_TIME 00000000

```

Now I make a P2WPKH tx out of it. In general, the segwit transaction is composed this way:

> [nVersion][marker][flag][txins][txouts][witness][nLockTime]

```
VERSION
 01000000

SEGWIT (BIP141): this is a segwit tx, marker=00
       (BIP141): flag=01

TX_IN COUNT [var_int]: hex=03, decimal=3
  TX_IN[0] 9D62373BF1838B4E4F497836DB0D8EDEFFBCAD64B1474904BCDA8DC37937C0B0
  TX_IN[0] hex=01000000, reversed=00000001, decimal=1
  TX_IN[0] Script Length hex=00, decimal=0
  TX_IN[0] Sequence (uint32_t) FFFFFFFF

  TX_IN[1] B3CA9199969BD474BA0A609A1C01C3B6C2EB97885FC1DCAD2CD1704BE5EA0E06
  TX_IN[1] hex=00000000, reversed=00000000, decimal=0
  TX_IN[1] Script Length hex=00, decimal=0
  TX_IN[1] Sequence (uint32_t) FFFFFFFF

  TX_IN[2] 3E90870CB2B89307514498D010EE1A1F724EE578859B8F118902DB08A45B717F
  TX_IN[2] hex=01000000, reversed=00000001, decimal=1
  TX_IN[2] Script Length hex=00, decimal=0
  TX_IN[2] Sequence (uint32_t) FFFFFFFF

TX_OUT COUNT, hex=02, decimal=2
  TX_OUT[0] Value hex=D578110000000000, dec=1145045, bitcoin=0.01145045
  TX_OUT[0] PK_Script Length hex=19, dec=25
  TX_OUT[0] pk_script 76A914342AB422C9E3EF285EFE9882AE54269ED9713DD688AC
 TX_OUT[1]
  TX_OUT[1] Value hex=20A1070000000000, dec=500000, bitcoin=0.00500000
  TX_OUT[1] PK_Script Length hex=19, dec=25
  TX_OUT[1] pk_script 76A91488D924F51033B74A895863A5FB57FD545529DF7D88AC

WITNESS TXIN[0] stack elements: hex=02, decimal=2
 WITNESS[0] data length (var_int), hex=48, decimal=72, data(uchar[]): 
  3045022100F3C0F555CD39198CAF77F6756256801FC57BCABE4892601B9DEF52259698F4
  0A0220230262841BFF151EB617F7D4CDDA6BF06D6FA791230C152E47F68BCDD0E6F64A01
 WITNESS[1] data length (var_int), hex=21, decimal=33, data(uchar[]):
  0204B3506D8903CA601C97A4ABAB6548E91004C535A5A45E21299A494B146859CA

WITNESS TXIN[1] stack elements: hex=02, decimal=2
 WITNESS[0] data length (var_int), hex=48, decimal=72, data(uchar[]):
  30450221009DBEB64DDD4646E1118503C87FEAA95C531B5178C6E543BE782F6ECB05E8FB
  E602203945570F8DC56C145617F0283FA4032E0B7895A9CF2A81C7B65665B8AC608CB001
 WITNESS[1] data length (var_int), hex=21, decimal=33, data(uchar[]):
  026602A5DC59C30F485B2C457AC8E2F617E27B10A1D2AE76F3231F9B01DFF08964

WITNESS TXIN[2] stack elements: hex=02, decimal=2
 WITNESS[0] data length (var_int), hex=48, decimal=72, data(uchar[]):
  304502210095E300886EC4DF78E39D6D0CF5E5E531DED8F42F00E5730C371D8951867DAD
  5B02203C55F9403F6C2AAC444213161A93A86661E7367D4007BE74FFB3981387CC1C7901
 WITNESS[1] data length (var_int), hex=21, decimal=33, data(uchar[]):
  023F0AADFEB71A4964C5087FEC8B052C8236051DAE838D4747543330C6B266CE6E

 LOCK_TIME
00000000

```

So I assume the segwit pattern can clearly be recognized, see that the signature length is given as "0", and at the end appears per input the witness data. More to it can be found in the [corresponding BIPs](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki), and of course in [the wiki](https://bitcoincore.org/en/segwit_wallet_dev/).

and the serialized hex would be this:

> 01000000**0001**03b0c03779c38ddabc044947b164adbcffde8e0ddb3678494f4e8b83f13b37629d0100000000ffffffff060eeae54b70d12caddcc15f8897ebc2b6c3011c9a600aba74d49b969991cab30000000000ffffffff7f715ba408db0289118f9b8578e54e721f1aee10d09844510793b8b20c87903e0100000000ffffffff02d5781100000000001976a914342ab422c9e3ef285efe9882ae54269ed9713dd688ac20a10700000000001976a91488d924f51033b74a895863a5fb57fd545529df7d88ac**02**483045022100f3c0f555cd39198caf77f6756256801fc57bcabe4892601b9def52259698f40a0220230262841bff151eb617f7d4cdda6bf06d6fa791230c152e47f68bcdd0e6f64a01210204b3506d8903ca601c97a4abab6548e91004c535a5a45e21299a494b146859ca024830450221009dbeb64ddd4646e1118503c87feaa95c531b5178c6e543be782f6ecb05e8fbe602203945570f8dc56c145617f0283fa4032e0b7895a9cf2a81c7b65665b8ac608cb00121026602a5dc59c30f485b2c457ac8e2f617e27b10a1d2ae76f3231f9b01dff089640248304502210095e300886ec4df78e39d6d0cf5e5e531ded8f42f00e5730c371d8951867dad5b02203c55f9403f6c2aac444213161a93a86661e7367d4007be74ffb3981387cc1c790121023f0aadfeb71a4964c5087fec8b052c8236051dae838d4747543330c6b266ce6e00000000


---

