-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
60 lines (47 loc) · 1.16 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
package transactions
import (
"strings"
t "github.com/Flur3x/go-chain/types"
"github.com/google/uuid"
logging "github.com/op/go-logging"
)
var log = logging.MustGetLogger("")
// New creates a "Transaction" with the given data.
func New(from t.Address, to t.Address, amount uint64, signer t.Signer) (t.Transaction, error) {
var err error
outputs := []t.Output{
t.Output{
Address: to,
Amount: amount,
},
t.Output{
Address: from,
Amount: 0, // TODO - replace with something like "senderWallet.balance - amount"
},
}
tx := t.Transaction{
ID: uuid.New(),
Input: t.Input{
Address: from,
Amount: amount,
},
Outputs: outputs,
Signature: t.Signature{},
}
hash, err := tx.Hash()
if err != nil {
return t.Transaction{}, nil
}
if tx.Signature, err = signer.Sign(hash); err != nil {
return t.Transaction{}, err
}
return tx, nil
}
// JoinTransactionsToString takes a slice of transactions and returns it as a single string.
func JoinTransactionsToString(txs []t.Transaction) string {
var stringSlice []string
for _, tx := range txs {
stringSlice = append(stringSlice, tx.String())
}
return strings.Join(stringSlice, ",")
}