Example #1
0
func setup(t *testing.T) (string, func(), error) {
	actName := fmt.Sprintf(".account%d", rand.Int31n(100))
	teardown := func() {}
	tmpDir := filepath.Join(os.TempDir(), actName)
	if err := os.MkdirAll(tmpDir, 0777); err != nil {
		return "", teardown, err
	}

	teardown = func() {
		if err := os.RemoveAll(tmpDir); err != nil {
			panic(err)
		}
	}
	account.InitDir(tmpDir)
	return tmpDir, teardown, nil
}
Example #2
0
func TestInitDir(t *testing.T) {
	// dir, teardown, err := setup(t)
	dir, _, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Println(dir)
	// defer teardown()
	// check the exitence of dir.
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		t.Fatal("account init dir failed")
	}

	// store some account.
	p, s := cipher.GenerateKeyPair()
	acnt := account.Account{
		Pubkey: p.Hex(),
		Seckey: s.Hex(),
	}

	v := fmt.Sprintf(`{"active_account":"%s","accounts":[{"pubkey":"%s", "seckey":"%s"}]}`, p.Hex(), p.Hex(), s.Hex())

	if err := ioutil.WriteFile(filepath.Join(dir, "data.act"), []byte(v), 0777); err != nil {
		t.Fatal(err)
	}

	// init dir again.
	account.InitDir(dir)

	// check if the account is loaded.
	a, err := account.Get(p.Hex())
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, acnt.Pubkey, a.Pubkey)
	assert.Equal(t, acnt.Seckey, a.Seckey)

	activeAccount, err := account.GetActive()
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, activeAccount.Pubkey, p.Hex())
}
Example #3
0
// Run start the client service.
func (se *Service) Run() {
	// init wallet dir
	wallet.InitDir(se.cfg.WalletDir)

	// init account dir
	account.InitDir(se.cfg.AccountDir)

	// register coins
	r := router.New(se)
	addr := fmt.Sprintf("localhost:%d", se.cfg.Port)
	if err := gui.LaunchWebInterface(addr, se.cfg.GuiDir, r); err != nil {
		panic(err)
	}

	go func() {
		// Wait a moment just to make sure the http service is up
		time.Sleep(time.Millisecond * 100)
		fulladdress := fmt.Sprintf("http://%s", addr)
		logger.Info("Launching System Browser with %s", fulladdress)
		if err := util.OpenBrowser(fulladdress); err != nil {
			logger.Error(err.Error())
		}
	}()
}