Skip to content

Commit 9043c3a

Browse files
authored
Update ferritext.ps1 to v0.2.0
1 parent f454d3f commit 9043c3a

File tree

1 file changed

+197
-14
lines changed

1 file changed

+197
-14
lines changed

ferritext.ps1

+197-14
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,205 @@
1+
12
#####
23
#
3-
# Ferritext by koh-gt
4-
# Place this script in the same directory level as ferrite-cli.exe
4+
# FECwall by koh-gt
55
#
66
# Tested to work on the following Ferrite Core versions:
77
#
88
# Recommended -- v3.1.0, v3.0.1, v3.0.0
99
# Depreciated -- v2.1.2, v2.1.1, v2.1.0, v2.0.0
1010
#
11-
# A Powershell script to add text inscriptions on the Ferrite blockchain.
11+
# A Powershell script to search for text inscriptions on the Ferrite blockchain.
1212
#
13+
# Place this script in the same directory level as ferrite-cli.exe
1314
#
1415
#####
1516

1617
[string] $rpcuser = "user"
1718
[string] $rpcpass = "password"
18-
[string] $wallet_name = "" # leave as "" for [default wallet] -- wallet.dat
1919
[string] $rpchost = "127.0.0.1"
2020

21-
# commands
21+
# get latest block height
2222
[string] $current_path = $PWD.Path
23+
24+
# commands
2325
[string] $ferrite_cli = "$current_path\ferrite-cli -rpcconnect=`"$rpchost`" -rpcuser=$rpcuser -rpcpassword=$rpcpass -testnet"
26+
[string] $getblockcount = "$ferrite_cli getblockcount"
27+
[string] $getblockhash = "$ferrite_cli getblockhash"
28+
[string] $getblock = "$ferrite_cli getblock"
29+
[string] $getrawtransaction = "$ferrite_cli getrawtransaction"
30+
31+
# blockchain variables
32+
[string] $maxheight = iex -Command $getblockcount
33+
[string] $genesishash = iex -Command "$getblockhash 0"
34+
35+
#[console]::Write("$maxheight`n$genesishash")
36+
37+
# limits
38+
$MAX_OP_RETURN_PER_BLOCK = 10
39+
40+
#####
41+
#
42+
# debug functions
43+
#
44+
#####
2445

2546
function hex([string] $text){return ([System.Text.Encoding]::UTF8.GetBytes($text) | ForEach-Object { $_.ToString("X2") }) -join ""}
2647

