Add swagger-ui for ICS0 and ICS20

This commit is contained in:
HaoyangLiu 2018-09-30 11:42:12 +08:00
parent 46e268efe2
commit 6e8b8e47ee
3 changed files with 337 additions and 234 deletions

View File

@ -11,10 +11,11 @@ tags:
description: Create and sign a send tx description: Create and sign a send tx
- name: version - name: version
description: Information about the app version description: Information about the app version
schemes:
- https
securityDefinitions: securityDefinitions:
kms: kms:
type: basic type: basic
paths: paths:
/version: /version:
get: get:
@ -48,15 +49,15 @@ paths:
schema: schema:
type: object type: object
properties: properties:
pub_key: id:
$ref: '#/definitions/PubKey' type: string
moniker: moniker:
type: string type: string
example: 159.89.198.221 example: validator-name
network: network:
type: string type: string
example: gaia-2 example: gaia-2
remote_addr: channels:
type: string type: string
listen_addr: listen_addr:
type: string type: string
@ -90,25 +91,25 @@ paths:
200: 200:
description: The latest block description: The latest block
schema: schema:
$ref: "#/definitions/Block" $ref: "#/definitions/QueryBlock"
/blocks/{height}: /blocks/{height}:
parameters:
- in: path
name: height
description: Block height
required: true
type: number
get: get:
summary: Get a block at a certain height summary: Get a block at a certain height
tags: tags:
- ICS0 - ICS0
produces: produces:
- application/json - application/json
parameters:
- in: path
name: height
description: Block height
required: true
type: number
responses: responses:
200: 200:
description: The block at a specific height description: The block at a specific height
schema: schema:
$ref: "#/definitions/Block" $ref: "#/definitions/QueryBlock"
404: 404:
description: Block at height is not available description: Block at height is not available
/validatorsets/latest: /validatorsets/latest:
@ -131,18 +132,18 @@ paths:
items: items:
$ref: "#/definitions/Validator" $ref: "#/definitions/Validator"
/validatorsets/{height}: /validatorsets/{height}:
parameters:
- in: path
name: height
description: Block height
required: true
type: number
get: get:
summary: Get a validator set a certain height summary: Get a validator set a certain height
tags: tags:
- ICS0 - ICS0
produces: produces:
- application/json - application/json
parameters:
- in: path
name: height
description: Block height
required: true
type: number
responses: responses:
200: 200:
description: The validator set at a specific block height description: The validator set at a specific block height
@ -158,58 +159,51 @@ paths:
404: 404:
description: Block at height not available description: Block at height not available
/txs/{hash}: /txs/{hash}:
parameters:
- in: path
name: hash
description: Tx hash
required: true
type: string
get: get:
summary: Get a Tx by hash summary: Get a Tx by hash
tags: tags:
- ICS0 - ICS0
produces: produces:
- application/json - application/json
parameters:
- in: path
name: hash
description: Tx hash
required: true
type: string
responses: responses:
200: 200:
description: Tx with the provided hash description: Tx with the provided hash
schema: schema:
$ref: "#/definitions/Tx" $ref: "#/definitions/TxQuery"
404: 404:
description: Tx not available for provided hash description: Tx not available for provided hash
/txs/search: /txs/search:
parameters:
- in: query
name: tag
schema:
type: string
example: "coin.sender=EE5F3404034C524501629B56E0DDC38FAD651F04"
required: true
- in: query
name: page
description: Pagination page
schema:
type: number
default: 0
- in: query
name: size
description: Pagination size
schema:
type: number
default: 50
get: get:
tags: tags:
- ICS0 - ICS0
summary: Query Tx summary: Query Tx
parameters:
- in: query
name: tag
type: string
description: "transaction tag, for instance: sender=`'cosmos1g9ahr6xhht5rmqven628nklxluzyv8z9jqjcmc'`"
required: true
- in: query
name: page
description: Pagination page
type: integer
- in: query
name: size
description: Pagination size
type: integer
responses: responses:
200: 200:
description: All Tx matching the provided tags description: All Tx matching the provided tags
content: schema:
application/json: type: array
schema: items:
type: array $ref: "#/definitions/TxQuery"
items:
$ref: "#/definitions/Tx"
404: 404:
description: Pagination is out of bounds description: Pagination is out of bounds
/txs: /txs:
@ -217,91 +211,107 @@ paths:
tags: tags:
- ICS0 - ICS0
summary: broadcast Tx summary: broadcast Tx
description: broadcast tx
parameters:
- in: body
name: txBroadcast
description: build a StdTx transaction and serilize it to a byte array with amino, then the `"tx"` field in the post body will be the base64 encoding of the byte array.
required: true
schema:
$ref: "#/definitions/TendertmintTx"
responses: responses:
200: 200:
description: Broadcast tx result description: Broadcast tx result
content: /tx/sign:
application/json:
schema:
type: array
items:
$ref: "#/definitions/Tx"
/txs/sign:
post: post:
tags: tags:
- ICS20 - ICS20
summary: Sign a Tx summary: Sign a Tx
description: Sign a Tx providing locally stored account and according password description: Sign a Tx providing locally stored account and according password
security: parameters:
- sign: [] - in: body
requestBody: name: sendToken
content: description: sign tx
application/json: required: true
schema: schema:
$ref: "#/definitions/TxBuild" $ref: "#/definitions/TxSign"
responses: responses:
200: 200:
description: The signed Tx description: The signed Tx
content: schema:
application/json: $ref: "#/definitions/StdTx"
schema:
$ref: "#/definitions/TxSigned"
401: 401:
description: Account name and/or password where wrong description: Account name and/or password where wrong
/txs/broadcast: /tx/broadcast:
post: post:
tags: tags:
- ICS20 - ICS20
summary: Send signed Tx summary: Send signed Tx
requestBody: description: Sign a Tx providing locally stored account and according password
content: consumes:
application/json: - application/json
schema: parameters:
$ref: "#/definitions/TxSigned" - in: body
name: txBroadcast
description: broadcast tx
required: true
schema:
$ref: "#/definitions/TxBroadcast"
responses: responses:
202: 202:
description: Tx was send and will probably be added to the next block description: Tx was send and will probably be added to the next block
400: 400:
description: The Tx was malformated description: The Tx was malformated
/bank/balances/{address}: /bank/balances/{address}:
parameters:
- in: path
name: address
description: Account address in bech32 format
required: true
type: string
get: get:
summary: Get the account balances summary: Get the account balances
tags: tags:
- ICS20 - ICS20
produces: produces:
- application/json - application/json
parameters:
- in: path
name: address
description: Account address in bech32 format
required: true
type: string
responses: responses:
200: 200:
description: Account balances description: Account balances
schema: schema:
$ref: "#/definitions/Balance" type: array
items:
$ref: "#/definitions/Coin"
204: 204:
description: There is no data for the requested account. This is not a 404 as the account might exist, just does not hold data. description: There is no data for the requested account. This is not a 404 as the account might exist, just does not hold data.
/bank/accounts/{address}/transfers: /bank/accounts/{address}/transfers:
parameters:
- in: path
name: address
description: Account address in bech32 format
required: true
type: string
post: post:
summary: Send coins (build -> sign -> send) summary: Send coins (build -> sign -> send)
description: Send coins (build -> sign -> send)
tags: tags:
- ICS20 - ICS20
security:
- kms: []
consumes: consumes:
- application/json - application/json
parameters: parameters:
- in: query
name: simulate
description: if true, ignore the gas field and perform a simulation of a transaction, but don't broadcast it
required: false
type: string
- in: query
name: generate_only
description: if true, build an unsigned transaction and write it back
required: false
type: string
- in: path
name: address
description: Account address in bech32 format
required: true
type: string
- in: body - in: body
name: account name: account
description: The password of the account to remove from the KMS description: The password of the account to remove from the KMS
required: true
schema: schema:
type: object type: object
properties: properties:
@ -312,11 +322,15 @@ paths:
amount: amount:
type: array type: array
items: items:
$ref: "#/definitions/Coins" $ref: "#/definitions/Coin"
chain_id: chain_id:
type: string type: string
squence: squence:
type: number type: string
gas:
type: string
fee:
type: string
responses: responses:
202: 202:
description: Tx was send and will probably be added to the next block description: Tx was send and will probably be added to the next block
@ -324,23 +338,46 @@ paths:
description: The Tx was malformated description: The Tx was malformated
definitions: definitions:
KVPair:
type: object
properties:
key:
type: string
value:
type: string
Fee:
type: object
properties:
gas:
type: string
amount:
type: array
items:
$ref: "#/definitions/Coin"
Msg:
type: string
Address: Address:
type: string type: string
description: bech32 encoded addres description: bech32 encoded addres
example: cosmos:zgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq example: cosmoszgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq
ValidatorAddress: ValidatorAddress:
type: string type: string
description: bech32 encoded addres description: bech32 encoded addres
example: cosmosvaloper:zgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq example: cosmosvaloper:zgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq
PubKey: PubKey:
type: string type: object
description: bech32 encoded public key properties:
example: cosmospub:zgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq type:
type: string
example: "tendermint/PubKeySecp256k1"
value:
type: string
example: "Avz04VhtKJh8ACCVzlI8aTosGy0ikFXKIVHQ3jKMrosH"
ValidatorPubKey: ValidatorPubKey:
type: string type: string
description: bech32 encoded public key description: bech32 encoded public key
example: cosmosvalconspub:zgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq example: cosmosvalconspub:zgnkwr7eyyv643dllwfpdwensmgdtz89yu73zq
Coins: Coin:
type: object type: object
properties: properties:
denom: denom:
@ -352,96 +389,90 @@ definitions:
Hash: Hash:
type: string type: string
example: EE5F3404034C524501629B56E0DDC38FAD651F04 example: EE5F3404034C524501629B56E0DDC38FAD651F04
Tx: Result:
type: object type: object
properties: properties:
type: log:
type: string type: string
enum: gas_wanted:
- stake/delegate type: string
data: example: "0"
type: object gas_used:
TxChain: type: string
example: "0"
tags:
type: array
items:
$ref: "#/definitions/KVPair"
TxQuery:
type: object type: object
properties: properties:
type: hash:
type: string type: string
default: chain/tx height:
data: type: number
tx:
$ref: "#/definitions/StdTx"
result:
$ref: "#/definitions/Result"
TendertmintTx:
type: object
properties:
tx:
type: string
return:
type: string
example: block
TxBroadcast:
type: object
properties:
tx:
$ref: "#/definitions/StdTx"
TxSign:
type: object
properties:
tx:
$ref: "#/definitions/StdTx"
name:
type: string
password:
type: string
chain_id:
type: string
account_number:
type: string
example: "0"
sequence:
type: string
example: "0"
append_sig:
type: boolean
example: true
StdTx:
type: object
properties:
msg:
type: array
items:
$ref: "#/definitions/Msg"
fee:
$ref: "#/definitions/Fee"
memo:
type: string
signature:
type: object type: object
properties: properties:
chain_id: signature:
type: string type: string
example: gaia-2 example: MEUCIQD02fsDPra8MtbRsyB1w7bqTM55Wu138zQbFcWx4+CFyAIge5WNPfKIuvzBZ69MyqHsqD8S1IwiEp+iUb6VSdtlpgY=
expires_at: pub_key:
type: number $ref: "#/definitions/PubKey"
example: 0 account_number:
tx: type: string
type: object example: "0"
properties: sequence:
type: type: string
type: string example: "0"
default: nonce
data:
type: object
properties:
sequence:
type: number
example: 0
signers:
type: array
items:
type: object
properties:
chain:
type: string
example: ''
app:
type: string
default: sigs
addr:
$ref: "#/definitions/Address"
tx:
$ref: "#/definitions/Tx"
TxBuild:
type: object
properties:
type:
type: string
default: sigs/one
data:
type: object
properties:
tx:
$ref: "#/definitions/Tx"
signature:
type: object
properties:
Sig:
type: string
default: ''
Pubkey:
type: string
default: ''
TxSigned:
type: object
properties:
type:
type: string
default: sigs/one
data:
type: object
properties:
tx:
$ref: "#/definitions/Tx"
signature:
type: object
properties:
Sig:
type: string
example: 81B11E717789600CC192B26F452A983DF13B985EE75ABD9DD9E68D7BA007A958
Pubkey:
$ref: "#/definitions/PubKey"
Account: Account:
type: object type: object
properties: properties:
@ -452,20 +483,6 @@ definitions:
$ref: "#/definitions/Address" $ref: "#/definitions/Address"
pub_key: pub_key:
$ref: "#/definitions/PubKey" $ref: "#/definitions/PubKey"
Balance:
type: object
properties:
height:
type: number
example: 123456
coins:
type: array
items:
$ref: "#/definitions/Coins"
credit:
type: array
items:
type: object
BlockID: BlockID:
type: object type: object
properties: properties:
@ -479,60 +496,99 @@ definitions:
example: 0 example: 0
hash: hash:
$ref: "#/definitions/Hash" $ref: "#/definitions/Hash"
BlockHeader:
type: object
properties:
chain_id:
type: string
example: gaia-2
height:
type: number
example: 1
time:
type: string
example: '2017-12-30T05:53:09.287+01:00'
num_txs:
type: number
example: 0
last_block_id:
$ref: "#/definitions/BlockID"
total_txs:
type: number
example: 35
last_commit_hash:
$ref: "#/definitions/Hash"
data_hash:
$ref: "#/definitions/Hash"
validators_hash:
$ref: "#/definitions/Hash"
consensus_hash:
$ref: "#/definitions/Hash"
app_hash:
$ref: "#/definitions/Hash"
last_results_hash:
$ref: "#/definitions/Hash"
evidence_hash:
$ref: "#/definitions/Hash"
Block: Block:
type: object type: object
properties: properties:
header: header:
type: object $ref: "#/definitions/BlockHeader"
properties:
chain_id:
type: string
example: gaia-2
height:
type: number
example: 1
time:
type: string
example: '2017-12-30T05:53:09.287+01:00'
num_txs:
type: number
example: 0
last_block_id:
$ref: "#/definitions/BlockID"
total_txs:
type: number
example: 35
last_commit_hash:
$ref: "#/definitions/Hash"
data_hash:
$ref: "#/definitions/Hash"
validators_hash:
$ref: "#/definitions/Hash"
consensus_hash:
$ref: "#/definitions/Hash"
app_hash:
$ref: "#/definitions/Hash"
last_results_hash:
$ref: "#/definitions/Hash"
evidence_hash:
$ref: "#/definitions/Hash"
txs: txs:
type: array type: array
items: items:
$ref: "#/definitions/Tx" type: string
evidence: evidence:
type: array type: array
items: items:
type: object type: string
last_commit: last_commit:
type: object type: object
properties: properties:
blockID: block_id:
$ref: "#/definitions/BlockID" $ref: "#/definitions/BlockID"
precommits: precommits:
type: array type: array
items: items:
type: object type: object
properties:
validator_address:
type: string
validator_index:
type: string
example: "0"
height:
type: string
example: "0"
round:
type: string
example: "0"
timestamp:
type: string
example: '2017-12-30T05:53:09.287+01:00'
type:
type: number
example: 2
block_id:
$ref: "#/definitions/BlockID"
signature:
type: string
example: '7uTC74QlknqYWEwg7Vn6M8Om7FuZ0EO4bjvuj6rwH1mTUJrRuMMZvAAqT9VjNgP0RA/TDp6u/92AqrZfXJSpBQ=='
BlockMeta:
type: object
properties:
header:
$ref: "#/definitions/BlockHeader"
block_id:
$ref: "#/definitions/BlockID"
QueryBlock:
type: object
properties:
block_meta:
$ref: "#/definitions/BlockMeta"
block:
$ref: "#/definitions/Block"
Validator: Validator:
type: object type: object
properties: properties:
@ -546,5 +602,3 @@ definitions:
accum: accum:
type: number type: number
example: 1000 example: 1000
schemes:
- https

