OSDN Git Service

e1e9bafeb0a04d07b2be014bc08c707b33b68d73
[bytom/bytom.git] / api / contract.go
1 package api
2
3 import (
4         "context"
5         "strings"
6
7         "github.com/bytom/bytom/contract"
8         "github.com/bytom/bytom/crypto/sha3pool"
9         chainjson "github.com/bytom/bytom/encoding/json"
10         "github.com/bytom/bytom/errors"
11         "github.com/bytom/bytom/protocol/vm/vmutil"
12 )
13
14 // pre-define errors for supporting bytom errorFormatter
15 var (
16         ErrNullContract      = errors.New("contract is empty")
17         ErrNullContractID    = errors.New("contract id is empty")
18         ErrNullContractAlias = errors.New("contract alias is empty")
19 )
20
21 // POST /create-asset
22 func (a *API) createContract(_ context.Context, ins struct {
23         Alias    string             `json:"alias"`
24         Contract chainjson.HexBytes `json:"contract"`
25 }) Response {
26         ins.Alias = strings.TrimSpace(ins.Alias)
27         if ins.Alias == "" {
28                 return NewErrorResponse(ErrNullContractAlias)
29         }
30
31         if ins.Contract == nil {
32                 return NewErrorResponse(ErrNullContract)
33         }
34
35         var hash [32]byte
36         sha3pool.Sum256(hash[:], ins.Contract)
37
38         registerProgram, err := vmutil.RegisterProgram(ins.Contract)
39         if err != nil {
40                 return NewErrorResponse(err)
41         }
42
43         callProgram, err := vmutil.CallContractProgram(hash[:])
44         if err != nil {
45                 return NewErrorResponse(err)
46         }
47
48         c := &contract.Contract{
49                 Hash:            hash[:],
50                 Alias:           ins.Alias,
51                 Contract:        ins.Contract,
52                 CallProgram:     callProgram,
53                 RegisterProgram: registerProgram,
54         }
55         if err := a.wallet.ContractReg.SaveContract(c); err != nil {
56                 return NewErrorResponse(err)
57         }
58
59         return NewSuccessResponse(c)
60 }
61
62 // POST /update-contract-alias
63 func (a *API) updateContractAlias(_ context.Context, ins struct {
64         ID    chainjson.HexBytes `json:"id"`
65         Alias string             `json:"alias"`
66 }) Response {
67         if ins.ID == nil {
68                 return NewErrorResponse(ErrNullContractID)
69         }
70
71         ins.Alias = strings.TrimSpace(ins.Alias)
72         if ins.Alias == "" {
73                 return NewErrorResponse(ErrNullContractAlias)
74         }
75
76         if err := a.wallet.ContractReg.UpdateContract(ins.ID, ins.Alias); err != nil {
77                 return NewErrorResponse(err)
78         }
79
80         return NewSuccessResponse(nil)
81 }
82
83 // POST /get-contract
84 func (a *API) getContract(_ context.Context, ins struct {
85         ID chainjson.HexBytes `json:"id"`
86 }) Response {
87         if ins.ID == nil {
88                 return NewErrorResponse(ErrNullContractID)
89         }
90
91         c, err := a.wallet.ContractReg.GetContract(ins.ID)
92         if err != nil {
93                 return NewErrorResponse(err)
94         }
95
96         return NewSuccessResponse(c)
97 }
98
99 // POST /list-contracts
100 func (a *API) listContracts(_ context.Context) Response {
101         cs, err := a.wallet.ContractReg.ListContracts()
102         if err != nil {
103                 return NewErrorResponse(err)
104         }
105
106         return NewSuccessResponse(cs)
107 }