<p>There might not be an easy answer, but the developper documentation is probably a first stop (<a href="https://bitcoin.org/en/developer-examples#complex-raw-transaction" rel="nofollow noreferrer">https://bitcoin.org/en/developer-examples#complex-raw-transaction</a>). Also I used regularily a block explorer (e.g. blockchain.info) to display the transaction and also its "raw" format. This allows to see the tx details in it's JSON format. Also the book from Andreas helped alot (Mastering Bitcoin). And finally there is also this forum here (the search function is good), and bitcointalk.org. Oh, then there is the wiki as well: <a href="https://en.bitcoin.it/wiki/Protocol_documentation#tx" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_documentation#tx</a>. </p>

<blockquote>
  <p>For the inputs, is it as simple as taking all the input transactions
  into little-endian, noting the correct number in inputs, and going
  from there?</p>
</blockquote>

<p>No, I don't think so. There is no such thing as simple :-) 
You are on the right path, additionally when assembling a raw, unsigned transaction, and you have to deal with field lengths, and with "var_int", which are explained in developper docs. Here is a sample tx, so you get an idea:</p>

<pre><code>VERSION                                 01000000
TX_IN COUNT [var_int]:                  hex=03, decimal=3
 TX_IN[0]
  TX_IN[0] OutPoint hash (char[32])     94FAE0AC28792796063F23F4A4BA4F977A9599D1579C5AAE7CE6DDA4F8A6B1BB
  TX_IN[0] OutPoint index (uint32_t)    hex=14040000, reversed=00000414, decimal=1044
  TX_IN[0] Script Length (var_int)      hex=19, decimal=25
  TX_IN[0] Script Sig (uchar[])         76A914A438060482FCD835754EA4518C70CC2085AF48FA88AC
  TX_IN[0] Sequence (uint32_t)          FFFFFFFF
 TX_IN[1]
  TX_IN[1] OutPoint hash (char[32])     A3E719B12275357B15FC5DECD9088A0964FE860D49F026F2152E71F681AC3FA4
  TX_IN[1] OutPoint index (uint32_t)    hex=31040000, reversed=00000431, decimal=1073
  TX_IN[1] Script Length (var_int)      hex=19, decimal=25
  TX_IN[1] Script Sig (uchar[])         76A914A438060482FCD835754EA4518C70CC2085AF48FA88AC
  TX_IN[1] Sequence (uint32_t)          FFFFFFFF
 TX_IN[2]
  TX_IN[2] OutPoint hash (char[32])     874CD4C4E1683C43A98A9DAA0926BEA37C10616F165AC35481E8181BFD449C65
  TX_IN[2] OutPoint index (uint32_t)    hex=E0010000, reversed=000001E0, decimal=480
  TX_IN[2] Script Length (var_int)      hex=19, decimal=25
  TX_IN[2] Script Sig (uchar[])         76A914A438060482FCD835754EA4518C70CC2085AF48FA88AC
  TX_IN[2] Sequence (uint32_t)          FFFFFFFF

TX_OUT COUNT                            hex=01, decimal=1
 TX_OUT[0]
  TX_OUT[0] Value (uint64_t)            hex=8038010000000000, reversed_hex=0000000000013880, dec=80000, bitcoin=0.00080000
  TX_OUT[0] PK_Script Length (var_int)  hex=19, dec=25
  TX_OUT[0] pk_script (uchar[])         76A914C2DF275D78E506E17691FD6F0C63C43D15C897FC88AC
LOCK_TIME                               00000000
</code></pre>

<blockquote>
  <p>For outputs, do you just have an individual block of to satoshiBytes
  to scriptPubKey?</p>
</blockquote>

<p>It is similiar to the example above, just don't have one at hand. But the majority of tx have a spend-to address, and a return address, so you might look a tx up in the block explorer...
Happy coding ! :-)</p>

CC BY-SA 3.0
2017-11-12T21:59:26.603Z
Did you create this particular transaction? If so, can I get an idea of how you went about doing it? I don't care if it's in golang or not, I'm just trying to get a feel for how all of these pieces glue together.
tmesser
CC BY-SA 3.0
2017-11-12T22:10:14.283Z
yup, manually assembled with a set of shell scripts. When you are on unixoide systems, they work with Linux, MacOS and OpenBSD. Here: github.com/pebwindkraft/trx_cl_suite
pebwindkraft
CC BY-SA 3.0
2017-11-12T20:33:40.023Z
2017-11-12T20:33:40.023Z
pebwindkraft
<p>I am using golang's <a href="https://github.com/btcsuite/btcd" rel="nofollow noreferrer">btcd</a> as a basis for creating some server-side tools for handling transactions. I found a good <a href="https://github.com/prettymuchbryce/hellobitcoin" rel="nofollow noreferrer">example</a> for creating a basic one-input-one-output transaction to get me started on the implementation, and I have also <a href="https://en.bitcoin.it/wiki/Transaction" rel="nofollow noreferrer">read</a> <a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">the documentation</a> <a href="https://en.bitcoin.it/wiki/Protocol_documentation#Transaction_Verification" rel="nofollow noreferrer">heavily</a> on the technical specifics here.  I have also looked at the other <a href="https://github.com/btcsuite/" rel="nofollow noreferrer">btcsuite</a> libraries pretty heavily to try to gain more insight. To be clear, I am NOT expecting any of these tools to issue the transaction or anything like that; I understand that this was written out of btcd in particular for very, very good reasons. I am just trying to use these tools to create valid transactions that I can issue later.</p>

