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/blockchain/txbuilder"
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 // POST /get-transaction
112 func (a *API) getTransaction(ctx context.Context, txInfo struct {
113 TxID string `json:"tx_id"`
115 var annotatedTx *query.AnnotatedTx
118 annotatedTx, err = a.wallet.GetTransactionByTxID(txInfo.TxID)
120 // transaction not found in blockchain db, search it from unconfirmed db
121 annotatedTx, err = a.wallet.GetUnconfirmedTxByTxID(txInfo.TxID)
123 return NewErrorResponse(err)
127 return NewSuccessResponse(annotatedTx)
130 // POST /list-transactions
131 func (a *API) listTransactions(ctx context.Context, filter struct {
132 ID string `json:"id"`
133 AccountID string `json:"account_id"`
134 Detail bool `json:"detail"`
135 Unconfirmed bool `json:"unconfirmed"`
136 From uint `json:"from"`
137 Count uint `json:"count"`
139 transactions := []*query.AnnotatedTx{}
141 var transaction *query.AnnotatedTx
144 transaction, err = a.wallet.GetTransactionByTxID(filter.ID)
145 if err != nil && filter.Unconfirmed {
146 transaction, err = a.wallet.GetUnconfirmedTxByTxID(filter.ID)
150 return NewErrorResponse(err)
152 transactions = []*query.AnnotatedTx{transaction}
154 transactions, err = a.wallet.GetTransactions(filter.AccountID)
156 return NewErrorResponse(err)
159 if filter.Unconfirmed {
160 unconfirmedTxs, err := a.wallet.GetUnconfirmedTxs(filter.AccountID)
162 return NewErrorResponse(err)
164 transactions = append(unconfirmedTxs, transactions...)
168 if filter.Detail == false {
169 txSummary := a.wallet.GetTransactionsSummary(transactions)
170 start, end := getPageRange(len(txSummary), filter.From, filter.Count)
171 return NewSuccessResponse(txSummary[start:end])
173 start, end := getPageRange(len(transactions), filter.From, filter.Count)
174 return NewSuccessResponse(transactions[start:end])
177 // POST /get-unconfirmed-transaction
178 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
179 TxID chainjson.HexBytes `json:"tx_id"`
182 copy(tmpTxID[:], filter.TxID[:])
184 txHash := bc.NewHash(tmpTxID)
185 txPool := a.chain.GetTxPool()
186 txDesc, err := txPool.GetTransaction(&txHash)
188 return NewErrorResponse(err)
193 Version: txDesc.Tx.Version,
194 Size: txDesc.Tx.SerializedSize,
195 TimeRange: txDesc.Tx.TimeRange,
196 Inputs: []*query.AnnotatedInput{},
197 Outputs: []*query.AnnotatedOutput{},
198 StatusFail: txDesc.StatusFail,
201 resOutID := txDesc.Tx.ResultIds[0]
202 resOut := txDesc.Tx.Entries[*resOutID]
203 switch out := resOut.(type) {
204 case *bc.IntraChainOutput:
205 tx.MuxID = *out.Source.Ref
207 tx.MuxID = *out.Source.Ref
209 tx.MuxID = *out.Source.Ref
212 for i := range txDesc.Tx.Inputs {
213 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
215 for i := range txDesc.Tx.Outputs {
216 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
219 return NewSuccessResponse(tx)
222 type unconfirmedTxsResp struct {
223 Total uint64 `json:"total"`
224 TxIDs []bc.Hash `json:"tx_ids"`
227 // POST /list-unconfirmed-transactions
228 func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
231 txPool := a.chain.GetTxPool()
232 txs := txPool.GetTransactions()
233 for _, txDesc := range txs {
234 txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
237 return NewSuccessResponse(&unconfirmedTxsResp{
238 Total: uint64(len(txIDs)),
243 // RawTx is the tx struct for getRawTransaction
245 ID bc.Hash `json:"tx_id"`
246 Version uint64 `json:"version"`
247 Size uint64 `json:"size"`
248 TimeRange uint64 `json:"time_range"`
249 Inputs []*query.AnnotatedInput `json:"inputs"`
250 Outputs []*query.AnnotatedOutput `json:"outputs"`
251 Fee uint64 `json:"fee"`
254 // POST /decode-raw-transaction
255 func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
256 Tx types.Tx `json:"raw_transaction"`
260 Version: ins.Tx.Version,
261 Size: ins.Tx.SerializedSize,
262 TimeRange: ins.Tx.TimeRange,
263 Inputs: []*query.AnnotatedInput{},
264 Outputs: []*query.AnnotatedOutput{},
267 for i := range ins.Tx.Inputs {
268 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(&ins.Tx, uint32(i)))
270 for i := range ins.Tx.Outputs {
271 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(&ins.Tx, i))
274 tx.Fee = txbuilder.CalculateTxFee(&ins.Tx)
275 return NewSuccessResponse(tx)
278 // POST /list-unspent-outputs
279 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
280 AccountID string `json:"account_id"`
281 AccountAlias string `json:"account_alias"`
282 ID string `json:"id"`
283 Unconfirmed bool `json:"unconfirmed"`
284 SmartContract bool `json:"smart_contract"`
285 From uint `json:"from"`
286 Count uint `json:"count"`
288 accountID := filter.AccountID
289 if filter.AccountAlias != "" {
290 acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
292 return NewErrorResponse(err)
296 accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract)
298 UTXOs := []query.AnnotatedUTXO{}
299 for _, utxo := range accountUTXOs {
300 UTXOs = append([]query.AnnotatedUTXO{{
301 AccountID: utxo.AccountID,
302 OutputID: utxo.OutputID.String(),
303 SourceID: utxo.SourceID.String(),
304 AssetID: utxo.AssetID.String(),
306 SourcePos: utxo.SourcePos,
307 Program: fmt.Sprintf("%x", utxo.ControlProgram),
308 ControlProgramIndex: utxo.ControlProgramIndex,
309 Address: utxo.Address,
310 ValidHeight: utxo.ValidHeight,
311 Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
312 AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
316 start, end := getPageRange(len(UTXOs), filter.From, filter.Count)
317 return NewSuccessResponse(UTXOs[start:end])
321 func (a *API) gasRate() Response {
322 gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
323 return NewSuccessResponse(gasrate)
326 // PubKeyInfo is structure of pubkey info
327 type PubKeyInfo struct {
328 Pubkey string `json:"pubkey"`
329 Path []chainjson.HexBytes `json:"derivation_path"`
332 // AccountPubkey is detail of account pubkey info
333 type AccountPubkey struct {
334 RootXPub chainkd.XPub `json:"root_xpub"`
335 PubKeyInfos []PubKeyInfo `json:"pubkey_infos"`
338 func getPubkey(account *account.Account, change bool, index uint64) (*ed25519.PublicKey, []chainjson.HexBytes, error) {
339 rawPath, err := signers.Path(account.Signer, signers.AccountKeySpace, change, index)
343 derivedXPub := account.XPubs[0].Derive(rawPath)
344 pubkey := derivedXPub.PublicKey()
345 var path []chainjson.HexBytes
346 for _, p := range rawPath {
347 path = append(path, chainjson.HexBytes(p))
350 return &pubkey, path, nil
353 // POST /list-pubkeys
354 func (a *API) listPubKeys(ctx context.Context, ins struct {
355 AccountID string `json:"account_id"`
356 AccountAlias string `json:"account_alias"`
357 PublicKey string `json:"public_key"`
360 account := &account.Account{}
361 if ins.AccountAlias != "" {
362 account, err = a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
364 account, err = a.wallet.AccountMgr.FindByID(ins.AccountID)
368 return NewErrorResponse(err)
371 pubKeyInfos := []PubKeyInfo{}
372 if account.DeriveRule == signers.BIP0032 {
373 idx := a.wallet.AccountMgr.GetContractIndex(account.ID)
374 for i := uint64(1); i <= idx; i++ {
375 pubkey, path, err := getPubkey(account, false, i)
377 return NewErrorResponse(err)
379 if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(*pubkey) {
382 pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
383 Pubkey: hex.EncodeToString(*pubkey),
387 } else if account.DeriveRule == signers.BIP0044 {
388 idx := a.wallet.AccountMgr.GetBip44ContractIndex(account.ID, true)
389 for i := uint64(1); i <= idx; i++ {
390 pubkey, path, err := getPubkey(account, true, i)
392 return NewErrorResponse(err)
394 if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(*pubkey) {
397 pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
398 Pubkey: hex.EncodeToString(*pubkey),
403 idx = a.wallet.AccountMgr.GetBip44ContractIndex(account.ID, false)
404 for i := uint64(1); i <= idx; i++ {
405 pubkey, path, err := getPubkey(account, false, i)
407 return NewErrorResponse(err)
409 if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(*pubkey) {
412 pubKeyInfos = append(pubKeyInfos, PubKeyInfo{
413 Pubkey: hex.EncodeToString(*pubkey),
419 if len(pubKeyInfos) == 0 {
420 return NewErrorResponse(errors.New("Not found publickey for the account"))
423 return NewSuccessResponse(&AccountPubkey{
424 RootXPub: account.XPubs[0],
425 PubKeyInfos: pubKeyInfos,