-
Notifications
You must be signed in to change notification settings - Fork 11
/
action.go
102 lines (92 loc) 路 2.4 KB
/
action.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
package main
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strings"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/logger"
"github.com/MixinNetwork/trusted-group/mtg"
"github.com/gofrs/uuid/v5"
)
type Action struct {
Destination string `json:"destination,omitempty"`
Tag string `json:"tag,omitempty"`
Receivers []string `json:"receivers,omitempty"`
Threshold int64 `json:"threshold,omitempty"`
Extra string `json:"extra,omitempty"`
}
func decryptData(data string, collectible bool) ([]byte, error) {
mep := mtg.DecodeMixinExtra(data)
if mep == nil {
return nil, fmt.Errorf("invalid mixin extra pack %s", data)
}
if collectible {
return []byte(mep.M), nil
}
b, err := base64.RawURLEncoding.Strict().DecodeString(mep.M)
if err != nil {
return nil, err
}
// TODO encryption and decryption
return b, nil
}
func (p *Proxy) decodeAction(u *User, memo, assetId string, collectible bool) (*Action, error) {
b, err := decryptData(memo, collectible)
logger.Verbosef("Proxy.decryptData(%s) => %x %v", memo, b, err)
if err != nil || len(b) != 68 {
return nil, nil
}
if uuid.FromBytesOrNil(b[:16]).String() != MVMRegistryId {
return nil, nil
}
if hex.EncodeToString(b[16:36]) != strings.ToLower(MVMStorageContract[2:]) {
return nil, nil
}
k := new(big.Int).SetBytes(b[36:])
val, err := p.storage.Read(nil, k)
if err != nil {
return nil, err
}
logger.Verbosef("Proxy.storage.Read(%x) => %x %v", k.Bytes(), val, err)
var act Action
err = json.Unmarshal(val, &act)
if err != nil {
return nil, nil
}
logger.Verbosef("Proxy.decodeAction(%v, %s, %v) => %v %v", u, memo, collectible, act, err)
if act.Destination != "" && collectible {
return nil, nil
}
if act.Destination != "" {
asset, err := store.readAsset(assetId)
if err != nil {
panic(err)
}
chainId := crypto.NewHash([]byte(asset.ChainID))
if verifyDestination(chainId, act.Destination) != nil {
return nil, nil
}
if len(act.Receivers) > 0 || act.Threshold != 0 {
return nil, nil
}
return &act, nil
}
if len(act.Receivers) > 0 {
if len(act.Receivers) > 7 {
return nil, nil
}
if act.Threshold <= 0 || act.Threshold > int64(len(act.Receivers)) {
return nil, nil
}
for _, r := range act.Receivers {
if id, _ := uuid.FromString(r); id.String() != r {
return nil, nil
}
}
return &act, nil
}
return nil, nil
}