diff --git a/package.json b/package.json index 4de48e9..2b73b44 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "base58check": "^2.0.0", "bech32": "^1.1.3", "bs58": "^4.0.1", - "content-hash": "^2.4.1", + "content-hash": "^2.4.4", "cross-fetch": "^3.0.2", "eth-ens-namehash": "^2.0.8", "ethers": "4.0.36", diff --git a/src/utils/contents.js b/src/utils/contents.js index 81a6604..01e7e22 100644 --- a/src/utils/contents.js +++ b/src/utils/contents.js @@ -1,7 +1,7 @@ import contentHash from 'content-hash' import { utils } from 'ethers' -const supportedCodecs = ['ipfs-ns', 'swarm-ns', 'onion', 'onion3'] +const supportedCodecs = ['ipfs-ns', 'swarm-ns', 'onion', 'onion3', 'zeronet'] export function decodeContenthash(encoded) { let decoded, protocolType, error @@ -20,6 +20,8 @@ export function decodeContenthash(encoded) { protocolType = 'onion' } else if (codec === 'onion3') { protocolType = 'onion3' + } else if (codec === 'zeronet') { + protocolType = 'zeronet' } else { decoded = encoded } @@ -47,7 +49,7 @@ export function encodeContenthash(text) { let content, contentType let encoded = false if (!!text) { - let matched = text.match(/^(ipfs|bzz|onion|onion3):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/) + let matched = text.match(/^(ipfs|bzz|onion|onion3|zeronet):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/) if (matched) { contentType = matched[1] content = matched[2] @@ -64,12 +66,14 @@ export function encodeContenthash(text) { } } else if (contentType === 'onion') { if(content.length == 16) { - encoded = '0x' + contentHash.encode('onion', content); + encoded = '0x' + contentHash.encode('onion', content) } } else if (contentType === 'onion3') { if(content.length == 56) { - encoded = '0x' + contentHash.encode('onion3', content); + encoded = '0x' + contentHash.encode('onion3', content) } + } else if (contentType === 'zeronet') { + encoded = '0x' + contentHash.encode('zeronet', content) } else { console.warn('Unsupported protocol or invalid value', { contentType, diff --git a/src/utils/contents.test.js b/src/utils/contents.test.js index 38ada4b..92a56f8 100644 --- a/src/utils/contents.test.js +++ b/src/utils/contents.test.js @@ -51,6 +51,16 @@ describe('test contenthash utility functions for swarm', () => { '0xbd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164' ) }) + + test('encodeContentHash returns encoded hash for zeronet protocol', () => { + const encodedContentHash = encodeContenthash( + 'zeronet://1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D' + ) + + expect(encodedContentHash).toBe( + '0xe6013148654c4c6f34757a6a614c65744678364e4833504d774650337162526254663344' + ) + }) }) describe('decodeContentHash', () => { @@ -99,6 +109,16 @@ describe('test contenthash utility functions for swarm', () => { expect(decoded.protocolType).toBe('onion3') expect(decoded.error).toBe(undefined) }) + + test('decodeContentHash returns decoded contenthash for zeronet protocol', () => { + const decoded = decodeContenthash( + '0xe6013148654c4c6f34757a6a614c65744678364e4833504d774650337162526254663344' + ) + + expect(decoded.decoded).toBe('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D') + expect(decoded.protocolType).toBe('zeronet') + expect(decoded.error).toBe(undefined) + }) }) describe('isValidContent', () => { @@ -132,6 +152,14 @@ describe('test contenthash utility functions for swarm', () => { expect(valid).toBe(true) }) + test('isValidContent returns true for real contenthash for zeronet protocol', () => { + const valid = isValidContenthash( + '0xe6013148654c4c6f34757a6a614c65744678364e4833504d774650337162526254663344' + ) + + expect(valid).toBe(true) + }) + test('isValidContent returns false for non hex', () => { const valid = isValidContenthash( '0xe40101fa011b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef31680z' diff --git a/src/utils/records.js b/src/utils/records.js index 1c0bc08..c00e756 100644 --- a/src/utils/records.js +++ b/src/utils/records.js @@ -27,7 +27,7 @@ export function getPlaceholder(recordType, contentType) { return 'Enter an Ethereum address' case 'content': if (contentType === 'contenthash') { - return 'Enter a content hash (eg: ipfs://..., bzz://..., onion://..., onion3://...)' + return 'Enter a content hash (eg: ipfs://..., bzz://..., onion://..., onion3://..., zeronet://...)' } else { return 'Enter a content' } diff --git a/yarn.lock b/yarn.lock index ae89b04..a847262 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2783,13 +2783,13 @@ content-disposition@0.5.2: resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.2.tgz#0cf68bb9ddf5f2be7961c3a85178cb85dba78cb4" integrity sha1-DPaLud318r55YcOoUXjLhdunjLQ= -content-hash@^2.4.1: - version "2.4.3" - resolved "https://registry.yarnpkg.com/content-hash/-/content-hash-2.4.3.tgz#89e6d295bbf2c53fb69b3d8fb43ae572ac7b6096" - integrity sha512-QBfQRlBBCJP94fV8zcUMChGMKzQMAZX6rn36yldc2A16C47tWdOTYjPosyZ7/AfdyW/xB5cP3RgZIAomnUDGIA== +content-hash@^2.4.4: + version "2.4.4" + resolved "https://registry.yarnpkg.com/content-hash/-/content-hash-2.4.4.tgz#4bec87caecfcff8cf1a37645301cbef4728a083f" + integrity sha512-3FaUsqt7VR725pVxe0vIScGI5efmpryIXdVSeXafQ63fb5gRGparAQlAGxTSOiv0yRg7YeliseXuB20ByD1duQ== dependencies: cids "^0.6.0" - multicodec "^0.5.4" + multicodec "^0.5.5" multihashes "^0.4.15" content-type@~1.0.4: @@ -6344,10 +6344,10 @@ multibase@~0.6.0: dependencies: base-x "3.0.4" -multicodec@^0.5.4: - version "0.5.4" - resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-0.5.4.tgz#ab2afb0cd00cd853c5a2eecb0dd1404dc7745388" - integrity sha512-0lPLiZ58b2jyXylx2qgda9/6N0YCNIpBxRsZ8sxYayVjEKh58XyNN74VTTQOR/ZCQFgbj0CsqfyRpEDPPlOMkw== +multicodec@^0.5.5: + version "0.5.5" + resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-0.5.5.tgz#55c2535b44eca9ea40a13771420153fe075bb36d" + integrity sha512-1kOifvwAqp9IdiiTKmpK2tS+LY6GHZdKpk3S2EvW4T32vlwDyA3hJoZtGauzqdedUPVNGChnTksEotVOCVlC+Q== dependencies: varint "^5.0.0"