-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigma.go
291 lines (251 loc) · 6.77 KB
/
sigma.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package sigma
import (
"encoding/binary"
"fmt"
"strconv"
bsm "github.com/bitcoin-sv/go-sdk/compat/bsm"
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
hash "github.com/bitcoin-sv/go-sdk/primitives/hash"
"github.com/bitcoin-sv/go-sdk/script"
"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoin-sv/go-sdk/util"
)
type Algorithm string
const Prefix = "SIGMA"
// const sigmaHex = "5349474d41"
const (
BSM Algorithm = "BSM"
)
type AuthToken struct {
Type string
Value string
Key string
}
type RemoteSigningResponse struct {
Address string
Sig string
Message string
Ts int
}
type Sig struct {
Address string
Signature []byte
Algorithm Algorithm
Vin int
TargetVout int
}
type SignResponse struct {
Sig
SigmaScript *script.Script
SignedTx *transaction.Transaction
}
type Sigma struct {
inputHash []byte
dataHash []byte
transaction *transaction.Transaction
sigmaInstance int
refVin int
targetVout int
sig Sig
}
func NewSigma(transaction transaction.Transaction, targetVout, sigmaInstance, refVin int) *Sigma {
return &Sigma{
transaction: &transaction,
targetVout: targetVout,
sigmaInstance: sigmaInstance,
refVin: refVin,
}
}
func (s *Sigma) SetHashes() {
s.inputHash = s.GetInputHash()
s.dataHash = s.getDataHash()
}
func (s *Sigma) SetTargetVout(targetVout int) {
s.targetVout = targetVout
}
func (s *Sigma) SetSigmaInstance(sigmaInstance int) {
s.sigmaInstance = sigmaInstance
}
func (s *Sigma) GetMessageHash() []byte {
combinedBytes := append(s.inputHash, s.dataHash...)
return hash.Sha256d(combinedBytes)
}
func (s *Sigma) Sign(privateKey *ec.PrivateKey) *SignResponse {
// Get the message hash to sign
s.SetHashes()
messageHash := s.GetMessageHash()
signature, err := bsm.SignMessage(privateKey, messageHash)
if err != nil {
fmt.Printf("error signing message: %v\n", err)
return nil
}
// Get the address from the public key
add, err := script.NewAddressFromPublicKey(privateKey.PubKey(), true)
if err != nil {
fmt.Printf("error getting address from public key: %v\n", err)
return nil
}
vin := s.refVin
if vin == -1 {
vin = s.targetVout
}
output := s.transaction.Outputs[s.targetVout]
hasOpReturn := false
for pos := 0; pos < len(*output.LockingScript); {
if op, err := output.LockingScript.ReadOp(&pos); err != nil {
fmt.Printf("error reading op: %v\n", err)
return nil
} else if op.Op == script.OpRETURN {
hasOpReturn = true
break
}
}
if hasOpReturn {
output.LockingScript.AppendPushData([]byte{'|'})
} else {
output.LockingScript.AppendOpcodes(script.OpRETURN)
}
output.LockingScript.AppendPushData([]byte(Prefix))
output.LockingScript.AppendPushData([]byte(BSM))
output.LockingScript.AppendPushData([]byte(add.AddressString))
output.LockingScript.AppendPushData(signature)
output.LockingScript.AppendPushData([]byte(strconv.Itoa(vin)))
s.sig = Sig{
Algorithm: BSM,
Address: add.AddressString,
Signature: signature,
Vin: vin,
TargetVout: s.targetVout,
}
return &SignResponse{
SigmaScript: output.LockingScript,
SignedTx: s.transaction,
Sig: s.sig,
}
}
func (s *Sigma) RemoteSign(keyHost string, authToken *AuthToken) (SignResponse, error) {
// implementation needed
return SignResponse{}, nil
}
// GetSig gets the target instance and returns its values
func (s *Sigma) GetSig(scr *script.Script) (*Sig, error) {
occurrences := 0
for pos := 0; pos < len(*scr); {
if op, err := scr.ReadOp(&pos); err != nil {
return nil, err
} else if op.Op == script.OpRETURN || (op.Op == script.OpDATA1 && op.Data[0] == '|') {
if op, err = scr.ReadOp(&pos); err != nil {
return nil, err
}
if op.Op == script.OpDATA5 && string(op.Data) == Prefix {
if occurrences == s.sigmaInstance {
if algoOp, err := scr.ReadOp(&pos); err != nil {
return nil, err
} else if addOp, err := scr.ReadOp(&pos); err != nil {
return nil, err
} else if sigOp, err := scr.ReadOp(&pos); err != nil {
return nil, err
} else if vinOp, err := scr.ReadOp(&pos); err != nil {
return nil, err
} else if vin, err := strconv.Atoi(string(vinOp.Data)); err != nil {
return nil, err
} else {
return &Sig{
Algorithm: Algorithm(string(algoOp.Data)),
Address: string(addOp.Data),
Signature: sigOp.Data,
Vin: vin,
}, nil
}
}
occurrences++
}
}
}
return nil, fmt.Errorf("no signature found")
}
func (s *Sigma) Verify() bool {
script := s.transaction.Outputs[s.targetVout].LockingScript
sig, err := s.GetSig(script)
if err != nil {
fmt.Println("Error parsing script:", err)
return false
}
s.sig = *sig
err = bsm.VerifyMessage(s.sig.Address, s.sig.Signature, s.GetMessageHash())
if err != nil {
fmt.Printf("Error verifying signature: %v\n", err)
return false
}
return true
}
func (s *Sigma) GetInputHash() []byte {
vin := s.refVin
if vin == -1 {
vin = s.targetVout
}
txIn := s.transaction.Inputs[vin]
if txIn != nil {
indexBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(indexBytes, uint32(txIn.SourceTxOutIndex))
outpointBytes := append(util.ReverseBytes(txIn.SourceTXID.CloneBytes()), indexBytes...)
return hash.Sha256(outpointBytes)
}
// return dummy hash or handle error
return hash.Sha256(make([]byte, 32))
}
func (s *Sigma) getDataHash() []byte {
if s.transaction == nil {
fmt.Println("error getting transaction")
return nil
}
output := s.getTargetTxOut()
if output == nil {
fmt.Println("error getting target tx out")
return nil
}
occurrences := 0
prevPos := 0
for pos := 0; pos < len(*output.LockingScript); {
op, err := output.LockingScript.ReadOp(&pos)
if err != nil {
break
}
if op.Op == script.OpRETURN || (op.Op == script.OpDATA1 && op.Data[0] == '|') {
if op, err := output.LockingScript.ReadOp(&pos); err != nil {
break
} else if op.Op == script.OpDATA5 && string(op.Data) == Prefix {
if occurrences == s.sigmaInstance {
// The -1 accounts for either the OP_RETURN or "|" separator which is not signed
return hash.Sha256((*output.LockingScript)[:prevPos])
}
occurrences++
}
}
prevPos = pos
}
// If no endIndex found, return the hash for the entire script
return hash.Sha256(*output.LockingScript)
}
func (s *Sigma) getTargetTxOut() *transaction.TransactionOutput {
if s.transaction == nil {
return nil
}
return s.transaction.Outputs[s.targetVout]
}
func (s *Sigma) GetSigInstanceCount() int {
output := s.getTargetTxOut()
if output == nil {
// handle error or return 0 if appropriate
return 0
}
count := 0
if scriptChunks, err := script.DecodeScript(*output.LockingScript); err == nil {
for _, chunk := range scriptChunks {
if chunk.Op == script.OpDATA5 && string(chunk.Data) == Prefix {
count++
}
}
}
return count
}