OSDN Git Service

b244b7ec9a3f06ef84e00768282ce1169eb2649e
[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         ClaimScript      chainjson.HexBytes `json:"claim_script"`
38 }
39
40 func (a *API) getPeginAddress(ctx context.Context, ins struct {
41         AccountID    string `json:"account_id"`
42         AccountAlias string `json:"account_alias"`
43 }) Response {
44
45         accountID := ins.AccountID
46         if ins.AccountAlias != "" {
47                 account, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
48                 if err != nil {
49                         return NewErrorResponse(err)
50                 }
51
52                 accountID = account.ID
53         }
54
55         mainchainAddress, claimScript, err := a.wallet.AccountMgr.CreatePeginAddress(accountID, false)
56         if err != nil {
57                 return NewErrorResponse(err)
58         }
59
60         return NewSuccessResponse(fundingResp{
61                 MainchainAddress: mainchainAddress,
62                 ClaimScript:      claimScript,
63         })
64 }
65
66 func (a *API) getPeginContractAddress(ctx context.Context, ins struct {
67         AccountID    string `json:"account_id"`
68         AccountAlias string `json:"account_alias"`
69 }) Response {
70
71         accountID := ins.AccountID
72         if ins.AccountAlias != "" {
73                 account, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
74                 if err != nil {
75                         return NewErrorResponse(err)
76                 }
77
78                 accountID = account.ID
79         }
80
81         mainchainAddress, claimScript, err := a.wallet.AccountMgr.CreatePeginContractAddress(accountID, false)
82         if err != nil {
83                 return NewErrorResponse(err)
84         }
85
86         return NewSuccessResponse(fundingResp{
87                 MainchainAddress: mainchainAddress,
88                 ClaimScript:      claimScript,
89         })
90 }