Skip to content

Commit e9b6a24

Browse files
maschadachingbrain
andauthored
fix: remove deprecated multihashes library (#2522)
Closes #2264 --------- Co-authored-by: Alex Potsides <[email protected]>
1 parent 3319ff4 commit e9b6a24

File tree

7 files changed

+37
-39
lines changed

7 files changed

+37
-39
lines changed

interop/BrowserDockerfile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# syntax=docker/dockerfile:1
1+
FROM mcr.microsoft.com/playwright
22

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

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

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

11-
COPY --from=js-libp2p-base /app/ /app/
12+
RUN npm i
13+
RUN npm run build
1214

1315
# We install browsers here instead of the cached version so that we use the latest browsers at run time.
1416
# Ideally this would also be pinned, but playwright controls this, so there isn't much we can do about it.

interop/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ COPY ./packages ./packages
99
COPY ./interop ./interop
1010

1111
# disable colored output and CLI animation from test runners
12-
ENV CI true
12+
ENV CI=true
1313

1414
RUN npm i
1515
RUN npm run build

packages/transport-webrtc/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
"it-pushable": "^3.2.3",
6565
"it-stream-types": "^2.0.1",
6666
"multiformats": "^13.1.0",
67-
"multihashes": "^4.0.3",
6867
"node-datachannel": "^0.10.0",
6968
"p-defer": "^4.0.1",
7069
"p-event": "^6.0.1",

packages/transport-webrtc/src/error.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ export function unimplemented (methodName: string): UnimplementedError {
111111
}
112112

113113
export class UnsupportedHashAlgorithmError extends WebRTCTransportError {
114-
constructor (algo: string) {
115-
super(`unsupported hash algorithm: ${algo}`, codes.ERR_HASH_NOT_SUPPORTED)
114+
constructor (algo: number) {
115+
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)
116116
this.name = 'WebRTC/UnsupportedHashAlgorithmError'
117117
}
118118
}
119119

120-
export function unsupportedHashAlgorithm (algorithm: string): UnsupportedHashAlgorithmError {
121-
return new UnsupportedHashAlgorithmError(algorithm)
120+
export function unsupportedHashAlgorithmCode (code: number): UnsupportedHashAlgorithmError {
121+
return new UnsupportedHashAlgorithmError(code)
122122
}

packages/transport-webrtc/src/private-to-public/sdp.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { bases } from 'multiformats/basics'
2-
import * as multihashes from 'multihashes'
3-
import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from '../error.js'
1+
import { type Multiaddr } from '@multiformats/multiaddr'
2+
import { bases, digest } from 'multiformats/basics'
3+
import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithmCode } from '../error.js'
44
import { CERTHASH_CODE } from './transport.js'
55
import type { LoggerOptions } from '@libp2p/interface'
6-
import type { Multiaddr } from '@multiformats/multiaddr'
7-
import type { HashCode, HashName } from 'multihashes'
6+
import type { MultihashDigest } from 'multiformats/hashes/interface'
87

98
/**
109
* Get base2 | identity decoders
@@ -71,40 +70,39 @@ export function certhash (ma: Multiaddr): string {
7170
/**
7271
* Convert a certhash into a multihash
7372
*/
74-
export function decodeCerthash (certhash: string): { code: HashCode, name: HashName, length: number, digest: Uint8Array } {
75-
const mbdecoded = mbdecoder.decode(certhash)
76-
return multihashes.decode(mbdecoded)
73+
export function decodeCerthash (certhash: string): MultihashDigest {
74+
return digest.decode(mbdecoder.decode(certhash))
7775
}
7876

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

8886
if (sdp == null) {
8987
throw invalidFingerprint(fingerprint, ma.toString())
9088
}
9189

92-
return [`${prefix.toUpperCase()} ${sdp.join(':').toUpperCase()}`, fingerprint]
90+
return [`${prefix} ${sdp.join(':').toUpperCase()}`, fingerprint]
9391
}
9492

9593
/**
9694
* Normalize the hash name from a given multihash has name
9795
*/
98-
export function toSupportedHashFunction (name: multihashes.HashName): string {
99-
switch (name) {
100-
case 'sha1':
101-
return 'sha-1'
102-
case 'sha2-256':
103-
return 'sha-256'
104-
case 'sha2-512':
105-
return 'sha-512'
96+
export function toSupportedHashFunction (code: number): 'SHA-1' | 'SHA-256' | 'SHA-512' {
97+
switch (code) {
98+
case 0x11:
99+
return 'SHA-1'
100+
case 0x12:
101+
return 'SHA-256'
102+
case 0x13:
103+
return 'SHA-512'
106104
default:
107-
throw unsupportedHashAlgorithm(name)
105+
throw unsupportedHashAlgorithmCode(code)
108106
}
109107
}
110108

packages/transport-webrtc/src/private-to-public/transport.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { transportSymbol, serviceCapabilities } from '@libp2p/interface'
33
import * as p from '@libp2p/peer-id'
44
import { protocols } from '@multiformats/multiaddr'
55
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
6-
import * as multihashes from 'multihashes'
6+
import * as Digest from 'multiformats/hashes/digest'
77
import { concat } from 'uint8arrays/concat'
88
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
99
import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from '../error.js'
@@ -135,7 +135,7 @@ export class WebRTCDirectTransport implements Transport {
135135
const certificate = await RTCPeerConnection.generateCertificate({
136136
name: 'ECDSA',
137137
namedCurve: 'P-256',
138-
hash: sdp.toSupportedHashFunction(remoteCerthash.name)
138+
hash: sdp.toSupportedHashFunction(remoteCerthash.code)
139139
} as any)
140140

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

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

294-
return concat([prefix, local, remote])
294+
return concat([prefix, local.bytes, remote])
295295
}
296296
}

packages/transport-webrtc/test/sdp.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ describe('SDP', () => {
3939

4040
// sha2-256 multihash 0x12 permanent
4141
// https://github.com/multiformats/multicodec/blob/master/table.csv
42-
expect(decoded.name).to.equal('sha2-256')
4342
expect(decoded.code).to.equal(0x12)
44-
expect(decoded.length).to.equal(32)
43+
expect(decoded.size).to.equal(32)
4544
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')
4645
})
4746

0 commit comments

Comments
 (0)