-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
60 lines (52 loc) · 1.79 KB
/
builder.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
package tx
import (
"encoding/hex"
"strings"
"github.com/GenesisCommunity/go-genesis/packages/consts"
"github.com/GenesisCommunity/go-genesis/packages/converter"
"github.com/GenesisCommunity/go-genesis/packages/crypto"
"github.com/GenesisCommunity/go-genesis/packages/model"
log "github.com/sirupsen/logrus"
"gopkg.in/vmihailenco/msgpack.v2"
)
// BuildTransaction creates transaction
func BuildTransaction(smartTx SmartContract, privKey, pubKey string, params ...string) error {
signPrms := []string{smartTx.ForSign()}
signPrms = append(signPrms, params...)
signature, err := crypto.Sign(
privKey,
strings.Join(signPrms, ","),
)
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Error("signing by node private key")
return err
}
smartTx.BinSignatures = converter.EncodeLengthPlusData(signature)
if smartTx.PublicKey, err = hex.DecodeString(pubKey); err != nil {
log.WithFields(log.Fields{"type": consts.ConversionError, "error": err}).Error("decoding public key from hex")
return err
}
data, err := msgpack.Marshal(smartTx)
if err != nil {
log.WithFields(log.Fields{"type": consts.MarshallingError, "error": err}).Error("marshalling smart contract to msgpack")
return err
}
data = append([]byte{128}, data...)
hash, err := crypto.Hash(data)
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Error("calculating hash of smart contract")
return err
}
tx := &model.Transaction{
Hash: hash,
Data: data[:],
Type: int8(converter.BinToDecBytesShift(&data, 1)),
KeyID: smartTx.KeyID,
HighRate: model.TransactionRateOnBlock,
}
if err = tx.Create(); err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating new transaction")
return err
}
return nil
}