示例#1
0
func TestNewWallet(t *testing.T) {
	wltDir, teardown, err := setup(t)
	assert.Nil(t, err)
	defer teardown()

	testData := []struct {
		Type coin.Type
		Seed string
		Path string
	}{
		{coin.Bitcoin, "sd123", filepath.Join(wltDir, "bitcoin_sd123.wlt")},
		{coin.Bitcoin, "sd234", filepath.Join(wltDir, "bitcoin_sd234.wlt")},
		{coin.Skycoin, "sd123", filepath.Join(wltDir, "skycoin_sd123.wlt")},
		{coin.Skycoin, "sd234", filepath.Join(wltDir, "skycoin_sd234.wlt")},
	}

	for _, d := range testData {
		if _, err := wallet.New(d.Type, d.Seed); err != nil {
			t.Errorf("create %s wallet of seed:%s failed, err:%s", d.Type, d.Seed, err)
			return
		}

		// check the existence of wallet file.
		if _, err := os.Stat(d.Path); os.IsNotExist(err) {
			t.Error("create wallet failed")
			return
		}
	}
}
示例#2
0
func TestIsExist(t *testing.T) {
	_, teardown, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}

	defer teardown()

	testData := []struct {
		Type coin.Type
		Seed string
	}{
		{coin.Bitcoin, "sd666"},
		{coin.Bitcoin, "sd667"},
		{coin.Skycoin, "sd666"},
		{coin.Skycoin, "sd667"},
	}

	for _, d := range testData {
		id := wallet.MakeWltID(d.Type, d.Seed)
		if wallet.IsExist(id) {
			t.Fatalf("wallet:%s should not exist", id)
		}

		_, err := wallet.New(d.Type, d.Seed)
		if err != nil {
			t.Fatalf("creat wallet :%s failed", id)
		}

		if !wallet.IsExist(id) {
			t.Fatalf("wallet:%s should exist", id)
		}
	}
}
示例#3
0
func TestRemove(t *testing.T) {
	wltDir, teardown, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}
	defer teardown()

	// create wallet
	testData := []struct {
		Type coin.Type
		Seed string
		ID   string
	}{
		{coin.Bitcoin, "sd777", "bitcoin_sd777"},
		{coin.Skycoin, "sd777", "skycoin_sd777"},
	}

	for _, d := range testData {
		wlt, err := wallet.New(d.Type, d.Seed)
		if err != nil {
			t.Fatal(err)
		}

		// remove this wlt.
		if err := wallet.Remove(wlt.GetID()); err != nil {
			t.Fatal(err)
		}

		// check if the wlt file is already removed.
		path := filepath.Join(wltDir, fmt.Sprintf("%s.%s", d.ID, wallet.Ext))
		if _, err := os.Stat(path); !os.IsNotExist(err) {
			t.Fatal("remove wallet failed")
		}
	}
}
示例#4
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)
	}
}
示例#5
0
func makeWallets(dir string, items []walletItem) (wallets, error) {
	f := func() {
		logger.Debug("wallet dir:%s", dir)
		wallet.InitDir(dir)
	}
	initWalletOnce.Do(f)
	wlts := wallets{ids: make(map[string]string)}
	// create wallets if not exist.
	for _, item := range items {
		id := wallet.MakeWltID(item.Type, item.Seed)
		if !wallet.IsExist(id) {
			_, err := wallet.New(item.Type, item.Seed)
			if err != nil {
				return wallets{}, err
			}
		}
		wlts.ids[item.Type] = id
	}
	return wlts, nil
}
示例#6
0
func TestNewAddresses(t *testing.T) {
	wltDir, teardown, err := setup(t)
	// wltDir, _, err := setup(t)
	assert.Nil(t, err)
	defer teardown()
	testData := []struct {
		Type    coin.Type
		Seed    string
		Num     int
		Entries []coin.AddressEntry
	}{
		{
			Type: coin.Bitcoin,
			Seed: "sd999",
			Num:  2,
			Entries: []coin.AddressEntry{
				{
					Address: "1FLZTRDS51eiMGu1MwV75VmQPags7UjysZ",
					Public:  "0378c76e20e4f93730e67bb469bc7186681a8c85023088b64c70930e78d4aff690",
					Secret:  "L4fDKYKxMSoZ3EUfKHacykA5cM8h6EXeqQ1w2TrpeQ7f81cR5EhT",
				},
				{
					Address: "1HsUndbHFjRMSXuGyxo1kzVMsQcuhpJcwE",
					Public:  "0270d2d9b6df46e1b22effee8a3dfb42f6c3fe69b4361158b6101b451f6cced51c",
					Secret:  "Kz9vEMVPXTzTEXFrP4Pmnv79UfPRr2HWgZoQt4VAWzbUauF2MrNf",
				},
			},
		},
		{
			Type: coin.Skycoin,
			Seed: "sd888",
			Num:  2,
			Entries: []coin.AddressEntry{
				{
					Address: "fYJPkCTqdChw3sPSGUgze9nuGMNtC5DvPY",
					Public:  "02ba572a03c8471822c308e5d041aba549b35676a0ef1c737b4517eef70c32377e",
					Secret:  "2f4aacc72a6d192e04ec540328689588caf4167d71904bdb870a4a2cee7f29c8",
				},
				{
					Address: "t6t7bJ9Ruxq9z44pYQT5AkEeAjGjgantU",
					Public:  "039f4b6a110a9c5c38da08a0bff133edf07472348a4dc4c9d63b178fe26807606e",
					Secret:  "b720d3c0f67f3c91e23805237f182e78121b90890f483133cc46f9d91232cf4c",
				},
			},
		},
	}

	for _, d := range testData {
		// new wallet
		wlt, err := wallet.New(d.Type, d.Seed)
		assert.Nil(t, err)

		for i := 0; i < d.Num; i++ {
			if _, err := wallet.NewAddresses(wlt.GetID(), 1); err != nil {
				t.Fatal(err)
			}
		}
		path := filepath.Join(wltDir, fmt.Sprintf("%s.%s", wlt.GetID(), wallet.Ext))
		cnt, err := ioutil.ReadFile(path)
		if err != nil {
			t.Fatal(err)
		}
		for _, e := range d.Entries {
			if !strings.Contains(string(cnt), e.Address) {
				t.Fatalf("not contains address:%s", e.Address)
			}

			if !strings.Contains(string(cnt), e.Public) {
				t.Fatalf("not cointains pubkey:%s", e.Public)
			}
			if !strings.Contains(string(cnt), e.Secret) {
				t.Fatalf("not cointains seckey:%s", e.Secret)
			}
		}
	}
}
示例#7
0
func TestGetAddresses(t *testing.T) {
	_, teardown, err := setup(t)
	assert.Nil(t, err)
	defer teardown()
	testData := []struct {
		Type    coin.Type
		Seed    string
		Num     int
		Entries []coin.AddressEntry
	}{
		{
			Type: coin.Bitcoin,
			Seed: "sd999",
			Num:  2,
			Entries: []coin.AddressEntry{
				{
					Address: "1FLZTRDS51eiMGu1MwV75VmQPags7UjysZ",
					Public:  "0378c76e20e4f93730e67bb469bc7186681a8c85023088b64c70930e78d4aff690",
					Secret:  "L4fDKYKxMSoZ3EUfKHacykA5cM8h6EXeqQ1w2TrpeQ7f81cR5EhT",
				},
				{
					Address: "1HsUndbHFjRMSXuGyxo1kzVMsQcuhpJcwE",
					Public:  "0270d2d9b6df46e1b22effee8a3dfb42f6c3fe69b4361158b6101b451f6cced51c",
					Secret:  "Kz9vEMVPXTzTEXFrP4Pmnv79UfPRr2HWgZoQt4VAWzbUauF2MrNf",
				},
			},
		},
		{
			Type: coin.Skycoin,
			Seed: "sd888",
			Num:  2,
			Entries: []coin.AddressEntry{
				{
					Address: "fYJPkCTqdChw3sPSGUgze9nuGMNtC5DvPY",
					Public:  "02ba572a03c8471822c308e5d041aba549b35676a0ef1c737b4517eef70c32377e",
					Secret:  "2f4aacc72a6d192e04ec540328689588caf4167d71904bdb870a4a2cee7f29c8",
				},
				{
					Address: "t6t7bJ9Ruxq9z44pYQT5AkEeAjGjgantU",
					Public:  "039f4b6a110a9c5c38da08a0bff133edf07472348a4dc4c9d63b178fe26807606e",
					Secret:  "b720d3c0f67f3c91e23805237f182e78121b90890f483133cc46f9d91232cf4c",
				},
			},
		},
	}

	for _, d := range testData {
		// new wallet
		wlt, err := wallet.New(d.Type, d.Seed)
		if err != nil {
			t.Fatal(err)
		}

		if _, err := wallet.NewAddresses(wlt.GetID(), d.Num); err != nil {
			t.Fatal(err)
		}

		addrs, err := wallet.GetAddresses(wlt.GetID())
		if err != nil {
			t.Fatal(err)
		}

		for _, e := range d.Entries {
			find := func(addr string) bool {
				for _, a := range addrs {
					if a == addr {
						return true
					}
				}
				return false
			}
			if !find(e.Address) {
				t.Fatal("GetAddresses failed")
			}
		}
	}
}