|
| 1 | +const crypto = require('crypto'); |
| 2 | + |
| 3 | +const DAY_IN_MS = 24 * 60 * 60 * 1000; |
| 4 | + |
| 5 | +class ConditionProofError extends Error { |
| 6 | + constructor(statusCode, code, message, details = {}) { |
| 7 | + super(message); |
| 8 | + this.name = 'ConditionProofError'; |
| 9 | + this.statusCode = statusCode; |
| 10 | + this.code = code; |
| 11 | + this.details = details; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function toIsoTimestamp(value, fieldName) { |
| 16 | + if (!value) { |
| 17 | + throw new ConditionProofError( |
| 18 | + 400, |
| 19 | + `MISSING_${fieldName.toUpperCase()}`, |
| 20 | + `${fieldName} is required.`, |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + const date = new Date(value); |
| 25 | + if (Number.isNaN(date.getTime())) { |
| 26 | + throw new ConditionProofError( |
| 27 | + 400, |
| 28 | + `INVALID_${fieldName.toUpperCase()}`, |
| 29 | + `${fieldName} must be a valid ISO timestamp.`, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + return date.toISOString(); |
| 34 | +} |
| 35 | + |
| 36 | +function sha256(inputBuffer) { |
| 37 | + return crypto.createHash('sha256').update(inputBuffer).digest('hex'); |
| 38 | +} |
| 39 | + |
| 40 | +function serializeProofForHash(proof) { |
| 41 | + return JSON.stringify({ |
| 42 | + lease_id: proof.lease_id, |
| 43 | + move_in_started_at: proof.move_in_started_at, |
| 44 | + submitted_at: proof.submitted_at, |
| 45 | + note_hash: proof.note_hash, |
| 46 | + photos: proof.photos.map((photo) => ({ |
| 47 | + captured_at: photo.captured_at, |
| 48 | + content_hash: photo.content_hash, |
| 49 | + filename: photo.filename, |
| 50 | + mime_type: photo.mime_type, |
| 51 | + })), |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +function createConditionProofService({ store }) { |
| 56 | + if (!store) { |
| 57 | + throw new Error('A condition proof store is required.'); |
| 58 | + } |
| 59 | + |
| 60 | + return { |
| 61 | + async createProof({ |
| 62 | + leaseId, |
| 63 | + moveInStartedAt, |
| 64 | + submittedAt, |
| 65 | + note, |
| 66 | + photos, |
| 67 | + }) { |
| 68 | + if (!leaseId || !String(leaseId).trim()) { |
| 69 | + throw new ConditionProofError( |
| 70 | + 400, |
| 71 | + 'MISSING_LEASE_ID', |
| 72 | + 'leaseId is required.', |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const normalizedLeaseId = String(leaseId).trim(); |
| 77 | + const normalizedMoveInStartedAt = toIsoTimestamp( |
| 78 | + moveInStartedAt, |
| 79 | + 'move_in_started_at', |
| 80 | + ); |
| 81 | + const normalizedSubmittedAt = toIsoTimestamp( |
| 82 | + submittedAt || new Date().toISOString(), |
| 83 | + 'submitted_at', |
| 84 | + ); |
| 85 | + |
| 86 | + const moveInStart = new Date(normalizedMoveInStartedAt).getTime(); |
| 87 | + const submitted = new Date(normalizedSubmittedAt).getTime(); |
| 88 | + const windowExpiresAt = new Date(moveInStart + DAY_IN_MS).toISOString(); |
| 89 | + |
| 90 | + if (submitted < moveInStart || submitted > moveInStart + DAY_IN_MS) { |
| 91 | + throw new ConditionProofError( |
| 92 | + 403, |
| 93 | + 'CONDITION_PROOF_WINDOW_CLOSED', |
| 94 | + 'Condition proof submissions are only allowed within the first 24 hours after move-in.', |
| 95 | + { |
| 96 | + lease_id: normalizedLeaseId, |
| 97 | + move_in_started_at: normalizedMoveInStartedAt, |
| 98 | + submitted_at: normalizedSubmittedAt, |
| 99 | + window_expires_at: windowExpiresAt, |
| 100 | + }, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + const normalizedNote = typeof note === 'string' ? note.trim() : ''; |
| 105 | + const normalizedPhotos = Array.isArray(photos) ? photos : []; |
| 106 | + if (!normalizedNote && normalizedPhotos.length === 0) { |
| 107 | + throw new ConditionProofError( |
| 108 | + 400, |
| 109 | + 'EMPTY_CONDITION_PROOF', |
| 110 | + 'At least one timestamped photo or a condition note is required.', |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + const processedPhotos = normalizedPhotos.map((photo, index) => { |
| 115 | + if (!photo || typeof photo !== 'object') { |
| 116 | + throw new ConditionProofError( |
| 117 | + 400, |
| 118 | + 'INVALID_PHOTO_PAYLOAD', |
| 119 | + `photos[${index}] must be an object.`, |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + const filename = String(photo.filename || '').trim(); |
| 124 | + const mimeType = String(photo.mime_type || 'application/octet-stream').trim(); |
| 125 | + const dataBase64 = String(photo.data_base64 || '').trim(); |
| 126 | + const capturedAt = toIsoTimestamp( |
| 127 | + photo.captured_at || normalizedSubmittedAt, |
| 128 | + `photos[${index}].captured_at`, |
| 129 | + ); |
| 130 | + |
| 131 | + if (!filename || !dataBase64) { |
| 132 | + throw new ConditionProofError( |
| 133 | + 400, |
| 134 | + 'INVALID_PHOTO_PAYLOAD', |
| 135 | + `photos[${index}] must include filename and data_base64.`, |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + const capturedTime = new Date(capturedAt).getTime(); |
| 140 | + if (capturedTime < moveInStart || capturedTime > moveInStart + DAY_IN_MS) { |
| 141 | + throw new ConditionProofError( |
| 142 | + 403, |
| 143 | + 'PHOTO_CAPTURE_OUTSIDE_ALLOWED_WINDOW', |
| 144 | + `photos[${index}] was captured outside the first 24 hours after move-in.`, |
| 145 | + { |
| 146 | + lease_id: normalizedLeaseId, |
| 147 | + captured_at: capturedAt, |
| 148 | + move_in_started_at: normalizedMoveInStartedAt, |
| 149 | + window_expires_at: windowExpiresAt, |
| 150 | + }, |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + let fileBuffer; |
| 155 | + try { |
| 156 | + fileBuffer = Buffer.from(dataBase64, 'base64'); |
| 157 | + } catch (_error) { |
| 158 | + throw new ConditionProofError( |
| 159 | + 400, |
| 160 | + 'INVALID_PHOTO_ENCODING', |
| 161 | + `photos[${index}].data_base64 must be valid base64.`, |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + if (!fileBuffer.length) { |
| 166 | + throw new ConditionProofError( |
| 167 | + 400, |
| 168 | + 'INVALID_PHOTO_ENCODING', |
| 169 | + `photos[${index}].data_base64 must decode to non-empty content.`, |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + return { |
| 174 | + filename, |
| 175 | + mime_type: mimeType, |
| 176 | + captured_at: capturedAt, |
| 177 | + size_bytes: fileBuffer.length, |
| 178 | + content_hash: sha256(fileBuffer), |
| 179 | + }; |
| 180 | + }); |
| 181 | + |
| 182 | + const noteHash = normalizedNote |
| 183 | + ? sha256( |
| 184 | + Buffer.from( |
| 185 | + JSON.stringify({ |
| 186 | + submitted_at: normalizedSubmittedAt, |
| 187 | + note: normalizedNote, |
| 188 | + }), |
| 189 | + ), |
| 190 | + ) |
| 191 | + : null; |
| 192 | + |
| 193 | + const proof = { |
| 194 | + proof_id: crypto.randomUUID(), |
| 195 | + lease_id: normalizedLeaseId, |
| 196 | + move_in_started_at: normalizedMoveInStartedAt, |
| 197 | + submitted_at: normalizedSubmittedAt, |
| 198 | + window_expires_at: windowExpiresAt, |
| 199 | + note: normalizedNote, |
| 200 | + note_hash: noteHash, |
| 201 | + photos: processedPhotos, |
| 202 | + }; |
| 203 | + |
| 204 | + proof.proof_hash = sha256(Buffer.from(serializeProofForHash(proof))); |
| 205 | + await store.save(proof); |
| 206 | + return proof; |
| 207 | + }, |
| 208 | + |
| 209 | + async listProofs(leaseId) { |
| 210 | + if (!leaseId || !String(leaseId).trim()) { |
| 211 | + throw new ConditionProofError( |
| 212 | + 400, |
| 213 | + 'MISSING_LEASE_ID', |
| 214 | + 'leaseId is required.', |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + return store.listByLeaseId(String(leaseId).trim()); |
| 219 | + }, |
| 220 | + |
| 221 | + async getArbitrationPacket(leaseId) { |
| 222 | + const proofs = await this.listProofs(leaseId); |
| 223 | + if (proofs.length === 0) { |
| 224 | + throw new ConditionProofError( |
| 225 | + 404, |
| 226 | + 'CONDITION_PROOF_NOT_FOUND', |
| 227 | + 'No immutable proof of condition was found for this lease.', |
| 228 | + { lease_id: String(leaseId).trim() }, |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + const immutableProofRootHash = sha256( |
| 233 | + Buffer.from( |
| 234 | + JSON.stringify( |
| 235 | + proofs.map((proof) => ({ |
| 236 | + proof_id: proof.proof_id, |
| 237 | + proof_hash: proof.proof_hash, |
| 238 | + })), |
| 239 | + ), |
| 240 | + ), |
| 241 | + ); |
| 242 | + |
| 243 | + return { |
| 244 | + lease_id: String(leaseId).trim(), |
| 245 | + proof_count: proofs.length, |
| 246 | + immutable_proof_root_hash: immutableProofRootHash, |
| 247 | + proofs, |
| 248 | + soroban_arbitration_hook: { |
| 249 | + hook: 'condition-proof-arbitration', |
| 250 | + lease_id: String(leaseId).trim(), |
| 251 | + immutable_proof_root_hash: immutableProofRootHash, |
| 252 | + }, |
| 253 | + }; |
| 254 | + }, |
| 255 | + }; |
| 256 | +} |
| 257 | + |
| 258 | +module.exports = { |
| 259 | + ConditionProofError, |
| 260 | + createConditionProofService, |
| 261 | +}; |
0 commit comments