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

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"github.com/piotrnar/gocoin/lib/btc"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  // oneSendTo is resolved while parsing "-send" parameter.
    11  type oneSendTo struct {
    12  	addr   *btc.BtcAddr
    13  	amount uint64
    14  }
    15  
    16  var (
    17  	// set in parse_spend():
    18  	spendBtc, feeBtc, changeBtc uint64
    19  	sendTo                      []oneSendTo
    20  )
    21  
    22  // parse_spend parses the "-send ..." parameter.
    23  func parse_spend() {
    24  	outs := strings.Split(*send, ",")
    25  
    26  	for i := range outs {
    27  		tmp := strings.Split(strings.Trim(outs[i], " "), "=")
    28  		if len(tmp) != 2 {
    29  			println("The outputs must be in a format address1=amount1[,addressN=amountN]")
    30  			cleanExit(1)
    31  		}
    32  
    33  		a, e := btc.NewAddrFromString(tmp[0])
    34  		if e != nil {
    35  			println("NewAddrFromString:", e.Error())
    36  			cleanExit(1)
    37  		}
    38  		assert_address_version(a)
    39  
    40  		am, er := btc.StringToSatoshis(tmp[1])
    41  		if er != nil {
    42  			println("Incorrect amount: ", tmp[1], er.Error())
    43  			cleanExit(1)
    44  		}
    45  		if *subfee && i == 0 {
    46  			am -= curFee
    47  		}
    48  
    49  		sendTo = append(sendTo, oneSendTo{addr: a, amount: am})
    50  		spendBtc += am
    51  	}
    52  }
    53  
    54  // parse_batch parses the "-batch ..." parameter.
    55  func parse_batch() {
    56  	f, e := os.Open(*batch)
    57  	if e == nil {
    58  		defer f.Close()
    59  		td := bufio.NewReader(f)
    60  		var lcnt int
    61  		for {
    62  			li, _, _ := td.ReadLine()
    63  			if li == nil {
    64  				break
    65  			}
    66  			lcnt++
    67  			tmp := strings.SplitN(strings.Trim(string(li), " "), "=", 2)
    68  			if len(tmp) < 2 {
    69  				println("Error in the batch file line", lcnt)
    70  				cleanExit(1)
    71  			}
    72  			if tmp[0][0] == '#' {
    73  				continue // Just a comment-line
    74  			}
    75  
    76  			a, e := btc.NewAddrFromString(tmp[0])
    77  			if e != nil {
    78  				println("NewAddrFromString:", e.Error())
    79  				cleanExit(1)
    80  			}
    81  			assert_address_version(a)
    82  
    83  			am, e := btc.StringToSatoshis(tmp[1])
    84  			if e != nil {
    85  				println("StringToSatoshis:", e.Error())
    86  				cleanExit(1)
    87  			}
    88  
    89  			sendTo = append(sendTo, oneSendTo{addr: a, amount: am})
    90  			spendBtc += am
    91  		}
    92  	} else {
    93  		println(e.Error())
    94  		cleanExit(1)
    95  	}
    96  }
    97  
    98  // send_request returns true if spend operation has been requested.
    99  func send_request() bool {
   100  	feeBtc = curFee
   101  	if *send != "" {
   102  		parse_spend()
   103  	}
   104  	if *batch != "" {
   105  		parse_batch()
   106  	}
   107  	return len(sendTo) > 0
   108  }