Signing License Agreements

Here’s how to sign a license agreement:

import { SpaceportSDK } from '@spaceport/sdk'
import { privateKeyToAccount } from 'viem/accounts'
import * as fs from 'fs'
import * as path from 'path'

async function signAgreement() {
  // Initialize SDK with licensee wallet
  const account = privateKeyToAccount(`0x${process.env.LICENSEE_PRIVATE_KEY}`)
  const sdk = SpaceportSDK.createWithAccount(account, {
    chain: 'devnet',
    apiKey: process.env.SDK_API_KEY,
  })

  // 1. Get or create user
  let currentUser
  try {
    currentUser = await sdk.users.getCurrentUser()
  } catch (error) {
    currentUser = await sdk.users.create({
      email: '[email protected]',
      firstName: 'Bob',
      lastName: 'Licensee',
    })
  }

  // 2. Get available listings
  const listings = await sdk.listings.getAll({ status: 'published' })
  const firstListing = listings[0]

  // 3. Get smart license details
  const smartLicense = await sdk.smartLicenses.getByHash(
    firstListing.smart_license_hash,
    'published'
  )

  // 4. Prepare signing details
  const { requiredDetails, requiredFilesMetadata } =
    sdk.agreements.getRequiredSigningDetails(smartLicense)

  // Load required PDF file
  const pdfPath = path.join(__dirname, '../media/pdf-sample_0.pdf')
  const pdfBuffer = await fs.promises.readFile(pdfPath)
  const pdfFile = new File([pdfBuffer], 'originalGameConcept.pdf', { type: 'application/pdf' })

  // Fill in required details
  const licenseeSigningDetails = {
    details: {
      licenseeName: 'Acme Corporation',
      licenseeOrgAddress: '123 Business St, Metropolis, NY 10001',
      licenseeAppointeeName: 'Jane Smith',
      licenseeAppointeeEmail: '[email protected]',
      licenseeJurisdiction: 'Delaware, USA',
      licenseeNotificationName: 'Legal Department',
      licenseeNotificationEmail: '[email protected]',
      // Add other required fields...
    },
    files: {
      originalGameConcept: pdfFile,
    },
  }

  // 5. Create and sign agreement
  const result = await sdk.agreements.createAndSign({
    coreListingId: firstListing.id,
    onChainListingIds: firstListing.assets.map((asset) => asset.on_chain_listing_id),
    licenseeSigningDetails,
    storageAccess: 'public',
  })

  // 6. Monitor job status
  if (result.jobId) {
    const jobStatus = await sdk.utils.pollJobStatus(result.jobId, {
      timeout: 300000,
      interval: 3000,
    })
    console.log('Agreement signing status:', jobStatus.status)
  }
}