test-ic-wallet-middleware-icrc
Version:
Ic middleware wallet ICRC protocol
64 lines (63 loc) • 3.41 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.AllowanceRepository = void 0;
const common_1 = require("@ic-wallet-middleware/common");
const typedi_1 = require("typedi");
let AllowanceRepository = class AllowanceRepository {
allowanceDataStorage;
constructor(allowanceDataStorage) {
this.allowanceDataStorage = allowanceDataStorage;
}
async getAssetAllowances(ledgerAddress) {
const storageAllowances = await this.allowanceDataStorage.getItems();
const allowances = storageAllowances.filter((a) => a.ledgerAddress === ledgerAddress);
return allowances;
}
async getAssetAllowance(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId) {
const documentId = this.getAllowanceDocumentId(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId);
const storageAllowance = await this.allowanceDataStorage.getItem(documentId);
if (!storageAllowance) {
throw new common_1.ValidationError("allowance.not.found", "", "Allowance Not Found");
}
return storageAllowance;
}
async addAllowance(model) {
const newAllowance = {
spenderPrincipal: model.spenderPrincipal,
spenderSubId: model.spenderSubId.toString(),
ledgerAddress: model.ledgerAddress,
subAccountId: model.subAccountId.toString()
};
await this.allowanceDataStorage.addItem(newAllowance);
return newAllowance;
}
async removeAllowance(form) {
await this.allowanceDataStorage.deleteItem(this.getAllowanceDocumentId(form.spenderPrincipal, form.ledgerAddress, form.subAccountId.toString(), form.spenderSubId.toString()));
}
async isExistStorageAllowance(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId) {
const id = this.getAllowanceDocumentId(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId);
const item = await this.allowanceDataStorage.getItem(id);
return item ? true : false;
}
getAllowanceDocumentId(spenderPrincipal, ledgerAddress, subAccountId, spenderSubId) {
return `${spenderPrincipal}_${ledgerAddress}_${subAccountId}_${spenderSubId}`;
}
};
exports.AllowanceRepository = AllowanceRepository;
exports.AllowanceRepository = AllowanceRepository = __decorate([
(0, typedi_1.Service)(),
__param(0, (0, typedi_1.Inject)("IAllowanceDataStorage")),
__metadata("design:paramtypes", [Object])
], AllowanceRepository);