Skip to content

Commit

Permalink
fix: manual exeption for onion/onion3 encoding length
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Sep 11, 2023
1 parent 5d8135a commit 4ca4dba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/ensjs/src/utils/contentHash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,10 @@ describe('encodeContentHash', () => {
it.each(displayArray)('$input => $encoded', ({ input, encoded }) => {
expect(encodeContentHash(input)).toEqual(encoded)
})
it('fails to encode onion with invalid length', () => {
expect(() => encodeContentHash('onion://123')).toThrow()
})
it('fails to encode onion3 with invalid length', () => {
expect(() => encodeContentHash('onion3://123')).toThrow()
})
})
6 changes: 6 additions & 0 deletions packages/ensjs/src/utils/contentHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,11 @@ export function encodeContentHash(text: string): Hex {

const internalCodec = getInternalCodec(typeData.protocolType)

// manual exceptions for onion/onion3 which are just utf8 encoded
if (internalCodec === 'onion' && typeData.decoded.length !== 16)
throw new InvalidContentHashError()
if (internalCodec === 'onion3' && typeData.decoded.length !== 56)
throw new InvalidContentHashError()

return `0x${encode(internalCodec, typeData.decoded)}`
}

0 comments on commit 4ca4dba

Please sign in to comment.