Skip to content

Conversation

@v1rtl
Copy link
Contributor

@v1rtl v1rtl commented May 5, 2025

Description

Implements basic support for GetRemoteCertificate using getRemoteCertificates

I'm not sure how to write tests for it, do I mock JS globals?

Reference issue

Partially addresses libp2p/go-libp2p#3277

@codecov
Copy link

codecov bot commented May 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.15%. Comparing base (210cd95) to head (028d12e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3119      +/-   ##
==========================================
- Coverage   84.24%   84.15%   -0.09%     
==========================================
  Files          80       80              
  Lines        9112     9112              
==========================================
- Hits         7676     7668       -8     
- Misses       1019     1023       +4     
- Partials      417      421       +4     
Flag Coverage Δ
go 84.15% <ø> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Sean-Der
Copy link
Member

Sean-Der commented May 6, 2025

@talentlessguy that sounds good to me! However you think is best to test it.

The WASM stuff can be frustrating. So w/e works I am in support of :)

@v1rtl
Copy link
Contributor Author

v1rtl commented May 6, 2025

This demo in JavaScript works:

function bufferToHex(buffer) {
  const byteArray = new Uint8Array(buffer)
  return Array.from(byteArray)
    .map((b) => b.toString(16).padStart(2, '0'))
    .join('')
    .toUpperCase()
}

async function run() {
  const pcConfig = {
    iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
  }

  const localPC = new RTCPeerConnection(pcConfig)
  const remotePC = new RTCPeerConnection(pcConfig)

  const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
  const [track] = stream.getAudioTracks()
  localPC.addTrack(track, stream)

  localPC.onicecandidate = (e) => {
    if (e.candidate) remotePC.addIceCandidate(e.candidate)
  }
  remotePC.onicecandidate = (e) => {
    if (e.candidate) localPC.addIceCandidate(e.candidate)
  }

  localPC.onconnectionstatechange = () => {
    console.log('Connection state changed:', localPC.connectionState)
    if (localPC.connectionState === 'connected') {
      const [sender] = localPC.getSenders()
      if (sender?.transport instanceof RTCDtlsTransport) {
        const dtlsTransport = sender.transport
        const certs = dtlsTransport.getRemoteCertificates()
        if (certs.length > 0) {
          console.log(`Got ${certs.length} remote certificate(s):`) // Got 1 remote certificate
          certs.forEach((cert, i) => {
            console.log(`Certificate ${i + 1}:\n${bufferToHex(cert)}`) // DER raw cert
          })
        } else {
          console.warn('No remote certificates returned.')
        }
      } else {
        console.warn('DTLS transport not available.')
      }
    }
  }

  const offer = await localPC.createOffer()
  await localPC.setLocalDescription(offer)
  await remotePC.setRemoteDescription(offer)
  const answer = await remotePC.createAnswer()
  await remotePC.setLocalDescription(answer)
  await localPC.setRemoteDescription(answer)
}

run().catch(console.error)

Outputs a DER certificate

I'll try to write tests based on this demo. I'm also not sure if pion/webrtc implements onconnectionstatechange event listening for JS, at least I haven't seen it anywhere

@Sean-Der
Copy link
Member

Sean-Der commented May 6, 2025

OnConnectionStateChange is available for the WASM PeerConnection. Is it not working for you?

@v1rtl
Copy link
Contributor Author

v1rtl commented May 6, 2025

OnConnectionStateChange is available for the WASM PeerConnection. Is it not working for you?

I haven't tested it personally yet. I will try to recreate the demo using Go+WASM soon-ish to make sure it works exactly the same, and then write tests that will mock Web APIs

@JoeTurki JoeTurki added this to the V4.2.0 milestone Dec 4, 2025
@JoeTurki
Copy link
Member

JoeTurki commented Dec 4, 2025

@v1rtl hello, I'm adding a mock and merging this right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants