Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #285 from ethereumjs/add-address-equals
Browse files Browse the repository at this point in the history
Add `Address.equals(address: Address)`
  • Loading branch information
ryanio committed Jan 26, 2021
2 parents dd2882d + 93c43f7 commit e19262e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/address.ts
Expand Up @@ -75,11 +75,18 @@ export class Address {
return new Address(generateAddress2(from.buf, salt, initCode))
}

/**
* Is address equal to another.
*/
equals(address: Address): boolean {
return this.buf.equals(address.buf)
}

/**
* Is address zero.
*/
isZero(): boolean {
return this.buf.equals(Address.zero().buf)
return this.equals(Address.zero())
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/address.spec.ts
Expand Up @@ -84,4 +84,22 @@ describe('Address', () => {
addressBuf.fill(0)
assert.equal(address.toString(), str)
})

it('should compare equality properly', () => {
const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a'
const address1 = Address.fromString(str)
const address2 = new Address(Buffer.from(str.slice(2), 'hex'))
assert.ok(address1.equals(address2))
assert.ok(address1.buf.equals(address2.buf))

const str2 = '0xcd4EC7b66fbc029C116BA9Ffb3e59351c20B5B06'
const address3 = Address.fromString(str2)
assert.ok(!address1.equals(address3))

const address3LowerCase = Address.fromString(str2.toLowerCase())
assert.ok(address3.equals(address3LowerCase))

const address4 = Address.zero()
assert.ok(!address1.equals(address4))
})
})

0 comments on commit e19262e

Please sign in to comment.