forked from echovl/cardano-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cardano_node.go
160 lines (134 loc) · 2.9 KB
/
cardano_node.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
package cardano
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
const (
testnetMagic = 1097911063
)
type cardanoNode interface {
QueryUtxos(Address) ([]Utxo, error)
QueryTip() (NodeTip, error)
SubmitTx(Transaction) error
}
type Utxo struct {
Address Address
TxId TransactionID
Amount uint64
Index uint64
}
type NodeTip struct {
Epoch uint64
Block uint64
Slot uint64
}
type cardanoCli struct {
socketPath string
}
type cardanoCliTip struct {
Epoch uint64
Hash string
Slot uint64
Block uint64
Era string
}
type cardanoCliTx struct {
Type string `json:"type"`
Description string `json:"description"`
CborHex string `json:"cborHex"`
}
func newCli() *cardanoCli {
return &cardanoCli{}
}
//TODO: add ability to use mainnet and testnet
func (cli *cardanoCli) QueryUtxos(address Address) ([]Utxo, error) {
out, err := runCommand("cardano-cli", "query", "utxo", "--address", string(address), "--testnet-magic", "1097911063")
if err != nil {
return nil, err
}
counter := 0
utxos := []Utxo{}
for {
line, err := out.ReadString(byte('\n'))
if err != nil {
break
}
if counter >= 2 {
args := strings.Fields(line)
if len(args) < 4 {
return nil, fmt.Errorf("malformed cli response")
}
txId := TransactionID(args[0])
index, err := ParseUint64(args[1])
if err != nil {
return nil, err
}
amount, err := ParseUint64(args[2])
if err != nil {
return nil, err
}
utxos = append(utxos, Utxo{
TxId: txId,
Index: index,
Amount: amount,
Address: address,
})
}
counter++
}
return utxos, nil
}
//TODO: add ability to use mainnet and testnet
func (cli *cardanoCli) QueryTip() (NodeTip, error) {
out, err := runCommand("cardano-cli", "query", "tip", "--testnet-magic", "1097911063")
if err != nil {
return NodeTip{}, err
}
cliTip := &cardanoCliTip{}
err = json.Unmarshal(out.Bytes(), cliTip)
if err != nil {
return NodeTip{}, err
}
return NodeTip{
Epoch: cliTip.Epoch,
Block: cliTip.Block,
Slot: cliTip.Slot,
}, nil
}
//TODO: add ability to use mainnet and testnet
func (cli *cardanoCli) SubmitTx(tx Transaction) error {
const txFileName = "txsigned.temp"
txPayload := cardanoCliTx{
Type: "Tx MaryEra",
Description: "",
CborHex: tx.CborHex(),
}
txPayloadJson, err := json.Marshal(txPayload)
if err != nil {
return err
}
err = ioutil.WriteFile(txFileName, txPayloadJson, 777)
if err != nil {
return err
}
out, err := runCommand("cardano-cli", "transaction", "submit", "--tx-file", txFileName, "--testnet-magic", "1097911063")
fmt.Print(out.String())
err = os.Remove(txFileName)
return err
}
func runCommand(cmd string, arg ...string) (*bytes.Buffer, error) {
out := &bytes.Buffer{}
command := exec.Command(cmd, arg...)
command.Stdout = out
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
return nil, err
}
return out, nil
}