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

Commit

Permalink
Implement EIP1191
Browse files Browse the repository at this point in the history
  • Loading branch information
alcuadrado committed Jun 24, 2019
1 parent 599ba5b commit afdf86e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 16 deletions.
17 changes: 13 additions & 4 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ export const isZeroAddress = function(address: string): boolean {

/**
* Returns a checksummed address.
*
* If a chainId is provided, the result will be an EIP-1191 checksum. Otherwise, it will be an
* EIP-55 checksum.
*/
export const toChecksumAddress = function(address: string): string {
export const toChecksumAddress = function(address: string, chainId?: number): string {
address = ethjsUtil.stripHexPrefix(address).toLowerCase()
const hash = keccak(address).toString('hex')

const prefix = chainId !== undefined ? chainId.toString() + '0x' : ''

const hash = keccak(prefix + address).toString('hex')
let ret = '0x'

for (let i = 0; i < address.length; i++) {
Expand All @@ -50,9 +56,12 @@ export const toChecksumAddress = function(address: string): string {

/**
* Checks if the address is a valid checksummed address.
*
* If a chainId is provided, the address is compared to an EIP-1191 checksum. Otherwise, it will be
* compared to an EIP-55 checksum.
*/
export const isValidChecksumAddress = function(address: string): boolean {
return isValidAddress(address) && toChecksumAddress(address) === address
export const isValidChecksumAddress = function(address: string, chainId?: number): boolean {
return isValidAddress(address) && toChecksumAddress(address, chainId) === address
}

/**
Expand Down
104 changes: 92 additions & 12 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ describe('isValidSignature', function () {
// FIXME: add homestead test
})

const checksumAddresses = [
const eip55ChecksumAddresses = [
// All caps
'0x52908400098527886E0F7030069857D2E4169EE7',
'0x8617E340B3D01FA5F11F306F4090FD50E238070D',
Expand All @@ -656,23 +656,103 @@ const checksumAddresses = [
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb'
]

const eip1191ChecksummAddresses = {
1: [
'0x88021160c5C792225E4E5452585947470010289d',
'0x27b1FdB04752bBc536007a920D24ACB045561c26',
'0x52908400098527886e0f7030069857D2e4169EE7',
'0x5aaeB6053f3E94C9b9A09f33669435e7Ef1bEAed',
'0x8617E340b3d01FA5F11F306f4090FD50E238070d',
'0xd1220a0CF47C7B9Be7A2E6ba89F429762E7B9Adb',
'0xdBf03b407c01e7cD3CBea99509d93f8dDDC8C6fB',
'0xDe709F2102306220921060314715629080E2fb77',
'0xfb6916095Ca1dF60bB79cE92ce3ea74C37c5D359',
],
30: [
'0x6549F4939460DE12611948B3F82B88C3C8975323',
'0x27b1FdB04752BBc536007A920D24ACB045561c26',
'0x3599689E6292B81B2D85451025146515070129Bb',
'0x52908400098527886E0F7030069857D2E4169ee7',
'0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD',
'0x8617E340b3D01Fa5f11f306f4090fd50E238070D',
'0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB',
'0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB',
'0xDe709F2102306220921060314715629080e2FB77',
'0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359'
],
31: [
'0x42712D45473476B98452F434E72461577D686318',
'0x27B1FdB04752BbC536007a920D24acB045561C26',
'0x3599689e6292b81b2D85451025146515070129Bb',
'0x52908400098527886E0F7030069857D2e4169EE7',
'0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd',
'0x66f9664F97F2b50f62d13eA064982F936DE76657',
'0x8617e340b3D01fa5F11f306F4090Fd50e238070d',
'0xDE709F2102306220921060314715629080e2Fb77',
'0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359',
'0xd1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB',
'0xdbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB'
]
}

describe('.toChecksumAddress()', function () {
it('should work', function () {
for (let i = 0; i < checksumAddresses.length; i++) {
let tmp = checksumAddresses[i]
assert.equal(ethUtils.toChecksumAddress(tmp.toLowerCase()), tmp)
}
describe("EIP55", function () {
it('should work', function () {
for (let i = 0; i < eip55ChecksumAddresses.length; i++) {
let tmp = eip55ChecksumAddresses[i]
assert.equal(ethUtils.toChecksumAddress(tmp.toLowerCase()), tmp)
}
})
})

describe("EIP1191", function () {
it('Should encode the example addresses correctly', function () {
for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) {
for (const addr of addresses) {
assert.equal(ethUtils.toChecksumAddress(addr.toLowerCase(), chainId), addr)
}
}
})
})
})

describe('.isValidChecksumAddress()', function () {
it('should return true', function () {
for (let i = 0; i < checksumAddresses.length; i++) {
assert.equal(ethUtils.isValidChecksumAddress(checksumAddresses[i]), true)
}
describe("EIP55", function () {
it('should return true', function () {
for (let i = 0; i < eip55ChecksumAddresses.length; i++) {
assert.equal(ethUtils.isValidChecksumAddress(eip55ChecksumAddresses[i]), true)
}
})
it('should validate', function () {
assert.equal(ethUtils.isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a'), false)
})
})
it('should validate', function () {
assert.equal(ethUtils.isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a'), false)

describe("EIP1191", function () {
it('Should return true for the example addresses', function () {
for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) {
for (const addr of addresses) {
assert.equal(ethUtils.isValidChecksumAddress(addr, chainId), true)
}
}
})

it("Should return false for invalid cases", function () {
// If we set the chain id, an EIP55 encoded address should be invalid
for (let i = 0; i < eip55ChecksumAddresses.length; i++) {
assert.equal(ethUtils.isValidChecksumAddress(eip55ChecksumAddresses[i], 1), false)
}

assert.equal(ethUtils.isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a', 1), false)
})

it("Should return false if the wrong chain id is used", function () {
for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) {
for (const addr of addresses) {
assert.equal(ethUtils.isValidChecksumAddress(addr, chainId + 1), false)
}
}
})
})
})

Expand Down

0 comments on commit afdf86e

Please sign in to comment.