All files / src/sdk Certificate.ts

5.4% Statements 4/74
0% Branches 0/6
0% Functions 0/5
5.47% Lines 4/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 23528x   28x 28x             28x                                                                                                                                                                                                                                                                                                                                                                                                                                                                
import { Utils } from "@bsv/sdk"
import { Base64String, CertificateFieldNameUnder50Bytes, HexString, OutpointString, PubKeyHex } from "./Wallet.interfaces"
import { WalletCrypto } from "./WalletCrypto"
import { KeyDeriver } from "./KeyDeriver"
 
/**
 * Represents an Identity Certificate as per the Wallet interface specifications.
 *
 * This class provides methods to serialize and deserialize certificates, as well as signing and verifying the certificate's signature.
 */
export class Certificate {
  /**
   * Type identifier for the certificate, base64 encoded string, 32 bytes.
   */
  type: Base64String
 
  /**
   * Unique serial number of the certificate, base64 encoded string, 32 bytes.
   */
  serialNumber: Base64String
 
  /**
   * The public key belonging to the certificate's subject, compressed public key hex string.
   */
  subject: PubKeyHex
 
  /**
   * Public key of the certifier who issued the certificate, compressed public key hex string.
   */
  certifier: PubKeyHex
 
  /**
   * The outpoint used to confirm that the certificate has not been revoked (TXID.OutputIndex), as a string.
   */
  revocationOutpoint: OutpointString
 
  /**
   * All the fields present in the certificate, with field names as keys and field values as strings.
   */
  fields: Record<CertificateFieldNameUnder50Bytes, string>
 
  /**
   * Certificate signature by the certifier's private key, DER encoded hex string.
  */
  signature?: HexString
 
  /**
   * Constructs a new Certificate.
   *
   * @param {Base64String} type - Type identifier for the certificate, base64 encoded string, 32 bytes.
   * @param {Base64String} serialNumber - Unique serial number of the certificate, base64 encoded string, 32 bytes.
   * @param {PubKeyHex} subject - The public key belonging to the certificate's subject, compressed public key hex string.
   * @param {PubKeyHex} certifier - Public key of the certifier who issued the certificate, compressed public key hex string.
   * @param {OutpointString} revocationOutpoint - The outpoint used to confirm that the certificate has not been revoked (TXID.OutputIndex), as a string.
   * @param {Record<CertificateFieldNameUnder50Bytes, string>} fields - All the fields present in the certificate.
   * @param {HexString} signature - Certificate signature by the certifier's private key, DER encoded hex string.
   */
  constructor (
    type: Base64String,
    serialNumber: Base64String,
    subject: PubKeyHex,
    certifier: PubKeyHex,
    revocationOutpoint: OutpointString,
    fields: Record<CertificateFieldNameUnder50Bytes, string>,
    signature?: HexString
  ) {
    this.type = type
    this.serialNumber = serialNumber
    this.subject = subject
    this.certifier = certifier
    this.revocationOutpoint = revocationOutpoint
    this.fields = fields
    this.signature = signature
  }
 
  /**
   * Serializes the certificate into binary format, with or without a signature.
   *
   * @param {boolean} [includeSignature=true] - Whether to include the signature in the serialization.
   * @returns {number[]} - The serialized certificate in binary format.
   */
  toBin (includeSignature: boolean = true): number[] {
    const writer = new Utils.Writer()
 
    // Write type (Base64String, 32 bytes)
    const typeBytes = Utils.toArray(this.type, 'base64')
    writer.write(typeBytes)
 
    // Write serialNumber (Base64String, 32 bytes)
    const serialNumberBytes = Utils.toArray(this.serialNumber, 'base64')
    writer.write(serialNumberBytes)
 
    // Write subject (33 bytes compressed PubKeyHex)
    const subjectBytes = Utils.toArray(this.subject, 'hex')
    writer.write(subjectBytes)
 
    // Write certifier (33 bytes compressed PubKeyHex)
    const certifierBytes = Utils.toArray(this.certifier, 'hex')
    writer.write(certifierBytes)
 
    // Write revocationOutpoint (TXID + OutputIndex)
    const [txid, outputIndex] = this.revocationOutpoint.split('.')
    const txidBytes = Utils.toArray(txid, 'hex')
    writer.write(txidBytes)
    writer.writeVarIntNum(Number(outputIndex))
 
    // Write fields
    const fieldEntries = Object.entries(this.fields)
    writer.writeVarIntNum(fieldEntries.length)
    for (const [fieldName, fieldValue] of fieldEntries) {
      // Field name
      const fieldNameBytes = Utils.toArray(fieldName, 'utf8')
      writer.writeVarIntNum(fieldNameBytes.length)
      writer.write(fieldNameBytes)
 
      // Field value
      const fieldValueBytes = Utils.toArray(fieldValue, 'utf8')
      writer.writeVarIntNum(fieldValueBytes.length)
      writer.write(fieldValueBytes)
    }
 
    // Write signature if included
    Iif (includeSignature && this.signature && this.signature.length > 0) {
      const signatureBytes = Utils.toArray(this.signature, 'hex')
      writer.writeVarIntNum(signatureBytes.length)
      writer.write(signatureBytes)
    }
 
    return writer.toArray()
  }
 
