<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>

CC BY-SA 3.0
2017-08-20T06:07:33.140Z
Thanks @Pieter. I tried to do this using existing bitcoin-cli operations and it didn't work (Reference: Edited Question). Please advise
dark knight
CC BY-SA 3.0
2017-08-20T06:55:05.370Z
You're using "sendfrom" in an attempt to control which inputs are spent. That is not what what sendfrom does. Accounts are just bean counters; inputs don't belong to a specific account, they belong to the whole wallet. Accounts just let you account for things. You need to use createrawtransaction with spefific inputs, or spend enough money so that the segwit coins get selected automatically.
Pieter Wuille
CC BY-SA 3.0
2017-08-20T20:12:10.537Z
oh ok got it ! I will do a createrawtransaction then. I just thought there would be easier way to help make segwit transactions in bitcoin-core/qt ..so that people can take advantage of rather than manually construct using rawtransaction (like a simple check box in GUI or a flag in rpc command).  Anyway that is out of scope of this thread. Will check on bitcoincore github  . Thanks a lot @Pieter
dark knight
CC BY-SA 3.0
2017-08-25T07:55:44.590Z
Pieter Wuille, are you going to publish your SegWit address vanity software?
MCCCS
CC BY-SA 3.0
2017-08-20T05:10:47.163Z
2017-08-20T05:10:47.163Z
Pieter Wuille
<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>

<bitcoind><segregated-witness><bitcoin-cli>
Spend Segwit address output
dark knight
# Spend Segwit address output

(Asked by: username_0 on 2017-08-19)

I want to create a segwit transaction using bitcoind on testnet2. For this I created a P2SH-P2WPKH address as mentioned [here](https://bitcoin.stackexchange.com/questions/57832/how-to-generate-segwit-address) 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 ?

**Tags:** bitcoind, segregated-witness, bitcoin-cli


---

## 1 answers

### Answer by username_1 on 2017-08-20. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks @username_1. I tried to do this using existing bitcoin-cli operations and it didn't work (Reference: Edited Question). Please advise  
> **username_1**: You're using "sendfrom" in an attempt to control which inputs are spent. That is not what what sendfrom does. Accounts are just bean counters; inputs don't belong to a specific account, they belong to the whole wallet. Accounts just let you account for things. You need to use createrawtransaction with spefific inputs, or spend enough money so that the segwit coins get selected automatically.  
> **username_0**: oh ok got it ! I will do a createrawtransaction then. I just thought there would be easier way to help make segwit transactions in bitcoin-core/qt ..so that people can take advantage of rather than manually construct using rawtransaction (like a simple check box in GUI or a flag in rpc command).  Anyway that is out of scope of this thread. Will check on bitcoincore github  . Thanks a lot @username_1  
> **username_2**: username_1 username_1, are you going to publish your SegWit address vanity software?  

---

