-
Notifications
You must be signed in to change notification settings - Fork 3
/
transaction.go
109 lines (75 loc) · 3.73 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
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
// Package transactions outlines helper methods for the SummerCash tx api.
package transactions
import (
"context"
"errors"
"math/big"
summercashAccounts "github.com/SummerCash/go-summercash/accounts"
"github.com/SummerCash/go-summercash/common"
summercashCommon "github.com/SummerCash/go-summercash/common"
"github.com/SummerCash/go-summercash/config"
transactionProto "github.com/SummerCash/go-summercash/intrnl/rpc/proto/transaction"
transactionServer "github.com/SummerCash/go-summercash/intrnl/rpc/transaction"
"github.com/SummerCash/go-summercash/types"
"github.com/SummerCash/go-summercash/validator"
"github.com/SummerCash/summercash-wallet-server/accounts"
)
/* BEGIN EXPORTED METHODS */
// NewTransaction creates, signs, and publishes a new transaction from a given user to a given address.
func NewTransaction(accountsDB *accounts.DB, username string, password string, recipientAddress *common.Address, amount float64, payload []byte) (*types.Transaction, error) {
summercashCommon.DataDir = common.DataDir // Set data dir
account, err := accountsDB.QueryAccountByUsername(username) // Query account
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
if authenticated := accountsDB.Auth(username, password); !authenticated { // Check could not authenticate
return &types.Transaction{}, errors.New("invalid username or password") // Return found error
}
accountChain, err := types.ReadChainFromMemory(account.Address) // Read chain
if err != nil { // Check for errors
accountChain, err = types.NewChain(account.Address) // Initialize chain
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
}
var parentTransaction *types.Transaction // Init parent tx buffer
targetNonce := uint64(0.0) // Init target nonce
if len(accountChain.Transactions) > 0 { // Check has txs
parentTransaction = accountChain.Transactions[len(accountChain.Transactions)-1] // Set parent transaction
targetNonce = accountChain.CalculateTargetNonce() // Set nonce
}
transaction, err := types.NewTransaction(targetNonce, parentTransaction, &account.Address, recipientAddress, big.NewFloat(amount), payload) // Initialize transaction
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
summercashAccount, err := summercashAccounts.ReadAccountFromMemory(account.Address) // Read account
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
config, err := config.ReadChainConfigFromMemory() // Read config from memory
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
validator := validator.Validator(validator.NewStandardValidator(config)) // Initialize validator
err = types.SignTransaction(transaction, summercashAccount.PrivateKey) // Sign transaction
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
err = validator.ValidateTransaction(transaction) // Validate transaction
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
err = transaction.WriteToMemory() // Write tx to mempool
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
rpcServer := new(transactionServer.Server) // Initialize mock RPC server
publishCtx, cancel := context.WithCancel(context.Background()) // Get ctx
defer cancel() // Cancel
_, err = rpcServer.Publish(publishCtx, &transactionProto.GeneralRequest{Address: transaction.Hash.String()}) // Publish
if err != nil { // Check for errors
return &types.Transaction{}, err // Return found error
}
return transaction, nil // Return tx
}
/* END EXPORTED METHODS */