user26481: I want to create a segwit transaction using bitcoind on testnet2. For this I created a P2SH-P2WPKH  address as mentioned here and sent coins to it. Now, how do I create a segwit transaction to spend from this segwit address. Is there any easy way other than createrawtransaction ?
As suggested I tried using normal bitcoin payment operations to achieve this, but it didn't work !
Created segwit address and sent funds to it. Looking good !
enigma@enigma-laptop:~$ bitcoin-cli getnewaddress
mybVcLQk3BdDWvQFKFrX6cfsMUDA2XyQUv
enigma@enigma-laptop:~$ bitcoin-cli addwitnessaddress mybVcLQk3BdDWvQFKFrX6cfsMUDA2XyQUv
2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV
enigma@enigma-laptop:~$ bitcoin-cli setaccount "2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV" "segwit_testnet"
enigma@enigma-laptop:~$ bitcoin-cli getaddressesbyaccount "segwit_testnet"
[
  "2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV"
]
enigma@enigma-laptop:~$ bitcoin-cli listtransactions "segwit_testnet"
[
  {
    "account": "segwit_testnet",
    "address": "2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV",
    "category": "receive",
    "amount": 0.01111000,
    "label": "segwit_testnet",
    "vout": 1,
    "confirmations": 0,
    "trusted": true,
    "txid": "1a9f518606078495460edd0b98ac5e7bfd1a4dd5cc982d20eff4f67333a09257",
    "walletconflicts": [
    ],
    "time": 1503208157,
    "timereceived": 1503208157,
    "bip125-replaceable": "no"
  }
]

Trying to spend from segwit address output
enigma@enigma-laptop:~$ bitcoin-cli sendfrom "segwit_testnet" mh8tGnF6RCsnWUMTw1WL9UWjjgyMRRTM8t 0.0088 0
9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674
enigma@enigma-laptop:~$ bitcoin-cli getrawtransaction 9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674 1
{
  "hex": "020000000191158541159bfcaecb5f51cf219aa5f412bfa4e6dc24cedb1d941926edd661a8000000006b4830450221008e7a6ac64baf93451f05307bb5e0c7544d7667a9793ce691487a5afaaf1b3839022043415d2a85d61be67912c3a9ea1b42dfdfc1a6997bba2d2fef998ad999aa0c23012102dfd3b9834bc783a9a86136ca9d3941f85798902e3d3c70b056e8fb26a204bf2dfeffffff02806d0d00000000001976a91411c3be38b2bef73be3d80d92cadd265dfaa84fe288ac5b60891c000000001976a914c11b02c5c06a5e68cf59fcba24d3a74e38bb404f88ac5dff1100",
  "txid": "9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674",
  "hash": "9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674",
  "size": 226,
  "vsize": 226,
  "version": 2,
  "locktime": 1179485,
  "vin": [
    {
      "txid": "a861d6ed2619941ddbce24dce6a4bf12f4a59a21cf515fcbaefc9b1541851591",
      "vout": 0,
      "scriptSig": {
        "asm": "30450221008e7a6ac64baf93451f05307bb5e0c7544d7667a9793ce691487a5afaaf1b3839022043415d2a85d61be67912c3a9ea1b42dfdfc1a6997bba2d2fef998ad999aa0c23[ALL] 02dfd3b9834bc783a9a86136ca9d3941f85798902e3d3c70b056e8fb26a204bf2d",
        "hex": "4830450221008e7a6ac64baf93451f05307bb5e0c7544d7667a9793ce691487a5afaaf1b3839022043415d2a85d61be67912c3a9ea1b42dfdfc1a6997bba2d2fef998ad999aa0c23012102dfd3b9834bc783a9a86136ca9d3941f85798902e3d3c70b056e8fb26a204bf2d"
      },
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.00880000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 11c3be38b2bef73be3d80d92cadd265dfaa84fe2 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a91411c3be38b2bef73be3d80d92cadd265dfaa84fe288ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mh8tGnF6RCsnWUMTw1WL9UWjjgyMRRTM8t"
        ]
      }
    }, 
    {
      "value": 4.78765147,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 c11b02c5c06a5e68cf59fcba24d3a74e38bb404f OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914c11b02c5c06a5e68cf59fcba24d3a74e38bb404f88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "my814pfgD9Cexktur58B4nSY5vQfCqJQ1g"
        ]
      }
    }
  ],
  "blockhash": "0000000074ba5f011ef6bf50e387b701188359f3e0b60b2169c3109eedda0295",
  "confirmations": 1,
  "time": 1503208357,
  "blocktime": 1503208357
}

The vin txid should be 1a9f518606078495460edd0b98ac5e7bfd1a4dd5cc982d20eff4f67333a09257 and it should spend the segwit address txout. Why did this not work ? 

user208: If you used addwitnessaddress on your wallet, and sent money to the resulting P2SH-P2WPKH address, the resulting outputs will be considered owned by your wallet. As a result, they're available for all normal coin selection operations. That includes using listunspent, createrawtransaction and signrawtransaction, but also all normal payment operations like sendtoaddress. Those segwit outputs just become part of your wallet, and the wallet will spend them whenever needed.

26481: <p>I want to create a segwit transaction using bitcoind on testnet2. For this I created a P2SH-P2WPKH  address as mentioned <a href="https://bitcoin.stackexchange.com/questions/57832/how-to-generate-segwit-address">here</a> and sent coins to it. Now, how do I create a segwit transaction to spend from this segwit address. Is there any easy way other than <em>createrawtransaction</em> ?</p>

