@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
145 lines • 5.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const network_1 = __importDefault(require("@ledgerhq/live-network/network"));
class ChainwatchAccountManager {
chainwatchBaseUrl;
userId;
network;
suffixes;
constructor(chainwatchBaseUrl, userId, network) {
this.chainwatchBaseUrl = chainwatchBaseUrl;
this.userId = userId;
this.network = network;
this.suffixes = [];
}
async getChainwatchAccount() {
try {
const { data } = await (0, network_1.default)({
method: "GET",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/`,
});
return data;
}
catch {
return;
}
}
async removeChainwatchAccount() {
try {
await (0, network_1.default)({
method: "DELETE",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/`,
});
}
catch {
return;
}
}
async registerNewChainwatchAccount() {
try {
const { data } = await (0, network_1.default)({
method: "PUT",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/`,
});
return data;
}
catch {
return;
}
}
getAccountAddress(account) {
return account.freshAddress;
}
accountAlreadySubscribed(account) {
const address = this.getAccountAddress(account);
return (address &&
this.suffixes.some(suffix => address?.toLowerCase()?.endsWith(suffix.toLowerCase())));
}
async registerNewAccountsAddresses(accountsToRegister) {
try {
const addresses = accountsToRegister
.filter(account => this.getAccountAddress(account) && !this.accountAlreadySubscribed(account))
.map(account => this.getAccountAddress(account));
if (addresses.length > 0) {
await (0, network_1.default)({
method: "PUT",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/addresses/`,
data: addresses,
});
}
}
catch {
return;
}
}
async removeAccountsAddresses(accountsToRemove) {
try {
const addresses = accountsToRemove
.filter(account => this.getAccountAddress(account) && this.accountAlreadySubscribed(account))
.map(account => this.getAccountAddress(account));
if (addresses.length > 0) {
await (0, network_1.default)({
method: "DELETE",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/addresses/`,
data: addresses,
});
}
}
catch {
return;
}
}
async registerNewMonitor(monitor) {
try {
await (0, network_1.default)({
method: "PUT",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/monitor/`,
data: {
confirmations: this.network.nbConfirmations,
type: monitor,
},
});
}
catch {
return;
}
}
async registerNewTarget(target) {
try {
await (0, network_1.default)({
method: "PUT",
url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/target/`,
data: {
equipment: this.userId,
type: target,
},
});
}
catch {
return;
}
}
async setupChainwatchAccount() {
// Get or set Chainwatch Account
const chainwatchAccount = (await this.getChainwatchAccount()) || (await this.registerNewChainwatchAccount());
if (chainwatchAccount) {
this.suffixes = chainwatchAccount?.suffixes || [];
// Set Chainwatch account's monitors (receive and send) if they don't exist yet
if (!chainwatchAccount?.monitors?.find(monitor => monitor.type === "send")) {
await this.registerNewMonitor("send");
}
if (!chainwatchAccount?.monitors?.find(monitor => monitor.type === "receive")) {
await this.registerNewMonitor("receive");
}
// Set Chainwatch account's target (braze) if it doesn't exist yet
if (!chainwatchAccount?.targets?.find(target => target.type === "braze")) {
await this.registerNewTarget("braze");
}
}
}
}
exports.default = ChainwatchAccountManager;
//# sourceMappingURL=ChainwatchAccountManager.js.map