forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
89 lines (80 loc) · 2.18 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
package key
import (
"encoding/binary"
"github.com/aergoio/aergo/types"
"github.com/btcsuite/btcd/btcec"
sha256 "github.com/minio/sha256-simd"
)
//Sign return sign with key in the store
func (ks *Store) Sign(addr Address, pass string, hash []byte) ([]byte, error) {
k, err := ks.getKey(addr, pass)
if k == nil {
return nil, err
}
key, _ := btcec.PrivKeyFromBytes(btcec.S256(), k)
sign, err := key.Sign(hash)
if err != nil {
return nil, err
}
return sign.Serialize(), nil
}
func SignTx(tx *types.Tx, key *aergokey) error {
hash := CalculateHashWithoutSign(tx.Body)
sign, err := key.Sign(hash)
if err != nil {
return err
}
tx.Body.Sign = sign.Serialize()
tx.Hash = tx.CalculateTxHash()
return nil
}
//SignTx return transaction which signed with unlocked key. if requester is nil, requester is assumed to tx.Account
func (ks *Store) SignTx(tx *types.Tx, requester []byte) error {
addr := tx.Body.Account
if requester != nil {
addr = requester
}
keyPair, exist := ks.unlocked[types.EncodeAddress(addr)]
if !exist {
return types.ErrShouldUnlockAccount
}
return SignTx(tx, keyPair.key)
}
//VerifyTx return result to varify sign
func VerifyTx(tx *types.Tx) error {
return VerifyTxWithAddress(tx, tx.Body.Account)
}
func VerifyTxWithAddress(tx *types.Tx, address []byte) error {
txBody := tx.Body
hash := CalculateHashWithoutSign(txBody)
sign, err := btcec.ParseSignature(txBody.Sign, btcec.S256())
if err != nil {
return err
}
pubkey, err := btcec.ParsePubKey(address, btcec.S256())
if err != nil {
return err
}
if !sign.Verify(hash, pubkey) {
return types.ErrSignNotMatch
}
return nil
}
//VerifyTx return result to varify sign
func (ks *Store) VerifyTx(tx *types.Tx) error {
return VerifyTx(tx)
}
//CalculateHashWithoutSign return hash of tx without sign field
func CalculateHashWithoutSign(txBody *types.TxBody) []byte {
h := sha256.New()
binary.Write(h, binary.LittleEndian, txBody.Nonce)
h.Write(txBody.Account)
h.Write(txBody.Recipient)
h.Write(txBody.Amount)
h.Write(txBody.Payload)
binary.Write(h, binary.LittleEndian, txBody.GasLimit)
h.Write(txBody.GasPrice)
binary.Write(h, binary.LittleEndian, txBody.Type)
h.Write(txBody.ChainIdHash)
return h.Sum(nil)
}