move things to _attic
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
## basecoin-server
|
||||
|
||||
### Proxy server
|
||||
This package exposes access to key management i.e
|
||||
- creating
|
||||
- listing
|
||||
- updating
|
||||
- deleting
|
||||
|
||||
The HTTP handlers can be embedded in a larger server that
|
||||
does things like signing transactions and posting them to a
|
||||
Tendermint chain (which requires domain-knowledge of the transaction
|
||||
types and is out of scope of this generic app).
|
||||
|
||||
### Key Management
|
||||
We expose a couple of methods for safely managing your keychain.
|
||||
If you are embedding this in a larger server, you will typically
|
||||
want to mount all these paths /keys.
|
||||
|
||||
HTTP Method | Route | Description
|
||||
---|---|---
|
||||
POST|/|Requires a name and passphrase to create a brand new key
|
||||
GET|/|Retrieves the list of all available key names, along with their public key and address
|
||||
GET|/{name} | Updates the passphrase for the given key. It requires you to correctly provide the current passphrase, as well as a new one.
|
||||
DELETE|/{name} | Permanently delete this private key. It requires you to correctly provide the current passphrase.
|
||||
@@ -1,192 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
keys "github.com/tendermint/go-crypto/keys"
|
||||
"github.com/tendermint/tmlibs/common"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk"
|
||||
keycmd "github.com/cosmos/cosmos-sdk/client/commands/keys"
|
||||
)
|
||||
|
||||
type Keys struct {
|
||||
algo string
|
||||
manager keys.Manager
|
||||
}
|
||||
|
||||
func DefaultKeysManager() keys.Manager {
|
||||
return keycmd.GetKeyManager()
|
||||
}
|
||||
|
||||
func NewDefaultKeysManager(algo string) *Keys {
|
||||
return New(DefaultKeysManager(), algo)
|
||||
}
|
||||
|
||||
func New(manager keys.Manager, algo string) *Keys {
|
||||
return &Keys{
|
||||
algo: algo,
|
||||
manager: manager,
|
||||
}
|
||||
}
|
||||
|
||||
func (k *Keys) GenerateKey(w http.ResponseWriter, r *http.Request) {
|
||||
ckReq := &CreateKeyRequest{
|
||||
Algo: k.algo,
|
||||
}
|
||||
if err := common.ParseRequestAndValidateJSON(r, ckReq); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
key, seed, err := k.manager.Create(ckReq.Name, ckReq.Passphrase, ckReq.Algo)
|
||||
if err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
res := &CreateKeyResponse{Key: key, Seed: seed}
|
||||
common.WriteSuccess(w, res)
|
||||
}
|
||||
|
||||
func (k *Keys) GetKey(w http.ResponseWriter, r *http.Request) {
|
||||
query := mux.Vars(r)
|
||||
name := query["name"]
|
||||
key, err := k.manager.Get(name)
|
||||
if err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
common.WriteSuccess(w, &key)
|
||||
}
|
||||
|
||||
func (k *Keys) ListKeys(w http.ResponseWriter, r *http.Request) {
|
||||
keys, err := k.manager.List()
|
||||
if err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
common.WriteSuccess(w, keys)
|
||||
}
|
||||
|
||||
var (
|
||||
errNonMatchingPathAndJSONKeyNames = errors.New("path and json key names don't match")
|
||||
)
|
||||
|
||||
func (k *Keys) UpdateKey(w http.ResponseWriter, r *http.Request) {
|
||||
uReq := new(UpdateKeyRequest)
|
||||
if err := common.ParseRequestAndValidateJSON(r, uReq); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
query := mux.Vars(r)
|
||||
name := query["name"]
|
||||
if name != uReq.Name {
|
||||
common.WriteError(w, errNonMatchingPathAndJSONKeyNames)
|
||||
return
|
||||
}
|
||||
|
||||
if err := k.manager.Update(uReq.Name, uReq.OldPass, uReq.NewPass); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
key, err := k.manager.Get(uReq.Name)
|
||||
if err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
common.WriteSuccess(w, &key)
|
||||
}
|
||||
|
||||
func (k *Keys) DeleteKey(w http.ResponseWriter, r *http.Request) {
|
||||
dReq := new(DeleteKeyRequest)
|
||||
if err := common.ParseRequestAndValidateJSON(r, dReq); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
query := mux.Vars(r)
|
||||
name := query["name"]
|
||||
if name != dReq.Name {
|
||||
common.WriteError(w, errNonMatchingPathAndJSONKeyNames)
|
||||
return
|
||||
}
|
||||
|
||||
if err := k.manager.Delete(dReq.Name, dReq.Passphrase); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := &common.ErrorResponse{Success: true}
|
||||
common.WriteSuccess(w, resp)
|
||||
}
|
||||
|
||||
func doPostTx(w http.ResponseWriter, r *http.Request) {
|
||||
tx := new(sdk.Tx)
|
||||
if err := common.ParseRequestAndValidateJSON(r, tx); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
commit, err := PostTx(*tx)
|
||||
if err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
common.WriteSuccess(w, commit)
|
||||
}
|
||||
|
||||
func doSign(w http.ResponseWriter, r *http.Request) {
|
||||
sr := new(SignRequest)
|
||||
if err := common.ParseRequestAndValidateJSON(r, sr); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
tx := sr.Tx
|
||||
if err := SignTx(sr.Name, sr.Password, tx); err != nil {
|
||||
common.WriteError(w, err)
|
||||
return
|
||||
}
|
||||
common.WriteSuccess(w, tx)
|
||||
}
|
||||
|
||||
// mux.Router registrars
|
||||
|
||||
// RegisterPostTx is a mux.Router handler that exposes POST
|
||||
// method access to post a transaction to the blockchain.
|
||||
func RegisterPostTx(r *mux.Router) error {
|
||||
r.HandleFunc("/tx", doPostTx).Methods("POST")
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterAllCRUD is a convenience method to register all
|
||||
// CRUD for keys to allow access by methods and routes:
|
||||
// POST: /keys
|
||||
// GET: /keys
|
||||
// GET: /keys/{name}
|
||||
// POST, PUT: /keys/{name}
|
||||
// DELETE: /keys/{name}
|
||||
func (k *Keys) RegisterAllCRUD(r *mux.Router) error {
|
||||
r.HandleFunc("/keys", k.GenerateKey).Methods("POST")
|
||||
r.HandleFunc("/keys", k.ListKeys).Methods("GET")
|
||||
r.HandleFunc("/keys/{name}", k.GetKey).Methods("GET")
|
||||
r.HandleFunc("/keys/{name}", k.UpdateKey).Methods("POST", "PUT")
|
||||
r.HandleFunc("/keys/{name}", k.DeleteKey).Methods("DELETE")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterSignTx is a mux.Router handler that
|
||||
// exposes POST method access to sign a transaction.
|
||||
func RegisterSignTx(r *mux.Router) error {
|
||||
r.HandleFunc("/sign", doSign).Methods("POST")
|
||||
return nil
|
||||
}
|
||||
|
||||
// End of mux.Router registrars
|
||||
@@ -1,29 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/tendermint/go-crypto/keys"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk"
|
||||
"github.com/cosmos/cosmos-sdk/client/commands"
|
||||
keycmd "github.com/cosmos/cosmos-sdk/client/commands/keys"
|
||||
)
|
||||
|
||||
// PostTx is same as a tx
|
||||
func PostTx(tx sdk.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
|
||||
packet := wire.BinaryBytes(tx)
|
||||
// post the bytes
|
||||
node := commands.GetNode()
|
||||
return node.BroadcastTxCommit(packet)
|
||||
}
|
||||
|
||||
// SignTx will modify the tx in-place, adding a signature if possible
|
||||
func SignTx(name, pass string, tx sdk.Tx) error {
|
||||
if sign, ok := tx.Unwrap().(keys.Signable); ok {
|
||||
manager := keycmd.GetKeyManager()
|
||||
return manager.Sign(name, pass, sign)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk"
|
||||
"github.com/cosmos/cosmos-sdk/modules/coin"
|
||||
"github.com/tendermint/go-crypto/keys"
|
||||
)
|
||||
|
||||
type CreateKeyRequest struct {
|
||||
Name string `json:"name,omitempty" validate:"required,min=3,printascii"`
|
||||
Passphrase string `json:"password,omitempty" validate:"required,min=10"`
|
||||
|
||||
// Algo is the requested algorithm to create the key
|
||||
Algo string `json:"algo,omitempty"`
|
||||
}
|
||||
|
||||
type DeleteKeyRequest struct {
|
||||
Name string `json:"name,omitempty" validate:"required,min=3,printascii"`
|
||||
Passphrase string `json:"password,omitempty" validate:"required,min=10"`
|
||||
}
|
||||
|
||||
type UpdateKeyRequest struct {
|
||||
Name string `json:"name,omitempty" validate:"required,min=3,printascii"`
|
||||
OldPass string `json:"password,omitempty" validate:"required,min=10"`
|
||||
NewPass string `json:"new_passphrase,omitempty" validate:"required,min=10"`
|
||||
}
|
||||
|
||||
type SignRequest struct {
|
||||
Name string `json:"name,omitempty" validate:"required,min=3,printascii"`
|
||||
Password string `json:"password,omitempty" validate:"required,min=10"`
|
||||
|
||||
Tx sdk.Tx `json:"tx" validate:"required"`
|
||||
}
|
||||
|
||||
type CreateKeyResponse struct {
|
||||
Key keys.Info `json:"key,omitempty"`
|
||||
Seed string `json:"seed_phrase,omitempty"`
|
||||
}
|
||||
|
||||
// SendInput is the request to send an amount from one actor to another.
|
||||
// Note: Not using the `validator:""` tags here because SendInput has
|
||||
// many fields so it would be nice to figure out all the invalid
|
||||
// inputs and report them back to the caller, in one shot.
|
||||
type SendInput struct {
|
||||
Fees *coin.Coin `json:"fees"`
|
||||
Multi bool `json:"multi,omitempty"`
|
||||
Sequence uint32 `json:"sequence"`
|
||||
|
||||
To *sdk.Actor `json:"to"`
|
||||
From *sdk.Actor `json:"from"`
|
||||
Amount coin.Coins `json:"amount"`
|
||||
}
|
||||
Reference in New Issue
Block a user