|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module BSV |
| 4 | + module Auth |
| 5 | + # Utility module for retrieving verifiable certificates from a wallet. |
| 6 | + # |
| 7 | + # Used during the certificate exchange phase of the BSV Auth peer protocol. |
| 8 | + # Lists certificates matching the requested certifiers and types, then calls |
| 9 | + # +prove_certificate+ for each to obtain a verifier-specific keyring for |
| 10 | + # selective field revelation. |
| 11 | + # |
| 12 | + # NOTE: Issue #424 documents a known bug in +WalletClient#prove_certificate+ — it |
| 13 | + # uses the wrong protocol ID (+certificate field revelation+ vs +certificate field |
| 14 | + # encryption+) and an incorrect key ID format. Until that bug is fixed, the keyring |
| 15 | + # produced here will be cryptographically incompatible with the TS/Go SDKs. |
| 16 | + module GetVerifiableCertificates |
| 17 | + module_function |
| 18 | + |
| 19 | + # Retrieve verifiable certificates from a wallet for presentation to a verifier. |
| 20 | + # |
| 21 | + # @param wallet [#list_certificates, #prove_certificate] the subject's wallet. |
| 22 | + # Duck-typed — if the wallet does not respond to both methods, returns +[]+. |
| 23 | + # @param requested_certificates [Hash] with keys: |
| 24 | + # - +:certifiers+ [Array<String>] list of certifier public key hexes |
| 25 | + # - +:types+ [Hash] type (Base64 string) → array of field names to reveal |
| 26 | + # @param verifier_identity_key [String] the verifier's compressed public key hex |
| 27 | + # @return [Array<VerifiableCertificate>] list of verifiable certificates ready for |
| 28 | + # presentation, or +[]+ on any failure |
| 29 | + def get_verifiable_certificates(wallet, requested_certificates, verifier_identity_key) |
| 30 | + return [] unless wallet.respond_to?(:list_certificates) && wallet.respond_to?(:prove_certificate) |
| 31 | + |
| 32 | + certifiers = requested_certificates[:certifiers] || requested_certificates['certifiers'] || [] |
| 33 | + types_map = requested_certificates[:types] || requested_certificates['types'] || {} |
| 34 | + |
| 35 | + list_result = wallet.list_certificates( |
| 36 | + certifiers: certifiers, |
| 37 | + types: types_map.keys |
| 38 | + ) |
| 39 | + |
| 40 | + certificates = list_result[:certificates] || list_result['certificates'] || [] |
| 41 | + return [] if certificates.empty? |
| 42 | + |
| 43 | + certificates.map do |cert| |
| 44 | + cert_type = cert[:type] || cert['type'] |
| 45 | + fields_to_reveal = types_map[cert_type] || types_map[cert_type.to_s] || types_map[cert_type.to_sym] || [] |
| 46 | + |
| 47 | + prove_result = wallet.prove_certificate( |
| 48 | + certificate: cert, |
| 49 | + fields_to_reveal: fields_to_reveal, |
| 50 | + verifier: verifier_identity_key |
| 51 | + ) |
| 52 | + |
| 53 | + keyring = prove_result[:keyring_for_verifier] || |
| 54 | + prove_result['keyring_for_verifier'] || |
| 55 | + prove_result[:keyringForVerifier] || |
| 56 | + prove_result['keyringForVerifier'] || |
| 57 | + {} |
| 58 | + |
| 59 | + VerifiableCertificate.new( |
| 60 | + type: cert_type, |
| 61 | + serial_number: cert[:serial_number] || cert['serial_number'] || |
| 62 | + cert[:serialNumber] || cert['serialNumber'], |
| 63 | + subject: cert[:subject] || cert['subject'], |
| 64 | + certifier: cert[:certifier] || cert['certifier'], |
| 65 | + revocation_outpoint: cert[:revocation_outpoint] || cert['revocation_outpoint'] || |
| 66 | + cert[:revocationOutpoint] || cert['revocationOutpoint'], |
| 67 | + fields: cert[:fields] || cert['fields'] || {}, |
| 68 | + keyring: keyring, |
| 69 | + signature: cert[:signature] || cert['signature'] |
| 70 | + ) |
| 71 | + end |
| 72 | + rescue StandardError |
| 73 | + # Auto-fetch is best-effort: wallet may raise UnsupportedActionError, |
| 74 | + # key derivation errors, or other failures. The peer protocol handles |
| 75 | + # "no certificates" gracefully — the requesting peer enforces its own |
| 76 | + # certificate requirements independently. |
| 77 | + [] |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments