/
aspi.go
181 lines (170 loc) · 4.63 KB
/
aspi.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
package objs
import (
"github.com/MadBase/MadNet/application/objs/aspreimage"
mdefs "github.com/MadBase/MadNet/application/objs/capn"
"github.com/MadBase/MadNet/application/objs/uint256"
"github.com/MadBase/MadNet/crypto"
"github.com/MadBase/MadNet/errorz"
"github.com/MadBase/MadNet/utils"
capnp "zombiezen.com/go/capnproto2"
)
// ASPreImage holds the values required for an AtomicSwap object
type ASPreImage struct {
ChainID uint32
Value *uint256.Uint256
TXOutIdx uint32
IssuedAt uint32
Exp uint32
Owner *AtomicSwapOwner
//
preHash []byte
}
// UnmarshalBinary takes a byte slice and returns the corresponding
// ASPreImage object
func (b *ASPreImage) UnmarshalBinary(data []byte) error {
bc, err := aspreimage.Unmarshal(data)
if err != nil {
return err
}
return b.UnmarshalCapn(bc)
}
// MarshalBinary takes the ASPreImage object and returns the canonical
// byte slice
func (b *ASPreImage) MarshalBinary() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
bc, err := b.MarshalCapn(nil)
if err != nil {
return nil, err
}
return aspreimage.Marshal(bc)
}
// UnmarshalCapn unmarshals the capnproto definition of the object
func (b *ASPreImage) UnmarshalCapn(bc mdefs.ASPreImage) error {
if err := aspreimage.Validate(bc); err != nil {
return err
}
owner := &AtomicSwapOwner{}
if err := owner.UnmarshalBinary(bc.Owner()); err != nil {
return err
}
b.ChainID = bc.ChainID()
u32array := [8]uint32{}
u32array[0] = bc.Value()
u32array[1] = bc.Value1()
u32array[2] = bc.Value2()
u32array[3] = bc.Value3()
u32array[4] = bc.Value4()
u32array[5] = bc.Value5()
u32array[6] = bc.Value6()
u32array[7] = bc.Value7()
vObj := &uint256.Uint256{}
err := vObj.FromUint32Array(u32array)
if err != nil {
return err
}
b.Value = vObj
b.TXOutIdx = bc.TXOutIdx()
b.Owner = owner
b.IssuedAt = bc.IssuedAt()
b.Exp = bc.Exp()
return nil
}
// MarshalCapn marshals the object into its capnproto definition
func (b *ASPreImage) MarshalCapn(seg *capnp.Segment) (mdefs.ASPreImage, error) {
if b == nil {
return mdefs.ASPreImage{}, errorz.ErrInvalid{}.New("not initialized")
}
var bc mdefs.ASPreImage
if seg == nil {
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
return bc, err
}
tmp, err := mdefs.NewRootASPreImage(seg)
if err != nil {
return bc, err
}
bc = tmp
} else {
tmp, err := mdefs.NewASPreImage(seg)
if err != nil {
return bc, err
}
bc = tmp
}
owner, err := b.Owner.MarshalBinary()
if err != nil {
return bc, err
}
if err := bc.SetOwner(owner); err != nil {
return bc, nil
}
bc.SetChainID(b.ChainID)
u32array, err := b.Value.ToUint32Array()
if err != nil {
return bc, err
}
bc.SetValue(u32array[0])
bc.SetValue1(u32array[1])
bc.SetValue2(u32array[2])
bc.SetValue3(u32array[3])
bc.SetValue4(u32array[4])
bc.SetValue5(u32array[5])
bc.SetValue6(u32array[6])
bc.SetValue7(u32array[7])
bc.SetTXOutIdx(b.TXOutIdx)
bc.SetIssuedAt(b.IssuedAt)
bc.SetExp(b.Exp)
return bc, nil
}
// PreHash returns the PreHash of the object
func (b *ASPreImage) PreHash() ([]byte, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
if b.preHash != nil {
return utils.CopySlice(b.preHash), nil
}
msg, err := b.MarshalBinary()
if err != nil {
return nil, err
}
hsh := crypto.Hasher(msg)
b.preHash = hsh
return utils.CopySlice(b.preHash), nil
}
// IsExpired returns true if the current epoch is greater than exp
func (b *ASPreImage) IsExpired(currentHeight uint32) (bool, error) {
if b == nil {
return true, errorz.ErrInvalid{}.New("not initialized")
}
currentEpoch := utils.Epoch(currentHeight)
if currentEpoch >= b.Exp {
return true, nil
}
return false, nil
}
// ValidateSignature validates the signature for ASPreImage
func (b *ASPreImage) ValidateSignature(currentHeight uint32, msg []byte, sig *AtomicSwapSignature) error {
isExpired, err := b.IsExpired(currentHeight)
if err != nil {
return err
}
return b.Owner.ValidateSignature(msg, sig, isExpired)
}
// SignAsPrimary signs the ASPreImage as the primary account owner
func (b *ASPreImage) SignAsPrimary(msg []byte, signer *crypto.Secp256k1Signer, hashKey []byte) (*AtomicSwapSignature, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.Owner.SignAsPrimary(msg, signer, hashKey)
}
// SignAsAlternate signs the ASPreImage as the alternate account owner
func (b *ASPreImage) SignAsAlternate(msg []byte, signer *crypto.Secp256k1Signer, hashKey []byte) (*AtomicSwapSignature, error) {
if b == nil {
return nil, errorz.ErrInvalid{}.New("not initialized")
}
return b.Owner.SignAsAlternate(msg, signer, hashKey)
}