|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/blinklabs-io/gouroboros/ledger/common" |
| 8 | + "github.com/urfave/cli/v2" |
| 9 | + |
| 10 | + "github.com/functionally/nacatgunma/cardano" |
| 11 | +) |
| 12 | + |
| 13 | +func CardanoCmds() *cli.Command { |
| 14 | + return &cli.Command{ |
| 15 | + Name: "cardano", |
| 16 | + Usage: "Interact with Cardano", |
| 17 | + Subcommands: []*cli.Command{ |
| 18 | + cardanoDatumCmd(), |
| 19 | + cardanoInputsCmd(), |
| 20 | + cardanoMetadataCmd(), |
| 21 | + cardanoRedeemerCmd(), |
| 22 | + cardanoTipsCmd(), |
| 23 | + }, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func cardanoDatumCmd() *cli.Command { |
| 28 | + |
| 29 | + var headerCid string |
| 30 | + var script bool |
| 31 | + var credential string |
| 32 | + var datumFile string |
| 33 | + |
| 34 | + return &cli.Command{ |
| 35 | + Name: "datum", |
| 36 | + Usage: "Create datum for a tip.", |
| 37 | + Flags: []cli.Flag{ |
| 38 | + &cli.BoolFlag{ |
| 39 | + Name: "script", |
| 40 | + Required: true, |
| 41 | + Usage: "Whether the credential is a script instead of a public key", |
| 42 | + Destination: &script, |
| 43 | + }, |
| 44 | + &cli.StringFlag{ |
| 45 | + Name: "credential-hash", |
| 46 | + Required: true, |
| 47 | + Usage: "Blake2b224 hash of the credential, in hexadecimal", |
| 48 | + Destination: &credential, |
| 49 | + }, |
| 50 | + &cli.StringFlag{ |
| 51 | + Name: "header-cid", |
| 52 | + Required: true, |
| 53 | + Usage: "The CID of the block header", |
| 54 | + Destination: &headerCid, |
| 55 | + }, |
| 56 | + &cli.StringFlag{ |
| 57 | + Name: "datum-file", |
| 58 | + Value: "/dev/stdout", |
| 59 | + Usage: "Output file for JSON-formatted datum", |
| 60 | + Destination: &datumFile, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Action: func(*cli.Context) error { |
| 64 | + datum, err := cardano.NewDatum(script, credential, headerCid) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + datumBytes, err := datum.ToJSON() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + return os.WriteFile(datumFile, datumBytes, 0644) |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +func cardanoInputsCmd() *cli.Command { |
| 79 | + |
| 80 | + var headerCid string |
| 81 | + var script bool |
| 82 | + var credential string |
| 83 | + var datumFile string |
| 84 | + var metadataKey uint |
| 85 | + var redeemerFile string |
| 86 | + var blockchain string |
| 87 | + var metadataFile string |
| 88 | + |
| 89 | + return &cli.Command{ |
| 90 | + Name: "inputs", |
| 91 | + Usage: "Create inputs for a tip.", |
| 92 | + Flags: []cli.Flag{ |
| 93 | + &cli.UintFlag{ |
| 94 | + Name: "metadata-key", |
| 95 | + Value: 58312, |
| 96 | + Usage: "Metadata key for the chain", |
| 97 | + Destination: &metadataKey, |
| 98 | + }, |
| 99 | + &cli.StringFlag{ |
| 100 | + Name: "blockchain", |
| 101 | + Value: "https://github.com/functionally/nacatgunma", |
| 102 | + Usage: "The IRI identifying the blockchain", |
| 103 | + Destination: &blockchain, |
| 104 | + }, |
| 105 | + &cli.BoolFlag{ |
| 106 | + Name: "script", |
| 107 | + Required: true, |
| 108 | + Usage: "Whether the credential is a script instead of a public key", |
| 109 | + Destination: &script, |
| 110 | + }, |
| 111 | + &cli.StringFlag{ |
| 112 | + Name: "credential-hash", |
| 113 | + Required: true, |
| 114 | + Usage: "Blake2b224 hash of the credential, in hexadecimal", |
| 115 | + Destination: &credential, |
| 116 | + }, |
| 117 | + &cli.StringFlag{ |
| 118 | + Name: "header-cid", |
| 119 | + Required: true, |
| 120 | + Usage: "The CID of the block header", |
| 121 | + Destination: &headerCid, |
| 122 | + }, |
| 123 | + &cli.StringFlag{ |
| 124 | + Name: "datum-file", |
| 125 | + Required: true, |
| 126 | + Usage: "Output file for JSON-formatted datum", |
| 127 | + Destination: &datumFile, |
| 128 | + }, |
| 129 | + &cli.StringFlag{ |
| 130 | + Name: "redeemer-file", |
| 131 | + Required: true, |
| 132 | + Usage: "Output file for JSON-formatted redeemer", |
| 133 | + Destination: &redeemerFile, |
| 134 | + }, |
| 135 | + &cli.StringFlag{ |
| 136 | + Name: "metadata-file", |
| 137 | + Required: true, |
| 138 | + Usage: "Output file for JSON-formatted metadata", |
| 139 | + Destination: &metadataFile, |
| 140 | + }, |
| 141 | + }, |
| 142 | + Action: func(*cli.Context) error { |
| 143 | + datum, err := cardano.NewDatum(script, credential, headerCid) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + datumBytes, err := datum.ToJSON() |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + err = os.WriteFile(datumFile, datumBytes, 0644) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + redeemerBytes, err := cardano.RedeemerJSON(metadataKey) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + err = os.WriteFile(redeemerFile, redeemerBytes, 0644) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + metadataBytes, err := cardano.MetadataJSON(metadataKey, blockchain, headerCid) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + return os.WriteFile(metadataFile, metadataBytes, 0644) |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | +} |
| 172 | + |
| 173 | +func cardanoMetadataCmd() *cli.Command { |
| 174 | + |
| 175 | + var headerCid string |
| 176 | + var metadataKey uint |
| 177 | + var blockchain string |
| 178 | + var metadataFile string |
| 179 | + |
| 180 | + return &cli.Command{ |
| 181 | + Name: "metadata", |
| 182 | + Usage: "Create metadata for a tip.", |
| 183 | + Flags: []cli.Flag{ |
| 184 | + &cli.UintFlag{ |
| 185 | + Name: "metadata-key", |
| 186 | + Value: 58312, |
| 187 | + Usage: "Metadata key for the chain", |
| 188 | + Destination: &metadataKey, |
| 189 | + }, |
| 190 | + &cli.StringFlag{ |
| 191 | + Name: "blockchain", |
| 192 | + Value: "https://github.com/functionally/nacatgunma", |
| 193 | + Usage: "The IRI identifying the blockchain", |
| 194 | + Destination: &blockchain, |
| 195 | + }, |
| 196 | + &cli.StringFlag{ |
| 197 | + Name: "header-cid", |
| 198 | + Required: true, |
| 199 | + Usage: "The CID of the block header", |
| 200 | + Destination: &headerCid, |
| 201 | + }, |
| 202 | + &cli.StringFlag{ |
| 203 | + Name: "metadata-file", |
| 204 | + Value: "/dev/stdout", |
| 205 | + Usage: "Output file for JSON-formatted metadata", |
| 206 | + Destination: &metadataFile, |
| 207 | + }, |
| 208 | + }, |
| 209 | + Action: func(*cli.Context) error { |
| 210 | + metadataBytes, err := cardano.MetadataJSON(metadataKey, blockchain, headerCid) |
| 211 | + if err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + return os.WriteFile(metadataFile, metadataBytes, 0644) |
| 215 | + }, |
| 216 | + } |
| 217 | + |
| 218 | +} |
| 219 | + |
| 220 | +func cardanoRedeemerCmd() *cli.Command { |
| 221 | + |
| 222 | + var metadataKey uint |
| 223 | + var redeemerFile string |
| 224 | + |
| 225 | + return &cli.Command{ |
| 226 | + Name: "redeemer", |
| 227 | + Usage: "Create redeemer for a tip.", |
| 228 | + Flags: []cli.Flag{ |
| 229 | + &cli.UintFlag{ |
| 230 | + Name: "metadata-key", |
| 231 | + Value: 58312, |
| 232 | + Usage: "Metadata key for the chain", |
| 233 | + Destination: &metadataKey, |
| 234 | + }, |
| 235 | + &cli.StringFlag{ |
| 236 | + Name: "redeemer-file", |
| 237 | + Value: "/dev/stdout", |
| 238 | + Usage: "Output file for JSON-formatted redeemer", |
| 239 | + Destination: &redeemerFile, |
| 240 | + }, |
| 241 | + }, |
| 242 | + Action: func(*cli.Context) error { |
| 243 | + redeemerBytes, err := cardano.RedeemerJSON(metadataKey) |
| 244 | + if err != nil { |
| 245 | + return err |
| 246 | + } |
| 247 | + return os.WriteFile(redeemerFile, redeemerBytes, 0644) |
| 248 | + }, |
| 249 | + } |
| 250 | + |
| 251 | +} |
| 252 | + |
| 253 | +func cardanoTipsCmd() *cli.Command { |
| 254 | + |
| 255 | + var nodeSocket string |
| 256 | + var networkMagic uint |
| 257 | + var address string |
| 258 | + var tipFile string |
| 259 | + |
| 260 | + return &cli.Command{ |
| 261 | + Name: "tips", |
| 262 | + Usage: "Find the tips.", |
| 263 | + Flags: []cli.Flag{ |
| 264 | + &cli.StringFlag{ |
| 265 | + Name: "node-socket", |
| 266 | + Required: true, |
| 267 | + Usage: "Path to the Cardano node socket", |
| 268 | + Destination: &nodeSocket, |
| 269 | + }, |
| 270 | + &cli.UintFlag{ |
| 271 | + Name: "network-magic", |
| 272 | + Value: 764824073, |
| 273 | + Usage: "Magic number for the Cardano network", |
| 274 | + Destination: &networkMagic, |
| 275 | + }, |
| 276 | + &cli.StringFlag{ |
| 277 | + Name: "script-address", |
| 278 | + Required: true, |
| 279 | + Usage: "Address of the Plutus script for the tip", |
| 280 | + Destination: &address, |
| 281 | + }, |
| 282 | + &cli.StringFlag{ |
| 283 | + Name: "tips-file", |
| 284 | + Value: "/dev/stdout", |
| 285 | + Usage: "Output JSON file for tip information", |
| 286 | + Destination: &tipFile, |
| 287 | + }, |
| 288 | + }, |
| 289 | + Action: func(*cli.Context) error { |
| 290 | + addr, err := common.NewAddress(address) |
| 291 | + if err != nil { |
| 292 | + return err |
| 293 | + } |
| 294 | + client, err := cardano.NewClient(nodeSocket, uint32(networkMagic)) |
| 295 | + if err != nil { |
| 296 | + return err |
| 297 | + } |
| 298 | + tips, err := client.TipsV1(addr) |
| 299 | + if err != nil { |
| 300 | + return err |
| 301 | + } |
| 302 | + jsonBytes, _ := json.MarshalIndent(cardano.TipReps(tips), "", " ") |
| 303 | + return os.WriteFile(tipFile, jsonBytes, 0644) |
| 304 | + }, |
| 305 | + } |
| 306 | + |
| 307 | +} |
0 commit comments