Skip to content

Commit

Permalink
fix!: identity hash is Hasher<"identity", 0>
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Dec 2, 2024
1 parent 75712e7 commit 9027487
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/hashes/identity.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { coerce } from '../bytes.js'
import * as Digest from './digest.js'
import { from } from './hasher.js'

const code: 0x0 = 0x0
const name = 'identity'

const encode: (input: Uint8Array) => Uint8Array = coerce

function digest (input: Uint8Array): Digest.Digest<typeof code, number> {
return Digest.create(code, encode(input))
}

export const identity = { code, name, encode, digest }
export const identity = from({
name,
code,
encode
})
19 changes: 4 additions & 15 deletions test/test-multihash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ describe('multihash', () => {
assert.deepStrictEqual(hash2.bytes, hash.bytes)
})

it('hash identity async', async () => {
// eslint-disable-next-line @typescript-eslint/await-thenable
it('hash identity', async () => {
const hash = await identity.digest(fromString('test'))
assert.deepStrictEqual(hash.code, identity.code)
assert.deepStrictEqual(identity.code, 0)
Expand All @@ -108,17 +107,6 @@ describe('multihash', () => {
assert.deepStrictEqual(hash2.code, identity.code)
assert.deepStrictEqual(hash2.bytes, hash.bytes)
})

it('hash identity sync', () => {
const hash = identity.digest(fromString('test'))
assert.deepStrictEqual(hash.code, identity.code)
assert.deepStrictEqual(identity.code, 0)
assert.deepStrictEqual(hash.digest, fromString('test'))

const hash2 = decodeDigest(hash.bytes)
assert.deepStrictEqual(hash2.code, identity.code)
assert.deepStrictEqual(hash2.bytes, hash.bytes)
})
})
describe('decode', () => {
for (const { encoding, hex, size } of valid) {
Expand Down Expand Up @@ -161,11 +149,12 @@ describe('multihash', () => {
})

describe('hasCode', () => {
it('asserts that a multihash has the expected code', () => {
it('asserts that a multihash has the expected code', async () => {
const buf = Uint8Array.from([0, 1, 2, 3])

// remove code type from MultihashDigest
const hash = decodeDigest(identity.digest(buf).bytes)
const digest = await identity.digest(buf)
const hash = decodeDigest(digest.bytes)

// a function that requires a specific type of multihash
function needIdentity (_: MultihashDigest<0x0>): void {
Expand Down

0 comments on commit 9027487

Please sign in to comment.