<p>I am still incredibly confused on the particulars in implementing multiple input, multiple output transactions and the scripts that we use on them.  A few topics:</p>

<ul>
<li>I'm <a href="https://github.com/btcsuite/btcd/blob/master/txscript/standard.go#L415" rel="nofollow noreferrer">familiar</a> with the script tools in btcd, the link goes to the most common one used, but all of these scripts are used for one specific output. There seems to be absolutely no documentation anywhere about how to provide scripting for multiple outputs. If I'm doing a normal P2PK or P2PKH transaction, do I simply concatenate the scripts for all the various outputs, or what?</li>
<li><p>In the <a href="https://github.com/prettymuchbryce/hellobitcoin/blob/master/transaction.go#L161" rel="nofollow noreferrer">create Transaction example</a> in the HelloBitcoin example I've linked above, the overall magic happens near the end with:</p>

<pre><code>buffer.Write(version)
buffer.Write(inputs)
buffer.Write(inputTransactionBytesReversed)
buffer.Write(outputIndexBytes)
buffer.WriteByte(byte(scriptSigLength))
buffer.Write(scriptSig)
buffer.Write(sequence)
buffer.Write(numOutputs)
buffer.Write(satoshiBytes)
buffer.WriteByte(byte(scriptPubKeyLength))
buffer.Write(scriptPubKey)
buffer.Write(lockTimeField)
</code></pre></li>
</ul>

<p>And I feel like there are a lot of unanswered questions on how to expand this to handle multiple inputs and multiple outputs. For the inputs, is it as simple as taking all the input transactions into little-endian, noting the correct number in <code>inputs</code>, and going from there? For outputs, do you just have an individual block of to <code>satoshiBytes</code> to <code>scriptPubKey</code>? I can't find in the documentation  how this is supposed to all glue together, and at this point I feel like I need some help with the nuts and bolts of this. Thanks in advance for any help - if we can get these two points answered I might end up making more questions from the stuff that will inevitably result from this.</p>

<transactions><script><btcd><go-lang>
Multiple input/output transactions using golang btcd
tmesser
# Multiple input/output transactions using golang btcd

(Asked by: username_0 on 2017-11-12)

I am using golang's [btcd](https://github.com/btcsuite/btcd) as a basis for creating some server-side tools for handling transactions. I found a good [example](https://github.com/prettymuchbryce/hellobitcoin) for creating a basic one-input-one-output transaction to get me started on the implementation, and I have also [read](https://en.bitcoin.it/wiki/Transaction) [the documentation](https://en.bitcoin.it/wiki/Script) [heavily](https://en.bitcoin.it/wiki/Protocol_documentation#Transaction_Verification) on the technical specifics here. I have also looked at the other [btcsuite](https://github.com/btcsuite/) libraries pretty heavily to try to gain more insight. To be clear, I am NOT expecting any of these tools to issue the transaction or anything like that; I understand that this was written out of btcd in particular for very, very good reasons. I am just trying to use these tools to create valid transactions that I can issue later.

I am still incredibly confused on the particulars in implementing multiple input, multiple output transactions and the scripts that we use on them. A few topics:

