Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
fix: Fetch local fingerprint from SDP
Browse files Browse the repository at this point in the history
Since firefox does not support `getFingerprints`, we fetch the local
fingerprint from the PeerConnection's `localDescription` field by
parsing the fingerprint line from the SDP.
  • Loading branch information
ckousik committed Apr 11, 2023
1 parent 6cc405c commit 5411ba5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
21 changes: 21 additions & 0 deletions src/sdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type { HashCode, HashName } from 'multihashes'
import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from './error.js'
import { CERTHASH_CODE } from './transport.js'

// import { detect } from 'detect-browser'

// const browser = detect()

const log = logger('libp2p:webrtc:sdp')

/**
Expand All @@ -15,6 +19,23 @@ const log = logger('libp2p:webrtc:sdp')
// @ts-expect-error - Not easy to combine these types.
export const mbdecoder: any = Object.values(bases).map(b => b.decoder).reduce((d, b) => d.or(b))

export function getLocalFingerprint (pc: RTCPeerConnection): string | undefined {
const localDescription = pc.localDescription
if (localDescription == null) {
return undefined
}
return getFingerprintFromSdp(localDescription.sdp)
}

const fingerprintRegex = /^a=fingerprint:(?:\w+-[0-9]+)\s(?<fingerprint>(:?[0-9a-fA-F]{2})+)$/gm
function getFingerprintFromSdp (sdp: string): string | undefined {
const searchResult = fingerprintRegex.exec(sdp)
if (searchResult == null) {
return ''
}

return searchResult.groups?.fingerprint
}
/**
* Get base2 | identity decoders
*/
Expand Down
15 changes: 4 additions & 11 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,12 @@ export class WebRTCDirectTransport implements Transport {
throw invalidArgument('no local certificate')
}

const localCert = pc.getConfiguration().certificates?.at(0)

if (localCert === undefined || localCert.getFingerprints().length === 0) {
throw invalidArgument('no fingerprint on local certificate')
}

const localFingerprint = localCert.getFingerprints()[0]

if (localFingerprint.value === undefined) {
throw invalidArgument('no fingerprint on local certificate')
const localFingerprint = sdp.getLocalFingerprint(pc)
if (localFingerprint == null) {
throw invalidArgument('no local fingerprint found')
}

const localFpString = localFingerprint.value.replace(/:/g, '')
const localFpString = localFingerprint.toLowerCase().replaceAll(':', '')
const localFpArray = uint8arrayFromString(localFpString, 'hex')
const local = multihashes.encode(localFpArray, hashCode)
const remote: Uint8Array = sdp.mbdecoder.decode(sdp.certhash(ma))
Expand Down

0 comments on commit 5411ba5

Please sign in to comment.