OSDN Git Service

Merge pull request #1137 from Bytom/v1.0.3_fix
[bytom/bytom-spv.git] / api / api.go
index f43620e..4578b62 100644 (file)
@@ -43,9 +43,11 @@ const (
 
 // Response describes the response standard.
 type Response struct {
-       Status string      `json:"status,omitempty"`
-       Msg    string      `json:"msg,omitempty"`
-       Data   interface{} `json:"data,omitempty"`
+       Status      string      `json:"status,omitempty"`
+       Code        string      `json:"code,omitempty"`
+       Msg         string      `json:"msg,omitempty"`
+       ErrorDetail string      `json:"error_detail,omitempty"`
+       Data        interface{} `json:"data,omitempty"`
 }
 
 //NewSuccessResponse success response
@@ -53,9 +55,34 @@ func NewSuccessResponse(data interface{}) Response {
        return Response{Status: SUCCESS, Data: data}
 }
 
+//FormatErrResp format error response
+func FormatErrResp(err error) (response Response) {
+       response = Response{Status: FAIL}
+       root := errors.Root(err)
+       // Some types cannot be used as map keys, for example slices.
+       // If an error's underlying type is one of these, don't panic.
+       // Just treat it like any other missing entry.
+       defer func() {
+               if err := recover(); err != nil {
+                       response.ErrorDetail = ""
+               }
+       }()
+
+       if info, ok := respErrFormatter[root]; ok {
+               response.Code = info.ChainCode
+               response.Msg = info.Message
+               response.ErrorDetail = errors.Detail(err)
+       }
+       return response
+}
+
 //NewErrorResponse error response
 func NewErrorResponse(err error) Response {
-       return Response{Status: FAIL, Msg: err.Error()}
+       response := FormatErrResp(err)
+       if response.Msg == "" {
+               response.Msg = err.Error()
+       }
+       return response
 }
 
 type waitHandler struct {
@@ -174,30 +201,32 @@ func (a *API) buildHandler() {
                m.Handle("/create-account-receiver", jsonHandler(a.createAccountReceiver))
                m.Handle("/list-addresses", jsonHandler(a.listAddresses))
                m.Handle("/validate-address", jsonHandler(a.validateAddress))
+               m.Handle("/list-pubkeys", jsonHandler(a.listPubKeys))
 
                m.Handle("/create-asset", jsonHandler(a.createAsset))
                m.Handle("/update-asset-alias", jsonHandler(a.updateAssetAlias))
+               m.Handle("/get-asset", jsonHandler(a.getAsset))
                m.Handle("/list-assets", jsonHandler(a.listAssets))
 
                m.Handle("/create-key", jsonHandler(a.pseudohsmCreateKey))
                m.Handle("/list-keys", jsonHandler(a.pseudohsmListKeys))
                m.Handle("/delete-key", jsonHandler(a.pseudohsmDeleteKey))
                m.Handle("/reset-key-password", jsonHandler(a.pseudohsmResetPassword))
-
-               m.Handle("/export-private-key", jsonHandler(a.walletExportKey))
-               m.Handle("/import-private-key", jsonHandler(a.walletImportKey))
-               m.Handle("/import-key-progress", jsonHandler(a.keyImportProgress))
+               m.Handle("/sign-message", jsonHandler(a.signMessage))
 
                m.Handle("/build-transaction", jsonHandler(a.build))
                m.Handle("/sign-transaction", jsonHandler(a.pseudohsmSignTemplates))
-               m.Handle("/submit-transaction", jsonHandler(a.submit))
-               // TODO remove this api, separate sign and submit process
-               m.Handle("/sign-submit-transaction", jsonHandler(a.signSubmit))
+
                m.Handle("/get-transaction", jsonHandler(a.getTransaction))
                m.Handle("/list-transactions", jsonHandler(a.listTransactions))
 
                m.Handle("/list-balances", jsonHandler(a.listBalances))
                m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
+
+               m.Handle("/backup-wallet", jsonHandler(a.backupWalletImage))
+               m.Handle("/restore-wallet", jsonHandler(a.restoreWalletImage))
+               m.Handle("/rescan-wallet", jsonHandler(a.rescanWallet))
+               m.Handle("/wallet-info", jsonHandler(a.getWalletInfo))
        } else {
                log.Warn("Please enable wallet")
        }
@@ -205,8 +234,6 @@ func (a *API) buildHandler() {
        m.Handle("/", alwaysError(errors.New("not Found")))
        m.Handle("/error", jsonHandler(a.walletError))
 
-       m.Handle("/net-info", jsonHandler(a.getNetInfo))
-
        m.Handle("/create-access-token", jsonHandler(a.createAccessToken))
        m.Handle("/list-access-tokens", jsonHandler(a.listAccessTokens))
        m.Handle("/delete-access-token", jsonHandler(a.deleteAccessToken))
@@ -218,19 +245,37 @@ func (a *API) buildHandler() {
        m.Handle("/delete-transaction-feed", jsonHandler(a.deleteTxFeed))
        m.Handle("/list-transaction-feeds", jsonHandler(a.listTxFeeds))
 
-       m.Handle("/block-hash", jsonHandler(a.getBestBlockHash))
-       m.Handle("/get-block-header-by-hash", jsonHandler(a.getBlockHeaderByHash))
-       m.Handle("/get-block-header-by-height", jsonHandler(a.getBlockHeaderByHeight))
+       m.Handle("/submit-transaction", jsonHandler(a.submit))
+       m.Handle("/estimate-transaction-gas", jsonHandler(a.estimateTxGas))
+
+       m.Handle("/get-unconfirmed-transaction", jsonHandler(a.getUnconfirmedTx))
+       m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconfirmedTxs))
+       m.Handle("/decode-raw-transaction", jsonHandler(a.decodeRawTransaction))
+
+       m.Handle("/get-block-hash", jsonHandler(a.getBestBlockHash))
+       m.Handle("/get-block-header", jsonHandler(a.getBlockHeader))
        m.Handle("/get-block", jsonHandler(a.getBlock))
        m.Handle("/get-block-count", jsonHandler(a.getBlockCount))
-       m.Handle("/get-block-transactions-count-by-hash", jsonHandler(a.getBlockTransactionsCountByHash))
-       m.Handle("/get-block-transactions-count-by-height", jsonHandler(a.getBlockTransactionsCountByHeight))
+       m.Handle("/get-difficulty", jsonHandler(a.getDifficulty))
+       m.Handle("/get-hash-rate", jsonHandler(a.getHashRate))
 
        m.Handle("/is-mining", jsonHandler(a.isMining))
-       m.Handle("/gas-rate", jsonHandler(a.gasRate))
+       m.Handle("/set-mining", jsonHandler(a.setMining))
+
        m.Handle("/get-work", jsonHandler(a.getWork))
+       m.Handle("/get-work-json", jsonHandler(a.getWorkJSON))
        m.Handle("/submit-work", jsonHandler(a.submitWork))
-       m.Handle("/set-mining", jsonHandler(a.setMining))
+       m.Handle("/submit-work-json", jsonHandler(a.submitWorkJSON))
+
+       m.Handle("/verify-message", jsonHandler(a.verifyMessage))
+       m.Handle("/decode-program", jsonHandler(a.decodeProgram))
+
+       m.Handle("/gas-rate", jsonHandler(a.gasRate))
+       m.Handle("/net-info", jsonHandler(a.getNetInfo))
+
+       m.Handle("/list-peers", jsonHandler(a.listPeers))
+       m.Handle("/disconnect-peer", jsonHandler(a.disconnectPeer))
+       m.Handle("/connect-peer", jsonHandler(a.connectPeer))
 
        handler := latencyHandler(m, walletEnable)
        handler = maxBytesHandler(handler) // TODO(tessr): consider moving this to non-core specific mux