"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressCache = void 0;
const bufio_1 = require("@node-dlc/bufio");
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
const MessageType_1 = require("../MessageType");
class AddressCache {
    constructor() {
        this.type = AddressCache.type;
        this.cacheSPKs = [];
    }
    static fromAddressCache(addressesBlacklist, network) {
        const instance = new AddressCache();
        for (const blacklistAddress of Object.keys(addressesBlacklist)) {
            instance.cacheSPKs.push(bitcoinjs_lib_1.address.toOutputScript(blacklistAddress, network));
        }
        return instance;
    }
    static deserialize(buf) {
        const instance = new AddressCache();
        const reader = new bufio_1.BufferReader(buf);
        reader.readUInt16BE(); // read type
        reader.readBigSize(); // num_cache_spks
        while (!reader.eof) {
            const cacheSPKLen = reader.readBigSize();
            const cacheSPK = reader.readBytes(Number(cacheSPKLen));
            instance.cacheSPKs.push(cacheSPK);
        }
        return instance;
    }
    toAddressCache(network) {
        const addressCache = [];
        for (const cacheSPK of this.cacheSPKs) {
            addressCache[bitcoinjs_lib_1.address.fromOutputScript(cacheSPK, network)] = true;
        }
        return addressCache;
    }
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeUInt16BE(this.type);
        writer.writeBigSize(this.cacheSPKs.length);
        for (const cacheSPK of this.cacheSPKs) {
            writer.writeBigSize(cacheSPK.length);
            writer.writeBytes(cacheSPK);
        }
        return writer.toBuffer();
    }
}
exports.AddressCache = AddressCache;
AddressCache.type = MessageType_1.MessageType.AddressCache;
//# sourceMappingURL=AddressCache.js.map