Skip to content

Commit

Permalink
contentHash updates + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jul 18, 2023
1 parent 71541d9 commit 9cf065c
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 222 deletions.
5 changes: 3 additions & 2 deletions packages/ensjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-disable @typescript-eslint/naming-convention */
const config = {
verbose: true,
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts'],
transform: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
transformIgnorePatterns: [],
moduleNameMapper: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'^(\\.{1,2}/.*)\\.js$': '$1',
'^multiformats$': '<rootDir>/node_modules/multiformats/src/index.js',
'^multiformats/(.*)$': '<rootDir>/node_modules/multiformats/src/$1',
},
}

Expand Down
3 changes: 2 additions & 1 deletion packages/ensjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"dependencies": {
"@adraffy/ens-normalize": "1.9.0",
"@ensdomains/address-encoder": "^0.2.18",
"@ensdomains/content-hash": "^2.5.7",
"@ensdomains/content-hash": "^3.0.0-beta.5",
"@ensdomains/dnsprovejs": "^0.4.1",
"abitype": "^0.8.0",
"cbor": "^8.1.0",
Expand Down Expand Up @@ -137,6 +137,7 @@
"jest": "^29.5.0",
"jest-environment-node": "^29.5.0",
"jest-localstorage-mock": "^2.4.21",
"multiformats": "^12.0.1",
"solc": "^0.8.13",
"ts-jest": "^29.1.1",
"ts-node": "^10.7.0",
Expand Down
1 change: 0 additions & 1 deletion packages/ensjs/src/@types/@ensdomains/content-hash.d.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/ensjs/src/errors/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,11 @@ export class CampaignReferenceTooLargeError extends BaseError {
})
}
}

export class InvalidContentHashError extends BaseError {
override name = 'InvalidContentHashError'

constructor() {
super('Invalid content hash')
}
}
4 changes: 2 additions & 2 deletions packages/ensjs/src/functions/public/_getContentHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { publicResolverContenthashSnippet } from '../../contracts/publicResolver
import type { Prettify, SimpleTransactionRequest } from '../../types.js'
import { EMPTY_ADDRESS } from '../../utils/consts.js'
import {
decodeContenthash,
decodeContentHash,
type DecodedContentHash,
} from '../../utils/contentHash.js'
import { generateFunction } from '../../utils/generateFunction.js'
Expand Down Expand Up @@ -44,7 +44,7 @@ const decode = async (
data,
})

return decodeContenthash(response)
return decodeContentHash(response)
}

const _getContentHash = generateFunction({ encode, decode })
Expand Down
4 changes: 2 additions & 2 deletions packages/ensjs/src/functions/subgraph/getNameHistory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { formatsByCoinType } from '@ensdomains/address-encoder'
import { gql } from 'graphql-request'
import type { ClientWithEns } from '../../contracts/consts.js'
import { decodeContenthash } from '../../utils/contentHash.js'
import { decodeContentHash } from '../../utils/contentHash.js'
import { namehash } from '../../utils/normalise.js'
import { createSubgraphClient } from './client.js'
import type {
Expand Down Expand Up @@ -300,7 +300,7 @@ const getNameHistory = async (
}
}
case 'ContenthashChanged': {
const { decoded: contentHash, protocolType } = decodeContenthash(
const { decoded: contentHash, protocolType } = decodeContentHash(
event.hash,
) || { protocolType: null, decoded: null }
return {
Expand Down
132 changes: 132 additions & 0 deletions packages/ensjs/src/utils/contentHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import {
decodeContentHash,
encodeContentHash,
getProtocolType,
isValidContentHash,
} from './contentHash.js'

const ipfs = {
decoded: 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4',
encoded:
'0xe3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f',
} as const

const ipns = {
decoded: 'k51qzi5uqu5dihst24f3rp2ej4co9berxohfkxaenbq1wjty7nrd5e9xp4afx1',
encoded:
'0xe50101720024080112205cbd1cc86ac20d6640795809c2a185bb2504538a2de8076da5a6971b8acb4715',
} as const

const swarm = {
decoded: 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162',
encoded:
'0xe40101fa011b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162',
} as const

const onion = {
decoded: 'zqktlwi4fecvo6ri',
encoded: '0xbc037a716b746c776934666563766f367269',
} as const

const onion3 = {
decoded: 'p53lf57qovyuvwsc6xnrppyply3vtqm7l6pcobkmyqsiofyeznfu5uqd',
encoded:
'0xbd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164',
} as const

const skynet = {
decoded: 'CABAB_1Dt0FJsxqsu_J4TodNCbCGvtFf1Uys_3EgzOlTcg',
encoded:
'0x90b2c60508004007fd43b74149b31aacbbf2784e874d09b086bed15fd54cacff7120cce95372',
} as const

const arweave = {
decoded: 'ys32Pt8uC7TrVxHdOLByOspfPEq2LO63wREHQIM9SJQ',
encoded:
'0x90b2ca05cacdf63edf2e0bb4eb5711dd38b0723aca5f3c4ab62ceeb7c1110740833d4894',
} as const

const typeArray = [
{
type: 'ipfs',
...ipfs,
},
{
type: 'ipns',
...ipns,
},
{
type: 'bzz',
...swarm,
},
{
type: 'onion',
...onion,
},
{
type: 'onion3',
...onion3,
},
{
type: 'sia',
...skynet,
},
{
type: 'ar',
...arweave,
},
] as const

const displayArray = typeArray.map((item) => ({
...item,
input: `${item.type}://${item.decoded}`,
}))

describe('decodeContentHash', () => {
it.each(typeArray)(
'$encoded => $type, $decoded',
({ type, encoded, decoded }) => {
expect(decodeContentHash(encoded)).toEqual({
protocolType: type,
decoded,
})
},
)
it('returns null when undefined', () => {
expect(decodeContentHash(undefined as any)).toBeNull()
})
it('returns null when empty bytes', () => {
expect(decodeContentHash('0x')).toBeNull()
})
})
describe('isValidContentHash', () => {
it('returns true for valid content hash', () => {
expect(
isValidContentHash(
'0xe3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f',
),
).toBe(true)
})
it('returns false for invalid content hash', () => {
expect(isValidContentHash('0x1234')).toBe(false)
})
})
describe('getProtocolType', () => {
it.each(displayArray)(
'$input => $type, $decoded',
({ input, type, decoded }) => {
expect(getProtocolType(input)).toEqual({
protocolType: type,
decoded,
})
},
)
it('returns null for invalid content hash', () => {
expect(getProtocolType('https://random')).toBe(null)
})
})
describe('encodeContentHash', () => {
it.each(displayArray)('$input => $encoded', ({ input, encoded }) => {
expect(encodeContentHash(input)).toEqual(encoded)
})
})
Loading

0 comments on commit 9cf065c

Please sign in to comment.