forked from dgyoshi/cfd-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend_txout_transaction.go
140 lines (126 loc) · 3.5 KB
/
append_txout_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
package main
import (
"context"
"flag"
"fmt"
cfd "github.com/cryptogarageinc/cfd-go"
)
// AppendTxOutCmd append tx output.
type AppendTxOutCmd struct {
cmd string
flagSet *flag.FlagSet
txFilePath *string
tx *string
isElements *bool
amount *int64
asset *string
address *string
lockingScript *string
isDestroyAmount *bool
isFee *bool
}
// NewAppendTxOutCmd returns a new AppendTxOutCmd struct.
func NewAppendTxOutCmd() *AppendTxOutCmd {
return &AppendTxOutCmd{}
}
// Command returns the command name.
func (cmd *AppendTxOutCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *AppendTxOutCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *AppendTxOutCmd) Init() {
cmd.cmd = "appendtxout"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.txFilePath = cmd.flagSet.String("file", "", "transaction data file path")
cmd.tx = cmd.flagSet.String("tx", "", "transaction in hex format")
cmd.isElements = cmd.flagSet.Bool("elements", false, "elements mode")
cmd.amount = cmd.flagSet.Int64("amount", int64(0), "utxo amount")
cmd.asset = cmd.flagSet.String("asset", "", "utxo asset")
cmd.address = cmd.flagSet.String("address", "", "address or confidential address")
cmd.lockingScript = cmd.flagSet.String("lockingscript", "", "locking script")
cmd.isDestroyAmount = cmd.flagSet.Bool("destroy", false, "destroy amount")
cmd.isFee = cmd.flagSet.Bool("fee", false, "fee output")
}
// GetFlagSet returns the flag set for this command.
func (cmd *AppendTxOutCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *AppendTxOutCmd) Do(ctx context.Context) {
var err error
data := NewTransactionCacheData()
tx := *cmd.tx
if *cmd.tx == "" && *cmd.txFilePath != "" {
data, err = ReadTransactionCache(*cmd.txFilePath)
if err != nil {
fmt.Println(err)
return
}
tx = data.Hex
}
if tx == "" {
fmt.Println("tx is required")
return
}
// other output parameter check
if len(*cmd.asset) > 0 && len(*cmd.asset) != 64 {
fmt.Println("asset size invalid.")
return
}
var handle uintptr
if *cmd.isElements {
handle, err = cfd.CfdGoInitializeConfidentialTransactionByHex(
data.Hex)
} else {
handle, err = cfd.CfdGoInitializeTransactionByHex(data.Hex)
}
if err != nil {
fmt.Println(err)
return
}
defer cfd.CfdGoFreeTransactionHandle(handle)
if *cmd.isElements {
if *cmd.isFee {
err = cfd.CfdGoAddConfidentialTxOutputFee(handle,
*cmd.asset, *cmd.amount)
} else if *cmd.isDestroyAmount {
err = cfd.CfdGoAddConfidentialTxOutputDestroyAmount(handle,
*cmd.asset, *cmd.amount)
} else if len(*cmd.lockingScript) > 0 {
err = cfd.CfdGoAddConfidentialTxOutputByScript(handle,
*cmd.asset, *cmd.amount, *cmd.lockingScript)
} else {
err = cfd.CfdGoAddConfidentialTxOutput(handle,
*cmd.asset, *cmd.amount, *cmd.address)
}
} else {
if len(*cmd.lockingScript) > 0 {
err = cfd.CfdGoAddTxOutputByScript(handle,
*cmd.amount, *cmd.lockingScript)
} else {
err = cfd.CfdGoAddTxOutput(handle, *cmd.amount, *cmd.address)
}
}
if err != nil {
fmt.Println(err)
return
}
txHex, err := cfd.CfdGoFinalizeTransaction(handle)
if err != nil {
fmt.Println(err)
return
}
if *cmd.txFilePath != "" {
data.Hex = txHex
_, err = WriteTransactionCache(*cmd.txFilePath, data)
if err != nil {
fmt.Println(err)
return
}
}
fmt.Printf("append txout:\n%s\n", txHex)
}