/
keystore.go
48 lines (38 loc) · 994 Bytes
/
keystore.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
package keystore
import (
"crypto/ecdsa"
"crypto/sha256"
"github.com/craftto/go-tron/pkg/address"
"github.com/craftto/go-tron/pkg/proto/core"
"github.com/ethereum/go-ethereum/crypto"
"google.golang.org/protobuf/proto"
)
type Keystore struct {
Address address.Address
privateKey ecdsa.PrivateKey
}
func ImportFromPrivateKey(privateKey string) (*Keystore, error) {
privateKeyECDSA, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
return &Keystore{
Address: address.PubkeyToAddress(privateKeyECDSA.PublicKey),
privateKey: *privateKeyECDSA,
}, nil
}
func (ks *Keystore) SignTx(tx *core.Transaction) (*core.Transaction, error) {
rawData, err := proto.Marshal(tx.GetRawData())
if err != nil {
return nil, err
}
h256h := sha256.New()
h256h.Write(rawData)
hash := h256h.Sum(nil)
signature, err := crypto.Sign(hash, &ks.privateKey)
if err != nil {
return nil, err
}
tx.Signature = append(tx.Signature, signature)
return tx, nil
}