diff --git a/packages/ensjs/src/utils/contentHash.test.ts b/packages/ensjs/src/utils/contentHash.test.ts index e47facd5..fdca9c6b 100644 --- a/packages/ensjs/src/utils/contentHash.test.ts +++ b/packages/ensjs/src/utils/contentHash.test.ts @@ -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() + }) }) diff --git a/packages/ensjs/src/utils/contentHash.ts b/packages/ensjs/src/utils/contentHash.ts index 18778913..3f95e7ea 100644 --- a/packages/ensjs/src/utils/contentHash.ts +++ b/packages/ensjs/src/utils/contentHash.ts @@ -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)}` }