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

fix: remove deprecated multihashes library #2522

Merged
merged 19 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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.9.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 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)

Check warning on line 115 in packages/transport-webrtc/src/error.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webrtc/src/error.ts#L115

Added line #L115 was not covered by tests
this.name = 'WebRTC/UnsupportedHashAlgorithmError'
}
}

export function unsupportedHashAlgorithm (algorithm: string): UnsupportedHashAlgorithmError {
return new UnsupportedHashAlgorithmError(algorithm)
export function unsupportedHashAlgorithmCode (code: number): UnsupportedHashAlgorithmError {
return new UnsupportedHashAlgorithmError(code)

Check warning on line 121 in packages/transport-webrtc/src/error.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webrtc/src/error.ts#L121

Added line #L121 was not covered by tests
}
31 changes: 14 additions & 17 deletions packages/transport-webrtc/src/private-to-public/sdp.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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,17 +69,16 @@
/**
* 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)

Expand All @@ -95,16 +92,16 @@
/**
* 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':
export function toSupportedHashFunction (code: number): string {
switch (code) {
case 0x11:
return 'sha1'

Check warning on line 98 in packages/transport-webrtc/src/private-to-public/sdp.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L98 was not covered by tests
case 0x12:
return 'sha-256'
case 'sha2-512':
case 0x13:
return 'sha-512'
default:
throw unsupportedHashAlgorithm(name)
throw unsupportedHashAlgorithmCode(code)

Check warning on line 104 in packages/transport-webrtc/src/private-to-public/sdp.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L104 was not covered by tests
}
}

Expand Down
26 changes: 20 additions & 6 deletions packages/transport-webrtc/src/private-to-public/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import * as p from '@libp2p/peer-id'
import { protocols } from '@multiformats/multiaddr'
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
import * as multihashes from 'multihashes'
import { sha1 } from 'multiformats/hashes/sha1'
import { sha256, sha512 } from 'multiformats/hashes/sha2'
import { concat } from 'uint8arrays/concat'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from '../error.js'
import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument, unsupportedHashAlgorithmCode } from '../error.js'
import { WebRTCMultiaddrConnection } from '../maconn.js'
import { DataChannelMuxerFactory } from '../muxer.js'
import { createStream } from '../stream.js'
Expand Down Expand Up @@ -135,7 +136,7 @@
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({ certificates: [certificate] })
Expand Down Expand Up @@ -192,7 +193,7 @@
// Do noise handshake.
// Set the Noise Prologue to libp2p-webrtc-noise:<FINGERPRINTS> before starting the actual Noise handshake.
// <FINGERPRINTS> is the concatenation of the of the two TLS fingerprints of A and B in their multihash byte representation, sorted in ascending order.
const fingerprintsPrologue = this.generateNoisePrologue(peerConnection, remoteCerthash.code, ma)
const fingerprintsPrologue = await this.generateNoisePrologue(peerConnection, remoteCerthash.code, ma)

Check warning on line 196 in packages/transport-webrtc/src/private-to-public/transport.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L196 was not covered by tests

// Since we use the default crypto interface and do not use a static key or early data,
// we pass in undefined for these parameters.
Expand Down Expand Up @@ -270,7 +271,7 @@
* 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 async generateNoisePrologue (pc: RTCPeerConnection, hasCode: number, ma: Multiaddr): Promise<Uint8Array> {
if (pc.getConfiguration().certificates?.length === 0) {
throw invalidArgument('no local certificate')
}
Expand All @@ -284,10 +285,23 @@

const localFpString = localFingerprint.trim().toLowerCase().replaceAll(':', '')
const localFpArray = uint8arrayFromString(localFpString, 'hex')
const local = multihashes.encode(localFpArray, hashCode)
const local = await encode(hasCode, localFpArray)

Check warning on line 288 in packages/transport-webrtc/src/private-to-public/transport.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L288 was not covered by tests
const remote: Uint8Array = sdp.mbdecoder.decode(sdp.certhash(ma))
const prefix = uint8arrayFromString('libp2p-webrtc-noise:')

return concat([prefix, local, remote])
}
}

async function encode (hasher: number, localFpArray: Uint8Array): Promise<Uint8Array> {
switch (hasher) {
case 0x11:
return (await sha1.digest(localFpArray)).bytes
case 0x12:
return (await sha256.digest(localFpArray)).bytes
case 0x13:
return (await sha512.digest(localFpArray)).bytes
default:
throw unsupportedHashAlgorithmCode(hasher)
}
}

Check warning on line 307 in packages/transport-webrtc/src/private-to-public/transport.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webrtc/src/private-to-public/transport.ts#L296-L307

Added lines #L296 - L307 were not covered by tests
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
Loading