@ic-wallet-kit/hpl
Version:
Ic middleware wallet HPL protocol
136 lines (135 loc) • 5.83 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HplStateCacheRepository = void 0;
const common_1 = require("@ic-wallet-kit/common");
require("reflect-metadata");
const typedi_1 = require("typedi");
let HplStateCacheRepository = class HplStateCacheRepository {
identifierService;
logger;
storage;
constructor(logger, identifierService, storage) {
this.identifierService = identifierService;
this.logger = logger;
this.storage = storage;
}
getHplRemotesToLookState(canisterId) {
const key = this.getRemotesToLookStateKey(canisterId);
return this.getInternalState(key);
}
setHplRemotesToLookState(canisterId, hplAsset) {
const key = this.getRemotesToLookStateKey(canisterId);
this.storage.setItem(key, (0, common_1.jsonStringify)(hplAsset));
}
removeHplRemotesToLookState(canisterId) {
const key = this.getRemotesToLookStateKey(canisterId);
this.storage.removeItem(key);
}
getHplVirtualAccountState(canisterId) {
const key = this.getVirtualAccountStateKey(canisterId);
return this.getInternalState(key);
}
setHplVirtualAccountState(canisterId, hplVirtualAccount) {
const key = this.getVirtualAccountStateKey(canisterId);
this.storage.setItem(key, (0, common_1.jsonStringify)(hplVirtualAccount));
}
removeHplVirtualAccountState(canisterId) {
const key = this.getVirtualAccountStateKey(canisterId);
this.storage.removeItem(key);
}
getHplAccountState(canisterId) {
const key = this.getAccountStateKey(canisterId);
return this.getInternalState(key);
}
setHplAccountState(canisterId, hplAccounts) {
const key = this.getAccountStateKey(canisterId);
this.storage.setItem(key, (0, common_1.jsonStringify)(hplAccounts));
}
getHplRemoteAccountState(canisterId) {
const key = this.getRemoteAccountStateKey(canisterId);
return this.getInternalState(key);
}
setHplRemoteAccountState(canisterId, hplAccounts) {
const key = this.getRemoteAccountStateKey(canisterId);
this.storage.setItem(key, (0, common_1.jsonStringify)(hplAccounts));
}
removeHplAccountState(canisterId) {
const key = this.getAccountStateKey(canisterId);
this.storage.removeItem(key);
}
getHplFtSuppliesState(canisterId) {
const key = this.getStateKey(canisterId);
return this.getInternalState(key);
}
setHplFtSuppliesState(canisterId, hplAsset) {
const key = this.getStateKey(canisterId);
this.storage.setItem(key, (0, common_1.jsonStringify)(hplAsset));
}
removeHplFtSuppliesState(canisterId) {
const key = this.getStateKey(canisterId);
this.storage.removeItem(key);
}
getHplAdminState(canisterId) {
const key = this.getAdminStateKey(canisterId);
return this.getInternalState(key);
}
setHplAdminState(canisterId, hplAsset) {
const key = this.getAdminStateKey(canisterId);
this.storage.setItem(key, (0, common_1.jsonStringify)(hplAsset));
}
removeAdminState(canisterId) {
const key = this.getAdminStateKey(canisterId);
this.storage.removeItem(key);
}
getInternalState(key) {
const value = this.storage.getItem(key);
if (value) {
try {
const model = (0, common_1.jsonParse)(value);
return model;
}
catch (e) {
this.logger.logError(e, "Wrong local cache data", [value]);
return undefined;
}
}
return undefined;
}
getStateKey(canisterId) {
return `${this.identifierService.getPrincipal()}-${canisterId}-hplFtSuppliesState`;
}
getAccountStateKey(canisterId) {
return `${this.identifierService.getPrincipal()}-${canisterId}-hplAccountState`;
}
getRemoteAccountStateKey(canisterId) {
return `${this.identifierService.getPrincipal()}-${canisterId}-hplRemoteAccountState`;
}
getVirtualAccountStateKey(canisterId) {
return `${this.identifierService.getPrincipal()}-${canisterId}-hplVirtualAccountState`;
}
getRemotesToLookStateKey(canisterId) {
return `${this.identifierService.getPrincipal()}-${canisterId}-hplRemotesToLookState`;
}
getAdminStateKey(canisterId) {
return `${this.identifierService.getPrincipal()}-${canisterId}-hplAdminState`;
}
};
exports.HplStateCacheRepository = HplStateCacheRepository;
exports.HplStateCacheRepository = HplStateCacheRepository = __decorate([
(0, typedi_1.Service)("IHplStateCacheRepository"),
__param(0, (0, typedi_1.Inject)("ILogger")),
__param(2, (0, typedi_1.Inject)("IStorage")),
__metadata("design:paramtypes", [Object, common_1.IdentifierService, Object])
], HplStateCacheRepository);