func BroadcastNewAddressTx(bitcoind *bitcoindwrapper.Bitcoind, lnClient *lndwrapper.Lnd, address [4]byte) (string, error) {

	//TxID for the input transaction we are using
	var unsOutTxID string
	//Amount of coins in the unspent output
	var unsOutAmmount float64
	//Amount of coins in the unspent output (satoshis)
	var unsOutAmmountSats int64
	//Vout of the unspent output
	var unsOutVout uint32
	//Address of the unspent output
	var unsOutAddress string

	opRetOutAmount := int64(registerOpRetOutAmount)
	txFeeAmount := int64(registerTxFeeAmount)
	var changeAmount int64
	var registerTxHash string

	// Get the current block count.
	blockCount, err := bitcoind.GetBlockCount()
	if err != nil {
		return "", err
	}
	log.Printf("Block count: %d", blockCount)

	// Get the list of unspent transaction outputs (utxos) and choose one with some coin
	unsOuts, err := bitcoind.ListUnspent()
	if err != nil {
		log.Fatal(err)
		return "", err
	}
	log.Printf("Num unspent outputs (utxos): %d", len(unsOuts))

	//Find an unspent output with enough BTC to create a new transaction
	if len(unsOuts) > 0 {
		// Iterate through all the unspent outputs
		for n, unsOut := range unsOuts {

			unsOutTxID = unsOut.TxID
			unsOutVout = unsOut.Vout
			unsOutAmmount = unsOut.Amount
			unsOutAmmountSats = int64(unsOutAmmount * math.Pow(10, 8))
			unsOutAddress = unsOut.Address
			fmt.Printf("Found an UTXO with %f unspent BTC. TXHash is: %s\n", unsOutAmmount, unsOutTxID)

			// Found what we were looking for and can now calculate the change amount
			if unsOutAmmountSats > opRetOutAmount+txFeeAmount {
				changeAmount = unsOutAmmountSats - (opRetOutAmount + txFeeAmount)
				break
			}

			//If we get to the last unspent output witout finding one output
			if n == len(unsOuts) {
				return "", errors.New("No outputs with enough BTC to create registration tx")
			}
		}

		//Create the transaction we will broadcast
		tx := wire.NewMsgTx(wire.TxVersion)

		//Create the first output (OP_RETURN)
		opReturnScript, err := hex.DecodeString(opReturnHex + pushDataHex + protocolIDHex + versionHex)
		if err != nil {
			return "", err
		}
		opReturnScript = append(opReturnScript, address[:]...)
		signedAddress := SignMessage(lnClient, address[:])
		opReturnScript = append(opReturnScript, signedAddress...)

		txOutputReturn := wire.NewTxOut(opRetOutAmount, opReturnScript)
		tx.AddTxOut(txOutputReturn)

		//Create the second output with the change back to out address
		p2pkhAddress, err := btcutil.DecodeAddress(unsOutAddress, &(chaincfg.TestNet3Params))
		if err != nil {
			return "", err
		}
		p2pkhScript, err := txscript.PayToAddrScript(p2pkhAddress)
		if err != nil {
			return "", err
		}
		txOutputP2pkh := wire.NewTxOut(changeAmount, p2pkhScript)
		tx.AddTxOut(txOutputP2pkh)

		//Create the input and add it to the transaction
		txHash, err := chainhash.NewHashFromStr(unsOutTxID)
		if err != nil {
			return "", err
		}
		prevOut := wire.NewOutPoint(txHash, unsOutVout)
		txIn := wire.NewTxIn(prevOut, []byte{txscript.OP_0, txscript.OP_0}, nil)
		tx.AddTxIn(txIn)

		var buffer = new(bytes.Buffer)
		err = tx.Serialize(buffer)
		if err != nil {
			return "", err
		}
		serializedTx := hex.EncodeToString(buffer.Bytes())

		//Sign the Transaction
		signedTx, err := bitcoind.SignRawTransactionWithWallet(serializedTx)
		if err != nil {
			return "", err
		}

		//Send the transaction
		registerTxHash, err = bitcoind.SendRawTransaction(signedTx.Hex)
		if err != nil {
			return "", err
		}
	} else {
		return "", errors.New("No spendable outputs to create registration tx")
	}

	return registerTxHash, nil
}
//BroadcastNewAddressTx broadcasts a new address regstration transaction into the blockchain
//Note: Requires bitcoin wallet to be unlocked
BroadcastNewAddressTx broadcasts a new address regstration transaction into the blockchain
Note: Requires bitcoin wallet to be unlocked
// BroadcastNewAddressTx broadcasts a new address regstration transaction into the blockchain // Note : Requires bitcoin wallet to be unlocked
func BroadcastNewAddressTx(bitcoind *bitcoindwrapper.Bitcoind, lnClient *lndwrapper.Lnd, address [4]byte) (string, error) {
	var unsOutTxID string
	var unsOutAmmount float64
	 (satoshis)
	var unsOutAmmountSats int64
	var unsOutVout uint32
	var unsOutAddress string
	opRetOutAmount := int64(registerOpRetOutAmount)
	txFeeAmount := int64(registerTxFeeAmount)
	var changeAmount int64
	var registerTxHash string
	blockCount, err := bitcoind.GetBlockCount()
	if err != nil {
		return "", err
	}
	log.Printf("Block count: %d", blockCount)
	unsOuts, err := bitcoind.ListUnspent()
	if err != nil {
		log.Fatal(err)
		return "", err
	}
	log.Printf("Num unspent outputs (utxos): %d", len(unsOuts))
	if len(unsOuts) > 0 {
		for n, unsOut := range unsOuts {
			unsOutTxID = unsOut.TxID
			unsOutVout = unsOut.Vout
			unsOutAmmount = unsOut.Amount
			unsOutAmmountSats = int64(unsOutAmmount * math.Pow(10, 8))
			unsOutAddress = unsOut.Address
			fmt.Printf("Found an UTXO with %f unspent BTC. TXHash is: %s\n", unsOutAmmount, unsOutTxID)
			if unsOutAmmountSats > opRetOutAmount+txFeeAmount {
				changeAmount = unsOutAmmountSats - (opRetOutAmount + txFeeAmount)
				break
			}
			if n == len(unsOuts) {
				return "", errors.New("No outputs with enough BTC to create registration tx")
			}
		}
		tx := wire.NewMsgTx(wire.TxVersion)
		opReturnScript, err := hex.DecodeString(opReturnHex + pushDataHex + protocolIDHex + versionHex)
		if err != nil {
			return "", err
		}
		opReturnScript = append(opReturnScript, address[:]...)
		signedAddress := SignMessage(lnClient, address[:])
		opReturnScript = append(opReturnScript, signedAddress...)
		txOutputReturn := wire.NewTxOut(opRetOutAmount, opReturnScript)
		tx.AddTxOut(txOutputReturn)
		p2pkhAddress, err := btcutil.DecodeAddress(unsOutAddress, &(chaincfg.TestNet3Params))
		if err != nil {
			return "", err
		}
		p2pkhScript, err := txscript.PayToAddrScript(p2pkhAddress)
		if err != nil {
			return "", err
		}
		txOutputP2pkh := wire.NewTxOut(changeAmount, p2pkhScript)
		tx.AddTxOut(txOutputP2pkh)
		txHash, err := chainhash.NewHashFromStr(unsOutTxID)
		if err != nil {
			return "", err
		}
		prevOut := wire.NewOutPoint(txHash, unsOutVout)
		txIn := wire.NewTxIn(prevOut, []byte{txscript.OP_0, txscript.OP_0}, nil)
		tx.AddTxIn(txIn)
		var buffer = new(bytes.Buffer)
		err = tx.Serialize(buffer)
		if err != nil {
			return "", err
		}
		serializedTx := hex.EncodeToString(buffer.Bytes())
		signedTx, err := bitcoind.SignRawTransactionWithWallet(serializedTx)
		if err != nil {
			return "", err
		}
		registerTxHash, err = bitcoind.SendRawTransaction(signedTx.Hex)
		if err != nil {
			return "", err
		}
	} else {
		return "", errors.New("No spendable outputs to create registration tx")
	}
	return registerTxHash, nil
}
func BroadcastNewAddressTx ( bitcoind * bitcoindwrapper . Bitcoind , lnClient * lndwrapper . Lnd , address [ 4 ] byte ) ( string , error ) { var unsOutTxID string var unsOutAmmount float64 var unsOutAmmountSats int64 var unsOutVout uint32 var unsOutAddress string opRetOutAmount := int64 ( registerOpRetOutAmount ) txFeeAmount := int64 ( registerTxFeeAmount ) var changeAmount int64 var registerTxHash string blockCount , err := bitcoind . GetBlockCount ( ) if err != nil { return " " , err } log . Printf ( " " , blockCount ) unsOuts , err := bitcoind . ListUnspent ( ) if err != nil { log . Fatal ( err ) return " " , err } log . Printf ( " " , len ( unsOuts ) ) if len ( unsOuts ) > 0 { for n , unsOut := range unsOuts { unsOutTxID = unsOut . TxID unsOutVout = unsOut . Vout unsOutAmmount = unsOut . Amount unsOutAmmountSats = int64 ( unsOutAmmount * math . Pow ( 10 , 8 ) ) unsOutAddress = unsOut . Address fmt . Printf ( " \n " , unsOutAmmount , unsOutTxID ) if unsOutAmmountSats > opRetOutAmount + txFeeAmount { changeAmount = unsOutAmmountSats - ( opRetOutAmount + txFeeAmount ) break } if n == len ( unsOuts ) { return " " , errors . New ( " " ) } } tx := wire . NewMsgTx ( wire . TxVersion ) opReturnScript , err := hex . DecodeString ( opReturnHex + pushDataHex + protocolIDHex + versionHex ) if err != nil { return " " , err } opReturnScript = append ( opReturnScript , address [ : ] ... ) signedAddress := SignMessage ( lnClient , address [ : ] ) opReturnScript = append ( opReturnScript , signedAddress ... ) txOutputReturn := wire . NewTxOut ( opRetOutAmount , opReturnScript ) tx . AddTxOut ( txOutputReturn ) p2pkhAddress , err := btcutil . DecodeAddress ( unsOutAddress , & ( chaincfg . TestNet3Params ) ) if err != nil { return " " , err } p2pkhScript , err := txscript . PayToAddrScript ( p2pkhAddress ) if err != nil { return " " , err } txOutputP2pkh := wire . NewTxOut ( changeAmount , p2pkhScript ) tx . AddTxOut ( txOutputP2pkh ) txHash , err := chainhash . NewHashFromStr ( unsOutTxID ) if err != nil { return " " , err } prevOut := wire . NewOutPoint ( txHash , unsOutVout ) txIn := wire . NewTxIn ( prevOut , [ ] byte { txscript . OP_0 , txscript . OP_0 } , nil ) tx . AddTxIn ( txIn ) var buffer = new ( bytes . Buffer ) err = tx . Serialize ( buffer ) if err != nil { return " " , err } serializedTx := hex . EncodeToString ( buffer . Bytes ( ) ) signedTx , err := bitcoind . SignRawTransactionWithWallet ( serializedTx ) if err != nil { return " " , err } registerTxHash , err = bitcoind . SendRawTransaction ( signedTx . Hex ) if err != nil { return " " , err } } else { return " " , errors . New ( " " ) } return registerTxHash , nil }
BroadcastNewAddressTx broadcasts a new address regstration transaction into the blockchain
Note: Requires bitcoin wallet to be unlocked
BroadcastNewAddressTx broadcasts a new address regstration transaction into the blockchain Note : Requires bitcoin wallet to be unlocked
//TxID for the input transaction we are using //Amount of coins in the unspent output //Amount of coins in the unspent output (satoshis) //Vout of the unspent output //Address of the unspent output // Get the current block count. // Get the list of unspent transaction outputs (utxos) and choose one with some coin //Find an unspent output with enough BTC to create a new transaction // Iterate through all the unspent outputs // Found what we were looking for and can now calculate the change amount //If we get to the last unspent output witout finding one output //Create the transaction we will broadcast //Create the first output (OP_RETURN) //Create the second output with the change back to out address //Create the input and add it to the transaction //Sign the Transaction //Send the transaction