test-ic-wallet-middleware-icrc
Version:
Ic middleware wallet ICRC protocol
137 lines (136 loc) • 6.77 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.ContactRepository = void 0;
const common_1 = require("@ic-wallet-middleware/common");
require("reflect-metadata");
const typedi_1 = require("typedi");
let ContactRepository = class ContactRepository {
contactDataStorage;
constructor(contactDataStorage) {
this.contactDataStorage = contactDataStorage;
}
async getContacts() {
return await this.contactDataStorage.getItems();
}
async addContact(contact) {
await this.contactDataStorage.addItem(contact);
}
async updateContactName(principal, newName) {
const contact = await this.getContactByPrincipal(principal);
contact.name = newName;
await this.contactDataStorage.updateItem(contact);
return contact;
}
async removeContact(principal) {
await this.contactDataStorage.deleteItem(principal);
}
async addAssetContact(form) {
const contact = await this.getContactByPrincipal(form.principal);
const asset = contact.assets.find((a) => a.ledgerAddress === form.ledgerAddress);
if (asset) {
throw new common_1.ValidationError("contact.asset.already.exists", "ledgerAddress", `Asset already exists. Ledger address: ${form.ledgerAddress}`);
}
const newAsset = {
ledgerAddress: form.ledgerAddress,
subAccounts: []
};
contact.assets.push(newAsset);
await this.contactDataStorage.updateItem(contact);
return contact;
}
async removeAssetContact(form) {
const contact = await this.getContactByPrincipal(form.principal);
contact.assets = contact.assets.filter((a) => a.ledgerAddress !== form.ledgerAddress);
await this.contactDataStorage.updateItem(contact);
return contact;
}
async addSubAccountContact(form) {
const contact = await this.getContactByPrincipal(form.principal);
const asset = this.getContactAsset(contact, form.ledgerAddress);
const subAccountId = form.subAccountId.toString();
const subAccount = asset.subAccounts.find((sa) => sa.subAccountId === subAccountId);
if (subAccount) {
throw new common_1.ValidationError("adding.contact.subaccount.already.exists", "oldSubAccountId", `SubAccount already exists. SubAccountId: ${form.subAccountId.toString()}`);
}
const newSubAccount = {
name: form.subAccountName,
subAccountId: subAccountId
};
asset.subAccounts.push(newSubAccount);
await this.contactDataStorage.updateItem(contact);
return contact;
}
async updateSubAccountContact(form) {
const contact = await this.getContactByPrincipal(form.principal);
const asset = this.getContactAsset(contact, form.ledgerAddress);
const subAccount = asset.subAccounts.find((sa) => sa.subAccountId === form.oldSubAccountId.toString());
if (!subAccount) {
throw new common_1.ValidationError("updating.contact.subaccount.not.exists", "oldSubAccountId", `SubAccount not exists. SubAccountId: ${form.oldSubAccountId.toString()}`);
}
if (form.newSubAccountId.notEquals(form.oldSubAccountId)) {
const existSubAccountId = asset.subAccounts.find((sa) => sa.subAccountId === form.newSubAccountId.toString());
if (existSubAccountId) {
throw new common_1.ValidationError("updating.contact.subaccount.index.already.exists", "newSubAccountIndex", `newSubAccountIndex already exists. SubAccountId: ${form.newSubAccountId.toString()}`);
}
}
subAccount.name = form.newSubAccountName;
subAccount.subAccountId = form.newSubAccountId.toString();
await this.contactDataStorage.updateItem(contact);
return contact;
}
async removeSubAccountContact(form) {
const contact = await this.getContactByPrincipal(form.principal);
const asset = this.getContactAsset(contact, form.ledgerAddress);
asset.subAccounts = asset.subAccounts.filter((sa) => sa.subAccountId !== form.subAccountId.toString());
await this.contactDataStorage.updateItem(contact);
return contact;
}
async removeAssetFromAllContacts(ledgerAddress) {
const contacts = await this.getContacts();
for (const contact of contacts) {
contact.assets = contact.assets.filter((a) => a.ledgerAddress !== ledgerAddress);
}
await this.contactDataStorage.updateItems(contacts);
}
async isContactExist(contactId) {
const contact = await this.contactDataStorage.getItem(contactId);
const result = contact ? true : false;
return result;
}
async getContactByPrincipal(principal) {
const contact = await this.contactDataStorage.getItem(principal);
if (!contact) {
throw new common_1.ValidationError("contact.not.exists", "principal", `Contact not exists. Principal: ${principal}`);
}
return contact;
}
getContactAssetOrDefault(contact, ledgerAddress) {
const asset = contact.assets.find((a) => a.ledgerAddress === ledgerAddress);
return asset;
}
getContactAsset(contact, ledgerAddress) {
const asset = this.getContactAssetOrDefault(contact, ledgerAddress);
if (!asset) {
throw new common_1.ValidationError("contact.asset.not.exists", "ledgerAddress", `Asset not exists. Ledger address: ${ledgerAddress}`);
}
return asset;
}
};
exports.ContactRepository = ContactRepository;
exports.ContactRepository = ContactRepository = __decorate([
(0, typedi_1.Service)("ContactRepository"),
__param(0, (0, typedi_1.Inject)("IContactDataStorage")),
__metadata("design:paramtypes", [Object])
], ContactRepository);