test-ic-wallet-middleware-icrc
Version:
Ic middleware wallet ICRC protocol
156 lines (155 loc) • 6.16 kB
JavaScript
;
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.AllowanceLocalCache = void 0;
const common_1 = require("@ic-wallet-middleware/common");
const typedi_1 = require("typedi");
let AllowanceLocalCache = class AllowanceLocalCache {
identifierService;
logger;
storage;
constructor(logger, identifierService, storage) {
this.identifierService = identifierService;
this.logger = logger;
this.storage = storage;
}
getAllowance(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId) {
const items = this.getItems();
let item = items.find((r) => r.ledgerAddress === ledgerAddress
&& r.spenderPrincipal === spenderPrincipal
&& r.subAccountId === subAccountId
&& r.spenderSubId === spenderSubId);
return item;
}
addAllowance(info) {
const allowance = {
ledgerAddress: info.ledgerAddress,
spenderPrincipal: info.spenderPrincipal,
spenderSubId: info.spenderSubId.toString(),
subAccountId: info.subAccountId.toString(),
amount: info.amount,
expiration: info.expiration
};
const items = this.getItems();
const item = items.find((r) => r.ledgerAddress === allowance.ledgerAddress
&& r.spenderPrincipal === allowance.spenderPrincipal
&& r.subAccountId === allowance.subAccountId
&& r.spenderSubId === allowance.spenderSubId);
if (!item) {
items.push(allowance);
}
else {
const index = items.indexOf(item);
items[index] = allowance;
}
this.setItem(items);
return allowance;
}
updateOrAddAllowance(allowance) {
const items = this.getItems();
let item = items.find((r) => r.ledgerAddress === allowance.ledgerAddress
&& r.spenderPrincipal === allowance.spenderPrincipal
&& r.subAccountId === allowance.subAccountId
&& r.spenderSubId === allowance.spenderSubId);
if (!item) {
items.push(allowance);
}
else {
const index = items.indexOf(item);
items[index] = allowance;
}
this.setItem(items);
return allowance;
}
removeAllowance(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId) {
let items = this.getItems();
items = items.filter((r) => !(r.ledgerAddress == ledgerAddress
&& r.spenderPrincipal == spenderPrincipal
&& r.subAccountId == subAccountId
&& r.spenderSubId == spenderSubId));
this.setItem(items);
}
getAllowanceForContact(senderPrincipal, ledgerAddress, subAccountId) {
const items = this.getItemsForContact();
let item = items.find((r) => r.ledgerAddress === ledgerAddress
&& r.senderPrincipal === senderPrincipal
&& r.subAccountId === subAccountId);
return item;
}
updateAllowanceForContact(allowance) {
const items = this.getItemsForContact();
let item = items.find((r) => r.ledgerAddress === allowance.ledgerAddress
&& r.senderPrincipal === allowance.senderPrincipal
&& r.subAccountId === allowance.subAccountId);
if (!item) {
items.push(allowance);
}
else {
const index = items.indexOf(item);
items[index] = allowance;
}
this.setItemForContact(items);
return allowance;
}
getItems() {
const key = this.getKey();
let result = [];
const value = this.storage.getItem(key);
if (value) {
try {
result = (0, common_1.jsonParse)(value);
}
catch (e) {
this.logger.logError(e, "Wrong local cache data", [value]);
}
}
return result;
}
getItemsForContact() {
const key = this.getKeyForContact();
const value = this.storage.getItem(key);
if (value) {
try {
const allowances = (0, common_1.jsonParse)(value);
return allowances;
}
catch (e) {
this.logger.logError(e, "Wrong local cache data", [value]);
return [];
}
}
return [];
}
setItem(allowances) {
const key = this.getKey();
this.storage.setItem(key, (0, common_1.jsonStringify)(allowances));
}
setItemForContact(allowances) {
const key = this.getKeyForContact();
this.storage.setItem(key, (0, common_1.jsonStringify)(allowances));
}
getKey() {
return `${this.identifierService.getPrincipal()}-allowances`;
}
getKeyForContact() {
return `${this.identifierService.getPrincipal()}-contact-allowances`;
}
};
exports.AllowanceLocalCache = AllowanceLocalCache;
exports.AllowanceLocalCache = AllowanceLocalCache = __decorate([
(0, typedi_1.Service)(),
__param(0, (0, typedi_1.Inject)("ILogger")),
__param(2, (0, typedi_1.Inject)("IStorage")),
__metadata("design:paramtypes", [Object, common_1.IdentifierService, Object])
], AllowanceLocalCache);