forked from tomtorggler/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoTools.ps1
279 lines (251 loc) · 7.63 KB
/
CryptoTools.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
function Convert-BchAddress {
<#
.SYNOPSIS
Converts Bitcoin Cash Address formats.
.DESCRIPTION
This function uses https://cashaddr.bitcoincash.org to convert Bitcoin Cash address formats. It supports legacy and bitcoincash: address formats.
The outout is a custom object containing all address formats and links to block explorers to for convenience.
.EXAMPLE
PS C:\> Convert-BchAddress -Address "1BppmEwfuWCB3mbGqah2YuQZEZQGK3MfWc"
This example converts a legacy address to the new bitcoincash format.
.EXAMPLE
PS C:\> Convert-BchAddress -Address "bitcoincash:qpmtetdtqpy5yhflnmmv8s35gkqfdnfdtywdqvue4p"
This example converts a new address to the legacy format.
.INPUTS
[string]
.OUTPUTS
[PSCustomObject]
.NOTES
More information: https://www.bitcoinabc.org/cashaddr
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
$Address
)
$uri = "https://cashaddr.bitcoincash.org/convert?address=$Address"
try {
$out = Invoke-RestMethod -Uri $uri -ErrorAction Stop
} catch {
Write-Warning "Could not connect."
}
$out | Add-Member -MemberType NoteProperty -Name "Blockchair" -Value "https://blockchair.com/bitcoin-cash/address/$($out.cashaddr.replace(":","%3A"))"
$out | Add-Member -MemberType NoteProperty -Name "Blockdozer" -Value "https://blockdozer.com/address/$($out.cashaddr.replace(":","%3A"))"
Write-Output $out
}
enum CryptoCurrencySymbol {
eth
btc
ltc
bch
}
class CryptoCurrency {
[string]$address
}
class ETH : CryptoCurrency {
ETH ($obj) {
$this.address = $obj.address
$this.balance = $obj.balance / 1000000000000000000
$this.sent = $obj.total_sent / 1000000000000000000
$this.received = $obj.total_received / 1000000000000000000
}
[double]$balance
[double]$sent
[double]$received
}
class BTC : CryptoCurrency {
BTC ($obj) {
$this.address = $obj.address
$this.balance = $obj.balance / 100000000
$this.sent = $obj.total_sent / 100000000
$this.received = $obj.total_received / 100000000
}
[double]$balance
[double]$sent
[double]$received
}
function Get-BlockCypherAddress {
[CmdletBinding()]
param(
[Parameter(
Mandatory=$true,
Position=0
)]
[string]
$Address,
[string]
$Version = "v1"
)
$Currency = Get-CurrencyFromAddress -Address $Address
$Address = $Address.Replace("0x","")
Write-Verbose "Currency is $Currency"
Invoke-RestMethod https://api.blockcypher.com/$version/$currency/main/addrs/$Address
}
function Get-BlockCypherBalance {
[CmdletBinding()]
param(
[Parameter(
Mandatory=$true,
Position=0
)]
[string]
$Address,
[string]
$Version = "v1"
)
$Currency = Get-CurrencyFromAddress -Address $Address
$Address = $Address.Replace("0x","")
Write-Verbose "Currency is $Currency"
$r = Invoke-RestMethod https://api.blockcypher.com/$version/$currency/main/addrs/$Address/balance
if ($r) {
switch($Currency) {
"eth" {
[ETH]::new($r)
}
Default {
[BTC]::new($r)
}
}
}
}
function Get-BlockCypherTransaction {
[CmdletBinding(DefaultParameterSetName="ByTxHash")]
param(
[Parameter(
Position=0,
ParameterSetName="ByTxHash",
Mandatory=$True
)]
[string]
$Hash,
[Parameter(
ParameterSetName="ByAddress",
Mandatory=$True
)]
[string]
$Address,
[string]
$Version = "v1",
[Parameter(ParameterSetName="ByTxHash")]
[CryptoCurrencySymbol]
$Currency = "eth"
)
# provide two possibilites of using the function,
# either get a specific tx by hash or get all tx for a certain address
if($Hash) {
$hash = $hash.Replace("^0x","")
Invoke-RestMethod https://api.blockcypher.com/$version/$currency/main/txs/$Hash
} elseif ($Address) {
Get-BlockCypherAddress -Address $Address | Select-Object -ExpandProperty txrefs
}
}
function Get-CurrencyFromAddress {
[CmdletBinding()]
param($Address)
switch ($Address) {
{ $_ -match "^[13][a-zA-Z0-9]{27,34}$" } { "btc" }
{ $_ -match "^L[a-km-zA-HJ-NP-Z1-9]{26,33}$" } { "ltc" }
{ $_ -match "^(0x)?[0-9a-f]{40}$" } { "eth" }
}
}
function Get-BchBalance {
[CmdletBinding()]
param(
$Address
)
$result = Invoke-RestMethod "https://api.blockchair.com/bitcoin-cash/dashboards/address/$address"
if($result){
$result.data
}
}
function Get-BchTransaction {
[CmdletBinding()]
param(
$Hash
)
$result = Invoke-RestMethod "https://api.blockchair.com/bitcoin-cash/transactions?q=hash($hash)"
if($result){
$result.data
}
}
#https://explorer.bitcoin.com/api/btc/addr/14hbuMuFCGSCzQr3TCVqPUKGkDGbepEoe2
#https://explorer.bitcoin.com/api/bch/txs/?address=14hbuMuFCGSCzQr3TCVqPUKGkDGbepEoe2
#https://explorer.bitcoin.com/api/bch/txs/?hash=ffe712907e11479bdca14445f66b5fbabd4a5e2c73bc7d7f1511429a470be036
function Get-ChainfeedTx {
[CmdletBinding()]
param (
[string]
$Hash
)
process {
$Response = Invoke-RestMethod https://chainfeed.org/tx/$Hash
New-Object -TypeName psobject -Property (@{
Sender = $Response.Sender
Text = [System.Text.Encoding]::ASCII.GetString($Response.data[-1].buf.data)
})
}
}
function get-unixtime {
param([datetime]$date)
if(-not $date) {
$date = ([datetime]::UtcNow).AddHours(-1)
}
$ts = New-TimeSpan -Start (Get-Date 01.01.1970) -End $date
[int]$ts.TotalSeconds
}
function invoke-whalealertapi {
param(
$endpoint = "status",
$apikey = $whalealertapi,
$baseUrl = "https://api.whale-alert.io/v1",
$currency,
$minvalue,
$start = (get-unixtime)
)
$header = @{
"X-WA-API-KEY" = $apikey
}
$uri = $baseUrl,$endpoint -join "/"
$uri += "?start=$start"
if($currency){
$uri += "¤cy=$currency"
}
if($minvalue){
$uri += "&min_value=$minvalue"
}
$r = Invoke-RestMethod -Uri $uri -Headers $header
$r.transactions
}
function convertto-ether {
param($i)
$i/1000000000000000000
}
function invoke-etherscanio {
param(
[Parameter()]
[ValidateSet("mainnet","goerli","rinkeby")]
$network = "mainnet",
[Parameter()]
[ValidateSet("account","transaction")]
$module = "account",
$apikey = $etherscanapi
)
if($network -eq "mainnet"){$url = ""} else {$url = "-$network"}
$baseUrl = "https://api{0}.etherscan.io/api?module={1}" -f $url,$module
if($address){
$baseUrl += "&action=balance&address=$address"
}
$baseUrl += "&apikey=$apikey"
$r = Invoke-RestMethod -Uri $baseUrl
$r
}
function Get-EtherScanBalance {
param(
$Address,
$apikey = "N9XZ94DDT1NBD18PJ8DZKXU76ZUNDVRQ83"
)
$out=invoke-etherscanio -address $Address
convertto-ether -i $out.result
}