48+
#####
49+
#
50+
# main functions
51+
#
52+
#####
53+
54+
function getblockarray([int] $height){
55+
56+
[string] $blockdatahash = iex -Command "$getblockhash $height"
57+
[string] $blockdata = iex -Command "$getblock $blockdatahash"
58+
[string[]] $blockdataarr = $blockdata -split ":"
59+
60+
# filter out headers, split by comma
61+
$blockdataarr = $blockdataarr[1..$blockdataarr.count]
62+
63+
# output clean string array from getblock output
64+
$cleanarrlength = $blockdataarr.count + 1
65+
$clean_blockdataarr = @(0..$cleanarrlength)
66+
67+
$blockdataarr_count = $blockdataarr.count
68+
for ($i = 0; $i -lt $blockdataarr_count; $i++){
69+
70+
$line = $blockdataarr[$i]
71+
72+
$linearr = $blockdataarr[$i] -split ","
73+
$linearr_count = $linearr.count
74+
75+
# remove the last element - it is the header for the next line
76+
if ($linearr_count -ne 1){
77+
[int] $len_linedata = $linearr_count - 2
78+
$linedata = $linearr[0..$len_linedata]
79+
} else {
80+
$linedata = $linearr
81+
}
82+
$linedata = $linedata.Trim("[", "]", " ", "{", "}", "`"")
83+
84+
[string[]] $clean_blockdataarr[$i] = $linedata
85+
86+
}
87+
return $clean_blockdataarr
88+
89+
}
90+
91+
# useful function to check for transaction count -- skip blocks that only contain one transaction
92+
function getblocktransactioncount([int] $blocknum){
93+
$blockdata = getblockarray($blocknum)
94+
return $blockdata[16]
95+
}
96+
97+
98+
# takes in output of getblock and returns array of transaction hashes
99+
function getblocktransactionids([Object[]] $blockarr){return $blockarr[9]}
100+
101+
# returns array of transaction hashes in block
102+
function getblocktransactionhashes([int]$blocknum){return getblocktransactionids(getblockarray($blocknum))}
103+
104+
# returns transaction data from transaction hashes
105+
function getrawtransactioninfo([string] $transactionhash){return iex -Command "$getrawtransaction $transactionhash 1"} # verbose
106+
107+
function getblocktxinfo([int]$blocknum){
108+
$transactionhash = getblocktransactionhashes($blocknum)
109+
110+
$transaction_num = $transactionhash.count
111+
if ($transaction_num -eq 1){
112+
return getrawtransactioninfo($transactionhash)
113+
} else {
114+
$output = @(1..$transaction_num)
115+
for ($i = 0; $i -lt $transaction_num; $i++){
116+
$txhash = $transactionhash[$i]
117+
$output[$i] = getrawtransactioninfo($txhash)
118+
}
119+
return $output
120+
}
121+
}
122+
123+
function GetBlockOpReturnHex([int]$blocknum) {
124+
$tx_hashes_output = (GetBlockTxInfo $blocknum) -split ':' |
125+
Where-Object { $_ -match '\b\w*OP_RETURN\w*\b' } |
126+
ForEach-Object {
127+
[regex]::Matches($_, '[0-9a-f]+').Value
128+
}
129+
130+
if ($tx_hashes_output) {
131+
return $tx_hashes_output
132+
}
133+
}
134+
135+
136+
function hex-to-str([string]$hex){
137+
138+
$arr = @(1..$hex.length)
139+
$arr_index = 0
140+
for ($i = 0; $i -lt $hex.Length; $i = $i + 2){
141+
$nib1 = $hex[$i]
142+
$nib2 = $hex[($i + 1)]
143+
$byte = [system.convert]::ToInt16("$nib1$nib2", 16)
144+
$char = [char][byte]$byte
145+
146+
$arr[$arr_index] = $char
147+
$arr_index++
148+
}
149+
150+
return [string]::join("", $arr[0..($arr_index - 1)])
151+
152+
}
153+
154+
function hexarr-to-strarr([Object[]] $hexarr){
155+
156+
$hexarr_count = $hexarr.count
157+
if ($hexarr_count -eq 1){
158+
[string] $hex = $hexarr
159+
$str_hex = hex-to-str($hex)
160+
# [console]::Write("$str_hex")
161+
} else {
162+
$arr = @(1..$hexarr_count)
163+
for ($i = 0; $i -lt $hexarr_count; $i++){
164+
[string] $hex = $hexarr[$i]
165+
$arr[$i] = hex-to-str($hex)
166+
}
167+
return $arr
168+
}
169+
170+
}
171+
172+
function getblockopreturnstr ([int] $blocknum){
173+
$blockopreturn = getblockopreturnhex($blocknum)
174+
$blockopreturn_count = $blockopreturn.count
175+
if ($blockopreturn_count -ne 0){
176+
return hexarr-to-strarr($blockopreturn)
177+
} else {
178+
return
179+
}
180+
}
181+
182+
$blocks = 6
183+
$START = $maxheight - $blocks
184+
185+
[console]::WriteLine("Message explorer:")
186+
for ($i = $start; $i -le $maxheight; $i++){
187+
[console]::WriteLine("Blockheight $i")
188+
getblockopreturnstr($i)
189+
}
190+
191+
#####
192+
193+
[console]::WriteLine("------------------------------`n")
27194
$messagedata = Read-Host "Input data here"
28195
$messagedata_length = $messagedata.Length
29196

30197
$data = hex($messagedata)
31-
[console]::Write("`n------------------------------`n`nConsole commands: `n`nInput: `ncreaterawtransaction $data")
198+
#[console]::Write("`n------------------------------`n`nConsole commands: `n`nInput: `ncreaterawtransaction $data")
32199
$raw_tx_output = iex -command ("$ferrite_cli createrawtransaction" + ' "[]" "{""""""data"""""":""""""' + "$data" + '""""""}"')
33-
[console]::Write("`nOutput: `n$raw_tx_output`n`nInput: fundrawtransaction $raw_tx_output")
200+
#[console]::Write("`nOutput: `n$raw_tx_output`n`nInput: fundrawtransaction $raw_tx_output")
34201
$fundrawtx_output = iex -command ("$ferrite_cli -rpcwallet=$wallet_name fundrawtransaction $raw_tx_output")
35-
[console]::Write("`nOutput:`n $fundrawtx_output`n`n------------------------------`n")
202+
#[console]::Write("`nOutput:`n $fundrawtx_output`n`n------------------------------`n")
36203

37204
# -or ($_ -match '0\.[0-9]+')
38205
$fundrawtx_arr = ($fundrawtx_output -split ":") -split ","
@@ -45,12 +212,13 @@ $fundrawtx_hex, $fundrawtx_fee = $fundrawtx_arr | ForEach-Object {
45212
}
46213
} | Select-Object -Property Hex, Fee | ForEach-Object { $_.Hex, $_.Fee }
47214

