Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utils tests #143

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/ensjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"start": "ts-node --files src/index.test.ts",
"test": "jest --runInBand",
"test:watch": "jest --watch --run-in-band",
"test:specific": "ts-node --files ",
"clean": "rm -rf ./dist ./README.md ./LICENSE",
"lint": "eslint ./src/* --no-error-on-unmatched-pattern",
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && echo > ./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
Expand Down
33 changes: 33 additions & 0 deletions packages/ensjs/src/utils/encoders/encodeAbi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { encodeAbi } from './encodeAbi.js'

describe('encodeAbi', () => {
it('encodes data as JSON', async () => {
const data = { foo: 'bar' }
const result = await encodeAbi({ encodeAs: 'json', data })
expect(result.contentType).toEqual(1)
expect(result.encodedData).toEqual('0x7b22666f6f223a22626172227d')
})

it('encodes data as zlib', async () => {
const data = { foo: 'bar' }
const result = await encodeAbi({ encodeAs: 'zlib', data })
expect(result.contentType).toEqual(2)
expect(result.encodedData).toEqual(
'0x789cab564acbcf57b2524a4a2c52aa05001d7a0434',
)
})

it('encodes data as cbor', async () => {
const data = { foo: 'bar' }
const result = await encodeAbi({ encodeAs: 'cbor', data })
expect(result.contentType).toEqual(4)
expect(result.encodedData).toEqual('0xa163666f6f63626172')
})

it('encodes data as uri', async () => {
const data = 'foo=bar'
const result = await encodeAbi({ encodeAs: 'uri', data })
expect(result.contentType).toEqual(8)
expect(result.encodedData).toEqual('0x666f6f3d626172')
})
})
32 changes: 32 additions & 0 deletions packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { encodeAbi } from './encodeAbi.js'
import { encodeSetAbi, type EncodeSetAbiParameters } from './encodeSetAbi.js'

describe('encodeSetAbi', () => {
const namehash =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
const contentType = 0
const encodedData = null

const parameters: EncodeSetAbiParameters = {
namehash,
contentType,
encodedData,
}

it('encodes the setAbi function data correctly', () => {
const expected =
'0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000'
const result = encodeSetAbi(parameters)
expect(result).toEqual(expected)
})

it('encodes the setAbi function data correctly with encodedData', async () => {
const result = encodeSetAbi({
namehash,
...(await encodeAbi({ encodeAs: 'json', data: { foo: 'bar' } })),
})
expect(result).toEqual(
'0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d7b22666f6f223a22626172227d00000000000000000000000000000000000000',
)
})
})
60 changes: 60 additions & 0 deletions packages/ensjs/src/utils/encoders/encodeSetAddr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { encodeSetAddr, type EncodeSetAddrParameters } from './encodeSetAddr.js'

const namehash =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'

describe('encodeSetAddr', () => {
const testCases: Array<[string, EncodeSetAddrParameters, string]> = [
[
'should encode setAddr with ETH address',
{
namehash,
coin: 'ETH',
value: '0x1234567890123456789012345678901234567890',
},
'0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141234567890123456789012345678901234567890000000000000000000000000',
],
[
'should encode setAddr with BTC address',
{
namehash,
coin: 'BTC',
value: 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq',
},
'0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000160014e8df018c7e326cc253faac7e46cdc51e68542c4200000000000000000000',
],
[
'should encode setAddr with coin type as number',
{
namehash,
coin: 60,
value: '0x1234567890123456789012345678901234567890',
},
'0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141234567890123456789012345678901234567890000000000000000000000000',
],
[
'should encode setAddr with coin type as string',
{
namehash,
coin: '60',
value: '0x1234567890123456789012345678901234567890',
},
'0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000141234567890123456789012345678901234567890000000000000000000000000',
],
[
'should encode setAddr with null value',
{
namehash,
coin: 'ETH',
value: null,
},
'0x8b95dd711234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000',
],
]

testCases.forEach(([description, parameters, expectedOutput]) => {
it(description, () => {
expect(encodeSetAddr(parameters)).toEqual(expectedOutput)
})
})
})
41 changes: 41 additions & 0 deletions packages/ensjs/src/utils/encoders/encodeSetContentHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getVersion } from '../../errors/error-utils.js'
import {
encodeSetContentHash,
type EncodeSetContentHashParameters,
} from './encodeSetContentHash.js'

describe('encodeSetContentHash', () => {
const namehash =
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
const contentHash = 'ipfs://QmXwMFNjzjRvZuPvzJfYJZ1QqX2QJjzj1YJZ1QqX2QJjzj'

it('encodes the function data correctly when contentHash is not null', () => {
const expectedEncodedData =
'0x304e6ade1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000026e301017012208e9cc47fde7ff64028480ec671a4ddb8f767a71ff71a73247f51a495a6f296340000000000000000000000000000000000000000000000000000'
const params: EncodeSetContentHashParameters = { namehash, contentHash }
expect(encodeSetContentHash(params)).toEqual(expectedEncodedData)
})

it('encodes the function data correctly when contentHash is null', () => {
const expectedEncodedData =
'0x304e6ade1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000'
const params: EncodeSetContentHashParameters = {
namehash,
contentHash: null,
}
expect(encodeSetContentHash(params)).toEqual(expectedEncodedData)
})

it('throws an error when contentHash is invalid', () => {
const params: EncodeSetContentHashParameters = {
namehash,
contentHash: 'invalid-content-hash',
}
expect(() => encodeSetContentHash(params))
.toThrowErrorMatchingInlineSnapshot(`
"Invalid content hash

Version: ${getVersion()}"
`)
})
})
21 changes: 21 additions & 0 deletions packages/ensjs/src/utils/encoders/encodeSetText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { encodeSetText, type EncodeSetTextParameters } from './encodeSetText.js'

describe('encodeSetText', () => {
const namehash =
'0x1234567890123456789012345678901234567890123456789012345678901234'
const key = 'email'
const value = '[email protected]'

const parameters: EncodeSetTextParameters = {
namehash,
key,
value,
}

it('encodes the function data correctly', () => {
const expected =
'0x10f13a8c1234567890123456789012345678901234567890123456789012345678901234000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005656d61696c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001074657374406578616d706c652e636f6d00000000000000000000000000000000'
const result = encodeSetText(parameters)
expect(result).toEqual(expected)
})
})
17 changes: 17 additions & 0 deletions packages/ensjs/src/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { truncateFormat } from './format.js'

describe('truncateFormat', () => {
it('should truncate a long name correctly', () => {
const longName =
'[0x12345678901234567890123456789012345678901234567890123456dfssss]'
const expected = '[0x1...sss]'
const result = truncateFormat(longName)
expect(result).toEqual(expected)
})

it('should not modify a short name', () => {
const shortName = 'example.eth'
const result = truncateFormat(shortName)
expect(result).toEqual(shortName)
})
})
6 changes: 0 additions & 6 deletions packages/ensjs/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
export const formatHashed = (name: string): string =>
name.replace(/(\[)(.{64})(\])/g, '0x$2')

export const truncateFormat = (name: string): string =>
name.replace(/(\[.{3})(.{58})(.{3}\])/g, '$1...$3')

export const bracketFormat = (name: string): string =>
name.replace(/(0x)(.{64})(?=\.)/g, '[$2]')
2 changes: 1 addition & 1 deletion packages/ensjs/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export {
type DecodedContentHash,
type ProtocolType,
} from './contentHash.js'
export { bracketFormat, formatHashed, truncateFormat } from './format.js'
export { truncateFormat } from './format.js'
export {
generateRecordCallArray,
type RecordOptions,
Expand Down
Loading