github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/tools/txmod/txmod.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/hex"
     5  	"os"
     6  
     7  	"github.com/piotrnar/gocoin/lib/btc"
     8  )
     9  
    10  func main() {
    11  	// modify transaciotn and save it back on disk (e.g. to change a fee)
    12  	if len(os.Args) < 2 {
    13  		println("Specify filename with the tranaction to modify")
    14  		return
    15  	}
    16  	d, er := os.ReadFile(os.Args[1])
    17  	if er != nil {
    18  		println(er.Error())
    19  		return
    20  	}
    21  	tx, _ := btc.NewTx(d) // assuming binary format
    22  	if tx == nil {
    23  		println("Not a valid tx file")
    24  		return
    25  	}
    26  	println(len(tx.TxOut), tx.TxOut[1].Value)
    27  	tx.TxOut[len(tx.TxOut)-1].Value -= 3000 // decrease value of the last output
    28  
    29  	// store on tisk as hex-encoded
    30  	os.WriteFile("newtx.txt", []byte(hex.EncodeToString(tx.Serialize())), 0700)
    31  }