-
Notifications
You must be signed in to change notification settings - Fork 6
/
hashlock.go
executable file
·181 lines (158 loc) · 4.32 KB
/
hashlock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
"encoding/json"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types"
)
var (
hlog = log.New("module", "exectype.hashlock")
)
func init() {
types.AllowUserExec = append(types.AllowUserExec, []byte(HashlockX))
types.RegistorExecutor(HashlockX, NewType())
types.RegisterDappFork(HashlockX, "Enable", 0)
}
// HashlockType def
type HashlockType struct {
types.ExecTypeBase
}
// NewType method
func NewType() *HashlockType {
c := &HashlockType{}
c.SetChild(c)
return c
}
// GetName 获取执行器名称
func (hashlock *HashlockType) GetName() string {
return HashlockX
}
// GetPayload method
func (hashlock *HashlockType) GetPayload() types.Message {
return &HashlockAction{}
}
// CreateTx method
func (hashlock *HashlockType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
hlog.Debug("hashlock.CreateTx", "action", action)
if action == "HashlockLock" {
var param HashlockLockTx
err := json.Unmarshal(message, ¶m)
if err != nil {
hlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawHashlockLockTx(¶m)
} else if action == "HashlockUnlock" {
var param HashlockUnlockTx
err := json.Unmarshal(message, ¶m)
if err != nil {
hlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawHashlockUnlockTx(¶m)
} else if action == "HashlockSend" {
var param HashlockSendTx
err := json.Unmarshal(message, ¶m)
if err != nil {
hlog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawHashlockSendTx(¶m)
}
return nil, types.ErrNotSupport
}
// GetTypeMap method
func (hashlock *HashlockType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Hlock": HashlockActionLock,
"Hsend": HashlockActionSend,
"Hunlock": HashlockActionUnlock,
}
}
// GetLogMap method
func (hashlock *HashlockType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{}
}
// CreateRawHashlockLockTx method
func CreateRawHashlockLockTx(parm *HashlockLockTx) (*types.Transaction, error) {
if parm == nil {
hlog.Error("CreateRawHashlockLockTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &HashlockLock{
Amount: parm.Amount,
Time: parm.Time,
Hash: common.Sha256([]byte(parm.Secret)),
ToAddress: parm.ToAddr,
ReturnAddress: parm.ReturnAddr,
}
lock := &HashlockAction{
Ty: HashlockActionLock,
Value: &HashlockAction_Hlock{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(HashlockX)),
Payload: types.Encode(lock),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(HashlockX)),
}
tx, err := types.FormatTx(types.ExecName(HashlockX), tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawHashlockUnlockTx method
func CreateRawHashlockUnlockTx(parm *HashlockUnlockTx) (*types.Transaction, error) {
if parm == nil {
hlog.Error("CreateRawHashlockUnlockTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &HashlockUnlock{
Secret: []byte(parm.Secret),
}
unlock := &HashlockAction{
Ty: HashlockActionUnlock,
Value: &HashlockAction_Hunlock{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(HashlockX)),
Payload: types.Encode(unlock),
Fee: parm.Fee,
To: address.ExecAddress(HashlockX),
}
tx, err := types.FormatTx(types.ExecName(HashlockX), tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawHashlockSendTx method
func CreateRawHashlockSendTx(parm *HashlockSendTx) (*types.Transaction, error) {
if parm == nil {
hlog.Error("CreateRawHashlockSendTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &HashlockSend{
Secret: []byte(parm.Secret),
}
send := &HashlockAction{
Ty: HashlockActionSend,
Value: &HashlockAction_Hsend{v},
}
tx := &types.Transaction{
Execer: []byte(HashlockX),
Payload: types.Encode(send),
Fee: parm.Fee,
To: address.ExecAddress(HashlockX),
}
tx, err := types.FormatTx(types.ExecName(HashlockX), tx)
if err != nil {
return nil, err
}
return tx, nil
}