-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
118 lines (104 loc) · 2.82 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
110
111
112
113
114
115
116
117
118
//Date: 2017 Q4
//Email: ali.mashatan@gmail.com
//Author: Ali Mashatan
package GoBigChainDBDriver
import (
"bytes"
"encoding/hex"
"encoding/json"
)
const (
CREATE = "CREATE"
GENESIS = "GENSIS"
TRANSFER = "TRANSFER"
VERSION = "1.0"
)
type transaction struct {
id string
asset JsonObj
assetId string
input input
output output
metadata JsonObj
operation string
version string
}
func NewCreateTransaction(asset JsonObj, metadata JsonObj) transaction {
trasaction := transaction{}
trasaction.operation = CREATE
trasaction.version = VERSION
trasaction.asset = JsonObj{"data": asset}
trasaction.assetId = ""
trasaction.metadata = metadata
trasaction.input = input{}
trasaction.output = output{}
return trasaction
}
func NewTransferTransaction(assetId string, metadata JsonObj) transaction {
trasaction := transaction{}
trasaction.operation = TRANSFER
trasaction.version = VERSION
trasaction.asset = JsonObj{"id": assetId}
trasaction.assetId = assetId
trasaction.metadata = metadata
trasaction.input = input{}
trasaction.output = output{}
return trasaction
}
func (t *transaction) AddOwnerBefore(publicKey *[]PublicKey, privateKey *[]PrivateKey, fulfill *JsonObj) error {
t.input.Add(publicKey, privateKey, fulfill)
return nil
}
func (t *transaction) AddOwnerAfter(publicKey *[]PublicKey, amount int) error {
t.output.Add(publicKey, amount)
return nil
}
func (t *transaction) Generate(withId bool, removeNull bool) (JsonObj, error) {
tx := JsonObj{}
if withId {
tx["id"] = t.id
}
if len(t.asset) != 0 {
tx["asset"] = t.asset
}
inputs := t.input.Generate(removeNull)
if len(inputs) != 0 {
tx["inputs"] = inputs
}
if len(t.metadata) != 0 {
tx["metadata"] = t.metadata
}
tx["operation"] = t.operation
outputs := t.output.Generate(removeNull)
if len(outputs) != 0 {
tx["outputs"] = outputs
}
tx["version"] = VERSION
return tx, nil
}
func (t *transaction) dump() []byte {
jo, _ := t.Generate(true, false)
b, _ := json.Marshal(jo)
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
return b
}
func (t *transaction) createId() string {
jo, _ := t.Generate(false, true)
b, _ := json.Marshal(jo)
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
println("*\nTX Before ID: ", string(b), "\n*\n")
return hex.EncodeToString(HashData(b))
}
func (t *transaction) Sign() error {
dm := t.dump()
t.output.Sign(dm)
t.id = t.createId()
dm = t.dump()
println("*\nTX Before Sign: ", string(dm), "\n*\n")
t.input.Sign(dm)
return nil
}