Skip to content

Commit

Permalink
fix: remove deprecated multihashes library (#2522)
Browse files Browse the repository at this point in the history
Closes #2264

---------

Co-authored-by: Alex Potsides <[email protected]>
  • Loading branch information
maschad and achingbrain authored Jul 13, 2024
1 parent 3319ff4 commit e9b6a24
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 39 deletions.
16 changes: 9 additions & 7 deletions interop/BrowserDockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/playwright

# Copied since we won't have the repo to use if expanding from cache.
WORKDIR /app

# Workaround: https://github.com/docker/cli/issues/996
ARG BASE_IMAGE=node-js-libp2p-head
FROM ${BASE_IMAGE} as js-libp2p-base
COPY package.json ./
COPY ./packages ./packages
COPY ./interop ./interop

FROM mcr.microsoft.com/playwright
# disable colored output and CLI animation from test runners
ENV CI=true

COPY --from=js-libp2p-base /app/ /app/
RUN npm i
RUN npm run build

# We install browsers here instead of the cached version so that we use the latest browsers at run time.
# Ideally this would also be pinned, but playwright controls this, so there isn't much we can do about it.
Expand Down
2 changes: 1 addition & 1 deletion interop/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY ./packages ./packages
COPY ./interop ./interop

# disable colored output and CLI animation from test runners
ENV CI true
ENV CI=true

RUN npm i
RUN npm run build
Expand Down
1 change: 0 additions & 1 deletion packages/transport-webrtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"it-pushable": "^3.2.3",
"it-stream-types": "^2.0.1",
"multiformats": "^13.1.0",
"multihashes": "^4.0.3",
"node-datachannel": "^0.10.0",
"p-defer": "^4.0.1",
"p-event": "^6.0.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/transport-webrtc/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ export function unimplemented (methodName: string): UnimplementedError {
}