<p>As suggested I tried using normal bitcoin payment operations to achieve this, but it didn't work !</p>

<p>Created segwit address and sent funds to it. Looking good !</p>

<pre><code>enigma@enigma-laptop:~$ bitcoin-cli getnewaddress
mybVcLQk3BdDWvQFKFrX6cfsMUDA2XyQUv
enigma@enigma-laptop:~$ bitcoin-cli addwitnessaddress mybVcLQk3BdDWvQFKFrX6cfsMUDA2XyQUv
2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV
enigma@enigma-laptop:~$ bitcoin-cli setaccount "2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV" "segwit_testnet"
enigma@enigma-laptop:~$ bitcoin-cli getaddressesbyaccount "segwit_testnet"
[
  "2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV"
]
enigma@enigma-laptop:~$ bitcoin-cli listtransactions "segwit_testnet"
[
  {
    "account": "segwit_testnet",
    "address": "2N14FtFJFhvvvwLKq8dQqQ9NPEmWQF9bwPV",
    "category": "receive",
    "amount": 0.01111000,
    "label": "segwit_testnet",
    "vout": 1,
    "confirmations": 0,
    "trusted": true,
    "txid": "1a9f518606078495460edd0b98ac5e7bfd1a4dd5cc982d20eff4f67333a09257",
    "walletconflicts": [
    ],
    "time": 1503208157,
    "timereceived": 1503208157,
    "bip125-replaceable": "no"
  }
]
</code></pre>

<p>Trying to spend from segwit address output</p>

<pre><code>enigma@enigma-laptop:~$ bitcoin-cli sendfrom "segwit_testnet" mh8tGnF6RCsnWUMTw1WL9UWjjgyMRRTM8t 0.0088 0
9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674
enigma@enigma-laptop:~$ bitcoin-cli getrawtransaction 9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674 1
{
  "hex": "020000000191158541159bfcaecb5f51cf219aa5f412bfa4e6dc24cedb1d941926edd661a8000000006b4830450221008e7a6ac64baf93451f05307bb5e0c7544d7667a9793ce691487a5afaaf1b3839022043415d2a85d61be67912c3a9ea1b42dfdfc1a6997bba2d2fef998ad999aa0c23012102dfd3b9834bc783a9a86136ca9d3941f85798902e3d3c70b056e8fb26a204bf2dfeffffff02806d0d00000000001976a91411c3be38b2bef73be3d80d92cadd265dfaa84fe288ac5b60891c000000001976a914c11b02c5c06a5e68cf59fcba24d3a74e38bb404f88ac5dff1100",
  "txid": "9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674",
  "hash": "9d3c5496f60376daf0db2aa683f7932cd8003dc16e7841e310cebf582ae25674",
  "size": 226,
  "vsize": 226,
  "version": 2,
  "locktime": 1179485,
  "vin": [
    {
      "txid": "a861d6ed2619941ddbce24dce6a4bf12f4a59a21cf515fcbaefc9b1541851591",
      "vout": 0,
      "scriptSig": {
        "asm": "30450221008e7a6ac64baf93451f05307bb5e0c7544d7667a9793ce691487a5afaaf1b3839022043415d2a85d61be67912c3a9ea1b42dfdfc1a6997bba2d2fef998ad999aa0c23[ALL] 02dfd3b9834bc783a9a86136ca9d3941f85798902e3d3c70b056e8fb26a204bf2d",
        "hex": "4830450221008e7a6ac64baf93451f05307bb5e0c7544d7667a9793ce691487a5afaaf1b3839022043415d2a85d61be67912c3a9ea1b42dfdfc1a6997bba2d2fef998ad999aa0c23012102dfd3b9834bc783a9a86136ca9d3941f85798902e3d3c70b056e8fb26a204bf2d"
      },
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.00880000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 11c3be38b2bef73be3d80d92cadd265dfaa84fe2 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a91411c3be38b2bef73be3d80d92cadd265dfaa84fe288ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mh8tGnF6RCsnWUMTw1WL9UWjjgyMRRTM8t"
        ]
      }
    }, 
    {
      "value": 4.78765147,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 c11b02c5c06a5e68cf59fcba24d3a74e38bb404f OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914c11b02c5c06a5e68cf59fcba24d3a74e38bb404f88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "my814pfgD9Cexktur58B4nSY5vQfCqJQ1g"
        ]
      }
    }
  ],
  "blockhash": "0000000074ba5f011ef6bf50e387b701188359f3e0b60b2169c3109eedda0295",
  "confirmations": 1,
  "time": 1503208357,
  "blocktime": 1503208357
}
</code></pre>

<p>The vin txid should be <em>1a9f518606078495460edd0b98ac5e7bfd1a4dd5cc982d20eff4f67333a09257</em> and it should spend the segwit address txout. Why did this not work ? </p>
 208: <p>If you used <code>addwitnessaddress</code> on your wallet, and sent money to the resulting P2SH-P2WPKH address, the resulting outputs will be considered owned by your wallet. As a result, they're available for all normal coin selection operations. That includes using <code>listunspent</code>, <code>createrawtransaction</code> and <code>signrawtransaction</code>, but also all normal payment operations like <code>sendtoaddress</code>. Those segwit outputs just become part of your wallet, and the wallet will spend them whenever needed.</p>
