-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathutils.go
More file actions
203 lines (177 loc) · 5.16 KB
/
Copy pathutils.go
File metadata and controls
203 lines (177 loc) · 5.16 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package sdk
import (
"fmt"
"os"
"strings"
"github.com/iost-official/go-iost/v3/account"
"github.com/iost-official/go-iost/v3/common"
"github.com/iost-official/go-iost/v3/crypto"
rpcpb "github.com/iost-official/go-iost/v3/rpc/pb"
jsonpb "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
///////////////////////////// file operation /////////////////////////////
func writeFile(output string, data []byte) error {
f, err := os.Create(output)
if err != nil {
return err
}
_, err = f.Write(data)
defer f.Close()
return err
}
// SaveProtoStructToJSONFile ...
func SaveProtoStructToJSONFile(pb proto.Message, fileName string) error {
r, err := (&jsonpb.MarshalOptions{EmitUnpopulated: true, Indent: " "}).Marshal(pb)
if err != nil {
return err
}
return writeFile(fileName, r)
}
// LoadProtoStructFromJSONFile ...
func LoadProtoStructFromJSONFile(fileName string, pb proto.Message) error {
bytes, err := os.ReadFile(fileName)
if err != nil {
return fmt.Errorf("failed to read file %v: %v", fileName, err)
}
err = jsonpb.Unmarshal(bytes, pb)
if err != nil {
return fmt.Errorf("invalid file %v: %v", fileName, err)
}
return nil
}
// MarshalTextString ...
func MarshalTextString(pb proto.Message) string {
r, err := (&jsonpb.MarshalOptions{EmitUnpopulated: true, Indent: " "}).Marshal(pb)
if err != nil {
return "json.Marshal error: " + err.Error()
}
return string(r)
}
////////////////////////////////// signature related /////////////////////////////////
func toRPCSign(sig *crypto.Signature) *rpcpb.Signature {
return &rpcpb.Signature{
Algorithm: rpcpb.Signature_Algorithm(sig.Algorithm),
Signature: sig.Sig,
PublicKey: sig.Pubkey,
}
}
// GetSignatureOfTx ...
func GetSignatureOfTx(t *rpcpb.TransactionRequest, kp *account.KeyPair, withSign bool) *rpcpb.Signature {
hash := common.Sha3(txToBytes(t, withSign))
sig := toRPCSign(kp.Sign(hash))
return sig
}
// GetSignAlgoByName ...
func GetSignAlgoByName(name string) crypto.Algorithm {
switch name {
case "secp256k1":
return crypto.Secp256k1
case "ed25519":
return crypto.Ed25519
default:
return crypto.Ed25519
}
}
// GetSignAlgoByEnum ...
func GetSignAlgoByEnum(enum rpcpb.Signature_Algorithm) crypto.Algorithm {
switch enum {
case rpcpb.Signature_SECP256K1:
return crypto.Secp256k1
case rpcpb.Signature_ED25519:
return crypto.Ed25519
default:
return crypto.Ed25519
}
}
// CheckPubKey check whether a string is a valid public key. Since it is not easy to check it fully, only check length here
func CheckPubKey(k string) bool {
bytes := common.Base58Decode(k)
return len(bytes) == 32
}
func ParsePrivKey(rawKey string) []byte {
var keyBytes []byte
if strings.HasPrefix(rawKey, "0x") {
keyBytes = common.ParseHex(strings.TrimPrefix(rawKey, "0x"))
} else {
keyBytes = common.Base58Decode(rawKey)
}
return keyBytes
}
// VerifySigForTx ...
func VerifySigForTx(t *rpcpb.TransactionRequest, sig *rpcpb.Signature, withSign bool) bool {
hash := common.Sha3(txToBytes(t, withSign))
return GetSignAlgoByEnum(sig.Algorithm).Verify(hash, sig.PublicKey, sig.Signature)
}
// VerifySignature verify signature of a signed tx
func VerifySignature(tx *rpcpb.TransactionRequest) error {
for _, sig := range tx.Signatures {
if !VerifySigForTx(tx, sig, false) {
return fmt.Errorf("invalid signature %v", tx)
}
}
for _, sig := range tx.PublisherSigs {
if !VerifySigForTx(tx, sig, true) {
return fmt.Errorf("invalid publisher signature %v", tx)
}
}
return nil
}
// NewAction ...
func NewAction(contract string, name string, data string) *rpcpb.Action {
return &rpcpb.Action{
Contract: contract,
ActionName: name,
Data: data,
}
}
/////////////////////////////////// serialize deserialize ///////////////////////////////////////////
func actionToBytes(a *rpcpb.Action) []byte {
se := common.NewSimpleEncoder()
se.WriteString(a.Contract)
se.WriteString(a.ActionName)
se.WriteString(a.Data)
return se.Bytes()
}
func amountToBytes(a *rpcpb.AmountLimit) []byte {
se := common.NewSimpleEncoder()
se.WriteString(a.Token)
se.WriteString(a.Value)
return se.Bytes()
}
func signatureToBytes(s *rpcpb.Signature) []byte {
se := common.NewSimpleEncoder()
se.WriteByte(byte(s.Algorithm))
se.WriteBytes(s.Signature)
se.WriteBytes(s.PublicKey)
return se.Bytes()
}
func txToBytes(t *rpcpb.TransactionRequest, withSign bool) []byte {
se := common.NewSimpleEncoder()
se.WriteInt64(t.Time)
se.WriteInt64(t.Expiration)
se.WriteInt64(int64(t.GasRatio * 100))
se.WriteInt64(int64(t.GasLimit * 100))
se.WriteInt64(t.Delay)
se.WriteInt32(int32(t.ChainId))
se.WriteBytes(nil)
se.WriteStringSlice(t.Signers)
actionBytes := make([][]byte, 0, len(t.Actions))
for _, a := range t.Actions {
actionBytes = append(actionBytes, actionToBytes(a))
}
se.WriteBytesSlice(actionBytes)
amountBytes := make([][]byte, 0, len(t.AmountLimit))
for _, a := range t.AmountLimit {
amountBytes = append(amountBytes, amountToBytes(a))
}
se.WriteBytesSlice(amountBytes)
if withSign {
signBytes := make([][]byte, 0, len(t.Signatures))
for _, sig := range t.Signatures {
signBytes = append(signBytes, signatureToBytes(sig))
}
se.WriteBytesSlice(signBytes)
}
return se.Bytes()
}