OSDN Git Service

Merge pull request #15 from Bytom/dev_ci
[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 }