/
sign.go
162 lines (114 loc) · 3.08 KB
/
sign.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
package tx
import (
"bytes"
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"io"
"github.com/apisit/rfc6979"
"github.com/inwecrypto/neogo/script"
)
func publicKeyToBytes(pub *ecdsa.PublicKey) (b []byte) {
/* See Certicom SEC1 2.3.3, pg. 10 */
x := pub.X.Bytes()
/* Pad X to 32-bytes */
paddedx := append(bytes.Repeat([]byte{0x00}, 32-len(x)), x...)
/* Add prefix 0x02 or 0x03 depending on ylsb */
if pub.Y.Bit(0) == 0 {
return append([]byte{0x02}, paddedx...)
}
return append([]byte{0x03}, paddedx...)
}
func rfc6979Sign(ecdsaPrivateKey *ecdsa.PrivateKey, data []byte) ([]byte, error) {
digest := sha256.Sum256(data)
r, s, err := rfc6979.SignECDSA(ecdsaPrivateKey, digest[:], sha256.New)
if err != nil {
return nil, err
}
params := ecdsaPrivateKey.Curve.Params()
curveOrderByteSize := params.P.BitLen() / 8
rBytes, sBytes := r.Bytes(), s.Bytes()
signature := make([]byte, curveOrderByteSize*2)
copy(signature[curveOrderByteSize-len(rBytes):], rBytes)
copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes)
return signature, nil
}
// Sign sign transaction
func (tx *Transaction) Sign(ecdsaPrivateKey *ecdsa.PrivateKey) ([]byte, string, error) {
var buff bytes.Buffer
if err := tx.writeSignData(&buff); err != nil {
return nil, "", err
}
tx.SignData = make([]byte, len(buff.Bytes()))
copy(tx.SignData, buff.Bytes())
txid := sha256.Sum256(tx.SignData)
txid = sha256.Sum256(txid[:])
tx.TxID = hex.EncodeToString(reverseBytes(txid[:]))
sign, err := rfc6979Sign(ecdsaPrivateKey, tx.SignData)
if err != nil {
return nil, "", err
}
tx.SignResult = sign
var stackScriptBuffer bytes.Buffer
signScript := script.New("test")
signScript.EmitPushBytes(sign)
signScript.Write(&stackScriptBuffer)
stackScript := stackScriptBuffer.Bytes()
address := publicKeyToBytes(&ecdsaPrivateKey.PublicKey)
signScript.Reset()
signScript.
EmitPushBytes(address).
Emit(script.CHECKSIG, nil)
var redeemScriptBuffer bytes.Buffer
signScript.Write(&redeemScriptBuffer)
tx.Scripts = []*Scripts{
&Scripts{
StackScript: stackScript,
RedeemScript: redeemScriptBuffer.Bytes(),
},
}
var rawTx bytes.Buffer
if err := tx.Write(&rawTx); err != nil {
return nil, "", err
}
return rawTx.Bytes(), tx.TxID, nil
}
func (tx *Transaction) writeSignData(writer io.Writer) error {
_, err := writer.Write([]byte{tx.Type, tx.Version})
if err != nil {
return err
}
if tx.Extend != nil {
if err := tx.Extend.Write(writer); err != nil {
return err
}
}
length := Varint(len(tx.Attributes))
if err := length.Write(writer); err != nil {
return err
}
for _, attr := range tx.Attributes {
if err := attr.Write(writer); err != nil {
return err
}
}
length = Varint(len(tx.Inputs))
if err := length.Write(writer); err != nil {
return err
}
for _, input := range tx.Inputs {
if err := input.Write(writer); err != nil {
return err
}
}
length = Varint(len(tx.Outputs))
if err := length.Write(writer); err != nil {
return err
}
for _, output := range tx.Outputs {
if err := output.Write(writer); err != nil {
return err
}
}
return nil
}