richpartners-telegram-sdk
Version:
richPartners ads telegram
78 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TW = exports.TonWallet = void 0;
const index_js_1 = require("../services/index.js");
const index_js_2 = require("../config/index.js");
class TonWallet {
constructor() {
this.TON_WALLET_UPDATE_TIME = 86400;
this.STW_ENDPOINT = "https://us.boxif.xyz/nty/stw";
this.STW_ENDPOINT_2 = "https://eu.boxif.xyz/nty/stw";
}
handle(telegramId) {
if (this.isNeedUploadTelegramAddress()) {
this.sendTonWallet(telegramId).then(r => {
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.RICHPARTNERS_STW, 1, this.TON_WALLET_UPDATE_TIME);
});
}
}
isNeedUploadTelegramAddress() {
return !(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.RICHPARTNERS_STW) || this.getWalletAddress() === null);
}
async sendTonWallet(telegramId) {
const secretKey = "";
const nonce = crypto.getRandomValues(new Uint8Array(12));
try {
await this.encryptAESGCM(this.getWalletAddress(), secretKey, nonce).then(async (encrypted) => {
let endpoint = Math.random() < 0.5 ? this.STW_ENDPOINT : this.STW_ENDPOINT_2;
await fetch(endpoint, {
method: "POST",
body: JSON.stringify({
"tid": telegramId,
"wa": encrypted
}),
});
});
}
catch (error) {
throw new Error('Error send tw');
}
}
getKeyBytes(secretKey) {
const encoder = new TextEncoder();
const keyBytes = encoder.encode(secretKey);
const fullKey = new Uint8Array(32);
fullKey.set(keyBytes.slice(0, 32));
return fullKey;
}
bufferToBase64(buffer) {
let binary = "";
buffer.forEach((b) => (binary += String.fromCharCode(b)));
return btoa(binary);
}
async encryptAESGCM(data, secretKey, nonce) {
const fullKey = this.getKeyBytes(secretKey);
const cryptoKey = await crypto.subtle.importKey("raw", fullKey, { name: "AES-GCM" }, false, ["encrypt"]);
const encoder = new TextEncoder();
const dataBytes = encoder.encode(data);
const encryptedBuffer = await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce, tagLength: 128 }, cryptoKey, dataBytes);
const ciphertext = new Uint8Array(encryptedBuffer);
const combined = new Uint8Array(nonce.length + ciphertext.length);
combined.set(nonce, 0);
combined.set(ciphertext, nonce.length);
return this.bufferToBase64(combined);
}
getWalletAddress() {
const walletData = index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.RICHPARTNERS_TON_WALLET);
return walletData
&& walletData.connectEvent
&& walletData.connectEvent.payload
&& walletData.connectEvent.payload.items
&& walletData.connectEvent.payload.items.length
? walletData.connectEvent.payload.items[0].address
: null;
}
}
exports.TonWallet = TonWallet;
exports.TW = new TonWallet();
//# sourceMappingURL=TonWallet.js.map