48-
[console]::Write("`nMessage:`n`n$messagedata`n`nLength: $messagedata_length char`n`nThe network fee for sending this message is`nFEC $fundrawtx_fee`n`n")
49-
Read-Host "Press Enter to sign this transaction..."
215+
$nextheight = $maxheight + 1
216+
#[console]::Write("Message at block $nextheight | $messagedata | Length: $messagedata_length char`nNetwork fee: TFEC $fundrawtx_fee`n")
217+
#Read-Host "Press Enter to sign this transaction..."
50218

51-
[console]::Write("`n------------------------------`n`nConsole commands: `n`nInput: `nsignrawtransactionwithwallet $fundrawtx_hex")
219+
#[console]::Write("`n------------------------------`n`nConsole commands: `n`nInput: `nsignrawtransactionwithwallet $fundrawtx_hex")
52220
$signrawtx_output = iex -Command ("$ferrite_cli -rpcwallet=$wallet_name signrawtransactionwithwallet $fundrawtx_hex")
53-
[console]::Write("`nOutput:`n $signrawtx_output`n`n------------------------------`n")
221+
#[console]::Write("`nOutput:`n $signrawtx_output`n`n------------------------------`n")
54222

55223
$signrawtx_arr = ($signrawtx_output -split ":") -split ","
56224
$signrawtx_hex = $signrawtx_arr |
@@ -61,8 +229,23 @@ $signrawtx_hex = $signrawtx_arr |
61229

62230
Read-Host "Press Enter to send this transaction..."
63231

64-
[console]::Write("`n------------------------------`n`nConsole commands: `n`nInput: `nsendrawtransactionwithwallet $signrawtx_mhex")
232+
#[console]::Write("`n------------------------------`n`nConsole commands: `n`nInput: `nsendrawtransactionwithwallet $signrawtx_mhex")
65233
$sendrawtx_output = iex -Command ("$ferrite_cli -rpcwallet=$wallet_name sendrawtransaction $signrawtx_hex")
66-
[console]::Write("`nOutput:`n$sendrawtx_output`n`n------------------------------`n")
234+
[console]::Write("Output:`n$sendrawtx_output`n------------------------------`nMessage sent.")
67235

68236
start-sleep 5000
237+
238+
239+
240+
241+
242+
243+
244+
245+
246+
247+
248+
249+
250+
251+

0 commit comments

Comments
 (0)