Exemplo n.º 1
0
func TestGetAccount(t *testing.T) {
	_, teardown, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}

	defer teardown()

	// new account
	a := account.New()
	account.Set(a)

	act, err := account.Get(a.Pubkey)
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, act, a)

	// add wallet id
	act.WltIDs[bitcoin.Type] = "bitcoin_sd19"
	act.WltIDs[skycoin.Type] = "skycoin_sd19"
	account.Set(act)

	newA, err := account.Get(act.Pubkey)
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, newA, act)
}
Exemplo n.º 2
0
func TestNewAndSet(t *testing.T) {
	dir, teardown, err := setup(t)
	// dir, _, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}
	defer teardown()

	a := account.New()
	a.WltIDs[bitcoin.Type] = "bitcoin_sd110"
	a.WltIDs[skycoin.Type] = "skycoin_sd110"
	account.Set(a)

	// get account
	d, err := ioutil.ReadFile(filepath.Join(dir, account.FileName()))
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(string(d), a.Pubkey) {
		t.Fatal("new account failed")
	}

	if !strings.Contains(string(d), a.Seckey) {
		t.Fatal("new account failed")
	}

	if !strings.Contains(string(d), "bitcoin_sd110") {
		t.Fatal("new account failed")
	}

	if !strings.Contains(string(d), "skycoin_sd110") {
		t.Fatal("new account failed")
	}
}
Exemplo n.º 3
0
// CreateWallet api for creating local wallet.
// mode: POST
// url: /api/v1/wallet?type=[:type]&seed=[:seed]
// params:
// 		type: bitcoin or skycoin
// 		seed: wallet seed.
func CreateWallet(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		rlt := &pp.EmptyRes{}
		for {
			// get coin type
			cp := r.FormValue("type")

			// get seed
			sd := r.FormValue("seed")
			if sd == "" {
				rlt = pp.MakeErrRes(errors.New("seed is required"))
				break
			}

			wlt, err := wallet.New(cp, sd)
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			// bind the wallet to current account.
			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			a.WltIDs[cp] = wlt.GetID()
			// update the account.
			account.Set(a)

			res := struct {
				Result *pp.Result `json:"result"`
				ID     string     `json:"id"`
			}{
				Result: pp.MakeResultWithCode(pp.ErrCode_Success),
				ID:     wlt.GetID(),
			}
			sendJSON(w, &res)
			return
		}
		sendJSON(w, rlt)
	}
}
Exemplo n.º 4
0
// CreateAccount handle the request of creating account.
func CreateAccount(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		// generate account pubkey/privkey pair, pubkey is the account id.
		errRlt := &pp.EmptyRes{}
		for {
			a := account.New()
			r := pp.CreateAccountReq{
				Pubkey: pp.PtrString(a.Pubkey),
			}
			res := pp.CreateAccountRes{}
			if err := sknet.EncryGet(se.GetServAddr(), "/create/account", r, &res); err != nil {
				logger.Error(err.Error())
				errRlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
				break
			}

			// acntRes := res.(*pp.CreateAccountRes)
			if !res.GetResult().GetSuccess() {
				sendJSON(w, res)
			} else {
				ret := struct {
					Result    pp.Result `json:"result"`
					Pubkey    string    `json:"pubkey"`
					CreatedAt int64     `json:"created_at"`
				}{
					Result:    *res.Result,
					Pubkey:    a.Pubkey,
					CreatedAt: res.GetCreatedAt(),
				}
				account.Set(a)
				sendJSON(w, &ret)
			}
			return
		}
		sendJSON(w, errRlt)
	}
}