createwallet.go raw

   1  package gui
   2  
   3  import (
   4  	"fmt"
   5  	"os"
   6  	"path/filepath"
   7  	"time"
   8  
   9  	"github.com/p9c/p9/pkg/qu"
  10  	"golang.org/x/exp/shiny/materialdesign/icons"
  11  
  12  	"github.com/p9c/p9/pkg/interrupt"
  13  
  14  	"github.com/p9c/p9/pkg/gel"
  15  	"github.com/p9c/p9/cmd/wallet"
  16  	"github.com/p9c/p9/pkg/chaincfg"
  17  	"github.com/p9c/p9/pkg/constant"
  18  	"github.com/p9c/p9/pkg/fork"
  19  
  20  	l "github.com/p9c/p9/pkg/gel/gio/layout"
  21  )
  22  
  23  const slash = string(os.PathSeparator)
  24  
  25  func (wg *WalletGUI) CreateWalletPage(gtx l.Context) l.Dimensions {
  26  	walletForm := wg.createWalletFormWidgets()
  27  	le := func(gtx l.Context, index int) l.Dimensions {
  28  		return wg.Inset(0.25, walletForm[index]).Fn(gtx)
  29  	}
  30  	return func(gtx l.Context) l.Dimensions {
  31  		return wg.Fill(
  32  			"DocBg", l.Center, 0, 0,
  33  			// wg.Inset(
  34  			// 	0.5,
  35  			wg.VFlex().
  36  				Flexed(
  37  					1,
  38  					wg.lists["createWallet"].Vertical().Start().Length(len(walletForm)).ListElement(le).Fn,
  39  				).
  40  				Rigid(
  41  					wg.createConfirmExitBar(),
  42  				).Fn,
  43  			// ).Fn,
  44  		).Fn(gtx)
  45  	}(gtx)
  46  }
  47  
  48  func (wg *WalletGUI) createConfirmExitBar() l.Widget {
  49  	return wg.VFlex().
  50  		// Rigid(
  51  		// 	wg.Inset(
  52  		// ,
  53  		// 			).Fn,
  54  		// 		).
  55  		Rigid(
  56  			wg.Inset(0.5,
  57  				wg.Flex().
  58  					Rigid(
  59  						func(gtx l.Context) l.Dimensions {
  60  							return wg.Flex().
  61  								Rigid(
  62  									wg.ButtonLayout(
  63  										wg.clickables["quit"].SetClick(
  64  											func() {
  65  												interrupt.Request()
  66  											},
  67  										),
  68  									).
  69  										CornerRadius(0.5).
  70  										Corners(0).
  71  										Background("PanelBg").
  72  										Embed(
  73  											wg.Inset(
  74  												0.25,
  75  												wg.Flex().AlignMiddle().
  76  													Rigid(
  77  														wg.Icon().
  78  															Scale(
  79  																gel.Scales["H4"],
  80  															).
  81  															Color("DocText").
  82  															Src(
  83  																&icons.
  84  																	MapsDirectionsRun,
  85  															).Fn,
  86  													).
  87  													Rigid(
  88  														wg.Inset(
  89  															0.5,
  90  															gel.EmptySpace(
  91  																0,
  92  																0,
  93  															),
  94  														).Fn,
  95  													).
  96  													Rigid(
  97  														wg.H6("exit").Color("DocText").Fn,
  98  													).
  99  													Rigid(
 100  														wg.Inset(
 101  															0.5,
 102  															gel.EmptySpace(
 103  																0,
 104  																0,
 105  															),
 106  														).Fn,
 107  													).
 108  													Fn,
 109  											).Fn,
 110  										).Fn,
 111  								).
 112  								Fn(gtx)
 113  						},
 114  					).
 115  					Flexed(
 116  						1,
 117  						gel.EmptyMaxWidth(),
 118  					).
 119  					Rigid(
 120  						func(gtx l.Context) l.Dimensions {
 121  							if !wg.createWalletInputsAreValid() {
 122  								gtx = gtx.Disabled()
 123  							}
 124  							return wg.Flex().
 125  								Rigid(
 126  									wg.ButtonLayout(
 127  										wg.clickables["createWallet"].SetClick(
 128  											func() {
 129  												go wg.createWalletAction()
 130  											},
 131  										),
 132  									).
 133  										CornerRadius(0).
 134  										Corners(0).
 135  										Background("Primary").
 136  										Embed(
 137  											// wg.Fill("DocText",
 138  											wg.Inset(
 139  												0.25,
 140  												wg.Flex().AlignMiddle().
 141  													Rigid(
 142  														wg.Icon().
 143  															Scale(
 144  																gel.Scales["H4"],
 145  															).
 146  															Color("DocText").
 147  															Src(
 148  																&icons.
 149  																	ContentCreate,
 150  															).Fn,
 151  													).
 152  													Rigid(
 153  														wg.Inset(
 154  															0.5,
 155  															gel.EmptySpace(
 156  																0,
 157  																0,
 158  															),
 159  														).Fn,
 160  													).
 161  													Rigid(
 162  														wg.H6("create").Color("DocText").Fn,
 163  													).
 164  													Rigid(
 165  														wg.Inset(
 166  															0.5,
 167  															gel.EmptySpace(
 168  																0,
 169  																0,
 170  															),
 171  														).Fn,
 172  													).
 173  													Fn,
 174  											).Fn,
 175  										).Fn,
 176  								).
 177  								Fn(gtx)
 178  						},
 179  					).
 180  					Fn,
 181  			).Fn,
 182  		).
 183  		Fn
 184  }
 185  
 186  func (wg *WalletGUI) createWalletPasswordsMatch() bool {
 187  	return wg.passwords["passEditor"].GetPassword() != "" &&
 188  		wg.passwords["confirmPassEditor"].GetPassword() != "" &&
 189  		len(wg.passwords["passEditor"].GetPassword()) >= 8 &&
 190  		wg.passwords["passEditor"].GetPassword() ==
 191  			wg.passwords["confirmPassEditor"].GetPassword()
 192  }
 193  
 194  func (wg *WalletGUI) createWalletInputsAreValid() bool {
 195  	return wg.createWalletPasswordsMatch() && wg.bools["ihaveread"].GetValue() && wg.createWords == wg.createMatch
 196  }
 197  
 198  func (wg *WalletGUI) createWalletAction() {
 199  	// wg.NodeRunCommandChan <- "stop"
 200  	D.Ln("clicked submit wallet")
 201  	wg.cx.Config.WalletFile.Set(filepath.Join(wg.cx.Config.DataDir.V(), wg.cx.ActiveNet.Name, constant.DbName))
 202  	dbDir := wg.cx.Config.WalletFile.V()
 203  	loader := wallet.NewLoader(wg.cx.ActiveNet, dbDir, 250)
 204  	// seed, _ := hex.DecodeString(wg.inputs["walletSeed"].GetText())
 205  	seed := wg.createSeed
 206  	pass := wg.passwords["passEditor"].GetPassword()
 207  	wg.cx.Config.WalletPass.Set(pass)
 208  	D.Ln("password", pass)
 209  	_ = wg.cx.Config.WriteToFile(wg.cx.Config.ConfigFile.V())
 210  	w, e := loader.CreateNewWallet(
 211  		[]byte(pass),
 212  		[]byte(pass),
 213  		seed,
 214  		time.Now(),
 215  		false,
 216  		wg.cx.Config,
 217  		qu.T(),
 218  	)
 219  	D.Ln("*** created wallet")
 220  	if E.Chk(e) {
 221  		// return
 222  	}
 223  	w.Stop()
 224  	D.Ln("shutting down wallet", w.ShuttingDown())
 225  	w.WaitForShutdown()
 226  	D.Ln("starting main app")
 227  	wg.cx.Config.Generate.T()
 228  	wg.cx.Config.GenThreads.Set(1)
 229  	wg.cx.Config.NodeOff.F()
 230  	wg.cx.Config.WalletOff.F()
 231  	_ = wg.cx.Config.WriteToFile(wg.cx.Config.ConfigFile.V())
 232  	// // we are going to assume the config is not manually misedited
 233  	// if apputil.FileExists(*wg.cx.Config.ConfigFile) {
 234  	// 	b, e := ioutil.ReadFile(*wg.cx.Config.ConfigFile)
 235  	// 	if e == nil {
 236  	// 		wg.cx.Config, wg.cx.ConfigMap = pod.EmptyConfig()
 237  	// 		e = json.Unmarshal(b, wg.cx.Config)
 238  	// 		if e != nil {
 239  	// 			E.Ln("error unmarshalling config", e)
 240  	// 			// os.Exit(1)
 241  	// 			panic(e)
 242  	// 		}
 243  	// 	} else {
 244  	// 		F.Ln("unexpected error reading configuration file:", e)
 245  	// 		// os.Exit(1)
 246  	// 		// return e
 247  	// 		panic(e)
 248  	// 	}
 249  	// }
 250  	*wg.noWallet = false
 251  	// interrupt.Request()
 252  	// wg.wallet.Stop()
 253  	// wg.wallet.Start()
 254  	// wg.node.Start()
 255  	// wg.miner.Start()
 256  	wg.unlockPassword.Editor().SetText(pass)
 257  	wg.unlockWallet(pass)
 258  	interrupt.RequestRestart()
 259  }
 260  
 261  func (wg *WalletGUI) createWalletTestnetToggle(b bool) {
 262  	D.Ln("testnet on?", b)
 263  	// if the password has been entered, we need to copy it to the variable
 264  	if wg.passwords["passEditor"].GetPassword() != "" ||
 265  		wg.passwords["confirmPassEditor"].GetPassword() != "" ||
 266  		len(wg.passwords["passEditor"].GetPassword()) >= 8 ||
 267  		wg.passwords["passEditor"].GetPassword() ==
 268  			wg.passwords["confirmPassEditor"].GetPassword() {
 269  		wg.cx.Config.WalletPass.Set(wg.passwords["confirmPassEditor"].GetPassword())
 270  		D.Ln("wallet pass", wg.cx.Config.WalletPass.V())
 271  	}
 272  	if b {
 273  		wg.cx.ActiveNet = &chaincfg.TestNet3Params
 274  		fork.IsTestnet = true
 275  	} else {
 276  		wg.cx.ActiveNet = &chaincfg.MainNetParams
 277  		fork.IsTestnet = false
 278  	}
 279  	I.Ln("activenet:", wg.cx.ActiveNet.Name)
 280  	D.Ln("setting ports to match network")
 281  	wg.cx.Config.Network.Set(wg.cx.ActiveNet.Name)
 282  	wg.cx.Config.P2PListeners.Set(
 283  		[]string{"0.0.0.0:" + wg.cx.ActiveNet.DefaultPort},
 284  	)
 285  	wg.cx.Config.P2PConnect.Set([]string{"127.0.0.1:" + wg.cx.ActiveNet.
 286  		DefaultPort})
 287  	address := fmt.Sprintf(
 288  		"127.0.0.1:%s",
 289  		wg.cx.ActiveNet.RPCClientPort,
 290  	)
 291  	wg.cx.Config.RPCListeners.Set([]string{address})
 292  	wg.cx.Config.RPCConnect.Set(address)
 293  	address = fmt.Sprintf("127.0.0.1:" + wg.cx.ActiveNet.WalletRPCServerPort)
 294  	wg.cx.Config.WalletRPCListeners.Set([]string{address})
 295  	wg.cx.Config.WalletServer.Set(address)
 296  	wg.cx.Config.NodeOff.F()
 297  	_ = wg.cx.Config.WriteToFile(wg.cx.Config.ConfigFile.V())
 298  }
 299