Ejemplo n.º 1
0
// Create a wallet Name is set by creation date
func walletCreate(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		logger.Info("API request made to create a wallet")
		seed := r.FormValue("seed")
		label := r.FormValue("label")
		wltName := wallet.NewWalletFilename()
		var wlt wallet.Wallet
		var err error
		// the wallet name may dup, rename it till no conflict.
		for {
			wlt, err = Wg.CreateWallet(wltName, wallet.OptSeed(seed), wallet.OptLabel(label))
			if err != nil && strings.Contains(err.Error(), "renaming") {
				wltName = wallet.NewWalletFilename()
				continue
			}
			break
		}

		if err := Wg.SaveWallet(wlt.GetID()); err != nil {
			wh.Error400(w, err.Error())
			return
		}

		rlt := wallet.NewReadableWallet(wlt)
		wh.SendOr500(w, rlt)
	}
}
Ejemplo n.º 2
0
// NewWalletRPC new wallet rpc
func NewWalletRPC(walletDir string, options ...wallet.Option) *WalletRPC {
	rpc := &WalletRPC{}
	if err := os.MkdirAll(walletDir, os.FileMode(0700)); err != nil {
		log.Panicf("Failed to create wallet directory %s: %v", walletDir, err)
	}

	rpc.WalletDirectory = walletDir
	for i := range options {
		rpc.Options = append(rpc.Options, options[i])
	}

	w, err := wallet.LoadWallets(rpc.WalletDirectory)
	if err != nil {
		log.Panicf("Failed to load all wallets: %v", err)
	}
	rpc.Wallets = w

	if len(rpc.Wallets) == 0 {
		wltName := wallet.NewWalletFilename()
		rpc.CreateWallet(wltName)

		if err := rpc.SaveWallet(wltName); err != nil {
			log.Panicf("Failed to save wallets to %s: %v", rpc.WalletDirectory, err)
		}

		// newWlt := wallet.NewWallet("", wltName, wltName) //deterministic
		// newWlt.GenerateAddresses(1)
		// rpc.Wallets.Add(newWlt)
		// errs := rpc.Wallets.Save(rpc.WalletDirectory)
		// if len(errs) != 0 {
		// 	log.Panicf("Failed to save wallets to %s: %v", rpc.WalletDirectory, errs)
		// }
	}

	return rpc
}