@arkade-os/sdk
Version:
Bitcoin wallet SDK with Taproot and Ark integration
106 lines (105 loc) • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContractRepositoryImpl = void 0;
const getContractStorageKey = (id, key) => `contract:${id}:${key}`;
const getCollectionStorageKey = (type) => `collection:${type}`;
class ContractRepositoryImpl {
constructor(storage) {
this.storage = storage;
}
async getContractData(contractId, key) {
const stored = await this.storage.getItem(getContractStorageKey(contractId, key));
if (!stored)
return null;
try {
const data = JSON.parse(stored);
return data;
}
catch (error) {
console.error(`Failed to parse contract data for ${contractId}:${key}:`, error);
return null;
}
}
async setContractData(contractId, key, data) {
try {
await this.storage.setItem(getContractStorageKey(contractId, key), JSON.stringify(data));
}
catch (error) {
console.error(`Failed to persist contract data for ${contractId}:${key}:`, error);
throw error; // Rethrow to notify caller of failure
}
}
async deleteContractData(contractId, key) {
try {
await this.storage.removeItem(getContractStorageKey(contractId, key));
}
catch (error) {
console.error(`Failed to remove contract data for ${contractId}:${key}:`, error);
throw error; // Rethrow to notify caller of failure
}
}
async getContractCollection(contractType) {
const stored = await this.storage.getItem(getCollectionStorageKey(contractType));
if (!stored)
return [];
try {
const collection = JSON.parse(stored);
return collection;
}
catch (error) {
console.error(`Failed to parse contract collection ${contractType}:`, error);
return [];
}
}
async saveToContractCollection(contractType, item, idField) {
const collection = await this.getContractCollection(contractType);
// Validate that the item has the required id field
const itemId = item[idField];
if (itemId === undefined || itemId === null) {
throw new Error(`Item is missing required field '${String(idField)}'`);
}
// Find existing item index without mutating the original collection
const existingIndex = collection.findIndex((i) => i[idField] === itemId);
// Build new collection without mutating the cached one
let newCollection;
if (existingIndex !== -1) {
// Replace existing item
newCollection = [
...collection.slice(0, existingIndex),
item,
...collection.slice(existingIndex + 1),
];
}
else {
// Add new item
newCollection = [...collection, item];
}
try {
await this.storage.setItem(getCollectionStorageKey(contractType), JSON.stringify(newCollection));
}
catch (error) {
console.error(`Failed to persist contract collection ${contractType}:`, error);
throw error; // Rethrow to notify caller of failure
}
}
async removeFromContractCollection(contractType, id, idField) {
// Validate input parameters
if (id === undefined || id === null) {
throw new Error(`Invalid id provided for removal: ${String(id)}`);
}
const collection = await this.getContractCollection(contractType);
// Build new collection without the specified item
const filtered = collection.filter((item) => item[idField] !== id);
try {
await this.storage.setItem(getCollectionStorageKey(contractType), JSON.stringify(filtered));
}
catch (error) {
console.error(`Failed to persist contract collection removal for ${contractType}:`, error);
throw error; // Rethrow to notify caller of failure
}
}
async clearContractData() {
await this.storage.clear();
}
}
exports.ContractRepositoryImpl = ContractRepositoryImpl;