diff --git a/interop/BrowserDockerfile b/interop/BrowserDockerfile index 69aa3b173f..b4a768df14 100644 --- a/interop/BrowserDockerfile +++ b/interop/BrowserDockerfile @@ -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. diff --git a/interop/Dockerfile b/interop/Dockerfile index 84f8e4562b..de4e131fcc 100644 --- a/interop/Dockerfile +++ b/interop/Dockerfile @@ -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 diff --git a/packages/transport-webrtc/package.json b/packages/transport-webrtc/package.json index 5bb3e2aacc..00d2d2e5a7 100644 --- a/packages/transport-webrtc/package.json +++ b/packages/transport-webrtc/package.json @@ -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", diff --git a/packages/transport-webrtc/src/error.ts b/packages/transport-webrtc/src/error.ts index c501f0ee0b..cde25484aa 100644 --- a/packages/transport-webrtc/src/error.ts +++ b/packages/transport-webrtc/src/error.ts @@ -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) } diff --git a/packages/transport-webrtc/src/private-to-public/sdp.ts b/packages/transport-webrtc/src/private-to-public/sdp.ts index 0520a820cf..ee3f489a5c 100644 --- a/packages/transport-webrtc/src/private-to-public/sdp.ts +++ b/packages/transport-webrtc/src/private-to-public/sdp.ts @@ -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 @@ -71,9 +70,8 @@ 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)) } /** @@ -81,7 +79,7 @@ export function decodeCerthash (certhash: string): { code: HashCode, name: HashN */ 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) @@ -89,22 +87,22 @@ export function ma2Fingerprint (ma: Multiaddr): string[] { 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) } } diff --git a/packages/transport-webrtc/src/private-to-public/transport.ts b/packages/transport-webrtc/src/private-to-public/transport.ts index 900f936a51..0a9626696f 100644 --- a/packages/transport-webrtc/src/private-to-public/transport.ts +++ b/packages/transport-webrtc/src/private-to-public/transport.ts @@ -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' @@ -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({ @@ -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') } @@ -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]) } } diff --git a/packages/transport-webrtc/test/sdp.spec.ts b/packages/transport-webrtc/test/sdp.spec.ts index cff8a794b1..a66c532339 100644 --- a/packages/transport-webrtc/test/sdp.spec.ts +++ b/packages/transport-webrtc/test/sdp.spec.ts @@ -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') })