regenerate.go raw

   1  package gui
   2  
   3  import (
   4  	"image"
   5  	"path/filepath"
   6  	"strconv"
   7  	"time"
   8  
   9  	"github.com/p9c/p9/pkg/amt"
  10  	"github.com/p9c/p9/pkg/btcaddr"
  11  
  12  	"github.com/atotto/clipboard"
  13  
  14  	"github.com/p9c/p9/pkg/gel/gio/op/paint"
  15  
  16  	"github.com/p9c/p9/pkg/qrcode"
  17  )
  18  
  19  func (wg *WalletGUI) GetNewReceivingAddress() {
  20  	D.Ln("GetNewReceivingAddress")
  21  	var addr btcaddr.Address
  22  	var e error
  23  	if addr, e = wg.WalletClient.GetNewAddress("default"); !E.Chk(e) {
  24  		D.Ln(
  25  			"getting new receiving address", addr.EncodeAddress(),
  26  			"previous:", wg.State.currentReceivingAddress.String.Load(),
  27  		)
  28  		// save to addressbook
  29  		var ae AddressEntry
  30  		ae.Address = addr.EncodeAddress()
  31  		var amount float64
  32  		if amount, e = strconv.ParseFloat(
  33  			wg.inputs["receiveAmount"].GetText(),
  34  			64,
  35  		); !E.Chk(e) {
  36  			if ae.Amount, e = amt.NewAmount(amount); E.Chk(e) {
  37  			}
  38  		}
  39  		msg := wg.inputs["receiveMessage"].GetText()
  40  		if len(msg) > 64 {
  41  			msg = msg[:64]
  42  		}
  43  		ae.Message = msg
  44  		ae.Created = time.Now()
  45  		if wg.State.IsReceivingAddress() {
  46  			wg.State.receiveAddresses = append(wg.State.receiveAddresses, ae)
  47  		} else {
  48  			wg.State.receiveAddresses = []AddressEntry{ae}
  49  			wg.State.isAddress.Store(true)
  50  		}
  51  		D.S(wg.State.receiveAddresses)
  52  		wg.State.SetReceivingAddress(addr)
  53  		filename := filepath.Join(wg.cx.Config.DataDir.V(), "state.json")
  54  		if e = wg.State.Save(filename, wg.cx.Config.WalletPass.Bytes(), false); E.Chk(e) {
  55  		}
  56  		wg.Invalidate()
  57  	}
  58  }
  59  
  60  func (wg *WalletGUI) GetNewReceivingQRCode(qrText string) {
  61  	wg.currentReceiveRegenerate.Store(false)
  62  	var qrc image.Image
  63  	D.Ln("generating QR code")
  64  	var e error
  65  	if qrc, e = qrcode.Encode(qrText, 0, qrcode.ECLevelL, 4); !E.Chk(e) {
  66  		iop := paint.NewImageOp(qrc)
  67  		wg.currentReceiveQRCode = &iop
  68  		wg.currentReceiveQR = wg.ButtonLayout(
  69  			wg.currentReceiveCopyClickable.SetClick(
  70  				func() {
  71  					D.Ln("clicked qr code copy clicker")
  72  					if e := clipboard.WriteAll(qrText); E.Chk(e) {
  73  					}
  74  				},
  75  			),
  76  		).
  77  			Background("white").
  78  			Embed(
  79  				wg.Inset(
  80  					0.125,
  81  					wg.Image().Src(*wg.currentReceiveQRCode).Scale(1).Fn,
  82  				).Fn,
  83  			).Fn
  84  	}
  85  }
  86