  /**
   * Deserializes a certificate from binary format.
   *
   * @param {number[]} bin - The binary data representing the certificate.
   * @returns {Certificate} - The deserialized Certificate object.
   */
  static fromBin (bin: number[]): Certificate {
    const reader = new Utils.Reader(bin)
 
    // Read type
    const typeBytes = reader.read(32)
    const type = Utils.toBase64(typeBytes)
 
    // Read serialNumber
    const serialNumberBytes = reader.read(32)
    const serialNumber = Utils.toBase64(serialNumberBytes)
 
    // Read subject (33 bytes)
    const subjectBytes = reader.read(33)
    const subject = Utils.toHex(subjectBytes)
 
    // Read certifier (33 bytes)
    const certifierBytes = reader.read(33)
    const certifier = Utils.toHex(certifierBytes)
 
    // Read revocationOutpoint
    const txidBytes = reader.read(32)
    const txid = Utils.toHex(txidBytes)
    const outputIndex = reader.readVarIntNum()
    const revocationOutpoint = `${txid}.${outputIndex}`
 
    // Read fields
    const numFields = reader.readVarIntNum()
    const fields: Record<CertificateFieldNameUnder50Bytes, string> = {}
    for (let i = 0; i < numFields; i++) {
      // Field name
      const fieldNameLength = reader.readVarIntNum()
      const fieldNameBytes = reader.read(fieldNameLength)
      const fieldName = Utils.toUTF8(fieldNameBytes)
 
      // Field value
      const fieldValueLength = reader.readVarIntNum()
      const fieldValueBytes = reader.read(fieldValueLength)
      const fieldValue = Utils.toUTF8(fieldValueBytes)
 
      fields[fieldName] = fieldValue
    }
 
    // Read signature if present
    let signature: string | undefined
    Iif (!reader.eof()) {
      const signatureLength = reader.readVarIntNum()
      const signatureBytes = reader.read(signatureLength)
      signature = Utils.toHex(signatureBytes)
    }
 
    return new Certificate(
      type,
      serialNumber,
      subject,
      certifier,
      revocationOutpoint,
      fields,
      signature
    )
  }
 
  /**
   * Verifies the certificate's signature.
   *
   * @returns {Promise<boolean>} - A promise that resolves to true if the signature is valid.
   */
  async verify (): Promise<boolean> {
    // A verifier can be any wallet capable of verifying signatures
    const verifier = new WalletCrypto(new KeyDeriver('anyone'))
    const verificationData = this.toBin(false) // Exclude the signature from the verification data
 
    const { valid } = await verifier.verifySignature({
      signature: Utils.toArray(this.signature, 'hex'),
      data: verificationData,
      protocolID: [2, 'certificate signature'],
      keyID: `${this.type} ${this.serialNumber}`,
      counterparty: this.certifier // The certifier is the one who signed the certificate
    })
    return valid
  }
 
  /**
   * Signs the certificate using the provided certifier wallet.
   *
   * @param {Wallet} certifier - The wallet representing the certifier.
   * @returns {Promise<void>}
   */
  async sign (certifier: WalletCrypto): Promise<void> {
    const preimage = this.toBin(false) // Exclude the signature when signing
    const { signature } = await certifier.createSignature({
      data: preimage,
      protocolID: [2, 'certificate signature'],
      keyID: `${this.type} ${this.serialNumber}`
    })
    this.signature = Utils.toHex(signature)
  }
}