forked from koh-gt/ferritext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ferritext.ps1
1742 lines (1380 loc) · 57.3 KB
/
ferritext.ps1
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#####
#
# FEXT by koh-gt
#
# Tested to work on the following Ferrite Core versions:
#
# Recommended -- v3.1.4, v3.1.3, v3.1.2, v3.1.1
# Deprecated -- v3.1.0, v3.0.1, v3.0.0
#
# A Powershell script to search for text inscriptions on the Ferrite blockchain.
#
# Place this script in the same directory level as ferrite-cli.exe
#
#####
$ferrite_coin_splash = "`n
-:+*#%@@@@@@@@@@%#*+:-
.:*%@@@@@@@@@@@@@@@@@@@@@@@@%*:.
-+%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%+-
':%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%:'
';@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;'
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+
-#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#-
:@@@@@@@@@@@@@@@@@@@@@@@@@@@-.:#@@@@@@@@@@@@@@@@@@@@@@@:
;@@@@@@@@@@@@@@@@@@@@@@@@@@%- :;%@@@@@@@@@@@@@@@@@@@@+
+@@@@@@@@@@@@@@@@@@@@@@@@@@%. .+#@@@@@@@@@@@@@@@@@@+
-@@@@@@@@@@@@@@@@@@@@@@@@@@# ;+- :*%@@@@@@@@@@@@@@@-
%@@@@@@@@@@@@@@@@@@@@@@@@@* .#@@@@*:. -@@@@@@@@@@@@@@@%
+@@@@@@@@@@@@@@@@@@@@@@@@@; .%@@@@@@@- :@@@@@@@@@@@@@@@@@+
#@@@@@@@@@@@@@@@@@@@@@@@@: -@@@@@@@%- +@@@@@@@@@@@@@@@@@@#
@@@@@@@@@@%%%%%%%%%%%%%%- :@@@@@@@%. +%%%%%%%%%%@@@@@@@@@@
@@@@@@@@@@ +@@@@@@@# @@@@@@@@@@
@@@@@@@@@@ ;@@@@@@@* @@@@@@@@@@
@@@@@@@@@@%%%%%%%%%%; *@@@@@@@+ .#%%%%%%%%%%%%%@@@@@@@@@@
#@@@@@@@@@@@@@@@@@@* .#@@@@@@@: -@@@@@@@@@@@@@@@@@@@@@@@@#
+@@@@@@@@@@@@@@@@@+ .%@@@@@@@- :@@@@@@@@@@@@@@@@@@@@@@@@@+
%@@@@@@@@@@@@@@@: :;%@@@%- +@@@@@@@@@@@@@@@@@@@@@@@@@%
-@@@@@@@@@@@@@@@@#:. -+*. ;@@@@@@@@@@@@@@@@@@@@@@@@@@-
+@@@@@@@@@@@@@@@@@@%+- #@@@@@@@@@@@@@@@@@@@@@@@@@@+
+@@@@@@@@@@@@@@@@@@@@@*:. .#@@@@@@@@@@@@@@@@@@@@@@@@@@;
:@@@@@@@@@@@@@@@@@@@@@@@#+-.%@@@@@@@@@@@@@@@@@@@@@@@@@@:
-#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#-
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+
';@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;'
':%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%:'
-+%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%+-
.:*%@@@@@@@@@@@@@@@@@@@@@@@@%*:.
-:+*#%@@@@@@@@@@%#*+:-
"
#####
#
# RPC parameters
#
#####
[string] $rpcuser = "user"
[string] $rpcpass = "password"
[string] $rpchost = "127.0.0.1"
$MAINNET_RPC_PORT = 9573
$TESTNET_RPC_PORT = 19573
#####
#
# Future
#
#####
[int] $UTF_ENABLE = 1
[int] $TESTNET = 0 # leave as 1 for testnet
#####
#
# Text return parameters
#
#####
# load latest blocks at startup from cli (default 30)
$INIT_BLOCKS_SHOW = 10 ##########################################
# 0 - blocks strings that contain non standard characters
# 1 - filters strings that contain non standard characters
$ALLOW_NONSTANDARD = 0
# 0 - only characters 32 to 126 " !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
# 1 - non-operation ascii from 32 to 126 and 128 to 255
$STANDARD_SETTING = 0
# show or skip invalid blocks
$SHOW_EMPTY_OR_INVALID_BLOCKS = 0
# number of digits for block number (default: 9)
$BLOCKNUM_DIGITS = 9
#####
#
# Window settings
#
#####
$WINDOW_HEIGHT = 40
$WINDOW_WIDTH = 100
$uisettings = (get-host).UI.RawUI
$b = $uisettings.WindowSize
$ba = $uisettings.MaxPhysicalWindowSize
# adjust buffer size according to max physical window size
$bf = $uisettings.BufferSize
$bf.Height = [math]::Ceiling($ba.Height - 1) # increase this so that buffer can handle more at once...
$bf.Width = [math]::Ceiling($ba.Width - 1)
$uisettings.BufferSize = $bf
# let window size be max physical window size
# $b.Height = $ba.Height - 1 # a bit smaller than maxphysical window size
# $b.Width = $ba.Width - 1 # a bit smaller than maxphysical window size
$b.Height = $WINDOW_HEIGHT
$b.Width = $WINDOW_WIDTH
$height_window = $b.Height
$width_window = $b.Width
$uisettings.WindowSize = $b # apply window size changes
#window title stat of columns x rows
[string] $titlename = "Ferritext Wall -- FEXT ~ $height_window x $width_window"
$uisettings.WindowTitle = $titlename # apply window name title changes
#####
if ($TESTNET) {
$COIN_SHORTHAND = "tFEC" # for units
} else {
$COIN_SHORTHAND = "FEC" # for units
}
# UTF-8 support for characters outside ASCII.
# Experimental !
# (Mandarin, Spanish, Hindi, Arabic, Bengali, Portuguese, Russian, Japanese)
if ($UTF_ENABLE){
[Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
[Console]::Writeline("你好,世界 | नमस्ते दुनिया | مرحبا بالعالم | ওহে বিশ্ব | Olá Mundo")
[console]::Writeline("Привет, мир | こんにちは、世界 | ਹੈਲੋ ਵਰਲਡ | హలో వరల్డ్ | नमस्कार जग")
[console]::Writeline("Xin chào thế giới | வணக்கம் உலகம் | 안녕하세요, 세계 | สวัสดีโลก | Γειά σας κόσμος")
[console]::Writeline("မင်္ဂလာပါ | Привіт, світе | नमस्कार संसार | سلام نړی | سلام دنیا")
# Hindi, Arabic fonts do not load in NSimSun
}
# Console codes
$esc = "$([char]27)"
$reset = "$esc[0m"
$highlight_white = "$esc[30;47m"
$red_text = "$esc[31;40m"
$green_text = "$esc[32;40m"
$SERVERCONN_TIMEOUT_MILLIS = 500
function test-ferrite-server-connection($rpchost, $port_test, $timeout) {
$requestCallback = $state = $null
$client = New-Object System.Net.Sockets.TcpClient
$beginConnect = $client.BeginConnect($rpchost,$port_test,$requestCallback,$state)
Start-Sleep -milli $timeout
if ($client.Connected) {
$open = $true
} else {
$open = $false
}
$client.Close()
if ($open){
[console]::Writeline("TCP test $rpchost port $port_test ($COIN_SHORTHAND)...$green_text`yes$reset")
} else {
[console]::Writeline("TCP test $rpchost port $port_test ($COIN_SHORTHAND)...$red_text no$reset")
}
return $open
}
function get-ferrite-server-status(){
if ($testnet -eq 0){
$port_test = $MAINNET_RPC_PORT
} else {
$port_test = $TESTNET_RPC_PORT
}
$ferrite_rpc_status = test-ferrite-server-connection($rpchost)($port_test)($SERVERCONN_TIMEOUT_MILLIS)
if ($ferrite_rpc_status){
[console]::Writeline("Connection test successful.")
} else {
[console]::Writeline("Connection cannot be made to $rpchost.")
[console]::Writeline("$highlight_white`Check if Ferrite Core or ferrited is running.$reset")
return $false
}
[console]::Writeline("Checking if process name is Ferrite Core.")
$process_names = "ferrite-qt"
$running = Get-Process | Where-Object { $_.ProcessName -like "*$process_names*" }
if ($running.Count -gt 0) {
$running | ForEach-Object {
[console]::Write("Ferrite Core...")
[console]::Write("$green_text`yes$reset ")
[console]::WriteLine("Client: $($_.ProcessName) (PID: $($_.Id))")
}
return $true
} else {
[console]::Write("$red_text no$reset`n")
[console]::WriteLine("$red_text Ferrite-qt is not running.$reset Filters:`"$process_names`"`nPlease run ferrite-qt or ferrited to use Ferritext.")
return $false
}
}
$ferrite_run_status = get-ferrite-server-status
if (-not $ferrite_run_status){
start-sleep -seconds 5000
break
}
############################################################################################################################################
#
# ferrite-cli searcher
#
############################################################################################################################################
$FERRITE_CLI_EXE = "ferrite-cli.exe"
$LATEST_VERSION_STR = "v3.1.3"
$LATEST_VNUM = 30103
$CUTOFF_VNUM = 30000
$SEARCH_TIMEOUT_SECONDS = 10
function ferrite-software-search($current_path, $ferrite_exe, $timeout){
# Define the timeout (in seconds)
# Start a background job to run Get-ChildItem
$job = Start-Job -ScriptBlock {
param (
[string]$path,
[string]$filename
)
Get-ChildItem -Path $path -Recurse -Include $filename
} -ArgumentList $current_path, "$ferrite_exe"
# Wait for the job to complete or timeout
$jobCompleted = $job | Wait-Job -Timeout $timeout
if ($jobCompleted) {
# Job completed within the timeout
$ferrite_cli_paths = Receive-Job -Job $job
$output = @(1..$ferrite_cli_paths.count)
$index = 0
foreach ($filepath in $ferrite_cli_paths){
$output[$index] = $filepath.FullName
$index++
}
return $output
} else {
# Timeout reached
Stop-Job -Job $job
Remove-Job -Job $job
[console]::Writeline("The operation exceeded the timeout of $timeoutSeconds seconds.`n$ferrite_exe cannot be found.")
}
}
# version checker
function get-numversion($versionString) {
# version XX.YY.ZZ.WW = 10000 * XX + 100 * YY + ZZ
# Extract the version number part (X.Y.Z)
$version_number = [regex]::Match($versionString, 'v(\d+\.\d+\.\d+)').Groups[1].Value
$parts = $version_number -split '\.'
$x = [int]$parts[0]
$y = [int]$parts[1]
$z = [int]$parts[2]
$numericVersion = (10000 * $x) + (100 * $y) + $z
return $numericVersion
}
function check-ferrite([string] $current_path){ # TODO Test for hardcoded C:\Program files ferrite-cli
try {
# Run the executable and capture the output
$output = & $current_path -version
# Display the captured output
$num_version = get-numversion($output)
return $num_version
} catch [System.Management.Automation.CommandNotFoundException] {
return $false # no such command ferrite-cli
} catch [System.Management.Automation.ItemNotFoundException] {
return $false # no such directory
}
}
function start-check-version-ferrite($ver, $pathx){
if ($ver) {
[console]::Write("$pathx...$ver`n")
if ($ver -ge $CUTOFF_VNUM){
[console]::Writeline("Using ferrite-cli $ver from $pathx")
return $true
} else {
return $false
}
} else {
[console]::Write("$pathx...no`n")
return $false
}
}
function start-checks-cli(){
[console]::Write("`n`nFerritext will search for the latest ferrite-cli`nLatest version: $LATEST_VERSION_STR ($LATEST_VNUM) Cutoff ($CUTOFF_VNUM)`n")
[console]::Write("Checking for ferrite-cli in same directory...`n")
$immediatev = check-ferrite("ferrite-cli.exe")
if (start-check-version-ferrite($immediatev)("current path")){
return "$CURRENT_PATH\ferrite-cli.exe"
}
$ferritefilepaths = @(
"C:\Program Files\Ferrite\daemon\ferrite-cli.exe", # v3.1.2 and future
"C:\Program Files\_Ferrite_Core\daemon\ferrite-cli.exe",
"C:\Program Files\Ferrite\ferrite-cli.exe",
"C:\Program Files\_Ferrite_Core\ferrite-cli.exe"
)
[console]::Write("Checking for ferrite-cli in default program file paths...`n")
foreach ($path in $ferritefilepaths){
$pathv = check-ferrite($path)
if (start-check-version-ferrite($pathv)($path)){
return $path
}
}
[console]::Write("Performing deeper search for ferrite-cli in child directories...`n")
$ferriteallpaths = ferrite-software-search($PWD.Path)($FERRITE_CLI_EXE)($SEARCH_TIMEOUT_SECONDS)
foreach ($ferritepath in $ferriteallpaths){
$ferritepathv = check-ferrite($ferritepath)
if (start-check-version-ferrite($ferritepathv)($ferritepath)){
return $ferritepath
}
}
[console]::Write("No suitable ferrite-cli found.")
return $false
}
function start-checks-index(){
[console]::Write("`n`nFerritext will search for the FEXT index in the same directory.`n")
if (Test-Path "$CURRENT_PATH\fextindex.dat" -PathType Leaf){
[console]::Write("`nFEXT index$green_text found.$reset")
return "$CURRENT_PATH\fextindex.dat"
}
$fextfilepaths = @(
"C:\Program Files\Ferrite\fextindex.dat",
"C:\Program Files\_Ferrite_Core\fextindex.dat"
)
foreach ($path in $fextfilepaths){
if (Test-Path $path -PathType Leaf){
[console]::Write("`nFEXT index$green_text found.$reset")
return $path
}
}
[console]::Write("`nFEXT index$red_text not found.$reset`nCurrently building index. Catching up...")
return $false
}
############################################################################################################################################
# get latest block height
[string] $CURRENT_PATH = $PWD.Path
$actual_path = start-checks-cli
$actual_path_directory = Split-Path -Path $actual_path -Parent
Set-location -Path "$actual_path_directory"
# FEXT index path
$FEXT_PATH = start-checks-index
if (-not $FEXT_PATH){
$FEXT_PATH = "$CURRENT_PATH\fextindex.dat"
# create FEXT index file, with a zero in the first line indicating no blocks synchronised
"0" | Out-File -FilePath "$FEXT_PATH" -Encoding UTF8
}
# get last saved FEXT index height
$FEXT_CONTENT = Get-Content $FEXT_PATH -Encoding UTF8
$fext_index_height = $FEXT_CONTENT | Select-Object -Index 0
[console]::Write("`nFEXT index currently at height $fext_index_height.")
#pause
#Start-Sleep 50
#####
#
# Console commands
#
#####
if ($TESTNET){$testnet_arg = "-testnet"} else {$testnet_arg = ""}
# commands
[string] $ferrite_cli = ".\ferrite-cli -rpcconnect=`"$rpchost`" -rpcuser=`"$rpcuser`" -rpcpassword=`"$rpcpass`" $testnet_arg"
[string] $listwallets = "$ferrite_cli listwallets"
[string] $getblockcount = "$ferrite_cli getblockcount"
[string] $getblockhash = "$ferrite_cli getblockhash"
[string] $getblock = "$ferrite_cli getblock"
[string] $getrawtransaction = "$ferrite_cli getrawtransaction"
[string] $getnetworkinfo = "$ferrite_cli getnetworkinfo"
[string] $getrawmempool = "$ferrite_cli getrawmempool"
# wallet commands
[string] $listwallets = "$ferrite_cli listwallets"
# transaction creation
[string] $createrawtransaction = "$ferrite_cli createrawtransaction"
# blockchain variables
[string] $maxheight = iex -Command $getblockcount
[string] $genesishash = iex -Command "$getblockhash 0"
# initial synchronisation
$blocks_show = $INIT_BLOCKS_SHOW
$last_block = $maxheight
$START_BLOCK = $last_block - $blocks_show
$LINES_DISPLAY_SHOW = $WINDOW_HEIGHT - 10 # unused
$MAX_DISPLAY_LINES_OUTPUT = 21 # maximum number of lines of last seen messages
$WALLETINFO_LINES = 2 # number of lines for wallet information
$BLOCK_UPDATE_INTERVAL = 10 #block update interval in seconds
$MEMPOOL_UPDATE_INTERVAL = 2 #block update interval in seconds
$FEXT_CHECK_UPDATE_INTERVAL = 1
$TIMEOUT_ALERT = 15 # sync timeout alert mode - if more than X seconds then sync will start
$TIMEOUT_ALERT_HYPER = 2
#####
#
# Chainparams
#
#####
# default update times
$BLOCK_TIME = 60
function get-block-update-interval($BLOCK_UPDATE_INTERVAL){
if ($BLOCK_UPDATE_INTERVAL -gt $BLOCK_TIME){
return $BLOCK_TIME
}
return $BLOCK_UPDATE_INTERVAL
}
function get-mempool-update-interval($BLOCK_UPDATE_INTERVAL, $MEMPOOL_UPDATE_INTERVAL){
if ($BLOCK_UPDATE_INTERVAL -gt $BLOCK_TIME){
$BLOCK_UPDATE_INTERVAL = $BLOCK_TIME
}
if ($MEMPOOL_UPDATE_INTERVAL -gt $BLOCK_UPDATE_INTERVAL){
$MEMPOOL_UPDATE_INTERVAL = $BLOCK_UPDATE_INTERVAL
}
return $MEMPOOL_UPDATE_INTERVAL
}
$BLOCK_UPDATE_INTERVAL = get-block-update-interval($BLOCK_UPDATE_INTERVAL)
$MEMPOOL_UPDATE_INTERVAL = get-mempool-update-interval($BLOCK_UPDATE_INTERVAL)($MEMPOOL_UPDATE_INTERVAL)
#####
#
# debug functions
#
#####
function print-object([Object[]] $arr){
foreach ($line in $arr){
[console]::Write("$line`n")
}
}
function print-object-multiline([Object[]] $arr){
$lines = $arr -join "`n"
[console]::Write("$lines`n")
}
$LINE_CLEAR = " " * $WINDOW_WIDTH
function clear-lines([int]$lines){
$LINES_CLEAR = "$LINE_CLEAR`n" * $lines
[console]::Write($LINES_CLEAR)
}
#####
#
# main functions
#
#####
# check for version
function get-networkinfo-subversion() {
$networkinfo = iex -Command "$getnetworkinfo"
$netinfo = $networkinfo | ConvertFrom-Json
$version = $netinfo.version
return $version
}
function get-message-fee-rate(){
[long] $ferrite_core_version = get-networkinfo-subversion
if ($ferrite_core_version -ge 3010200){
return 10, 2
} else {
return 0.1, 4
}
}
function get-skipblock-fee-rate([int] $height){
[long] $ferrite_core_version = get-networkinfo-subversion
if ($ferrite_core_version -ge 3010200){
# can skip blocks
} else {
#
}
}
# clear previous outputs from searching for ferrite-cli
[console]::Write("$ferrite_coin_splash")
[double] $MESSAGE_FEE_RATE, $DECIMAL_PRECISION = get-message-fee-rate
[double] $SKIPBLOCK_FEE_RATE = get-skipblock-fee-rate($maxheight) # fee rate for pushing stuck blocks - expensive!
# get mempool for unconfirmed transactions
function get-rawmempool(){
$rawmempool = iex -Command "$getrawmempool" | ConvertFrom-Json
return $rawmempool
}
# useful function to check for transaction count -- skip blocks that only contain one transaction
function get-blocktransactionhashes([int]$height){
[string] $blockdatahash = iex -Command "$getblockhash $height"
[string] $jsonblockdata = iex -Command "$getblock $blockdatahash"
$blockdata = $jsonblockdata | ConvertFrom-Json
$txdata = $blockdata.tx
return $txdata
}
function Get-BlockOpReturnHex-FromHeight([int]$height){
return Get-BlockOpReturnHex(get-blocktransactionhashes($height))
}
function get-tx-vout($tx){
# gets the hex value of the opreturn from the vout of the getrawtransaction output
return $tx.vout | Where-Object {$_.scriptPubKey.hex.StartsWith("6a")} | ForEach-Object {
$scriptpubkey_hex = $_.scriptPubKey.hex
$scriptpubkey_hex_substring = $scriptpubkey_hex.Substring(2, 2)
if ($scriptpubkey_hex_substring -eq "4c") {
$_.scriptPubKey.hex.Substring(6)
} elseif ($scriptpubkey_hex_substring -eq "4d") {
$_.scriptPubKey.hex.Substring(8)
} elseif ($scriptpubkey_hex_substring -eq "4e") {
$_.scriptPubKey.hex.Substring(12)
} else {
$_.scriptPubKey.hex.Substring(4)
}
}
}
# returns transaction data from transaction hashes
function Get-BlockOpReturnHex([Object[]]$txdata){
$txnum = $txdata.count
if ($txnum -eq 1){
$tx = iex -Command "$getrawtransaction $txdata 1" | ConvertFrom-Json
# return $tx.vout | Where-Object {$_.scriptPubKey.asm -match 'OP_RETURN'} | ForEach-Object { $_.scriptPubKey.asm } | ForEach-Object { $_ -replace '^OP_RETURN\s*', '' }
$tx_vout = get-tx-vout($tx)
return $tx_vout
}
$output = @(1..$txnum)
foreach ($i in 0..($txnum-1)) {
$txhash = $txdata[$i]
$tx = iex -Command "$getrawtransaction $txhash 1" | ConvertFrom-Json
$tx_vout = get-tx-vout($tx)
$output[$i] = $tx_vout
}
return $output
}
# ferritext explorer
function hex-to-str([string]$hexraw){
$hex = $hexraw -replace '[^0-9a-fA-F]', ''
$standard = 1
$arr = @(1..$hex.length)
$arr_index = 0
for ($i = 0; $i -lt $hex.Length; $i = $i + 2){
$nib1 = $hex[$i]
$nib2 = $hex[($i + 1)]
$byte = [system.convert]::ToInt16("$nib1$nib2", 16)
if (($byte -ge 32) -and ($byte -lt 127)){ # only standard ASCII
$char = [char][byte]$byte
$arr[$arr_index] = $char
$arr_index++
} else {
$standard = 0
}
}
if (($ALLOW_NONSTANDARD + $standard) -ne 0){ # $ALLOW_NONSTANDARD = 1 will force this to equal 1
return [string]::join("", $arr[0..($arr_index - 1)])
} else {
return ""
}
}
function hexarr-to-strarr([Object[]] $hexarr){
$hexarr_count = $hexarr.count
if ($hexarr_count -eq 1){
[string] $hex = $hexarr
$strdata = hex-to-str($hex)
if ($strdata -ne ""){
$str_hex = $strdata
}
return $str_hex
# [console]::Write("$str_hex")
} else {
$arr = @(1..$hexarr_count)
$valid_index = 0
for ($i = 0; $i -lt $hexarr_count; $i++){
[string] $hex = $hexarr[$i]
$strdata = hex-to-str($hex)
if ($strdata -ne ""){
$arr[$valid_index] = $strdata
$valid_index++
}
}
if ($valid_index -ne 0){
return $arr[0..($valid_index - 1)]
} else {
return $null
}
}
}
function get-output-2d-object-str-mempool([int] $maxheight){
$nextheight = $maxheight + 1
$rawmempool_tx = get-rawmempool
$rawmempool_tx_count = $rawmempool_tx.count
if ($rawmempool_tx_count -ne 0){
$nextblock_strarr = hexarr-to-strarr(Get-BlockOpReturnHex($rawmempool_tx)) # cannot index into null array - why? because mempool can be empty
} else {
$nextblock_strarr = $null
}
return @(@($nextheight, $nextblock_strarr))
}
function get-output-2d-object-str([int] $start_block, [int] $last_block){
if($start_block -eq $last_block){
$height = $start_block
$strarr = hexarr-to-strarr(Get-BlockOpReturnHex-FromHeight($height))
return @($height, $strarr)
}
$blocks = @($start_block..$last_block)
$output = @($start_block..$last_block)
$index = 0
foreach ($block in $blocks){
$height = $block
$strarr = hexarr-to-strarr(Get-BlockOpReturnHex-FromHeight($height))
$output[$index] = @($height, $strarr)
$index++
}
return $output
}
function format-output-arr($block_height, $block_data){
$spacer = " " * ($BLOCKNUM_DIGITS)
$block_height_str = ([string] $block_height).PadLeft($BLOCKNUM_DIGITS, " ")
$txindex = 0
$block_data_count = $block_data.count
$output_arr = ,$null * $block_data_count
foreach ($tx in $block_data){
if ($txindex -eq 0){
#[console]::WriteLine("$block_height_str | $tx")
$output_arr[$txindex] = "$block_height_str | $tx"
$txindex++
} else {
#[console]::WriteLine("$spacer | $tx")
$output_arr[$txindex] = "$spacer | $tx"
$txindex++
}
}
return $output_arr
}
function get-total-tx-multiblock($2d_object){
$total_lines = 0
foreach ($block in $2d_object){
$total_lines = $total_lines + $block[1].count
}
return $total_lines
}
function output-2d-object-str([Object[]] $2d_object){
# If the array is only @($height, $strarr), this will return 1
# if it is a nested array it will return 2 since the first element is an array
#
# case with only 1 block
$2d_object_count = $2d_object[0].Count
if ($2d_object_count -eq 1){
$block_height = $2d_object[0]
$block_data = $2d_object[1]
return format-output-arr($block_height)($block_data)
} else {
$total_lines = get-total-tx-multiblock($2d_object)
$ui_arr = ,$null * $total_lines
$line_index = 0
foreach ($block in $2d_object){
$block_height = $block[0]
$block_data = $block[1]
$block_output_arr = format-output-arr($block_height)($block_data)
foreach ($line in $block_output_arr){
$ui_arr[$line_index] = $line
$line_index++
}
}
return $ui_arr
}
}
function cursor-goto ([int] $x, [int] $y){
[console]::SetCursorPosition($x, $y)
}
function cursor-return (){
cursor-goto(0)(0)
}
function cursor-return-corner (){
cursor-goto($WINDOW_WIDTH - 1)($WINDOW_HEIGHT - 1)
}
function cursor-hide (){
$uisettings.CursorSize = 0 # hide cursor
}
function cursor-show (){
$uisettings.CursorSize = 1 # hide cursor
}
# "$esc[$offset_rows;$start_x`H"
$SHOW_FULL_STRINGS = 1 # shows entire string even those longer than $WINDOW_WIDTH
function process-oversized-str($ui_obj_string_arr){ # in case strings are too long, they wrap around and take multiple lines
[int] $lines = 0
$maxlinelength = $WINDOW_WIDTH - 1
foreach ($tx_string in $ui_obj_string_arr){
$strlength = $tx_string.length
if ($strlength -gt $maxlinelength){
$lines = $lines + [math]::Ceiling($strlength / $maxlinelength)
} else {
$lines++
}
}
if ($lines -eq $ui_obj_string_arr.count){
return $ui_obj_string_arr
}
$output_arr = ,$null * $lines
$index = 0
foreach ($tx_string in $ui_obj_string_arr){
$strlength = $tx_string.length
if ($strlength -gt $maxlinelength){
$splitstrings_num = [math]::Ceiling($strlength / $maxlinelength)
for ($i = 0; $i -lt $strlength; $i += $maxlinelength){
$output_arr[$index] = $tx_string.Substring($i, [Math]::Min($maxlinelength, $strlength - $i))
$index++
}
} else {
$output_arr[$index] = $tx_string
$index++
}
}
return $output_arr
}
function output-main-format-str($ui_obj, $ui_obj_mem, [int] $INDEX, [int] $MAX_LINES){
if ($MAX_LINES -eq 0){
return
}
$ui = process-oversized-str($ui_obj + $ui_obj_mem)
[int] $linecount = $ui.count
$start_offset = $linecount - $MAX_LINES # starting lines truncated because of MAX_LINES
if ($start_offset -lt 0){
$start_offset = 0
}
if (($start_offset - $INDEX) -lt 0){
# Error
[console]::WriteLine("`nINVALID INDEX FOR OUTPUT STRING WINDOW`n")
return
}
if ($linecount -gt $MAX_LINES){
return $ui[($start_offset - $INDEX)..($linecount - $INDEX - 1)]
} else {
return $ui
}
}
function indexchecker($ui_obj, $ui_obj_mem, [int] $INDEX, [int]$MAX_LINES){
if ($INDEX -lt 0){ # 0 is the latest block
return 0
}
$ui = process-oversized-str($ui_obj + $ui_obj_mem)
$linecount = $ui.count
if ($INDEX -gt ($linecount - $MAX_LINES)){
if (($linecount - $MAX_LINES) -lt 0){ # no need to use different index since MAX_LINES is sufficient
return 0
} else {
return $linecount - $MAX_LINES # the earliest tx available
}
}
return $INDEX
}
$update_output_wipe_line = " " * ($WINDOW_WIDTH - 1) + "`n"
function update-output-main-format-str($ui_obj, $ui_obj_mem, [int] $INDEX, [int] $MAX_LINES){
# replace with a cursor wipe <----------------------------------- once main loop has no cls
# WIP
cursor-return
$wipe_lines = $update_output_wipe_line * $MAX_LINES
[console]::Write($wipe_lines)
cursor-return
####
#[console]::WriteLine("print out $INDEX")
print-object-multiline(output-main-format-str($ui_obj)($ui_obj_mem)($INDEX)($MAX_LINES))
cursor-return-corner
}
function get-wallet-list(){
$wallet_list = iex -Command $listwallets | ConvertFrom-Json
return $wallet_list
}
function set-txfee([string] $wallet_name, $message_fee_set){
$txfee_status = iex -Command "$ferrite_cli -rpcwallet=`"$wallet_name`" settxfee $message_fee_set" # getwalletinfo has a variable wallet_name
return $txfee_status
}
function get-wallet-info([string] $wallet_name){
# returns wallet info in array form (converted from json) of a wallet name
[string] $getwalletinfo = "$ferrite_cli -rpcwallet=`"$wallet_name`" getwalletinfo" # getwalletinfo has a variable wallet_name
$wallet_info = iex -Command $getwalletinfo | ConvertFrom-Json
return $wallet_info
}
function get-wallet-balance($walletinfo){
return "{0:F$DECIMAL_PRECISION}" -f $walletinfo.balance
}
function generate-wallet-infoline($walletinfo){
[double] $walletinfo_balance = $walletinfo.balance # get balance in
[double] $walletinfo_unc_balance = $walletinfo.unconfirmed_balance
[double] $walletinfo_imm_balance = $walletinfo.immature_balance
$balance = "{0:F$DECIMAL_PRECISION}" -f $walletinfo_balance
# highlight red if zero
if ($walletinfo_balance -ne 0){
[string] $balance_str = " | $balance"
} else {
[string] $balance_str = " | $red_text$balance$reset"
}
if ($walletinfo_unc_balance -ne 0){
$unc_balance = "{0:F$DECIMAL_PRECISION}" -f $walletinfo_unc_balance
[string] $unc_balance_str = " + $unc_balance"
} else {
$unc_balance_str = ""
}
if ($walletinfo_imm_balance -ne 0){
$imm_balance = "{0:F$DECIMAL_PRECISION}" -f $walletinfo_imm_balance
[string] $imm_balance_str = " + $imm_balance"
} else {
$imm_balance_str = ""
}
return "$balance_str$unc_balance_str$imm_balance_str $COIN_SHORTHAND"
}
# ($wallet_select_index)($keypress_key)($keypress_keychar)($wallet_list)($disable_input)
function generate-wallet-output-line($walletname, $wallet_select_index, $longest, $offset_line_x, $index_i){
$walletinfo = get-wallet-info($walletname) #wallet info
$walletinfo_line = generate-wallet-infoline($walletinfo) # the string after the wallet name
if ($walletname -eq ""){
$defaultname = "[default wallet]"
$walletname_out = $defaultname.PadRight($longest)
} else {
$walletname_out = $walletname.PadRight($longest)
}
if ($index_i -eq $wallet_select_index){
return "$offset_line_x$highlight_white$walletname_out$reset$walletinfo_line"
} else {
return "$offset_line_x$walletname_out$walletinfo_line"
}
}
function display-select-wallet($wallet_select_index, $wallet_list){
#show balance too!
$offset_line_x = " " * $FERRITEXT_INPUT_OFFSET_X # spacing each line horizontal
$wallet_list_count = $wallet_list.count
$wallet_output_arr = ,$null * $wallet_list_count
# find longest name, else use 16 letters of padding for [default wallet]
$longest = 16 # [default wallet] has length 16 - used for wallets without name
foreach ($wname in $wlist){
$wname_length = $wname.length
if ($wname_length -gt $longest){
$longest = $wname_length
}
}
if ($wallet_list_count -eq 1){
$walletname = [string]$wallet_list
$wallet_output_arr = generate-wallet-output-line($walletname)($wallet_select_index)($longest)($offset_line_x)(0)
} else {
if ($wallet_list_count -ne 0){
for ($i = 0; $i -lt $wallet_list_count; $i++){
$walletname = $wallet_list[$i]
$wallet_output_arr[$i] = generate-wallet-output-line($walletname)($wallet_select_index)($longest)($offset_line_x)($i)
}
} else {
$wallet_output_arr = ""
}
}