-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxrpl-encoder.go
495 lines (419 loc) · 11.9 KB
/
xrpl-encoder.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package main
import (
"bufio"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"time"
binarycodec "github.com/xyield/xrpl-go/binary-codec"
"github.com/xyield/xrpl-go/binary-codec/definitions"
)
var (
dataInput = flag.String("d", "", "Directly provide HEX or JSON data as input.")
fileInput = flag.String("f", "", "Provide the path to a file (stored in the 'process/' directory) containing HEX or JSON data.")
helpFlag = flag.Bool("h", false, "Show help message")
batchInput = flag.Bool("b", false, "Provide the path to a subdirectory of 'process/' containing multiple HEX or JSON files.\nIf no subdirectory is provided, all files in the 'process/' directory will be processed.")
)
func main() {
flag.Parse()
// Check for help flag immediately
if *helpFlag {
displayHelp()
return
}
// Check if data input flag is provided
if *dataInput != "" {
processInput(*dataInput)
return
}
// Check if file input flag is provided
if *fileInput != "" {
content, err := os.ReadFile(*fileInput)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
processInput(string(content))
return
}
// Check if batch input flag is provided
if *batchInput {
// Check if there's a next argument and it's not another flag
if len(os.Args) > 2 && !strings.HasPrefix(os.Args[2], "-") {
processBatch(os.Args[2])
} else {
processBatch("")
}
return
}
// If no flags provided, enter the interactive menu loop
for {
// Display menu and get user choice
choice := displayMenu()
switch choice {
case 1, 2, 3, 4:
handleChoice(choice)
case 5:
fmt.Println("\nExiting the tool. Goodbye!")
return
default:
fmt.Println("\nInvalid choice!")
}
}
}
func displayMenu() int {
fmt.Print(`
WARNING: For very large data entries, you may overload your terminal when pasting with Direct Input (Option 1).
Consider using the File Input method (Option 2) for large datasets.
Choose input method:
1. Direct Input
2. File Input
3. Batch Processing (Directory Input)
4. Display Help
5. Exit
`)
var choice int
_, err := fmt.Scanln(&choice)
if err != nil {
fmt.Println("Error reading choice:", err)
return 0
}
return choice
}
func handleChoice(choice int) {
switch choice {
case 1:
// Direct Input logic
fmt.Print("\n\nPlease paste your JSON or HEX data and press Enter twice:", "\n\nInput Data:\n")
inputData := readMultiLineInput()
processInput(inputData)
pauseAndReturnToMenu()
case 2:
// File Input logic
fmt.Println("\nEnter the name of your input file (stored in the 'process/' directory):")
var fileName string
_, err := fmt.Scanln(&fileName)
if err != nil {
fmt.Println("Error reading file name:", err)
return
}
filePath := filepath.Join("process", fileName)
content, err := os.ReadFile(filePath) // #nosec G304
if err != nil {
fmt.Println("Error reading file:", err)
return
}
processInput(string(content))
pauseAndReturnToMenu()
case 3:
// Batch Processing logic
fmt.Println("Please enter the name of the folder (in the 'process/' directory) containing the files you want to process")
fmt.Println("or press Enter to process files in the 'process/' directory:")
reader := bufio.NewReader(os.Stdin)
dirPath, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading directory path:", err)
return
}
dirPath = strings.TrimSpace(dirPath)
processBatch(dirPath)
pauseAndReturnToMenu()
case 4:
displayHelp()
pauseAndReturnToMenu()
}
}
func processInput(inputData string) {
inputData = strings.TrimSpace(inputData)
if len(inputData) == 0 {
fmt.Println("Error: No input data provided.")
return
}
firstChar := inputData[0:1]
lastChar := inputData[len(inputData)-1:]
switch {
case (firstChar == "`" && lastChar == "`") || (firstChar == "\"" && lastChar == "\""):
inputData = inputData[1 : len(inputData)-1]
case firstChar == "`" || firstChar == "\"":
inputData = inputData[1:]
case lastChar == "`" || lastChar == "\"":
inputData = inputData[:len(inputData)-1]
}
outputFileContent := ""
_, err := hex.DecodeString(inputData)
if err != nil {
var jsonInput map[string]any
err := json.Unmarshal([]byte(inputData), &jsonInput)
if err != nil {
fmt.Println("error:", err)
return
}
jsonInput = processJSONFields(jsonInput).(map[string]any)
encoded, err := binarycodec.Encode(jsonInput)
if err != nil {
fmt.Println("error during encoding:", err)
return
}
fmt.Println("\nEncoded Tx Hex:\n\n", encoded)
outputFileContent = encoded
decoded, err := binarycodec.Decode(encoded)
if err != nil {
fmt.Println("error:", err)
return
}
jsonOutput, err := json.MarshalIndent(decoded, " ", " ")
if err != nil {
fmt.Println("error during JSON conversion:", err)
return
}
fmt.Println("\n\nChecking if Re-decoded Tx Hex matches the original Tx JSON...")
time.Sleep(1 * time.Second)
var original map[string]any
var reDecoded map[string]any
err = json.Unmarshal([]byte(inputData), &original)
if err != nil {
fmt.Println("error:", err)
return
}
err = json.Unmarshal(jsonOutput, &reDecoded)
if err != nil {
fmt.Println("error:", err)
return
}
if reflect.DeepEqual(standardizeHexStrings(original), standardizeHexStrings(reDecoded)) {
fmt.Println("\nSUCCESS ---> Re-decoded Tx JSON matches the original Tx JSON")
} else {
fmt.Println("\nFAIL ---> Re-decoded Tx Hex does not match original Tx JSON\nNote: Some fields in the raw JSON won't be encoded because they don't exist in the binary-codec definitions.json, or they are supposed to be omitted from the binary encoding. This is expected behavior.")
fmt.Println("\nRe-decoded Tx JSON:\n", string(jsonOutput))
}
} else {
hexEncodedTx := inputData
decoded, err := binarycodec.Decode(hexEncodedTx)
if err != nil {
fmt.Println("error:", err)
return
}
jsonOutput, err := json.MarshalIndent(decoded, "", " ")
if err != nil {
fmt.Println("error during JSON conversion:", err)
return
}
fmt.Println("\nDecoded Tx Json:\n\n", string(jsonOutput))
outputFileContent = string(jsonOutput)
encoded, err := binarycodec.Encode(decoded)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("\n\nChecking if Re-encoded Tx JSON matches the original Tx Hex...")
time.Sleep(1 * time.Second)
if encoded == hexEncodedTx {
fmt.Println("\nSUCCESS ---> Re-encoded Tx JSON matches the original Tx Hex")
} else {
fmt.Println("\nFAIL ---> Re-encoded Tx JSON does not match original Tx Hex")
fmt.Println("\nRe-encoded Tx Hex:\n", encoded)
}
}
if shouldSave, customName := askForFileOutput(); shouldSave {
writeOutputToFile(outputFileContent, customName)
}
}
func processBatch(subDir string) {
directory := "process"
if subDir != "" {
directory = filepath.Join(directory, subDir)
}
fmt.Println("\nProcessing directory:", directory)
fmt.Println("--------------------------------------------------------------------")
files, err := os.ReadDir(directory)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
if len(files) == 0 {
fmt.Println("No files found in the directory.")
return
}
for _, file := range files {
if !file.IsDir() {
fmt.Println("Processing file:", file.Name())
filePath := filepath.Join(directory, file.Name())
content, err := os.ReadFile(filePath) // #nosec G304
if err != nil {
fmt.Println("Error reading file:", err)
continue
}
processInput(string(content))
}
}
}
func displayHelp() {
fmt.Println(`
Usage: xrpl-encoder [OPTIONS]
Options:
-d Directly provide HEX or JSON data as input.
-f Provide the path to a file (stored in the 'process/' directory) containing HEX or JSON data.
-b Provide the path to a subdirectory of 'process/' containing multiple HEX or JSON files.
If no subdirectory is provided, files in the 'process/' directory will be processed.
-h Show help message
To use the tool in interactive mode, just run it without any flags.
`)
}
func readMultiLineInput() string {
scanner := bufio.NewScanner(os.Stdin)
const maxCapacity = 10 * 1024 * 1024
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
var lines []string
for scanner.Scan() {
line := scanner.Text()
if line == "" {
break
}
lines = append(lines, line)
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading from input:", err)
}
return strings.Join(lines, "\n")
}
func pauseAndReturnToMenu() {
fmt.Println("\nPress Enter to return to the main menu.")
reader := bufio.NewReader(os.Stdin)
_, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading from input:", err)
}
}
func processJSONFields(input any) any {
switch v := input.(type) {
case float64:
return int(v)
case map[string]any:
for key, value := range v {
if key == "Indexes" || key == "Hashes" || key == "Amendments" || key == "Nftokenoffers" {
if list, ok := value.([]any); ok {
v[key] = convertInterfaceSliceToStringSlice(list)
}
} else {
v[key] = processJSONFields(value)
}
}
case []any:
for i, value := range v {
v[i] = processJSONFields(value)
}
}
return input
}
func convertInterfaceSliceToStringSlice(slice []any) []string {
var stringSlice []string
for _, val := range slice {
strVal, ok := val.(string)
if ok {
stringSlice = append(stringSlice, strVal)
}
}
return stringSlice
}
func writeOutputToFile(output, customName string) {
err := os.MkdirAll("process/outputs", 0750)
if err != nil {
fmt.Println("Error creating outputs directory:", err)
return
}
filename := customName
extension := ".txt"
if isJSON(output) {
extension = ".json"
}
if filename == "output" {
i := 1
for fileExists(filepath.Join("process/outputs", filename+extension)) {
filename = fmt.Sprintf("output%d", i)
i++
}
} else if fileExists(filepath.Join("process/outputs", filename+extension)) {
i := 1
for fileExists(filepath.Join("process/outputs", filename+fmt.Sprintf("_%d", i)+extension)) {
i++
}
filename = filename + fmt.Sprintf("_%d", i)
}
filename = filepath.Join("process/outputs", filename+extension)
err = os.WriteFile(filename, []byte(output), 0600)
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
fmt.Println("\nOutput saved to", filename)
fmt.Println("\n--------------------------------------------------------------------")
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
func askForFileOutput() (bool, string) {
fmt.Println("\nWould you like to save the output to a file? (y/n) or (y filename): ")
reader := bufio.NewReader(os.Stdin)
answer, _ := reader.ReadString('\n')
answer = strings.TrimSpace(answer)
if strings.ToLower(answer) == "n" {
fmt.Println("\n--------------------------------------------------------------------")
return false, ""
}
parts := strings.SplitN(answer, " ", 2)
if len(parts) > 1 && strings.ToLower(parts[0]) == "y" {
return true, parts[1]
}
return strings.ToLower(parts[0]) == "y", "output"
}
func isJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
func standardizeHexStrings(i any) any {
switch v := i.(type) {
case map[string]any:
for key, value := range v {
if isUInt64Field(key) {
v[key] = standardizeUInt64(value.(string))
v[key] = standardizeHexStrings(v[key])
} else {
v[key] = standardizeHexStrings(value)
}
}
case []any:
for index, value := range v {
v[index] = standardizeHexStrings(value)
}
case string:
if looksLikeHex(v) {
return strings.ToUpper(v)
}
}
return i
}
func looksLikeHex(s string) bool {
if len(s) < 2 {
return false
}
for _, c := range s {
if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
return false
}
}
return true
}
func isUInt64Field(fieldName string) bool {
typeName, _ := definitions.Get().GetTypeNameByFieldName(fieldName)
return typeName == "UInt64"
}
func standardizeUInt64(value string) string {
return fmt.Sprintf("%016s", value)
}