8 log "github.com/sirupsen/logrus"
10 "github.com/vapor/account"
11 "github.com/vapor/asset"
12 "github.com/vapor/blockchain/query"
13 "github.com/vapor/blockchain/signers"
14 "github.com/vapor/common/arithmetic"
15 "github.com/vapor/consensus"
16 "github.com/vapor/crypto/ed25519"
17 "github.com/vapor/crypto/ed25519/chainkd"
18 chainjson "github.com/vapor/encoding/json"
19 "github.com/vapor/errors"
20 "github.com/vapor/protocol/bc"
21 "github.com/vapor/protocol/bc/types"
24 // POST /list-accounts
25 func (a *API) listAccounts(ctx context.Context, filter struct {
27 Alias string `json:"alias"`
29 accountID := filter.ID
30 if filter.Alias != "" {
31 acc, err := a.wallet.AccountMgr.FindByAlias(filter.Alias)
33 return NewErrorResponse(err)
38 accounts, err := a.wallet.AccountMgr.ListAccounts(accountID)
40 log.Errorf("listAccounts: %v", err)
41 return NewErrorResponse(err)
44 annotatedAccounts := []query.AnnotatedAccount{}
45 for _, acc := range accounts {
46 annotatedAccounts = append(annotatedAccounts, *account.Annotated(acc))
49 return NewSuccessResponse(annotatedAccounts)
53 func (a *API) getAsset(ctx context.Context, filter struct {
56 ass, err := a.wallet.AssetReg.GetAsset(filter.ID)
58 log.Errorf("getAsset: %v", err)
59 return NewErrorResponse(err)
62 annotatedAsset, err := asset.Annotated(ass)
64 return NewErrorResponse(err)
66 return NewSuccessResponse(annotatedAsset)
70 func (a *API) listAssets(ctx context.Context, filter struct {
73 assets, err := a.wallet.AssetReg.ListAssets(filter.ID)
75 log.Errorf("listAssets: %v", err)
76 return NewErrorResponse(err)
79 annotatedAssets := []*query.AnnotatedAsset{}
80 for _, ass := range assets {
81 annotatedAsset, err := asset.Annotated(ass)
83 return NewErrorResponse(err)
85 annotatedAssets = append(annotatedAssets, annotatedAsset)
87 return NewSuccessResponse(annotatedAssets)
90 // POST /list-balances
91 func (a *API) listBalances(ctx context.Context, filter struct {
92 AccountID string `json:"account_id"`
93 AccountAlias string `json:"account_alias"`
95 accountID := filter.AccountID
96 if filter.AccountAlias != "" {
97 acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
99 return NewErrorResponse(err)
104 balances, err := a.wallet.GetAccountBalances(accountID, "")
106 return NewErrorResponse(err)
108 return NewSuccessResponse(balances)
111 func (a *API) listAccountVotes(ctx context.Context, filter struct {
112 AccountID string `json:"account_id"`
113 AccountAlias string `json:"account_alias"`
115 accountID := filter.AccountID
116 if filter.AccountAlias != "" {
117 acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
119 return NewErrorResponse(err)
124 votes, err := a.wallet.GetAccountVotes(accountID, "")
126 return NewErrorResponse(err)
128 return NewSuccessResponse(votes)
131 // POST /get-transaction
132 func (a *API) getTransaction(ctx context.Context, txInfo struct {
133 TxID string `json:"tx_id"`
135 var annotatedTx *query.AnnotatedTx
138 annotatedTx, err = a.wallet.GetTransactionByTxID(txInfo.TxID)
140 // transaction not found in blockchain db, search it from unconfirmed db
141 annotatedTx, err = a.wallet.GetUnconfirmedTxByTxID(txInfo.TxID)
143 return NewErrorResponse(err)
147 return NewSuccessResponse(annotatedTx)
150 // POST /list-transactions
151 func (a *API) listTransactions(ctx context.Context, filter struct {
152 AccountID string `json:"account_id"`
153 AccountAlias string `json:"account_alias"`
154 StartTxID string `json:"start_tx_id"`
155 Detail bool `json:"detail"`
156 Unconfirmed bool `json:"unconfirmed"`
157 Count uint `json:"count"`
159 accountID := filter.AccountID
160 if filter.AccountAlias != "" {
161 acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
163 return NewErrorResponse(err)
169 return NewErrorResponse(account.ErrAccountIDEmpty)
172 transactions, err := a.wallet.GetTransactions(accountID, filter.StartTxID, filter.Count, filter.Unconfirmed)
174 return NewErrorResponse(err)
177 if filter.Detail == false {
178 txSummary := a.wallet.GetTransactionsSummary(transactions)
179 return NewSuccessResponse(txSummary)
182 return NewSuccessResponse(transactions)
185 // POST /get-unconfirmed-transaction
186 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
187 TxID chainjson.HexBytes `json:"tx_id"`
190 copy(tmpTxID[:], filter.TxID[:])
192 txHash := bc.NewHash(tmpTxID)
193 txPool := a.chain.GetTxPool()
194 txDesc, err := txPool.GetTransaction(&txHash)
196 return NewErrorResponse(err)
201 Version: txDesc.Tx.Version,
202 Size: txDesc.Tx.SerializedSize,
203 TimeRange: txDesc.Tx.TimeRange,
204 Inputs: []*query.AnnotatedInput{},
205 Outputs: []*query.AnnotatedOutput{},
206 StatusFail: txDesc.StatusFail,
209 resOutID := txDesc.Tx.ResultIds[0]
210 resOut := txDesc.Tx.Entries[*resOutID]
211 switch out := resOut.(type) {
212 case *bc.IntraChainOutput:
213 tx.MuxID = *out.Source.Ref
215 tx.MuxID = *out.Source.Ref
217 tx.MuxID = *out.Source.Ref
220 for i := range txDesc.Tx.Inputs {
221 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
223 for i := range txDesc.Tx.Outputs {
224 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
227 return NewSuccessResponse(tx)
230 type unconfirmedTxsResp struct {
231 Total uint64 `json:"total"`
232 TxIDs []bc.Hash `json:"tx_ids"`
235 // POST /list-unconfirmed-transactions
236 func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
239 txPool := a.chain.GetTxPool()
240 txs := txPool.GetTransactions()
241 for _, txDesc := range txs {
242 txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
245 return NewSuccessResponse(&unconfirmedTxsResp{
246 Total: uint64(len(txIDs)),
251 // RawTx is the tx struct for getRawTransaction
253 ID bc.Hash `json:"tx_id"`
254 Version uint64 `json:"version"`
255 Size uint64 `json:"size"`
256 TimeRange uint64 `json:"time_range"`
257 Inputs []*query.AnnotatedInput `json:"inputs"`
258 Outputs []*query.AnnotatedOutput `json:"outputs"`
259 Fee uint64 `json:"fee"`
262 // POST /decode-raw-transaction
263 func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
264 Tx types.Tx `json:"raw_transaction"`
268 Version: ins.Tx.Version,
269 Size: ins.Tx.SerializedSize,
270 TimeRange: ins.Tx.TimeRange,
271 Inputs: []*query.AnnotatedInput{},
272 Outputs: []*query.AnnotatedOutput{},
275 for i := range ins.Tx.Inputs {
276 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(&ins.Tx, uint32(i)))
278 for i := range ins.Tx.Outputs {
279 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(&ins.Tx, i))
282 tx.Fee, _ = arithmetic.CalculateTxFee(&ins.Tx)
283 return NewSuccessResponse(tx)
286 type ListUtxosReq struct {
287 AccountID string `json:"account_id"`
288 AccountAlias string `json:"account_alias"`
289 ID string `json:"id"`
290 Unconfirmed bool `json:"unconfirmed"`
291 SmartContract bool `json:"smart_contract"`
292 From uint `json:"from"`
293 Count uint `json:"count"`
296 // POST /list-unspent-outputs
297 func (a *API) listUnspentOutputs(ctx context.Context, filter ListUtxosReq) Response {
298 accountID := filter.AccountID
299 if filter.AccountAlias != "" {
300 acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
302 return NewErrorResponse(err)
306 accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract, false)
308 UTXOs := []query.AnnotatedUTXO{}
309 for _, utxo := range accountUTXOs {
310 UTXOs = append([]query.AnnotatedUTXO{{
311 AccountID: utxo.AccountID,
312 OutputID: utxo.OutputID.String(),
313 SourceID: utxo.SourceID.String(),
314 AssetID: utxo.AssetID.String(),
316 SourcePos: utxo.SourcePos,
317 Program: fmt.Sprintf("%x", utxo.ControlProgram),
318 ControlProgramIndex: utxo.ControlProgramIndex,
319 Address: utxo.Address,
320 ValidHeight: utxo.ValidHeight,
321 Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
322 AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
326 start, end := getPageRange(len(UTXOs), filter.From, filter.Count)
327 return NewSuccessResponse(UTXOs[start:end])
331 func (a *API) gasRate() Response {
332 gasrate := map[string]int64{"gas_rate": consensus.ActiveNetParams.VMGasRate}
333 return NewSuccessResponse(gasrate)
336 // PubKeyInfo is structure of pubkey info
337 type PubKeyInfo struct {
338 Pubkey string `json:"pubkey"`
339 Path []chainjson.HexBytes `json:"derivation_path"`
342 // AccountPubkey is detail of account pubkey info
343 type AccountPubkey struct {
344 RootXPub chainkd.XPub `json:"root_xpub"`
345 PubKeyInfos []PubKeyInfo `json:"pubkey_infos"`
348 func getPubkey(account *account.Account, change bool, index uint64) (*ed25519.PublicKey, []chainjson.HexBytes, error) {
349 rawPath, err := signers.Path(account.Signer, signers.AccountKeySpace, change, index)
353 derivedXPub := account.XPubs[0].Derive(rawPath)
354 pubkey := derivedXPub.PublicKey()
355 var path []chainjson.HexBytes
356 for _, p := range rawPath {
357 path = append(path, chainjson.HexBytes(p))
360 return &pubkey, path, nil
363 // POST /list-pubkeys
364 func (a *API) listPubKeys(ctx context.Context, ins struct {
365 AccountID string `json:"account_id"`
366 AccountAlias string `json:"account_alias"`
367 PublicKey string `json:"public_key"`
370 account := &account.Account{}
371 if ins.AccountAlias != "" {
372 account, err = a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
374 account, err = a.wallet.AccountMgr.FindByID(ins.AccountID)
378 return NewErrorResponse(err)
381 pubKeyInfos := []PubKeyInfo{}
382 if account.DeriveRule == signers.BIP0032 {
383 idx := a.wallet.AccountMgr.GetContractIndex(account.ID)
384 for i := uint64(1); i <= idx; i++ {
385 pubkey, path, err := getPubkey(account, false, i)
387 return NewErrorResponse(err)
389 if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(*pubkey) {
392 pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
393 Pubkey: hex.EncodeToString(*pubkey),
397 } else if account.DeriveRule == signers.BIP0044 {
398 idx := a.wallet.AccountMgr.GetBip44ContractIndex(account.ID, true)
399 for i := uint64(1); i <= idx; i++ {
400 pubkey, path, err := getPubkey(account, true, i)
402 return NewErrorResponse(err)
404 if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(*pubkey) {
407 pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
408 Pubkey: hex.EncodeToString(*pubkey),
413 idx = a.wallet.AccountMgr.GetBip44ContractIndex(account.ID, false)
414 for i := uint64(1); i <= idx; i++ {
415 pubkey, path, err := getPubkey(account, false, i)
417 return NewErrorResponse(err)
419 if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(*pubkey) {
422 pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
423 Pubkey: hex.EncodeToString(*pubkey),
429 if len(pubKeyInfos) == 0 {
430 return NewErrorResponse(errors.New("Not found publickey for the account"))
433 return NewSuccessResponse(&AccountPubkey{
434 RootXPub: account.XPubs[0],
435 PubKeyInfos: pubKeyInfos,