export class UnsupportedHashAlgorithmError extends WebRTCTransportError {
constructor (algo: string) {
super(`unsupported hash algorithm: ${algo}`, codes.ERR_HASH_NOT_SUPPORTED)
constructor (algo: number) {
super(`unsupported hash algorithm code: ${algo} please see the codes at https://github.com/multiformats/multicodec/blob/master/table.csv `, codes.ERR_HASH_NOT_SUPPORTED)
this.name = 'WebRTC/UnsupportedHashAlgorithmError'
}
}

export function unsupportedHashAlgorithm (algorithm: string): UnsupportedHashAlgorithmError {
return new UnsupportedHashAlgorithmError(algorithm)
export function unsupportedHashAlgorithmCode (code: number): UnsupportedHashAlgorithmError {
return new UnsupportedHashAlgorithmError(code)
}
36 changes: 17 additions & 19 deletions packages/transport-webrtc/src/private-to-public/sdp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { bases } from 'multiformats/basics'
import * as multihashes from 'multihashes'
import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from '../error.js'
import { type Multiaddr } from '@multiformats/multiaddr'
import { bases, digest } from 'multiformats/basics'
import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithmCode } from '../error.js'
import { CERTHASH_CODE } from './transport.js'
import type { LoggerOptions } from '@libp2p/interface'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { HashCode, HashName } from 'multihashes'
import type { MultihashDigest } from 'multiformats/hashes/interface'

/**
* Get base2 | identity decoders
Expand Down Expand Up @@ -71,40 +70,39 @@ export function certhash (ma: Multiaddr): string {
/**
* Convert a certhash into a multihash
*/
export function decodeCerthash (certhash: string): { code: HashCode, name: HashName, length: number, digest: Uint8Array } {
const mbdecoded = mbdecoder.decode(certhash)
return multihashes.decode(mbdecoded)
export function decodeCerthash (certhash: string): MultihashDigest {
return digest.decode(mbdecoder.decode(certhash))
}

/**
* Extract the fingerprint from a multiaddr
*/
export function ma2Fingerprint (ma: Multiaddr): string[] {
const mhdecoded = decodeCerthash(certhash(ma))
const prefix = toSupportedHashFunction(mhdecoded.name)
const prefix = toSupportedHashFunction(mhdecoded.code)
const fingerprint = mhdecoded.digest.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '')
const sdp = fingerprint.match(/.{1,2}/g)

if (sdp == null) {
throw invalidFingerprint(fingerprint, ma.toString())
}

return [`${prefix.toUpperCase()} ${sdp.join(':').toUpperCase()}`, fingerprint]
return [`${prefix} ${sdp.join(':').toUpperCase()}`, fingerprint]
}

/**
* Normalize the hash name from a given multihash has name
*/
export function toSupportedHashFunction (name: multihashes.HashName): string {
switch (name) {
case 'sha1':
return 'sha-1'
case 'sha2-256':
return 'sha-256'
case 'sha2-512':
return 'sha-512'
export function toSupportedHashFunction (code: number): 'SHA-1' | 'SHA-256' | 'SHA-512' {
switch (code) {
case 0x11:
return 'SHA-1'
case 0x12:
return 'SHA-256'
case 0x13:
return 'SHA-512'
default:
throw unsupportedHashAlgorithm(name)
throw unsupportedHashAlgorithmCode(code)
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/transport-webrtc/src/private-to-public/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { transportSymbol, serviceCapabilities } from '@libp2p/interface'
import * as p from '@libp2p/peer-id'
import { protocols } from '@multiformats/multiaddr'
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
import * as multihashes from 'multihashes'
import * as Digest from 'multiformats/hashes/digest'
import { concat } from 'uint8arrays/concat'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from '../error.js'
Expand Down Expand Up @@ -135,7 +135,7 @@ export class WebRTCDirectTransport implements Transport {
const certificate = await RTCPeerConnection.generateCertificate({
name: 'ECDSA',
namedCurve: 'P-256',
hash: sdp.toSupportedHashFunction(remoteCerthash.name)
hash: sdp.toSupportedHashFunction(remoteCerthash.code)
} as any)

const peerConnection = new RTCPeerConnection({
Expand Down Expand Up @@ -273,7 +273,7 @@ export class WebRTCDirectTransport implements Transport {
* Generate a noise prologue from the peer connection's certificate.
* noise prologue = bytes('libp2p-webrtc-noise:') + noise-responder fingerprint + noise-initiator fingerprint
*/
private generateNoisePrologue (pc: RTCPeerConnection, hashCode: multihashes.HashCode, ma: Multiaddr): Uint8Array {
private generateNoisePrologue (pc: RTCPeerConnection, hashCode: number, ma: Multiaddr): Uint8Array {
if (pc.getConfiguration().certificates?.length === 0) {
throw invalidArgument('no local certificate')
}
Expand All @@ -287,10 +287,10 @@ export class WebRTCDirectTransport implements Transport {

const localFpString = localFingerprint.trim().toLowerCase().replaceAll(':', '')
const localFpArray = uint8arrayFromString(localFpString, 'hex')
const local = multihashes.encode(localFpArray, hashCode)
const local = Digest.create(hashCode, localFpArray)
const remote: Uint8Array = sdp.mbdecoder.decode(sdp.certhash(ma))
const prefix = uint8arrayFromString('libp2p-webrtc-noise:')

return concat([prefix, local, remote])
return concat([prefix, local.bytes, remote])
}
}
3 changes: 1 addition & 2 deletions packages/transport-webrtc/test/sdp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ describe('SDP', () => {

// sha2-256 multihash 0x12 permanent
// https://github.com/multiformats/multicodec/blob/master/table.csv
expect(decoded.name).to.equal('sha2-256')
expect(decoded.code).to.equal(0x12)
expect(decoded.length).to.equal(32)
expect(decoded.size).to.equal(32)
expect(decoded.digest.toString()).to.equal('114,104,71,205,72,176,94,197,96,77,21,156,191,64,29,111,0,161,35,236,144,23,14,44,209,179,143,210,157,55,229,177')
})

Expand Down

0 comments on commit e9b6a24

Please sign in to comment.