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

feat: add hasCode to Digest #308

Merged
merged 1 commit into from
Sep 16, 2024
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
7 changes: 7 additions & 0 deletions src/hashes/digest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ export class Digest<Code extends number, Size extends number> implements Multiha
this.bytes = bytes
}
}

/**
* Used to check that the passed multihash has the passed code
*/
export function hasCode <T extends number> (digest: MultihashDigest, code: T): digest is MultihashDigest<T> {
return digest.code === code
}
2 changes: 1 addition & 1 deletion src/hashes/identity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { coerce } from '../bytes.js'
import * as Digest from './digest.js'

const code = 0x0
const code: 0x0 = 0x0
Copy link
Member Author

@achingbrain achingbrain Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary otherwise the type of code is derived as number and not 0 which means digestHasCode(hash, identity.code) hints that the type of hash is MultihashDigest<number> when we want it to be MultihashDigest<0x0>.

digestHasCode(hash, 0x0) works without this change but it'd be nice to be able to reuse identity.code.

const name = 'identity'

const encode: (input: Uint8Array) => Uint8Array = coerce
Expand Down
27 changes: 26 additions & 1 deletion test/test-multihash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { hash as slSha512 } from '@stablelib/sha512'
import { assert } from 'aegir/chai'
import { sha1 as chSha1 } from 'crypto-hash'
import { fromHex, fromString } from '../src/bytes.js'
import { decode as decodeDigest, create as createDigest } from '../src/hashes/digest.js'
import { decode as decodeDigest, create as createDigest, hasCode as digestHasCode } from '../src/hashes/digest.js'
import { identity } from '../src/hashes/identity.js'
import { sha1 } from '../src/hashes/sha1.js'
import { sha256, sha512 } from '../src/hashes/sha2.js'
import invalid from './fixtures/invalid-multihash.js'
import valid from './fixtures/valid-multihash.js'
import type { MultihashDigest } from '../src/cid.js'

const sample = (code: number | string, size: number, hex: string): Uint8Array => {
const toHex = (i: number | string): string => {
Expand Down Expand Up @@ -158,4 +159,28 @@ describe('multihash', () => {
assert.match(String(error), /Unknown type, must be binary type/)
}
})

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

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

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

}

assert.isTrue(digestHasCode(hash, identity.code))

// @ts-expect-error fails to compile as hash is MultihashDigest<number>
needIdentity(hash)

// hint to tsc that hash is MultihashDigest<0x0>
if (digestHasCode(hash, identity.code)) {
needIdentity(hash)
}
})
})
})
Loading