func getPrivKey(cp string) coin.GetPrivKey { return func(addr string) (string, error) { a, err := account.GetActive() if err != nil { return "", err } wltID := a.WltIDs[cp] if wltID == "" { return "", fmt.Errorf("does not have %s wallet", cp) } _, key, err := wallet.GetKeypair(wltID, addr) if err != nil { return "", err } return key, nil } }
// GetKeys get keys of specific address in wallet. // mode: GET // url: /api/v1/wallet/address/keys?id=[:id]&address=[:address] func GetKeys(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("no id")) break } // get address addr := r.FormValue("address") if addr == "" { rlt = pp.MakeErrRes(errors.New("no address")) break } p, s, err := wallet.GetKeypair(wltID, addr) if err != nil { logger.Error(err.Error()) rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError) break } res := struct { Result *pp.Result Pubkey string `json:"pubkey"` Seckey string `json:"seckey"` }{ Result: pp.MakeResultWithCode(pp.ErrCode_Success), Pubkey: p, Seckey: s, } sendJSON(w, &res) return } sendJSON(w, rlt) } }
// GetKeypair get pub/sec keys of specific address. func (wlts wallets) GetKeypair(cp string, addr string) (string, string, error) { if id, ok := wlts.ids[cp]; ok { return wallet.GetKeypair(id, addr) } return "", "", fmt.Errorf("%s wallet not supported", cp) }
func TestGetKeypair(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) } for _, e := range d.Entries { p, s, err := wallet.GetKeypair(wlt.GetID(), e.Address) if err != nil { t.Fatal(err) } if p != e.Public || s != e.Secret { t.Fatal("get key pair failed") } } } }