OSDN Git Service

fix bug and add tool for claim tx
[bytom/vapor.git] / api / receivers.go
1 package api
2
3 import (
4         "context"
5
6         "github.com/vapor/blockchain/txbuilder"
7         chainjson "github.com/vapor/encoding/json"
8 )
9
10 func (a *API) createAccountReceiver(ctx context.Context, ins struct {
11         AccountID    string `json:"account_id"`
12         AccountAlias string `json:"account_alias"`
13 }) Response {
14         accountID := ins.AccountID
15         if ins.AccountAlias != "" {
16                 account, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
17                 if err != nil {
18                         return NewErrorResponse(err)
19                 }
20
21                 accountID = account.ID
22         }
23
24         program, err := a.wallet.AccountMgr.CreateAddress(accountID, false)
25         if err != nil {
26                 return NewErrorResponse(err)
27         }
28
29         return NewSuccessResponse(&txbuilder.Receiver{
30                 ControlProgram: program.ControlProgram,
31                 Address:        program.Address,
32         })
33 }
34
35 type fundingResp struct {
36         MainchainAddress string             `json:"mainchain_address"`
37         ControlProgram   chainjson.HexBytes `json:"control_program,omitempty"`
38         ClaimScript      chainjson.HexBytes `json:"claim_script"`
39 }
40
41 func (a *API) getPeginAddress(ctx context.Context, ins struct {
42         AccountID    string `json:"account_id"`
43         AccountAlias string `json:"account_alias"`
44 }) Response {
45
46         accountID := ins.AccountID
47         if ins.AccountAlias != "" {
48                 account, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
49                 if err != nil {
50                         return NewErrorResponse(err)
51                 }
52
53                 accountID = account.ID
54         }
55
56         mainchainAddress, claimScript, err := a.wallet.AccountMgr.CreatePeginAddress(accountID, false)
57         if err != nil {
58                 return NewErrorResponse(err)
59         }
60
61         return NewSuccessResponse(fundingResp{
62                 MainchainAddress: mainchainAddress,
63                 ClaimScript:      claimScript,
64         })
65 }
66
67 func (a *API) getPeginContractAddress(ctx context.Context, ins struct {
68         AccountID    string `json:"account_id"`
69         AccountAlias string `json:"account_alias"`
70 }) Response {
71
72         accountID := ins.AccountID
73         if ins.AccountAlias != "" {
74                 account, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
75                 if err != nil {
76                         return NewErrorResponse(err)
77                 }
78
79                 accountID = account.ID
80         }
81
82         mainchainAddress, controlProgram, claimScript, err := a.wallet.AccountMgr.CreatePeginContractAddress(accountID, false)
83         if err != nil {
84                 return NewErrorResponse(err)
85         }
86
87         return NewSuccessResponse(fundingResp{
88                 MainchainAddress: mainchainAddress,
89                 ControlProgram:   controlProgram,
90                 ClaimScript:      claimScript,
91         })
92 }