<p>here's how to create an unsigned raw transaction in typescript with one input and one output.</p>

<pre><code>import * as bitcoin from 'bitcoinjs-lib'

const reverseHexBytes = (hex: string) =&gt; {
  const buff = Buffer.from(hex, 'hex')
  return buff.reverse().toString('hex')
}

const fixedPoint = (num: number): number =&gt; {
  return parseFloat(num.toFixed(8))
}

const outputAmount = 100
const inputTxHash = 'tx-hash-goes-here'
const index = 5
const fee = 0.001
const numOutputs = 1
const outputAddress = 'address-to-send-to-goes-here'

// input
const txInputs = '01'
const txHash = reverseHexBytes(inputTxHash)
const outputIndex = '0' + txIndex.toString() + '000000'
const script = ''
const sequence = 'ffffffff'
let tx = txVersion + txInputs + txHash + outputIndex + script + sequence
// output
const countHex = numOutputs.toString(16)
const countByte = (countHex.length === 1) ? '0' + countHex : countHex
tx += countByte
const amount = fixedPoint(outputAmount - fee)
const outputAmount = Math.floor(amount * (10**8))
let outputAmountHex = outputAmount.toString(16)
while (outputAmountHex.length &lt; 16) {
  outputAmountHex = '0' + outputAmountHex
}
const outputAmountBytes = reverseHexBytes(outputAmountHex)
tx += outputAmountBytes
const pubkey = bitcoin.address.fromBase58Check(outputAddress).hash.toString('hex')
tx += '1976a914' + pubkey + '88ac'
// lock time
tx += '00000000'
</code></pre>

<p>check out the protocol documentation for more specific information: <a href="https://en.bitcoin.it/wiki/Protocol_documentation#tx" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_documentation#tx</a></p>

<p>note: i copied and edited from something i've implemented previously, so if there's a mistake just let me know and i'll update it.</p>

CC BY-SA 4.0
2020-03-17T03:31:05.427Z
Thank u very much for this answer，but I would like to implement it in bitcoinj instead of bitcoinjs.
user103341
CC BY-SA 4.0
2020-03-17T05:25:07.903Z
there's only one line that uses bitcoinjs, everything else is vanilla code. you should be able to switch that out fairly easily, or look up how to decode a base58 encoded address to hex. the transaction composition part will be the same.
j4ys0n
CC BY-SA 4.0
2020-09-06T19:58:22.227Z
@j4ys0n Java and Javascript are completely different programming language, you can't just "switch" parts the code
Igor Artamonov
CC BY-SA 4.0
2020-09-07T21:18:16.143Z
@IgorArtamonov i realize that, which is why i clarified the language used. i assumed outlining the steps according to the bitcoin protocol documentation would be at least somewhat helpful.
j4ys0n
CC BY-SA 4.0
2020-09-07T22:59:09.383Z
I mean it's really different languages, so it only adds more confusion. With this example, to translate it you'd need to know TypeScript, JavaScript, bitcoinjs-lib and and how the latter use can be translated to BitcoinJ calls (which has almost no intersection in class names and methods). I would suggest to rewrite it to English, it will be easier to understand and apply to Java + BitcoinJ
Igor Artamonov
CC BY-SA 4.0
2020-03-17T02:05:23.130Z
2020-03-17T02:05:23.130Z
j4ys0n
<p>I want to create a raw transaction’s input and output, and sign it then broadcast to testnet. Now I know how to create the output, but I want to refer the transaction that I can input the UTXO hash and the unspent index, then add the input to this raw transaction.</p>

<blockchain><bitcoinj>
How can I create the input of raw transaction through the previous UTXO hash and the unspent index in bitcoinj？
user103341
# How can I create the input of raw transaction through the previous UTXO hash and the unspent index in bitcoinj？

(Asked by: username_0 on 2020-03-16)

I want to create a raw transaction’s input and output, and sign it then broadcast to testnet. Now I know how to create the output, but I want to refer the transaction that I can input the UTXO hash and the unspent index, then add the input to this raw transaction.

**Tags:** blockchain, bitcoinj


---

## 1 answers

### Answer by username_1 on 2020-03-17. Score: -2

here's how to create an unsigned raw transaction in typescript with one input and one output.
import * as bitcoin from 'bitcoinjs-lib'

const reverseHexBytes = (hex: string) => {
  const buff = Buffer.from(hex, 'hex')
  return buff.reverse().toString('hex')
}

const fixedPoint = (num: number): number => {
  return parseFloat(num.toFixed(8))
}

const outputAmount = 100
const inputTxHash = 'tx-hash-goes-here'
const index = 5
const fee = 0.001
const numOutputs = 1
const outputAddress = 'address-to-send-to-goes-here'

// input
const txInputs = '01'
const txHash = reverseHexBytes(inputTxHash)
const outputIndex = '0' + txIndex.toString() + '000000'
const script = ''
const sequence = 'ffffffff'
let tx = txVersion + txInputs + txHash + outputIndex + script + sequence
// output
const countHex = numOutputs.toString(16)
const countByte = (countHex.length === 1) ? '0' + countHex : countHex
tx += countByte
const amount = fixedPoint(outputAmount - fee)
const outputAmount = Math.floor(amount * (10**8))
let outputAmountHex = outputAmount.toString(16)
while (outputAmountHex.length < 16) {
  outputAmountHex = '0' + outputAmountHex
}
const outputAmountBytes = reverseHexBytes(outputAmountHex)
tx += outputAmountBytes
const pubkey = bitcoin.address.fromBase58Check(outputAddress).hash.toString('hex')
tx += '1976a914' + pubkey + '88ac'
// lock time
tx += '00000000'

check out the protocol documentation for more specific information: https://en.bitcoin.it/wiki/Protocol_documentation#tx
note: i copied and edited from something i've implemented previously, so if there's a mistake just let me know and i'll update it.

> **Comments:**
> **username_0**: Thank u very much for this answer，but I would like to implement it in bitcoinj instead of bitcoinjs.  
> **username_1**: there's only one line that uses bitcoinjs, everything else is vanilla code. you should be able to switch that out fairly easily, or look up how to decode a base58 encoded address to hex. the transaction composition part will be the same.  
> **username_2**: @username_1 Java and Javascript are completely different programming language, you can't just "switch" parts the code  
> **username_1**: @IgorArtamonov i realize that, which is why i clarified the language used. i assumed outlining the steps according to the bitcoin protocol documentation would be at least somewhat helpful.  
> **username_2**: I mean it's really different languages, so it only adds more confusion. With this example, to translate it you'd need to know TypeScript, JavaScript, bitcoinjs-lib and and how the latter use can be translated to BitcoinJ calls (which has almost no intersection in class names and methods). I would suggest to rewrite it to English, it will be easier to understand and apply to Java + BitcoinJ  

---

