-
Notifications
You must be signed in to change notification settings - Fork 13
/
mixinnet_crypto.go
147 lines (123 loc) · 2.62 KB
/
mixinnet_crypto.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
package mixin
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"
"golang.org/x/crypto/sha3"
)
const (
Operator0 = 0x00
Operator64 = 0x40
OperatorSum = 0xfe
OperatorCmp = 0xff
)
type (
Script []uint8
Hash [32]byte
TransactionExtra []byte
)
// Script
func NewThresholdScript(threshold uint8) Script {
return Script{OperatorCmp, OperatorSum, threshold}
}
func (s Script) VerifyFormat() error {
if len(s) != 3 {
return fmt.Errorf("invalid script %d", len(s))
}
if s[0] != OperatorCmp || s[1] != OperatorSum {
return fmt.Errorf("invalid script %d %d", s[0], s[1])
}
return nil
}
func (s Script) Validate(sum int) error {
err := s.VerifyFormat()
if err != nil {
return err
}
if sum < int(s[2]) {
return fmt.Errorf("invalid signature keys %d %d", sum, s[2])
}
return nil
}
func (s Script) String() string {
return hex.EncodeToString(s[:])
}
func (s Script) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(s.String())), nil
}
func (s *Script) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(unquoted)
if err != nil {
return err
}
*s = data
return nil
}
// Hash
func NewHash(data []byte) Hash {
return sha3.Sum256(data)
}
func HashFromString(src string) (Hash, error) {
var hash Hash
data, err := hex.DecodeString(src)
if err != nil {
return hash, err
}
if len(data) != len(hash) {
return hash, fmt.Errorf("invalid hash length %d", len(data))
}
copy(hash[:], data)
return hash, nil
}
func (h Hash) HasValue() bool {
zero := Hash{}
return !bytes.Equal(h[:], zero[:])
}
func (h Hash) String() string {
return hex.EncodeToString(h[:])
}
func (h Hash) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(h.String())), nil
}
func (h *Hash) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(unquoted)
if err != nil {
return err
}
if len(data) != len(h) {
return fmt.Errorf("invalid hash length %d", len(data))
}
copy(h[:], data)
return nil
}
// Transaction Extra
func (e TransactionExtra) String() string {
return base64.StdEncoding.EncodeToString(e[:])
}
func (e TransactionExtra) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(e.String())), nil
}
func (e *TransactionExtra) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(unquoted)
if err != nil {
if data, err = base64.StdEncoding.DecodeString(unquoted); err != nil {
return err
}
}
*e = data
return nil
}