示例#1
0
// NewAddress create address in wallet.
// mode: POST
// url: /api/v1/wallet/:id/address?&id=[:id]
// params:
// 		id: wallet id.
func NewAddress(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		var rlt *pp.EmptyRes
		for {
			// get wallet id
			wltID := r.FormValue("id")
			if wltID == "" {
				rlt = pp.MakeErrRes(errors.New("id is required"))
				break
			}

			addrEntries, err := wallet.NewAddresses(wltID, 1)
			if err != nil {
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}
			res := struct {
				Result  *pp.Result
				Address string `json:"address"`
			}{
				Result:  pp.MakeResultWithCode(pp.ErrCode_Success),
				Address: addrEntries[0].Address,
			}
			sendJSON(w, &res)
			return
		}
		sendJSON(w, rlt)
	}
}
示例#2
0
// NewAddresses create specific coin addresses.
func (wlts *wallets) NewAddresses(cp string, num int) ([]coin.AddressEntry, error) {
	if id, ok := wlts.ids[cp]; ok {
		return wallet.NewAddresses(id, num)
	}
	return []coin.AddressEntry{}, fmt.Errorf("%s wallet not supported", cp)
}
示例#3
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)
			}
		}
	}
}
示例#4
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")
			}
		}
	}
}