---
title: Transactions
description: "## Transaction Tutorial[https://developer.bitcoin.org/examples/transactions.html#transaction-tutorial](https://developer.bitcoin.org/examples/transactions.html#transaction-tutorial) Creating transactions is something most Bitcoin applications do. This section describes how to use Bitcoin Core’s [RPC](https://developer.bitcoin.org/reference/rpc/index.html) interface to create transactions with various attributes. Your applications may use something besides Bitcoin Core to create transactions, but in any system,…"
url: "https://bitcoindevelopers.org/docs/examples/transactions-example/"
updated: "2025-05-14"
category: Examples
---

# Transactions

## Transaction Tutorial[https://developer.bitcoin.org/examples/transactions.html#transaction-tutorial](https://developer.bitcoin.org/examples/transactions.html#transaction-tutorial)

Creating transactions is something most Bitcoin applications do. This section describes how to use Bitcoin Core’s [RPC](https://developer.bitcoin.org/reference/rpc/index.html) interface to create transactions with various attributes.

Your applications may use something besides Bitcoin Core to create transactions, but in any system, you will need to provide the same kinds of data to create transactions with the same attributes as those described below.

In order to use this tutorial, you will need to setup [Bitcoin Core](https://bitcoin.org/en/download) and create a [Regression test mode](https://bitcoindevelopers.org/encyclopedia/regression-test-mode/) environment with 50 BTC in your test [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/).

### Simple Spending[https://developer.bitcoin.org/examples/transactions.html#simple-spending](https://developer.bitcoin.org/examples/transactions.html#simple-spending)

Bitcoin Core provides several [RPCs](https://developer.bitcoin.org/reference/rpc/index.html) which handle all the details of spending, including creating change outputs and paying appropriate fees. Even advanced users should use these [RPCs](https://developer.bitcoin.org/reference/rpc/index.html) whenever possible to decrease the chance that [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) will be lost by mistake.

```
> bitcoin-cli -Regtest getnewaddress
mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou

> NEW_ADDRESS=mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou
```

Get a new Bitcoin [Address](https://bitcoindevelopers.org/encyclopedia/address/) and save it in the shell variable `$NEW_ADDRESS`.

```
> bitcoin-cli -Regtest sendtoaddress $NEW_ADDRESS 10.00
263c018582731ff54dc72c7d67e858c002ae298835501d80200f05753de0edf0
```

Send 10 [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/) to the [Address](https://bitcoindevelopers.org/encyclopedia/address/) using the [“sendtoaddress” RPC](https://developer.bitcoin.org/reference/rpc/sendtoaddress.html). The returned hex string is the transaction identifier ([Txid](https://bitcoindevelopers.org/encyclopedia/txid/)).

The [“sendtoaddress” RPC](https://developer.bitcoin.org/reference/rpc/sendtoaddress.html) automatically selects an unspent transaction [Output](https://bitcoindevelopers.org/encyclopedia/output/) ([UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/)) from which to spend the [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/). In this case, it withdrew the [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) from our only available [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/), the [Coinbase](https://bitcoindevelopers.org/encyclopedia/coinbase/) transaction for [Block](https://bitcoindevelopers.org/encyclopedia/block/) #1 which matured with the creation of [Block](https://bitcoindevelopers.org/encyclopedia/block/) #101. To spend a specific [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/), you could use the `sendfrom` [RPC](https://developer.bitcoin.org/reference/rpc/index.html) instead.

```
> bitcoin-cli -Regtest listunspent
[
]
```

Use the [“listunspent” RPC](https://developer.bitcoin.org/reference/rpc/listunspent.html) to display the UTXOs belonging to this [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/). The list is empty because it defaults to only showing confirmed UTXOs and we just spent our only confirmed [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/).

```
> bitcoin-cli -Regtest listunspent 0
```

```
[
    {
        "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d
                  80200f05753de0edf0",
        "vout" : 0,
        "Address" : "muhtvdmsnbQEPFuEmxcChX58fGvXaaUoVt",
        "ScriptPubKey" : "76a9149ba386253ea698158b6d34802bb9b550
                          f5ce36dd88ac",
        "amount" : 40.00000000,
        "Confirmations" : 0,
        "spendable" : true,
        "solvable" : true
    },
    {
        "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d
                  80200f05753de0edf0",
        "vout" : 1,
        "Address" : "mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou",
        "account" : "",
        "ScriptPubKey" : "76a914a57414e5ffae9ef5074bacbe10a320bb
                          2614e1f388ac",
        "amount" : 10.00000000,
        "Confirmations" : 0,
        "spendable" : true,
        "solvable" : true
    }
]
```

Re-running the [“listunspent” RPC](https://developer.bitcoin.org/reference/rpc/listunspent.html) with the argument “0” to also display unconfirmed transactions shows that we have two UTXOs, both with the same [Txid](https://bitcoindevelopers.org/encyclopedia/txid/). The first [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) shown is a change [Output](https://bitcoindevelopers.org/encyclopedia/output/) that [“sendtoaddress”](https://developer.bitcoin.org/reference/rpc/sendtoaddress.html) created using a new [Address](https://bitcoindevelopers.org/encyclopedia/address/) from the key pool. The second [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) shown is the spend to the [Address](https://bitcoindevelopers.org/encyclopedia/address/) we provided. If we had spent those [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) to someone else, that second transaction would not be displayed in our list of UTXOs.

```
> bitcoin-cli -Regtest -generate 1

> unset NEW_ADDRESS
```

Create a new [Block](https://bitcoindevelopers.org/encyclopedia/block/) to confirm the transaction above (takes less than a second) and clear the shell variable.

### Simple [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/)[https://developer.bitcoin.org/examples/transactions.html#simple-raw-transaction](https://developer.bitcoin.org/examples/transactions.html#simple-raw-transaction)

The [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) [RPCs](https://developer.bitcoin.org/reference/rpc/index.html) allow users to create custom transactions and delay broadcasting those transactions. However, mistakes made in raw transactions may not be detected by Bitcoin Core, and a number of [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) users have permanently lost large numbers of [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/), so please be careful using raw transactions on [Mainnet](https://bitcoindevelopers.org/encyclopedia/mainnet/).

This subsection covers one of the simplest possible raw transactions.

```
> bitcoin-cli -Regtest listunspent
```

```
[
    {
        "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d
                  80200f05753de0edf0",
        "vout" : 0,
        "Address" : "muhtvdmsnbQEPFuEmxcChX58fGvXaaUoVt",
        "ScriptPubKey" : "76a9149ba386253ea698158b6d34802bb9b550
                          f5ce36dd88ac",
        "amount" : 40.00000000,
        "Confirmations" : 1,
        "spendable" : true,
        "solvable" : true
    },
    {
        "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d
                  80200f05753de0edf0",
        "vout" : 1,
        "Address" : "mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou",
        "account" : "",
        "ScriptPubKey" : "76a914a57414e5ffae9ef5074bacbe10a320bb
                          2614e1f388ac",
        "amount" : 10.00000000,
        "Confirmations" : 1,
        "spendable" : true,
        "solvable" : true
    },
    {
        "Txid" : "3f4fa19803dec4d6a84fae3821da7ac7577080ef754512
                  94e71f9b20e0ab1e7b",
        "vout" : 0,
        "Address" : "mwJTL1dZG8BAP6X7Be3CNNcuVKi7Qqt7Gk",
        "ScriptPubKey" : "210260a275cccf0f4b106220725be516adba27
                          52db1bec8c5b7174c89c4c07891f88ac",
        "amount" : 50.00000000,
        "Confirmations" : 101,
        "spendable" : true,
        "solvable" : true
    }
]
```

```
> UTXO_TXID=3f4fa19803dec4d6a84fae3821da7ac7577080ef75451294e71f[...]
> UTXO_VOUT=0
```

Re-run [“listunspent”](https://developer.bitcoin.org/reference/rpc/listunspent.html). We now have three UTXOs: the two transactions we created before plus the [Coinbase](https://bitcoindevelopers.org/encyclopedia/coinbase/) transaction from [Block](https://bitcoindevelopers.org/encyclopedia/block/) #2. We save the [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) and [output index](https://developer.bitcoin.org/terms.html#term-output-index) number (vout) of that [Coinbase](https://bitcoindevelopers.org/encyclopedia/coinbase/) [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) to shell variables.

```
> bitcoin-cli -Regtest getnewaddress
mz6KvC4aoUeo6wSxtiVQTo7FDwPnkp6URG

> NEW_ADDRESS=mz6KvC4aoUeo6wSxtiVQTo7FDwPnkp6URG
```

Get a new [Address](https://bitcoindevelopers.org/encyclopedia/address/) to use in the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/).

```
## Outputs - inputs = Transaction fee, so always double-check your math!
> bitcoin-cli -Regtest createrawtransaction '''
    [
      {
        "Txid": "'$UTXO_TXID'",
        "vout": '$UTXO_VOUT'
      }
    ]
    ''' '''
    {
      "'$NEW_ADDRESS'": 49.9999
    }'''
01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4fa8d6c4de
0398a14f3f0000000000ffffffff01f0ca052a010000001976a914cbc20a7664
f2f69e5355aa427045bc15e7c6c77288ac00000000

> RAW_TX=01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4[...]
```

Using two arguments to the [“createrawtransaction” RPC](https://developer.bitcoin.org/reference/rpc/createrawtransaction.html), we create a new raw format transaction. The first argument (a JSON array) references the [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) of the [Coinbase](https://bitcoindevelopers.org/encyclopedia/coinbase/) transaction from [Block](https://bitcoindevelopers.org/encyclopedia/block/) #2 and the index number (0) of the [Output](https://bitcoindevelopers.org/encyclopedia/output/) from that transaction we want to spend. The second argument (a JSON object) creates the [Output](https://bitcoindevelopers.org/encyclopedia/output/) with the [Address](https://bitcoindevelopers.org/encyclopedia/address/) ([Public key](https://bitcoindevelopers.org/encyclopedia/public-key/) hash) and number of [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/) we want to transfer. We save the resulting raw format transaction to a shell variable.

![Warning icon](https://developer.bitcoin.org/_images/icon_warning.svg) **Warning:** [“createrawtransaction”](https://developer.bitcoin.org/reference/rpc/createrawtransaction.html) does not automatically create change outputs, so you can easily accidentally pay a large [Transaction fee](https://bitcoindevelopers.org/encyclopedia/transaction-fee/). In this example, our [Input](https://bitcoindevelopers.org/encyclopedia/input/) had 50.0000 [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/) and our [Output](https://bitcoindevelopers.org/encyclopedia/output/) (`$NEW_ADDRESS`) is being paid 49.9999 [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/), so the transaction will include a fee of 0.0001 [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/). If we had paid `$NEW_ADDRESS` only 10 [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/) with no other changes to this transaction, the [Transaction fee](https://bitcoindevelopers.org/encyclopedia/transaction-fee/) would be a whopping 40 [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/). See the Complex [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) subsection below for how to create a transaction with multiple outputs so you can send the change back to yourself.

```
> bitcoin-cli -Regtest decoderawtransaction $RAW_TX
```

```
{
    "Txid" : "c80b343d2ce2b5d829c2de9854c7c8d423c0e33bda264c4013
              8d834aab4c0638",
    "hash" : "c80b343d2ce2b5d829c2de9854c7c8d423c0e33bda264c40138d834aab4c0638",
    "size" : 85,
    "vsize" : 85,
    "version" : 1,
    "Locktime" : 0,
    "vin" : [
        {
            "Txid" : "3f4fa19803dec4d6a84fae3821da7ac7577080ef75
                      451294e71f9b20e0ab1e7b",
            "vout" : 0,
            "ScriptSig" : {
                "asm" : "",
                "hex" : ""
            },
            "sequence" : 4294967295
        }
    ],
    "vout" : [
        {
            "value" : 49.99990000,
            "n" : 0,
            "ScriptPubKey" : {
                "asm" : "OP_DUP OP_HASH160 cbc20a7664f2f69e5355a
                         a427045bc15e7c6c772 OP_EQUALVERIFY OP_CHECKSIG",
                "hex" : "76a914cbc20a7664f2f69e5355aa427045bc15e
                         7c6c77288ac",
                "reqSigs" : 1,
                "type" : "pubkeyhash",
                "addresses" : [
                    "mz6KvC4aoUeo6wSxtiVQTo7FDwPnkp6URG"
                ]
            }
        }
    ]
}
```

Use the [“decoderawtransaction” RPC](https://developer.bitcoin.org/reference/rpc/decoderawtransaction.html) to see exactly what the transaction we just created does.

```
> bitcoin-cli -Regtest signrawtransaction $RAW_TX
```

```
{
    "hex" : "01000000017b1eabe0209b1fe794124575ef807057c77ada213
             8ae4fa8d6c4de0398a14f3f00000000494830450221008949f0
             cb400094ad2b5eb399d59d01c14d73d8fe6e96df1a7150deb38
             8ab8935022079656090d7f6bac4c9a94e0aad311a4268e082a7
             25f8aeae0573fb12ff866a5f01ffffffff01f0ca052a0100000
             01976a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288
             ac00000000",
    "complete" : true
}
```

```
> SIGNED_RAW_TX=01000000017b1eabe0209b1fe794124575ef807057c77ada[...]
```

Use the `signrawtransaction` [RPC](https://developer.bitcoin.org/reference/rpc/index.html) to sign the transaction created by [“createrawtransaction”](https://developer.bitcoin.org/reference/rpc/createrawtransaction.html) and save the returned “hex” raw format signed transaction to a shell variable.

Even though the transaction is now complete, the Bitcoin Core [Node](https://bitcoindevelopers.org/encyclopedia/node/) we’re connected to doesn’t know anything about the transaction, nor does any other part of the [network](https://developer.bitcoin.org/devguide/p2p_network.html). We’ve created a spend, but we haven’t actually spent anything because we could simply unset the `$SIGNED_RAW_TX` variable to eliminate the transaction.

```
> bitcoin-cli -Regtest sendrawtransaction $SIGNED_RAW_TX
c7736a0a0046d5a8cc61c8c3c2821d4d7517f5de2bc66a966011aaa79965ffba
```

Send the signed transaction to the connected [Node](https://bitcoindevelopers.org/encyclopedia/node/) using the [“sendrawtransaction” RPC](https://developer.bitcoin.org/reference/rpc/sendrawtransaction.html). After accepting the transaction, the [Node](https://bitcoindevelopers.org/encyclopedia/node/) would usually then broadcast it to other peers, but we’re not currently connected to other peers because we started in [Regtest](https://bitcoindevelopers.org/encyclopedia/regtest/) mode.

```
> bitcoin-cli -Regtest -generate 1

> unset UTXO_TXID UTXO_VOUT NEW_ADDRESS RAW_TX SIGNED_RAW_TX
```

Generate a [Block](https://bitcoindevelopers.org/encyclopedia/block/) to confirm the transaction and clear our shell variables.

### Complex [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/)[https://developer.bitcoin.org/examples/transactions.html#complex-raw-transaction](https://developer.bitcoin.org/examples/transactions.html#complex-raw-transaction)

In this example, we’ll create a transaction with two inputs and two outputs. We’ll sign each of the inputs separately, as might happen if the two inputs belonged to different people who agreed to create a transaction together (such as a CoinJoin transaction).

```
> bitcoin-cli -Regtest listunspent
```

```
[
    {
        "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d
                  80200f05753de0edf0",
        "vout" : 0,
        "Address" : "muhtvdmsnbQEPFuEmxcChX58fGvXaaUoVt",
        "ScriptPubKey" : "76a9149ba386253ea698158b6d34802bb9b550
                          f5ce36dd88ac",
        "amount" : 40.00000000,
        "Confirmations" : 2,
        "spendable" : true,
        "solvable" : true
    },
    {
        "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d
                  80200f05753de0edf0",
        "vout" : 1,
        "Address" : "mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou",
        "account" : "",
        "ScriptPubKey" : "76a914a57414e5ffae9ef5074bacbe10a320bb
                          2614e1f388ac",
        "amount" : 10.00000000,
        "Confirmations" : 2,
        "spendable" : true,
        "solvable" : true
    },
    {
        "Txid" : "78203a8f6b529693759e1917a1b9f05670d036fbb12911
                  0ed26be6a36de827f3",
        "vout" : 0,
        "Address" : "n2KprMQm4z2vmZnPMENfbp2P1LLdAEFRjS",
        "ScriptPubKey" : "210229688a74abd0d5ad3b06ddff36fa9cd8ed
                          d181d97b9489a6adc40431fb56e1d8ac",
        "amount" : 50.00000000,
        "Confirmations" : 101,
        "spendable" : true,
        "solvable" : true
    },
    {
        "Txid" : "c7736a0a0046d5a8cc61c8c3c2821d4d7517f5de2bc66a
                  966011aaa79965ffba",
        "vout" : 0,
        "Address" : "mz6KvC4aoUeo6wSxtiVQTo7FDwPnkp6URG",
        "account" : "",
        "ScriptPubKey" : "76a914cbc20a7664f2f69e5355aa427045bc15
                          e7c6c77288ac",
        "amount" : 49.99990000,
        "Confirmations" : 1,
        "spendable" : true,
        "solvable" : true
    }
]
```

```
> UTXO1_TXID=78203a8f6b529693759e1917a1b9f05670d036fbb129110ed26[...]
> UTXO1_VOUT=0
> UTXO1_ADDRESS=n2KprMQm4z2vmZnPMENfbp2P1LLdAEFRjS

> UTXO2_TXID=263c018582731ff54dc72c7d67e858c002ae298835501d80200[...]
> UTXO2_VOUT=0
> UTXO2_ADDRESS=muhtvdmsnbQEPFuEmxcChX58fGvXaaUoVt
```

For our two inputs, we select two UTXOs by placing the [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) and [output index](https://developer.bitcoin.org/terms.html#term-output-index) numbers (vouts) in shell variables. We also save the addresses corresponding to the public keys (hashed or unhashed) used in those transactions. We need the addresses so we can get the corresponding private keys from our [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/).

```
> bitcoin-cli -Regtest dumpprivkey $UTXO1_ADDRESS
cSp57iWuu5APuzrPGyGc4PGUeCg23PjenZPBPoUs24HtJawccHPm

> bitcoin-cli -Regtest dumpprivkey $UTXO2_ADDRESS
cT26DX6Ctco7pxaUptJujRfbMS2PJvdqiSMaGaoSktHyon8kQUSg

> UTXO1_PRIVATE_KEY=cSp57iWuu5APuzrPGyGc4PGUeCg23PjenZPBPoUs24Ht[...]

> UTXO2_PRIVATE_KEY=cT26DX6Ctco7pxaUptJujRfbMS2PJvdqiSMaGaoSktHy[...]
```

Use the [“dumpprivkey” RPC](https://developer.bitcoin.org/reference/rpc/dumpprivkey.html) to get the private keys corresponding to the public keys used in the two UTXOs we will be spending. We need the private keys so we can sign each of the inputs separately.

![Warning icon](https://developer.bitcoin.org/_images/icon_warning.svg) **Warning:** Users should never manually manage private keys on [Mainnet](https://bitcoindevelopers.org/encyclopedia/mainnet/). As dangerous as raw transactions are (see warnings above), making a mistake with a [Private key](https://bitcoindevelopers.org/encyclopedia/private-key/) can be much worse—as in the case of a HD [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/) [cross-generational key compromise](https://developer.bitcoin.org/devguide/wallets.html#hardened-keys). These examples are to help you learn, not for you to emulate on [Mainnet](https://bitcoindevelopers.org/encyclopedia/mainnet/).

```
> bitcoin-cli -Regtest getnewaddress
n4puhBEeEWD2VvjdRC9kQuX2abKxSCMNqN
> bitcoin-cli -Regtest getnewaddress
n4LWXU59yM5MzQev7Jx7VNeq1BqZ85ZbLj

> NEW_ADDRESS1=n4puhBEeEWD2VvjdRC9kQuX2abKxSCMNqN
> NEW_ADDRESS2=n4LWXU59yM5MzQev7Jx7VNeq1BqZ85ZbLj
```

For our two outputs, get two new addresses.

```
## Outputs - inputs = Transaction fee, so always double-check your math!
> bitcoin-cli -Regtest createrawtransaction '''
    [
      {
        "Txid": "'$UTXO1_TXID'",
        "vout": '$UTXO1_VOUT'
      },
      {
        "Txid": "'$UTXO2_TXID'",
        "vout": '$UTXO2_VOUT'
      }
    ]
    ''' '''
    {
      "'$NEW_ADDRESS1'": 79.9999,
      "'$NEW_ADDRESS2'": 10
    }'''
0100000002f327e86da3e66bd20e1129b1fb36d07056f0b9a117199e75939652
6b8f3a20780000000000fffffffff0ede03d75050f20801d50358829ae02c058
e8677d2cc74df51f738285013c260000000000ffffffff02f028d6dc01000000
1976a914ffb035781c3c69e076d48b60c3d38592e7ce06a788ac00ca9a3b0000
00001976a914fa5139067622fd7e1e722a05c17c2bb7d5fd6df088ac00000000

> RAW_TX=0100000002f327e86da3e66bd20e1129b1fb36d07056f0b9a117199[...]
```

Create the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) using [“createrawtransaction”](https://developer.bitcoin.org/reference/rpc/createrawtransaction.html) much the same as before, except now we have two inputs and two outputs.

```
> bitcoin-cli -Regtest signrawtransaction $RAW_TX '[]' '''
    [
      "'$UTXO1_PRIVATE_KEY'"
    ]'''
```

```
{
    "hex" : "0100000002f327e86da3e66bd20e1129b1fb36d07056f0b9a11
             7199e759396526b8f3a20780000000049483045022100fce442
             ec52aa2792efc27fd3ad0eaf7fa69f097fdcefab017ea56d179
             9b10b2102207a6ae3eb61e11ffaba0453f173d1792f1b7bb8e7
             422ea945101d68535c4b474801fffffffff0ede03d75050f208
             01d50358829ae02c058e8677d2cc74df51f738285013c260000
             000000ffffffff02f028d6dc010000001976a914ffb035781c3
             c69e076d48b60c3d38592e7ce06a788ac00ca9a3b0000000019
             76a914fa5139067622fd7e1e722a05c17c2bb7d5fd6df088ac0
             0000000",
    "complete" : false
    "errors": [
    {
      "Txid": "c53f8f5ac0b6b10cdc77f543718eb3880fee6cf9b5e0cbf4edb2a59c0fae09a4",
      "vout": 0,
      "ScriptSig": "",
      "sequence": 4294967295,
      "error": "Operation not valid with the current stack size"
    }
  ]
}
```

```
> PARTLY_SIGNED_RAW_TX=0100000002f327e86da3e66bd20e1129b1fb36d07[...]
```

Signing the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) with `signrawtransaction` gets more complicated as we now have three arguments:

1. The unsigned [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/).
2. An empty array. We don’t do anything with this argument in this operation, but some valid JSON must be provided to get access to the later positional arguments.
3. The [Private key](https://bitcoindevelopers.org/encyclopedia/private-key/) we want to use to sign one of the inputs.

The result is a [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) with only one [Input](https://bitcoindevelopers.org/encyclopedia/input/) signed; the fact that the transaction isn’t fully signed is indicated by value of the `complete` JSON field. We save the incomplete, partly-signed [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) hex to a shell variable.

```
> bitcoin-cli -Regtest signrawtransaction $PARTLY_SIGNED_RAW_TX '[]' '''
    [
      "'$UTXO2_PRIVATE_KEY'"
    ]'''
```

```
{
    "hex" : "0100000002f327e86da3e66bd20e1129b1fb36d07056f0b9a11
             7199e759396526b8f3a20780000000049483045022100fce442
             ec52aa2792efc27fd3ad0eaf7fa69f097fdcefab017ea56d179
             9b10b2102207a6ae3eb61e11ffaba0453f173d1792f1b7bb8e7
             422ea945101d68535c4b474801fffffffff0ede03d75050f208
             01d50358829ae02c058e8677d2cc74df51f738285013c260000
             00006b483045022100b77f935ff366a6f3c2fdeb83589c79026
             5d43b3d2cf5e5f0047da56c36de75f40220707ceda75d8dcf2c
             caebc506f7293c3dcb910554560763d7659fb202f8ec324b012
             102240d7d3c7aad57b68aa0178f4c56f997d1bfab2ded3c2f94
             27686017c603a6d6ffffffff02f028d6dc010000001976a914f
             fb035781c3c69e076d48b60c3d38592e7ce06a788ac00ca9a3b
             000000001976a914fa5139067622fd7e1e722a05c17c2bb7d5f
             d6df088ac00000000",
    "complete" : true
}
```

To sign the second [Input](https://bitcoindevelopers.org/encyclopedia/input/), we repeat the process we used to sign the first [Input](https://bitcoindevelopers.org/encyclopedia/input/) using the second [Private key](https://bitcoindevelopers.org/encyclopedia/private-key/). Now that both inputs are signed, the `complete` result is *true*.

```
> unset PARTLY_SIGNED_RAW_TX RAW_TX NEW_ADDRESS1 [...]
```

Clean up the shell variables used. Unlike previous subsections, we’re not going to send this transaction to the connected [Node](https://bitcoindevelopers.org/encyclopedia/node/) with [“sendrawtransaction”](https://developer.bitcoin.org/reference/rpc/sendrawtransaction.html). This will allow us to illustrate in the Offline Signing subsection below how to spend a transaction which is not yet in the [Block](https://bitcoindevelopers.org/encyclopedia/block/) chain or memory pool.

### Offline Signing[https://developer.bitcoin.org/examples/transactions.html#offline-signing](https://developer.bitcoin.org/examples/transactions.html#offline-signing)

We will now spend the transaction created in the Complex [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) subsection above without sending it to the local [Node](https://bitcoindevelopers.org/encyclopedia/node/) first. This is the same basic process used by [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/) programs for offline signing—which generally means signing a transaction without access to the current [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) set.

Offline signing is safe. However, in this example we will also be spending an [Output](https://bitcoindevelopers.org/encyclopedia/output/) which is not part of the [Block](https://bitcoindevelopers.org/encyclopedia/block/) chain because the transaction containing it has never been broadcast. That can be unsafe:

![Warning icon](https://developer.bitcoin.org/_images/icon_warning.svg) **Warning:** Transactions which spend outputs from unconfirmed transactions are vulnerable to [Transaction malleability](https://bitcoindevelopers.org/encyclopedia/transaction-malleability/). Be sure to read about [Transaction malleability](https://bitcoindevelopers.org/encyclopedia/transaction-malleability/) and adopt good practices before spending unconfirmed transactions on [Mainnet](https://bitcoindevelopers.org/encyclopedia/mainnet/).

```
> OLD_SIGNED_RAW_TX=0100000002f327e86da3e66bd20e1129b1fb36d07056
      f0b9a117199e759396526b8f3a20780000000049483045022100fce442
      ec52aa2792efc27fd3ad0eaf7fa69f097fdcefab017ea56d1799b10b21
      02207a6ae3eb61e11ffaba0453f173d1792f1b7bb8e7422ea945101d68
      535c4b474801fffffffff0ede03d75050f20801d50358829ae02c058e8
      677d2cc74df51f738285013c26000000006b483045022100b77f935ff3
      66a6f3c2fdeb83589c790265d43b3d2cf5e5f0047da56c36de75f40220
      707ceda75d8dcf2ccaebc506f7293c3dcb910554560763d7659fb202f8
      ec324b012102240d7d3c7aad57b68aa0178f4c56f997d1bfab2ded3c2f
      9427686017c603a6d6ffffffff02f028d6dc010000001976a914ffb035
      781c3c69e076d48b60c3d38592e7ce06a788ac00ca9a3b000000001976
      a914fa5139067622fd7e1e722a05c17c2bb7d5fd6df088ac00000000
```

Put the previously signed (but not sent) transaction into a shell variable.

```
> bitcoin-cli -Regtest decoderawtransaction $OLD_SIGNED_RAW_TX
```

```
{
    "Txid" : "682cad881df69cb9df8f0c996ce96ecad758357ded2da03bad
              40cf18ffbb8e09",
    "hash" : "682cad881df69cb9df8f0c996ce96ecad758357ded2da03bad40cf18ffbb8e09",
    "size" : 340,
    "vsize" : 340,
    "version" : 1,
    "Locktime" : 0,
    "vin" : [
        {
            "Txid" : "78203a8f6b529693759e1917a1b9f05670d036fbb1
                      29110ed26be6a36de827f3",
            "vout" : 0,
            "ScriptSig" : {
                "asm" : "3045022100fce442ec52aa2792efc27fd3ad0ea
                         f7fa69f097fdcefab017ea56d1799b10b210220
                         7a6ae3eb61e11ffaba0453f173d1792f1b7bb8e
                         7422ea945101d68535c4b474801",
                "hex" : "483045022100FCE442ec52aa2792efc27fd3ad0
                         eaf7fa69f097fdcefab017ea56d1799b10b2102
                         207a6ae3eb61e11ffaba0453f173d1792f1b7bb
                         8e7422ea945101d68535c4b474801"
            },
            "sequence" : 4294967295
        },
        {
            "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835
                      501d80200f05753de0edf0",
            "vout" : 0,
            "ScriptSig" : {
                "asm" : "3045022100b77f935ff366a6f3c2fdeb83589c7
                         90265d43b3d2cf5e5f0047da56c36de75f40220
                         707ceda75d8dcf2ccaebc506f7293c3dcb91055
                         4560763d7659fb202f8ec324b01
                         02240d7d3c7aad57b68aa0178f4c56f997d1bfa
                         b2ded3c2f9427686017c603a6d6",
                "hex" : "483045022100b77f935ff366a6f3c2fdeb83589
                         c790265d43b3d2cf5e5f0047da56c36de75f402
                         20707ceda75d8dcf2ccaebc506f7293c3dcb910
                         554560763d7659fb202f8ec324b012102240d7d
                         3c7aad57b68aa0178f4c56f997d1bfab2ded3c2
                         f9427686017c603a6d6"
            },
            "sequence" : 4294967295
        }
    ],
    "vout" : [
        {
            "value" : 79.99990000,
            "n" : 0,
            "ScriptPubKey" : {
                "asm" : "OP_DUP OP_HASH160 ffb035781c3c69e076d48
                         b60c3d38592e7ce06a7 OP_EQUALVERIFY OP_CHECKSIG",
                "hex" : "76a914ffb035781c3c69e076d48b60c3d38592e
                         7ce06a788ac",
                "reqSigs" : 1,
                "type" : "pubkeyhash",
                "addresses" : [
                    "n4puhBEeEWD2VvjdRC9kQuX2abKxSCMNqN"
                ]
            }
        },
        {
            "value" : 10.00000000,
            "n" : 1,
            "ScriptPubKey" : {
                "asm" : "OP_DUP OP_HASH160 fa5139067622fd7e1e722
                         a05c17c2bb7d5fd6df0 OP_EQUALVERIFY OP_CHECKSIG",
                "hex" : "76a914fa5139067622fd7e1e722a05c17c2bb7d
                         5fd6df088ac",
                "reqSigs" : 1,
                "type" : "pubkeyhash",
                "addresses" : [
                    "n4LWXU59yM5MzQev7Jx7VNeq1BqZ85ZbLj"
                ]
            }
        }
    ]
}
```

```
> UTXO_TXID=682cad881df69cb9df8f0c996ce96ecad758357ded2da03bad40[...]
> UTXO_VOUT=1
> UTXO_VALUE=10.00000000
> UTXO_OUTPUT_SCRIPT=76a914fa5139067622fd7e1e722a05c17c2bb7d5fd6[...]
```

Decode the signed [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) so we can get its [Txid](https://bitcoindevelopers.org/encyclopedia/txid/). Also, choose a specific one of its UTXOs to spend and save that [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/)’s [output index](https://developer.bitcoin.org/terms.html#term-output-index) number (vout) and hex [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/) ([ScriptPubKey](https://bitcoindevelopers.org/encyclopedia/scriptpubkey/)) into shell variables.

```
> bitcoin-cli -Regtest getnewaddress
mfdCHEFL2tW9eEUpizk7XLZJcnFM4hrp78

> NEW_ADDRESS=mfdCHEFL2tW9eEUpizk7XLZJcnFM4hrp78
```

Get a new [Address](https://bitcoindevelopers.org/encyclopedia/address/) to spend the [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) to.

```
## Outputs - inputs = Transaction fee, so always double-check your math!
> bitcoin-cli -Regtest createrawtransaction '''
    [
      {
        "Txid": "'$UTXO_TXID'",
        "vout": '$UTXO_VOUT'
      }
    ]
    ''' '''
    {
      "'$NEW_ADDRESS'": 9.9999
    }'''
0100000001098ebbff18cf40ad3ba02ded7d3558d7ca6ee96c990c8fdfb99cf6
1d88ad2c680100000000ffffffff01f0a29a3b000000001976a914012e2ba6a0
51c033b03d712ca2ea00a35eac1e7988ac00000000

> RAW_TX=0100000001098ebbff18cf40ad3ba02ded7d3558d7ca6ee96c990c8[...]
```

Create the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) the same way we’ve done in the previous subsections.

```
> bitcoin-cli -Regtest signrawtransaction $RAW_TX
```

```
{
    "hex" : "0100000001098ebbff18cf40ad3ba02ded7d3558d7ca6ee
             96c990c8fdfb99cf61d88ad2c680100000000ffffffff01
             f0a29a3b000000001976a914012e2ba6a051c033b03d712
             ca2ea00a35eac1e7988ac00000000",
    "complete" : false
}
```

Attempt to sign the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) without any special arguments, the way we successfully signed the the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) in the Simple [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) subsection. If you’ve read the [Transaction section](https://developer.bitcoin.org/devguide/transactions.html) of the guide, you may know why the call fails and leaves the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) hex unchanged.

![Old Transaction Data Required To Be Signed](https://developer.bitcoin.org/_images/en-signing-output-to-spend.svg)

Old Transaction Data Required To Be Signed[https://developer.bitcoin.org/examples/transactions.html#id1](https://developer.bitcoin.org/examples/transactions.html#id1)

As illustrated above, the data that gets signed includes the [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) and vout from the previous transaction. That information is included in the [“createrawtransaction”](https://developer.bitcoin.org/reference/rpc/createrawtransaction.html) [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/). But the data that gets signed also includes the [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/) from the previous transaction, even though it doesn’t appear in either the unsigned or signed transaction.

In the other [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) subsections above, the previous [Output](https://bitcoindevelopers.org/encyclopedia/output/) was part of the [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) set known to the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/), so the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/) was able to use the [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) and [output index](https://developer.bitcoin.org/terms.html#term-output-index) number to find the previous [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/) and insert it automatically.

In this case, you’re spending an [Output](https://bitcoindevelopers.org/encyclopedia/output/) which is unknown to the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/), so it can’t automatically insert the previous [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/).

```
> bitcoin-cli -Regtest signrawtransaction $RAW_TX '''
    [
      {
        "Txid": "'$UTXO_TXID'",
        "vout": '$UTXO_VOUT',
        "ScriptPubKey": "'$UTXO_OUTPUT_SCRIPT'",
        "value": '$UTXO_VALUE'
      }
    ]'''
```

```
{
    "hex" : "0100000001098ebbff18cf40ad3ba02ded7d3558d7ca6ee96c9
             90c8fdfb99cf61d88ad2c68010000006b483045022100c3f92f
             b74bfa687d76ebe75a654510bb291b8aab6f89ded4fe26777c2
             eb233ad02207f779ce2a181cc4055cb0362aba7fd7a6f72d5db
             b9bd863f4faaf47d8d6c4b500121028e4e62d25760709806131
             b014e2572f7590e70be01f0ef16bfbd51ea5f389d4dffffffff
             01f0a29a3b000000001976a914012e2ba6a051c033b03d712ca
             2ea00a35eac1e7988ac00000000",
    "complete" : true
}
```

```
> SIGNED_RAW_TX=0100000001098ebbff18cf40ad3ba02ded7d3558d7ca6ee9[...]
```

Successfully sign the transaction by providing the previous [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/) and other required [Input](https://bitcoindevelopers.org/encyclopedia/input/) data.

This specific operation is typically what offline signing wallets do. The online [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/) creates the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) and gets the previous pubkey scripts for all the inputs. The user brings this information to the offline [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/). After displaying the transaction details to the user, the offline [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/) signs the transaction as we did above. The user takes the signed transaction back to the online [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/), which broadcasts it.

```
> bitcoin-cli -Regtest sendrawtransaction $SIGNED_RAW_TX
```

```
{"error": {"code":-22,"Message":"TX rejected"}}
```

Attempt to broadcast the second transaction before we’ve broadcast the first transaction. The [Node](https://bitcoindevelopers.org/encyclopedia/node/) rejects this attempt because the second transaction spends an [Output](https://bitcoindevelopers.org/encyclopedia/output/) which is not a [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) the [Node](https://bitcoindevelopers.org/encyclopedia/node/) knows about.

```
> bitcoin-cli -Regtest sendrawtransaction $OLD_SIGNED_RAW_TX
682cad881df69cb9df8f0c996ce96ecad758357ded2da03bad40cf18ffbb8e09
> bitcoin-cli -Regtest sendrawtransaction $SIGNED_RAW_TX
67d53afa1a8167ca093d30be7fb9dcb8a64a5fdecacec9d93396330c47052c57
```

Broadcast the first transaction, which succeeds, and then broadcast the second transaction—which also now succeeds because the [Node](https://bitcoindevelopers.org/encyclopedia/node/) now sees the [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/).

```
> bitcoin-cli -Regtest getrawmempool
```

```
[
    "67d53afa1a8167ca093d30be7fb9dcb8a64a5fdecacec9d93396330c47052c57",
    "682cad881df69cb9df8f0c996ce96ecad758357ded2da03bad40cf18ffbb8e09"
]
```

We have once again not generated an additional [Block](https://bitcoindevelopers.org/encyclopedia/block/), so the transactions above have not yet become part of the [Regtest](https://bitcoindevelopers.org/encyclopedia/regtest/) [Block](https://bitcoindevelopers.org/encyclopedia/block/) chain. However, they are part of the local [Node](https://bitcoindevelopers.org/encyclopedia/node/)’s memory pool.

```
> unset OLD_SIGNED_RAW_TX SIGNED_RAW_TX RAW_TX [...]
```

Remove old shell variables.

### [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/)[https://developer.bitcoin.org/examples/transactions.html#p2sh-multisig](https://developer.bitcoin.org/examples/transactions.html#p2sh-multisig)

In this subsection, we will create a [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/) [Address](https://bitcoindevelopers.org/encyclopedia/address/), spend [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) to it, and then spend those [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) from it to another [Address](https://bitcoindevelopers.org/encyclopedia/address/).

Creating a [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Address](https://bitcoindevelopers.org/encyclopedia/address/) is easy. [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) outputs have two parameters, the *minimum* number of signatures required (*m*) and the *number* of public keys to use to validate those signatures. This is called m-of-n, and in this case we’ll be using 2-of-3.

```
> bitcoin-cli -Regtest getnewaddress
mhAXF4Eq7iRyvbYk1mpDVBiGdLP3YbY6Dm
> bitcoin-cli -Regtest getnewaddress
moaCrnRfP5zzyhW8k65f6Rf2z5QpvJzSKe
> bitcoin-cli -Regtest getnewaddress
mk2QpYatsKicvFVuTAQLBryyccRXMUaGHP

> NEW_ADDRESS1=mhAXF4Eq7iRyvbYk1mpDVBiGdLP3YbY6Dm
> NEW_ADDRESS2=moaCrnRfP5zzyhW8k65f6Rf2z5QpvJzSKe
> NEW_ADDRESS3=mk2QpYatsKicvFVuTAQLBryyccRXMUaGHP
```

Generate three new P2PKH addresses. P2PKH addresses cannot be used with the [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) created below. (Hashing each [Public key](https://bitcoindevelopers.org/encyclopedia/public-key/) is unnecessary anyway—all the public keys are protected by a hash when the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) is hashed.) However, Bitcoin Core uses addresses as a way to reference the underlying full (unhashed) public keys it knows about, so we get the three new addresses above in order to use their public keys.

Recall from the Guide that the hashed public keys used in addresses obfuscate the full [Public key](https://bitcoindevelopers.org/encyclopedia/public-key/), so you cannot give an [Address](https://bitcoindevelopers.org/encyclopedia/address/) to another person or device as part of creating a typical [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Output](https://bitcoindevelopers.org/encyclopedia/output/) or [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/) [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/). You must give them a full [Public key](https://bitcoindevelopers.org/encyclopedia/public-key/).

```
> bitcoin-cli -Regtest validateaddress $NEW_ADDRESS3
```

```
{
    "isvalid" : true,
    "Address" : "mk2QpYatsKicvFVuTAQLBryyccRXMUaGHP",
    "ScriptPubKey" : "76a9143172b5654f6683c8fb146959d347ce303cae4ca788ac",
    "ismine" : true,
    "iswatchonly" : false,
    "isscript" : false,
    "pubkey" : "029e03a901b85534ff1e92c43c74431f7ce72046060fcf7a
                95c37e148f78c77255",
    "iscompressed" : true,
    "account" : ""
}
```

```
> NEW_ADDRESS3_PUBLIC_KEY=029e03a901b85534ff1e92c43c74431f7ce720[...]
```

Use the [“validateaddress” RPC](https://developer.bitcoin.org/reference/rpc/validateaddress.html) to display the full (unhashed) [Public key](https://bitcoindevelopers.org/encyclopedia/public-key/) for one of the addresses. This is the information which will actually be included in the [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/). This is also the information you would give another person or device as part of creating a [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Output](https://bitcoindevelopers.org/encyclopedia/output/) or [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/) [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/).

We save the [Address](https://bitcoindevelopers.org/encyclopedia/address/) returned to a shell variable.

```
> bitcoin-cli -Regtest createmultisig 2 '''
    [
      "'$NEW_ADDRESS1'",
      "'$NEW_ADDRESS2'",
      "'$NEW_ADDRESS3_PUBLIC_KEY'"
    ]'''
```

```
{
    "Address" : "2N7NaqSKYQUeM8VNgBy8D9xQQbiA8yiJayk",
    "RedeemScript" : "522103310188e911026cf18c3ce274e0ebb5f95b00
    7f230d8cb7d09879d96dbeab1aff210243930746e6ed6552e03359db521b
    088134652905bd2d1541fa9124303a41e95621029e03a901b85534ff1e92
    c43c74431f7ce72046060fcf7a95c37e148f78c7725553ae"
}
```

```
> P2SH_ADDRESS=2N7NaqSKYQUeM8VNgBy8D9xQQbiA8yiJayk
> P2SH_REDEEM_SCRIPT=522103310188e911026cf18c3ce274e0ebb5f95b007[...]
```

Use the [“createmultisig” RPC](https://developer.bitcoin.org/reference/rpc/createmultisig.html) with two arguments, the number (*n*) of signatures required and a list of addresses or public keys. Because P2PKH addresses can’t be used in the [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) created by this [RPC](https://developer.bitcoin.org/reference/rpc/index.html), the only addresses which can be provided are those belonging to a [Public key](https://bitcoindevelopers.org/encyclopedia/public-key/) in the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/). In this case, we provide two addresses and one [Public key](https://bitcoindevelopers.org/encyclopedia/public-key/)—all of which will be converted to public keys in the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/).

The P2SH [Address](https://bitcoindevelopers.org/encyclopedia/address/) is returned along with the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) which must be provided when we spend [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) sent to the P2SH [Address](https://bitcoindevelopers.org/encyclopedia/address/).

![Warning icon](https://developer.bitcoin.org/_images/icon_warning.svg) **Warning:** You must not lose the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/), especially if you don’t have a record of which public keys you used to create the [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/) [Address](https://bitcoindevelopers.org/encyclopedia/address/). You need the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) to spend any [Bitcoins](https://bitcoindevelopers.org/encyclopedia/bitcoins/) sent to the P2SH [Address](https://bitcoindevelopers.org/encyclopedia/address/). If you lose the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/), you can recreate it by running the same command above, with the public keys listed in the same order. However, if you lose both the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) and even one of the public keys, you will never be able to spend [Satoshis](https://bitcoindevelopers.org/encyclopedia/satoshis/) sent to that P2SH [Address](https://bitcoindevelopers.org/encyclopedia/address/).

Neither the [Address](https://bitcoindevelopers.org/encyclopedia/address/) nor the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) are stored in the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/) when you use [“createmultisig”](https://developer.bitcoin.org/reference/rpc/createmultisig.html). To store them in the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/), use the [“addmultisigaddress” RPC](https://developer.bitcoin.org/reference/rpc/addmultisigaddress.html) instead. If you add an [Address](https://bitcoindevelopers.org/encyclopedia/address/) to the [Wallet](https://bitcoindevelopers.org/encyclopedia/wallet/), you should also make a new backup.

```
> bitcoin-cli -Regtest sendtoaddress $P2SH_ADDRESS 10.00
7278d7d030f042ebe633732b512bcb31fff14a697675a1fe1884db139876e175

> UTXO_TXID=7278d7d030f042ebe633732b512bcb31fff14a697675a1fe1884[...]
```

Paying the [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/) [Address](https://bitcoindevelopers.org/encyclopedia/address/) with Bitcoin Core is as simple as paying a more common P2PKH [Address](https://bitcoindevelopers.org/encyclopedia/address/). Here we use the same command (but different variable) we used in the Simple Spending subsection. As before, this command automatically selects an [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/), creates a change [Output](https://bitcoindevelopers.org/encyclopedia/output/) to a new one of our P2PKH addresses if necessary, and pays a [Transaction fee](https://bitcoindevelopers.org/encyclopedia/transaction-fee/) if necessary.

We save that [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) to a shell variable as the [Txid](https://bitcoindevelopers.org/encyclopedia/txid/) of the [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) we plan to spend next.

```
> bitcoin-cli -Regtest getrawtransaction $UTXO_TXID 1
```

```
{
    "hex" : "0100000001f0ede03d75050f20801d50358829ae02c058e8677
             d2cc74df51f738285013c26010000006a47304402203c375959
             2bf608ab79c01596c4a417f3110dd6eb776270337e575cdafc6
             99af20220317ef140d596cc255a4067df8125db7f349ad94521
             2e9264a87fa8d777151937012102a92913b70f9fb15a7ea5c42
             df44637f0de26e2dad97d6d54957690b94cf2cd05ffffffff01
             00ca9a3b0000000017a9149af61346ce0aa2dffcf697352b4b7
             04c84dcbaff8700000000",
    "Txid" : "7278d7d030f042ebe633732b512bcb31fff14a697675a1fe18
              84db139876e175",
    "hash" : "7278d7d030f042ebe633732b512bcb31fff14a697675a1fe1884db139876e175",
    "size" : 189,
    "vsize" : 189,
    "version" : 1,
    "Locktime" : 0,
    "vin" : [
        {
            "Txid" : "263c018582731ff54dc72c7d67e858c002ae298835
                      501d80200f05753de0edf0",
            "vout" : 1,
            "ScriptSig" : {
                "asm" : "304402203c3759592bf608ab79c01596c4a417f
                         3110dd6eb776270337e575cdafc699af2022031
                         7ef140d596cc255a4067df8125db7f349ad9452
                         12e9264a87fa8d77715193701
                         02a92913b70f9fb15a7ea5c42df44637f0de26e
                         2dad97d6d54957690b94cf2cd05",
                "hex" : "47304402203c3759592bf608ab79c01596c4a41
                         7f3110dd6eb776270337e575cdafc699af20220
                         317ef140d596cc255a4067df8125db7f349ad94
                         5212e9264a87fa8d777151937012102a92913b7
                         0f9fb15a7ea5c42df44637f0de26e2dad97d6d5
                         4957690b94cf2cd05"
            },
            "sequence" : 4294967295
        }
    ],
    "vout" : [
        {
            "value" : 10.00000000,
            "n" : 0,
            "ScriptPubKey" : {
                "asm" : "OP_HASH160 9af61346ce0aa2dffcf697352b4b
                704c84dcbaff OP_EQUAL",
                "hex" : "a9149af61346ce0aa2dffcf697352b4b704c84d
                         cbaff87",
                "reqSigs" : 1,
                "type" : "scripthash",
                "addresses" : [
                    "2N7NaqSKYQUeM8VNgBy8D9xQQbiA8yiJayk"
                ]
            }
        }
    ]
}
```

```
> UTXO_VOUT=0
> UTXO_OUTPUT_SCRIPT=a9149af61346ce0aa2dffcf697352b4b704c84dcbaff87
```

We use the [“getrawtransaction” RPC](https://developer.bitcoin.org/reference/rpc/getrawtransaction.html) with the optional second argument (*true*) to get the decoded transaction we just created with [“sendtoaddress”](https://developer.bitcoin.org/reference/rpc/sendtoaddress.html). We choose one of the outputs to be our [UTXO](https://bitcoindevelopers.org/encyclopedia/utxo/) and get its [output index](https://developer.bitcoin.org/terms.html#term-output-index) number (vout) and [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/) ([ScriptPubKey](https://bitcoindevelopers.org/encyclopedia/scriptpubkey/)).

```
> bitcoin-cli -Regtest getnewaddress
mxCNLtKxzgjg8yyNHeuFSXvxCvagkWdfGU

> NEW_ADDRESS4=mxCNLtKxzgjg8yyNHeuFSXvxCvagkWdfGU
```

We generate a new P2PKH [Address](https://bitcoindevelopers.org/encyclopedia/address/) to use in the [Output](https://bitcoindevelopers.org/encyclopedia/output/) we’re about to create.

```
## Outputs - inputs = Transaction fee, so always double-check your math!
> bitcoin-cli -Regtest createrawtransaction '''
    [
      {
        "Txid": "'$UTXO_TXID'",
        "vout": '$UTXO_VOUT'
      }
   ]
   ''' '''
   {
     "'$NEW_ADDRESS4'": 9.998
   }'''

010000000175e1769813db8418fea17576694af1ff31cb2b512b7333e6eb42f0
30d0d778720000000000ffffffff01c0bc973b000000001976a914b6f64f5bf3
e38f25ead28817df7929c06fe847ee88ac00000000

> RAW_TX=010000000175e1769813db8418fea17576694af1ff31cb2b512b733[...]
```

We generate the [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) the same way we did in the Simple [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) subsection.

```
> bitcoin-cli -Regtest dumpprivkey $NEW_ADDRESS1
cVinshabsALz5Wg4tGDiBuqEGq4i6WCKWXRQdM8RFxLbALvNSHw7
> bitcoin-cli -Regtest dumpprivkey $NEW_ADDRESS3
cNmbnwwGzEghMMe1vBwH34DFHShEj5bcXD1QpFRPHgG9Mj1xc5hq

> NEW_ADDRESS1_PRIVATE_KEY=cVinshabsALz5Wg4tGDiBuqEGq4i6WCKWXRQd[...]
> NEW_ADDRESS3_PRIVATE_KEY=cNmbnwwGzEghMMe1vBwH34DFHShEj5bcXD1Qp[...]
```

We get the private keys for two of the public keys we used to create the transaction, the same way we got private keys in the Complex [Raw transaction](https://bitcoindevelopers.org/encyclopedia/raw-transaction/) subsection. Recall that we created a 2-of-3 [Multisig](https://bitcoindevelopers.org/encyclopedia/multisig/) [Pubkey script](https://bitcoindevelopers.org/encyclopedia/pubkey-script/), so signatures from two private keys are needed.

![Warning icon](https://developer.bitcoin.org/_images/icon_warning.svg) **Reminder:** Users should never manually manage private keys on [Mainnet](https://bitcoindevelopers.org/encyclopedia/mainnet/). See the warning in the [complex raw transaction section](https://developer.bitcoin.org/examples/transactions.html#complex-raw-transaction).

```
> bitcoin-cli -Regtest signrawtransaction $RAW_TX '''
    [
      {
        "Txid": "'$UTXO_TXID'",
        "vout": '$UTXO_VOUT',
        "ScriptPubKey": "'$UTXO_OUTPUT_SCRIPT'",
        "RedeemScript": "'$P2SH_REDEEM_SCRIPT'"
      }
    ]
    ''' '''
    [
      "'$NEW_ADDRESS1_PRIVATE_KEY'"
    ]'''
```

```
{
    "hex" : "010000000175e1769813db8418fea17576694af1ff31cb2b512
             b7333e6eb42f030d0d7787200000000b5004830450221008d5e
             c57d362ff6ef6602e4e756ef1bdeee12bd5c5c72697ef1455b3
             79c90531002202ef3ea04dfbeda043395e5bc701e4878c15baa
             b9c6ba5808eb3d04c91f641a0c014c69522103310188e911026
             cf18c3ce274e0ebb5f95b007f230d8cb7d09879d96dbeab1aff
             210243930746e6ed6552e03359db521b088134652905bd2d154
             1fa9124303a41e95621029e03a901b85534ff1e92c43c74431f
             7ce72046060fcf7a95c37e148f78c7725553aeffffffff01c0b
             c973b000000001976a914b6f64f5bf3e38f25ead28817df7929
             c06fe847ee88ac00000000",
    "complete" : false
}
```

```
> PARTLY_SIGNED_RAW_TX=010000000175e1769813db8418fea17576694af1f[...]
```

We make the first [Signature](https://bitcoindevelopers.org/encyclopedia/signature/). The [Input](https://bitcoindevelopers.org/encyclopedia/input/) argument (JSON object) takes the additional [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) parameter so that it can append the [Redeem script](https://bitcoindevelopers.org/encyclopedia/redeem-script/) to the [Signature](https://bitcoindevelopers.org/encyclopedia/signature/) script after the two signatures.

```
> bitcoin-cli -Regtest signrawtransaction $PARTLY_SIGNED_RAW_TX '''
    [
      {
        "Txid": "'$UTXO_TXID'",
        "vout": '$UTXO_VOUT',
        "ScriptPubKey": "'$UTXO_OUTPUT_SCRIPT'",
        "RedeemScript": "'$P2SH_REDEEM_SCRIPT'"
      }
    ]
    ''' '''
    [
      "'$NEW_ADDRESS3_PRIVATE_KEY'"
    ]'''
```

```
{
    "hex" : "010000000175e1769813db8418fea17576694af1ff31cb2b512
             b7333e6eb42f030d0d7787200000000fdfd0000483045022100
             8d5ec57d362ff6ef6602e4e756ef1bdeee12bd5c5c72697ef14
             55b379c90531002202ef3ea04dfbeda043395e5bc701e4878c1
             5baab9c6ba5808eb3d04c91f641a0c0147304402200bd8c62b9
             38e02094021e481b149fd5e366a212cb823187149799a68cfa7
             652002203b52120c5cf25ceab5f0a6b5cdb8eca0fd2f386316c
             9721177b75ddca82a4ae8014c69522103310188e911026cf18c
             3ce274e0ebb5f95b007f230d8cb7d09879d96dbeab1aff21024
             3930746e6ed6552e03359db521b088134652905bd2d1541fa91
             24303a41e95621029e03a901b85534ff1e92c43c74431f7ce72
             046060fcf7a95c37e148f78c7725553aeffffffff01c0bc973b
             000000001976a914b6f64f5bf3e38f25ead28817df7929c06fe
             847ee88ac00000000",
    "complete" : true
}
```

```
> SIGNED_RAW_TX=010000000175e1769813db8418fea17576694af1ff31cb2b[...]
```

The `signrawtransaction` call used here is nearly identical to the one used above. The only difference is the [Private key](https://bitcoindevelopers.org/encyclopedia/private-key/) used. Now that the two required signatures have been provided, the transaction is marked as complete.

```
> bitcoin-cli -Regtest sendrawtransaction $SIGNED_RAW_TX
430a4cee3a55efb04cbb8718713cab18dea7f2521039aa660ffb5aae14ff3f50
```

We send the transaction spending the [P2SH multisig](https://bitcoindevelopers.org/encyclopedia/p2sh-multisig/) [Output](https://bitcoindevelopers.org/encyclopedia/output/) to the local [Node](https://bitcoindevelopers.org/encyclopedia/node/), which accepts it.
