Skip to content

Commit

Permalink
Adds support to check for the ethereum "null address" (#296)
Browse files Browse the repository at this point in the history
* Many dreams have gone to the null address to die, this provides a
  simple check for developers to opt in to avoiding that fate.
  • Loading branch information
mattrasband authored Dec 17, 2024
1 parent e5e4181 commit 64ddfa7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/eth/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Eth

# The {Eth::Address} class to handle checksummed Ethereum addresses.
class Address
NULL = "0x0000000000000000000000000000000000000000"

# Provides a special checksum error if EIP-55 is violated.
class CheckSumError < StandardError; end
Expand Down Expand Up @@ -51,6 +52,13 @@ def valid?
end
end

# Checks that the address is the null address.
#
# @return [Boolean] true if the address is the null address.
def null?
address == NULL
end

# Generate a checksummed address.
#
# @return [String] prefixed hexstring representing an checksummed address.
Expand Down
24 changes: 22 additions & 2 deletions spec/eth/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
subject(:bob) { Address.new "0x7291d3cd257053bac810ee2c55fd7c154bd455af" }

it "generates functional addresses" do

# generates a functional key for alice of type Address
expect(alice).to be_an_instance_of Address
expect(bob).to be_an_instance_of Address
Expand All @@ -18,7 +17,6 @@
end

it "prefixes an unprefixed address" do

# ensure both addresses contains the 0x prefix
# alice's address was initialized without 0x
expect(alice.address).to start_with "0x"
Expand Down Expand Up @@ -125,6 +123,28 @@
end
end

describe ".null?" do
let(:null) { Address::NULL }
let(:addresses) do
[
"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed",
"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359",
"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb",
"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb",
]
end

it "returns true for the null address" do
expect(Address.new(null)).to be_null
end

it "returns false for a valid address" do
addresses.each do |address|
expect(Address.new(address)).not_to be_null
end
end
end

describe ".checksummed" do
let(:addresses) do
[
Expand Down

0 comments on commit 64ddfa7

Please sign in to comment.