-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitialize_transaction.go
182 lines (160 loc) · 4.47 KB
/
initialize_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
cfd "github.com/cryptogarageinc/cfd-go"
)
// UtxoData utxo data mapping.
type UtxoData struct {
Txid string `json:"txid"`
Vout uint32 `json:"vout"`
Amount int64 `json:"amount"`
Asset string `json:"asset"`
AssetBlinder string `json:"assetblinder"`
AssetCommitment string `json:"assetcommitment"`
AmountBlinder string `json:"blinder"`
AmountCommitment string `json:"amountcommitment"`
Descriptor string `json:"descriptor"`
ScriptsigTemplate string `json:"scriptsigTemplate"`
}
// TransactionCacheData transaction cache data mapping.
type TransactionCacheData struct {
Hex string `json:"hex"`
Utxos []UtxoData `json:"utxos"`
}
// NewTransactionCacheData returns a new TransactionCacheData struct.
func NewTransactionCacheData() *TransactionCacheData {
return &TransactionCacheData{}
}
// InitializeTransactionCmd initialize transaction hex.
type InitializeTransactionCmd struct {
cmd string
flagSet *flag.FlagSet
version *uint
locktime *uint
txFilePath *string
isElements *bool
}
// NewInitializeTransactionCmd returns a new InitializeTransactionCmd struct.
func NewInitializeTransactionCmd() *InitializeTransactionCmd {
return &InitializeTransactionCmd{}
}
// Command returns the command name.
func (cmd *InitializeTransactionCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *InitializeTransactionCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *InitializeTransactionCmd) Init() {
cmd.cmd = "initializetransaction"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.version = cmd.flagSet.Uint("version", uint(2), "tx version")
cmd.locktime = cmd.flagSet.Uint("locktime", uint(0), "locktime")
cmd.txFilePath = cmd.flagSet.String("file", "", "transaction data file path")
cmd.isElements = cmd.flagSet.Bool("elements", false, "elements mode")
}
// GetFlagSet returns the flag set for this command.
func (cmd *InitializeTransactionCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *InitializeTransactionCmd) Do(ctx context.Context) {
var tx string
var err error
var handle uintptr
if *cmd.isElements {
handle, err = cfd.CfdGoInitializeConfidentialTransaction(
uint32(*cmd.version),
uint32(*cmd.locktime))
if err == nil {
defer cfd.CfdGoFreeTransactionHandle(handle)
tx, err = cfd.CfdGoFinalizeTransaction(handle)
}
} else {
handle, err = cfd.CfdGoInitializeTransaction(
uint32(*cmd.version),
uint32(*cmd.locktime))
if err == nil {
defer cfd.CfdGoFreeTransactionHandle(handle)
tx, err = cfd.CfdGoFinalizeTransaction(handle)
}
}
if err != nil {
fmt.Println(err)
return
}
if *cmd.txFilePath == "" {
fmt.Printf("initialize transaction: %s\n", tx)
} else {
data := NewTransactionCacheData()
data.Hex = tx
_, err = os.Stat(*cmd.txFilePath)
if err == nil {
if err = os.Remove(*cmd.txFilePath); err != nil {
fmt.Println(err)
return
}
}
jsonData, err := json.Marshal(*data)
if err != nil {
fmt.Println(err)
return
}
var buf bytes.Buffer
err = json.Indent(&buf, jsonData, "", " ")
if err != nil {
fmt.Println(err)
return
}
indentJSON := buf.String()
err = ioutil.WriteFile(*cmd.txFilePath, []byte(indentJSON), 666)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("initialize transaction:\n%s\n", indentJSON)
}
}
// WriteTransactionCache write jsondata to file.
func WriteTransactionCache(path string, cache *TransactionCacheData) (jsonString string, err error) {
if cache == nil {
return "", errors.New("cahce is null")
}
jsonData, err := json.Marshal(*cache)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = json.Indent(&buf, jsonData, "", " ")
if err != nil {
return "", err
}
indentJSON := buf.String()
err = ioutil.WriteFile(path, []byte(indentJSON), 666)
return indentJSON, err
}
// ReadTransactionCache read jsondata from file.
func ReadTransactionCache(path string) (cache *TransactionCacheData, err error) {
_, err = os.Stat(path)
if err != nil {
return nil, errors.New("tx data file not found")
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var data TransactionCacheData
jsonString := strings.TrimSpace(string(bytes))
err = json.Unmarshal([]byte(jsonString), &data)
return &data, err
}