1

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 ?

3

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.

  • Thanks @Pieter. I tried to do this using existing bitcoin-cli operations and it didn't work (Reference: Edited Question). Please advise – dark knight Aug 20 '17 at 6:07
  • 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. – Pieter Wuille Aug 20 '17 at 6:55
  • 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 Aug 20 '17 at 20:12
  • Pieter Wuille, are you going to publish your SegWit address vanity software? – MCCCS Aug 25 '17 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.