-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
64 lines (50 loc) · 1.54 KB
/
transaction.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
package tronWallet
import (
"crypto/ecdsa"
"crypto/sha256"
"fmt"
"github.com/Amirilidan78/tron-wallet/enums"
"github.com/Amirilidan78/tron-wallet/grpcClient"
"github.com/Amirilidan78/tron-wallet/grpcClient/proto/api"
"github.com/ethereum/go-ethereum/crypto"
)
import (
"errors"
"github.com/golang/protobuf/proto"
)
func createTransactionInput(node enums.Node, fromAddressBase58 string, toAddressBase58 string, amountInSun int64) (*api.TransactionExtention, error) {
c, err := grpcClient.GetGrpcClient(node)
if err != nil {
return nil, err
}
return c.Transfer(fromAddressBase58, toAddressBase58, amountInSun)
}
func signTransaction(transaction *api.TransactionExtention, privateKey *ecdsa.PrivateKey) (*api.TransactionExtention, error) {
rawData, err := proto.Marshal(transaction.Transaction.GetRawData())
if err != nil {
return transaction, fmt.Errorf("proto marshal tx raw data error: %v", err)
}
h256h := sha256.New()
h256h.Write(rawData)
hash := h256h.Sum(nil)
signature, err := crypto.Sign(hash, privateKey)
if err != nil {
return transaction, fmt.Errorf("sign error: %v", err)
}
transaction.Transaction.Signature = append(transaction.Transaction.Signature, signature)
return transaction, nil
}
func broadcastTransaction(node enums.Node, transaction *api.TransactionExtention) error {
c, err := grpcClient.GetGrpcClient(node)
if err != nil {
return err
}
res, err := c.Broadcast(transaction.Transaction)
if err != nil {
return err
}
if res.Result != true {
return errors.New(res.Code.String())
}
return nil
}