bitcoin-cli を使ってSegwit Address を作る

# Introduction
今回は早稲田のd-dataのBlockchain講座(#ddata_bc)の課題としてBitcoin-Core上でSegwitを用いたTransactionの生成をしました。


> #####参考記事

> - [bitcoindをdockerで環境構築する](https://qiita.com/Naomasa/items/5053a5d9c33ae040bed2)
- [Bitcoin Segwit Transactions（送信）
](https://qiita.com/tnakagawa/items/684a4b77efe13b80817e)
- [Bitcoinのコマンド操作（生成、送信）](https://qiita.com/tnakagawa/items/bbf7a7ad9d6547d5db3d)

# Docker Containerを作る
> 1. Install Bitcoin Core

-> 別記事

# アドレスを作る

> 2.Create a non SegWit address.
> 3.Create two SegWit address.
Segwitでないアドレス（P2PKH)1つとSegwitアドレス(P2SH-SegWit)2つを作成する。
あとでそれぞれのアドレスから別々に送金をしたいので、別々のアカウントで管理する。

```:Non-Segwit
$ bitcoin-cli getnewaddress nonsegwit          
2NBBasuGC8i7cBYdmUtJyPzKen1J46d5RLD
```

```:First-Segwit
$ bitcoin-cli getnewaddress segwit2 p2sh-segwit
2N11WNmuHfUNyKsoAAoYvqmBPrpao372ymT
```

```:Second-Segwit
$ bitcoin-cli getnewaddress nonsegwit          
2NBBasuGC8i7cBYdmUtJyPzKen1J46d5RLD

```


# 送金する
> 4.Fund non SegWit address.

まず、最初に作ったP２PKHアドレスに10BTC送金する。

```
$ bitcoin-cli sendtoaddress 2NBBasuGC8i7cBYdmUtJyPzKen1J46d5RLD 10  
c34a44607c1944ca8b52552dd0abfd04d42f2feebae2a6f79377d631ac7502d8
```

ブロックを生成して、Transactionを確定する。

```
$ bitcoin-cli generate 1
[
 "2969f7925865898579c80c0297dbb03eee73a3e4838fa0001ec5402822698d3e"
]
```

ちゃんと送金できているか確認する。

```
$ bitcoin-cli getreceivedbyaddress 2NBBasuGC8i7cBYdmUtJyPzKen1J46d5RLD
10.00000000
```


> 5.Send from non SegWit address to first SegWit address. (display JSON)

P2PKHアドレスから一つ目のp2sh-segwitアドレスに3BTC送金する。

```
$ bitcoin-cli sendfrom nonsegwit 2NCjfVtqtwjaycSmppvZpqcURQXhjfbTaS8 3  
91ddd98b5dc841f0d407c0e7ec384793b4c6fd125541eec402540c73f8108a6b

$ bitcoin-cli generate 1
[
  "0cc4c0daaafdd70d601fe462c1fe55180b633e78da46ebc30a12f25e988d433d"
]

$ bitcoin-cli getreceivedbyaddress 2NCjfVtqtwjaycSmppvZpqcURQXhjfbTaS8
3.00000000
```

Transactionの中身を確認する。

```
$ bitcoin-cli getrawtransaction 91ddd98b5dc841f0d407c0e7ec384793b4c6fd125541eec402540c73f8108a6b 1
{
  "txid": "91ddd98b5dc841f0d407c0e7ec384793b4c6fd125541eec402540c73f8108a6b",
  "hash": "855ce4d338c116f4bad79aa894d16b34cbfae967f7fc65033754f01ef1590ea8",
  "version": 2,
  "size": 248,
  "vsize": 166,
  "locktime": 103,
  "vin": [
    {
      "txid": "c9c79c6179d0dc8d1a06e69b5e548dac74df60e985bf6ef843f4f7413d76b194",
      "vout": 1,
      "scriptSig": {
        "asm": "00149c106fc4e122f5f7f032d743ecd916b3c9ee3c01",
        "hex": "1600149c106fc4e122f5f7f032d743ecd916b3c9ee3c01"
      },
      "txinwitness": [
        "30450221009a235c9ffcc4e60884b8322658c7998646e889276f9c4ee2df087adc90c8e62302202dff1c8064073a0d7dabe2fb432a7c5644d0a41dfcd38ccec8e1771352611d4001",
        "03b1eb1c2e8a2ff72b96395c84a59def03e754edb1887efd5855aef316c60dec0d"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 1.99993360,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 54d54dae6701dc6f1dc045bf70822ece9f7b00fd OP_EQUAL",
        "hex": "a91454d54dae6701dc6f1dc045bf70822ece9f7b00fd87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2MzynGyqMmr8cz81WtySgd5En4tuS4tSeVg"
        ]
      }
    },
    {
      "value": 3.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 d5cb54f9fc22cf6e1d1883c6eeea54cf48e145ff OP_EQUAL",
        "hex": "a914d5cb54f9fc22cf6e1d1883c6eeea54cf48e145ff87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2NCjfVtqtwjaycSmppvZpqcURQXhjfbTaS8"
        ]
      }
    }
  ],
  "hex": "0200000000010194b1763d41f7f443f86ebf85e960df74ac8d545e9be6061a8ddcd079619cc7c901000000171600149c106fc4e122f5f7f032d743ecd916b3c9ee3c01feffffff0210a8eb0b0000000017a91454d54dae6701dc6f1dc045bf70822ece9f7b00fd8700a3e1110000000017a914d5cb54f9fc22cf6e1d1883c6eeea54cf48e145ff87024830450221009a235c9ffcc4e60884b8322658c7998646e889276f9c4ee2df087adc90c8e62302202dff1c8064073a0d7dabe2fb432a7c5644d0a41dfcd38ccec8e1771352611d40012103b1eb1c2e8a2ff72b96395c84a59def03e754edb1887efd5855aef316c60dec0d67000000",
  "blockhash": "0cc4c0daaafdd70d601fe462c1fe55180b633e78da46ebc30a12f25e988d433d",
  "confirmations": 1,
  "time": 1527323298,
  "blocktime": 1527323298
}

```

```
> 6.Send from first SegWit address to second SegWit address. (display JSON)

一つ目のp2sh-segwitアドレスから二つ目のp2sh-segwitアドレスに1BTC送金する。

```
$ bitcoin-cli sendfrom segwit1 2N11WNmuHfUNyKsoAAoYvqmBPrpao372ymT 1.0
2071756f706e08246c4ff359c8f73d7fa25d36f7fca30c3421eca9143fa67664
```

```
$ bitcoin-cli generate 1
[
  "14a28b1959bf3f66e0af7a22fa6966be4688c9f825947065597e6fbcbbb4591e"
]
$ bitcoin-cli getreceivedbyaddress 2N11WNmuHfUNyKsoAAoYvqmBPrpao372ymT
1.00000000
```

```
bitcoin-cli getrawtransaction 2071756f706e08246c4ff359c8f73d7fa25d36f7fca30c3421eca9143fa67664 1
{
  "txid": "2071756f706e08246c4ff359c8f73d7fa25d36f7fca30c3421eca9143fa67664",
  "hash": "6e458c0fbb3e59f43f1f77fbd3121c323f250bc9adb858639e2caa1a6df32180",
  "version": 2,
  "size": 247,
  "vsize": 166,
  "locktime": 104,
  "vin": [
    {
      "txid": "91ddd98b5dc841f0d407c0e7ec384793b4c6fd125541eec402540c73f8108a6b",
      "vout": 0,
      "scriptSig": {
        "asm": "001480d852d554362f25d0a9cb343c3d9c253e369ca6",
        "hex": "16001480d852d554362f25d0a9cb343c3d9c253e369ca6"
      },
      "txinwitness": [
        "304402204c8c11077446a70187bea9fdf827ef96addbe3fbfd624f75b5440ed74b241720022051647f2e164bd9eb3b18e014db001b54cc6b38036a665f6ec5ab06615b5a717701",
        "03aa6b449e5cf864404f77e5e464bce58f32fbd8aca1e0af43a29f196c2e973643"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.99990040,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 5748d766f5538e492a485bbe1f5fef3f6549bf3f OP_EQUAL",
        "hex": "a9145748d766f5538e492a485bbe1f5fef3f6549bf3f87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N1Ck35ntim8P1kok6PSPEe6t83HPJJc1Lk"
        ]
      }
    },
    {
      "value": 1.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 5528dcd572a7bb4edb58b47d72c7e5f17059cc11 OP_EQUAL",
        "hex": "a9145528dcd572a7bb4edb58b47d72c7e5f17059cc1187",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N11WNmuHfUNyKsoAAoYvqmBPrpao372ymT"
        ]
      }
    }
  ],
  "hex": "020000000001016b8a10f8730c5402c4ee415512fdc6b4934738ece7c007d4f041c85d8bd9dd91000000001716001480d852d554362f25d0a9cb343c3d9c253e369ca6feffffff0218baf5050000000017a9145748d766f5538e492a485bbe1f5fef3f6549bf3f8700e1f5050000000017a9145528dcd572a7bb4edb58b47d72c7e5f17059cc11870247304402204c8c11077446a70187bea9fdf827ef96addbe3fbfd624f75b5440ed74b241720022051647f2e164bd9eb3b18e014db001b54cc6b38036a665f6ec5ab06615b5a7177012103aa6b449e5cf864404f77e5e464bce58f32fbd8aca1e0af43a29f196c2e97364368000000",
  "blockhash": "14a28b1959bf3f66e0af7a22fa6966be4688c9f825947065597e6fbcbbb4591e",
  "confirmations": 1,
  "time": 1527323373,
  "blocktime": 1527323373
}
```