View File

@ -20,7 +20,7 @@ const (
// BroadcastBody Tx Broadcast Body // BroadcastBody Tx Broadcast Body
type BroadcastBody struct { type BroadcastBody struct {
TxBytes string `json:"tx"` TxBytes []byte `json:"tx"`
Return string `json:"return"` Return string `json:"return"`
} }
@ -42,7 +42,7 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
var output []byte var output []byte
switch m.Return { switch m.Return {
case flagBlock: case flagBlock:
res, err := cliCtx.BroadcastTx([]byte(m.TxBytes)) res, err := cliCtx.BroadcastTx(m.TxBytes)
if err != nil { if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
@ -53,7 +53,7 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
return return
} }
case flagSync: case flagSync:
res, err := cliCtx.BroadcastTxSync([]byte(m.TxBytes)) res, err := cliCtx.BroadcastTxSync(m.TxBytes)
if err != nil { if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
@ -64,7 +64,7 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
return return
} }
case flagAsync: case flagAsync:
res, err := cliCtx.BroadcastTxAsync([]byte(m.TxBytes)) res, err := cliCtx.BroadcastTxAsync(m.TxBytes)
if err != nil { if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return

View File

@ -20,6 +20,10 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec,
"/accounts/{address}", "/accounts/{address}",
QueryAccountRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), cliCtx), QueryAccountRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), cliCtx),
).Methods("GET") ).Methods("GET")
r.HandleFunc(
"/bank/balances/{address}",
QueryBalancesRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), cliCtx),
).Methods("GET")
r.HandleFunc( r.HandleFunc(
"/tx/sign", "/tx/sign",
SignTxRequestHandlerFn(cdc, cliCtx), SignTxRequestHandlerFn(cdc, cliCtx),
@ -70,3 +74,48 @@ func QueryAccountRequestHandlerFn(
w.Write(output) w.Write(output)
} }
} }
// query accountREST Handler
func QueryBalancesRequestHandlerFn(
storeName string, cdc *codec.Codec,
decoder auth.AccountDecoder, cliCtx context.CLIContext,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32addr := vars["address"]
addr, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't query account. Error: %s", err.Error()))
return
}
// the query will return empty if there is no data for this account
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
// decode the value
account, err := decoder(res)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't parse query result. Result: %s. Error: %s", res, err.Error()))
return
}
// print out whole account
output, err := cdc.MarshalJSON(account.GetCoins())
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error()))
return
}
w.Write(output)
}
}