-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_tx.go
94 lines (78 loc) · 2.01 KB
/
move_tx.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
// Copyright (C) 2019-2021, Lux Partners Limited. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"bytes"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/luxdefi/spacesvm/parser"
"github.com/luxdefi/spacesvm/tdata"
)
var _ UnsignedTransaction = &MoveTx{}
type MoveTx struct {
*BaseTx `serialize:"true" json:"baseTx"`
// Space is the namespace for the "SpaceInfo"
// whose owner can write and read value for the
// specific key space.
// The space must be ^[a-z0-9]{1,256}$.
Space string `serialize:"true" json:"space"`
// To is the recipient of the Space.
To common.Address `serialize:"true" json:"to"`
}
func (m *MoveTx) Execute(c *TransactionContext) error {
if err := parser.CheckContents(m.Space); err != nil {
return err
}
// Must transfer to someone
if bytes.Equal(m.To[:], zeroAddress[:]) {
return ErrNonActionable
}
// This prevents someone from transferring a space to themselves.
if bytes.Equal(m.To[:], c.Sender[:]) {
return ErrNonActionable
}
// Veify space is owned by sender
i, err := verifySpace(m.Space, c)
if err != nil {
return err
}
i.Owner = m.To
// Update space
if err := MoveSpaceInfo(c.Database, c.Sender, []byte(m.Space), i); err != nil {
return err
}
return nil
}
func (m *MoveTx) Copy() UnsignedTransaction {
to := make([]byte, common.AddressLength)
copy(to, m.To[:])
return &MoveTx{
BaseTx: m.BaseTx.Copy(),
Space: m.Space,
To: common.BytesToAddress(to),
}
}
func (m *MoveTx) TypedData() *tdata.TypedData {
return tdata.CreateTypedData(
m.Magic, Move,
[]tdata.Type{
{Name: tdSpace, Type: tdString},
{Name: tdTo, Type: tdAddress},
{Name: tdPrice, Type: tdUint64},
{Name: tdBlockID, Type: tdString},
},
tdata.TypedDataMessage{
tdSpace: m.Space,
tdTo: m.To.Hex(),
tdPrice: strconv.FormatUint(m.Price, 10),
tdBlockID: m.BlockID.String(),
},
)
}
func (m *MoveTx) Activity() *Activity {
return &Activity{
Typ: Move,
Space: m.Space,
To: m.To.Hex(),
}
}