/
wallet_utils.go
148 lines (127 loc) · 3.69 KB
/
wallet_utils.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
/*
Copyright TraceabilityChain Corp. 2018 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package wallet
import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"fmt"
"math/big"
"crypto/sha256"
"github.com/golang/protobuf/proto"
"github.com/traceabilitychain/tacchain/common/crypto"
"github.com/traceabilitychain/tacchain/common/crypto/secp256k1"
"github.com/traceabilitychain/tacchain/common/crypto/sha3"
pb "github.com/traceabilitychain/tacchain/protos/peer"
)
//--------------------------------------
func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
if len(pub) == 0 {
return nil
}
x, y := elliptic.Unmarshal(S256(), pub)
return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}
}
func NewECDSAPrivateKeyFromD(c elliptic.Curve, D *big.Int) *ecdsa.PrivateKey {
priv := new(ecdsa.PrivateKey)
priv.PublicKey.Curve = c
priv.D = D
priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(D.Bytes())
return priv
}
func SignJson(json []byte, priKey string) ([]byte, error) {
hashT := sha256.Sum256(json)
pri, err := HexToECDSA(priKey)
if err != nil {
return nil, nil
}
signature, err := crypto.Sign(hashT[:], pri)
if err != nil {
return nil, nil
}
return signature, nil
}
func GetInvokeHash(chaincodeSpec *pb.ChaincodeSpec, geneAlg string, senderSpec *pb.SenderSpec) ([]byte, error) {
content := &pb.SignContent{ChaincodeSpec: chaincodeSpec, IdGenerationAlg: geneAlg, SenderSpec: senderSpec}
protoBytes, err := proto.Marshal(content)
if err != nil {
return nil, err
}
hashT := sha256.Sum256(protoBytes)
return hashT[:], nil
}
func SignInvoke(chaincodeSpec *pb.ChaincodeSpec, geneAlg string, senderSpec *pb.SenderSpec, priKey string) ([]byte, error) {
content := &pb.SignContent{ChaincodeSpec: chaincodeSpec, IdGenerationAlg: geneAlg, SenderSpec: senderSpec}
protoBytes, err := proto.Marshal(content)
if err != nil {
return nil, err
}
return SignJson(protoBytes, priKey)
}
func GetSenderFromSignature(hashT []byte, signature []byte) (*Address, error) {
pub, err := crypto.Ecrecover(hashT, signature)
if err != nil {
return nil, fmt.Errorf("invalid signature: %v", err)
}
return PubkeyToAddress(*ToECDSAPub(pub)), nil
}
func Keccak256(data ...[]byte) []byte {
d := sha3.NewKeccak256()
for _, b := range data {
d.Write(b)
}
return d.Sum(nil)
}
func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) {
priv := new(ecdsa.PrivateKey)
priv.PublicKey.Curve = S256()
if 8*len(d) != priv.Params().BitSize {
return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize)
}
priv.D = new(big.Int).SetBytes(d)
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
return priv, nil
}
func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
b, err := hex.DecodeString(hexkey)
if err != nil {
return nil, err
}
return ToECDSA(b)
}
func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
if pub == nil || pub.X == nil || pub.Y == nil {
return nil
}
return elliptic.Marshal(S256(), pub.X, pub.Y)
}
func PubkeyToAddress(p ecdsa.PublicKey) *Address {
pubBytes := FromECDSAPub(&p)
return BytesToAddress(Keccak256(pubBytes[1:])[12:])
}
func GetAddressFromPrikey(priKey string) (*Address, error) {
ecdsa_prikey, err := HexToECDSA(priKey)
if err != nil {
return nil, err
}
return PubkeyToAddress(ecdsa_prikey.PublicKey), nil
}
func GetAddressHexFromPrikey(priKey string) (string, error) {
ecdsa_prikey, err := HexToECDSA(priKey)
if err != nil {
return "", err
}
return hex.EncodeToString(PubkeyToAddress(ecdsa_prikey.PublicKey).ToBytes()), nil
}
func HexToAddress(hexKey string) (*Address, error) {
b, err := hex.DecodeString(hexKey)
if err != nil {
return nil, err
}
return BytesToAddress(b), nil
}
func S256() elliptic.Curve {
return secp256k1.S256()
}