0

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.

-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.

| improve this answer | |
  • Thank u very much for this answer,but I would like to implement it in bitcoinj instead of bitcoinjs. – user103341 Mar 17 at 3:31
  • 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 Mar 17 at 5:25
  • 1
    @j4ys0n Java and Javascript are completely different programming language, you can't just "switch" parts the code – Igor Artamonov Sep 6 at 19:58
  • @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 Sep 7 at 21:18
  • 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 Sep 7 at 22:59

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.