eth-rb 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eth.rb +23 -2
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65e04da1733386b288cd4b5e77ab983e287e132a1b0102f8f33992ba013dda93
4
- data.tar.gz: 15bbf5486f20692b25dbab12610a8832a26dbd0721734dbcfcb23f6e222ad74f
3
+ metadata.gz: 7cad4ab46c86f240e38bf48d77832f0c067582133d1bbdb4e1690cd5b52afa77
4
+ data.tar.gz: 54bafb01cd2fa6c8cf5dbbd50ec9e1831de9b1aee9dbc2a1b8c6a96eb59a113c
5
5
  SHA512:
6
- metadata.gz: 346f28432cbea631f2eb7648318453209947e46fb4e33544b328adfaae8de1847d16aa7329b9793aad598cd5efc9ea622a3fc34133ffbf9b19521dfa25398cd1
7
- data.tar.gz: 04c7c839490c5a6f98e457268f8ca001a914af4af92fd41936e5a198b710a6a84f37873e7ac6a3e9269418cf57f6662cd8f4293ab03a363028eb1c7380e5a656
6
+ metadata.gz: 8c851f58edc21271766a2e9da90a14a47b90cde4ef48bcd9d99f0a178e7c4c5b9b74f3776e8c6660d5b477d38690c288ccff584c3839e27549ab6fc51c72dcde
7
+ data.tar.gz: ccc8efb8c5b74378088eca855d613320049a5abdaa7efe52f20847fb784928a653399a5b0f0ffce2f000afb884e0acb74709255b2e35730dfcd8892a4cf5a4ff
data/lib/eth.rb CHANGED
@@ -6,6 +6,7 @@
6
6
  # Usage:
7
7
  # ./eth.rb — show latest block number
8
8
  # ./eth.rb <address> — show ETH balance of <address>
9
+ # ./eth.rb <private_key> — derive public key + address, show its balance
9
10
  # ./eth.rb --price — show current ETH/USD price
10
11
  # ./eth.rb --genkey or -g — generate a new private key + address
11
12
  # ./eth.rb --pubkey <key> — derive public key + address from <key>
@@ -24,7 +25,7 @@ require "digest/keccak"
24
25
  ALCHEMY_URL = "https://eth-mainnet.g.alchemy.com/v2/alch_PhpVkmsabZhYV69otj1rF"
25
26
  COINGECKO_URL = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
26
27
  CHAIN_ID = 1 # Ethereum mainnet
27
- VERSION = "0.1.7"
28
+ VERSION = "0.1.8"
28
29
 
29
30
  # ── JSON-RPC ────────────────────────────────────────────────────────────────
30
31
 
@@ -314,6 +315,22 @@ def show_block_number
314
315
  puts "Hex: #{hex_block}"
315
316
  end
316
317
 
318
+ # True if str is a 64-hex-digit private key (0x prefix optional)
319
+ def private_key?(str)
320
+ normalize_hex(str).match?(/\A[0-9a-fA-F]{64}\z/)
321
+ end
322
+
323
+ # Derive the public key and address from a private key, then show the balance
324
+ def show_balance_from_key(priv_hex)
325
+ pub_hex = "0x#{private_key_to_pubkey(priv_hex).unpack1('H*')}"
326
+ addr = "0x#{private_key_to_address(priv_hex).unpack1('H*')}"
327
+
328
+ puts "Public Key: #{pub_hex}"
329
+ puts "Address: #{addr}"
330
+ puts ""
331
+ show_balance(addr)
332
+ end
333
+
317
334
  def show_balance(address)
318
335
  hex_wei = rpc_call("eth_getBalance", [address, "latest"])
319
336
  wei = hex_wei.to_i(16)
@@ -387,7 +404,11 @@ def run_cli(args)
387
404
  elsif args[0] == "--send" || args[0] == "send"
388
405
  cmd_send(args[1..])
389
406
  else
390
- show_balance(args[0])
407
+ if private_key?(args[0])
408
+ show_balance_from_key(args[0])
409
+ else
410
+ show_balance(args[0])
411
+ end
391
412
  end
392
413
  end
393
414
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eth-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Khalid Shaikh