* I'm [familiar](https://github.com/btcsuite/btcd/blob/master/txscript/standard.go#L415) with the script tools in btcd, the link goes to the most common one used, but all of these scripts are used for one specific output. There seems to be absolutely no documentation anywhere about how to provide scripting for multiple outputs. If I'm doing a normal P2PK or P2PKH transaction, do I simply concatenate the scripts for all the various outputs, or what?
* In the [create Transaction example](https://github.com/prettymuchbryce/hellobitcoin/blob/master/transaction.go#L161) in the HelloBitcoin example I've linked above, the overall magic happens near the end with:

  ```
  buffer.Write(version)
  buffer.Write(inputs)
  buffer.Write(inputTransactionBytesReversed)
  buffer.Write(outputIndexBytes)
  buffer.WriteByte(byte(scriptSigLength))
  buffer.Write(scriptSig)
  buffer.Write(sequence)
  buffer.Write(numOutputs)
  buffer.Write(satoshiBytes)
  buffer.WriteByte(byte(scriptPubKeyLength))
  buffer.Write(scriptPubKey)
  buffer.Write(lockTimeField)

  ```

And I feel like there are a lot of unanswered questions on how to expand this to handle multiple inputs and multiple outputs. For the inputs, is it as simple as taking all the input transactions into little-endian, noting the correct number in `inputs`, and going from there? For outputs, do you just have an individual block of to `satoshiBytes` to `scriptPubKey`? I can't find in the documentation how this is supposed to all glue together, and at this point I feel like I need some help with the nuts and bolts of this. Thanks in advance for any help - if we can get these two points answered I might end up making more questions from the stuff that will inevitably result from this.

**Tags:** transactions, script, btcd, go-lang


---

## 1 answers

### Answer by username_1 on 2017-11-12. Score: 3

There might not be an easy answer, but the developper documentation is probably a first stop (<https://bitcoin.org/en/developer-examples#complex-raw-transaction>). Also I used regularily a block explorer (e.g. blockchain.info) to display the transaction and also its "raw" format. This allows to see the tx details in it's JSON format. Also the book from Andreas helped alot (Mastering Bitcoin). And finally there is also this forum here (the search function is good), and bitcointalk.org. Oh, then there is the wiki as well: <https://en.bitcoin.it/wiki/Protocol_documentation#tx>.

> For the inputs, is it as simple as taking all the input transactions
> into little-endian, noting the correct number in inputs, and going
> from there?

No, I don't think so. There is no such thing as simple :-)
You are on the right path, additionally when assembling a raw, unsigned transaction, and you have to deal with field lengths, and with "var\_int", which are explained in developper docs. Here is a sample tx, so you get an idea:

```
VERSION                                 01000000
TX_IN COUNT [var_int]:                  hex=03, decimal=3
 TX_IN[0]
  TX_IN[0] OutPoint hash (char[32])     94FAE0AC28792796063F23F4A4BA4F977A9599D1579C5AAE7CE6DDA4F8A6B1BB
  TX_IN[0] OutPoint index (uint32_t)    hex=14040000, reversed=00000414, decimal=1044
  TX_IN[0] Script Length (var_int)      hex=19, decimal=25
  TX_IN[0] Script Sig (uchar[])         76A914A438060482FCD835754EA4518C70CC2085AF48FA88AC
  TX_IN[0] Sequence (uint32_t)          FFFFFFFF
 TX_IN[1]
  TX_IN[1] OutPoint hash (char[32])     A3E719B12275357B15FC5DECD9088A0964FE860D49F026F2152E71F681AC3FA4
  TX_IN[1] OutPoint index (uint32_t)    hex=31040000, reversed=00000431, decimal=1073
  TX_IN[1] Script Length (var_int)      hex=19, decimal=25
  TX_IN[1] Script Sig (uchar[])         76A914A438060482FCD835754EA4518C70CC2085AF48FA88AC
  TX_IN[1] Sequence (uint32_t)          FFFFFFFF
 TX_IN[2]
  TX_IN[2] OutPoint hash (char[32])     874CD4C4E1683C43A98A9DAA0926BEA37C10616F165AC35481E8181BFD449C65
  TX_IN[2] OutPoint index (uint32_t)    hex=E0010000, reversed=000001E0, decimal=480
  TX_IN[2] Script Length (var_int)      hex=19, decimal=25
  TX_IN[2] Script Sig (uchar[])         76A914A438060482FCD835754EA4518C70CC2085AF48FA88AC
  TX_IN[2] Sequence (uint32_t)          FFFFFFFF

TX_OUT COUNT                            hex=01, decimal=1
 TX_OUT[0]
  TX_OUT[0] Value (uint64_t)            hex=8038010000000000, reversed_hex=0000000000013880, dec=80000, bitcoin=0.00080000
  TX_OUT[0] PK_Script Length (var_int)  hex=19, dec=25
  TX_OUT[0] pk_script (uchar[])         76A914C2DF275D78E506E17691FD6F0C63C43D15C897FC88AC
LOCK_TIME                               00000000

```

> For outputs, do you just have an individual block of to satoshiBytes
> to scriptPubKey?

It is similiar to the example above, just don't have one at hand. But the majority of tx have a spend-to address, and a return address, so you might look a tx up in the block explorer...
Happy coding ! :-)

> **Comments:**
> **username_0**: Did you create this particular transaction? If so, can I get an idea of how you went about doing it? I don't care if it's in golang or not, I'm just trying to get a feel for how all of these pieces glue together.  
> **username_1**: yup, manually assembled with a set of shell scripts. When you are on unixoide systems, they work with Linux, MacOS and OpenBSD. Here: github.com/username_1/trx_cl_suite  

---

