"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base58Check = void 0;
const crypto_1 = require("@node-dlc/crypto");
const Base58_1 = require("./Base58");
const BitcoinError_1 = require("./BitcoinError");
const BitcoinErrorCode_1 = require("./BitcoinErrorCode");
class Base58Check {
    /**
     * Perform a base58 encoding by appends a 4-byte hash256 checksum
     * at the end of the value.
     * @param buf
     */
    static encode(buf) {
        return Base58_1.Base58.encode(Buffer.concat([buf, (0, crypto_1.hash256)(buf).slice(0, 4)]));
    }
    /**
     * Decodes a base58 check value. Throws error if checksum is invalid
     * @param buf
     */
    static decode(input) {
        const total = Base58_1.Base58.decode(input);
        const data = total.slice(0, total.length - 4);
        const checksum = total.slice(total.length - 4);
        const hash = (0, crypto_1.hash256)(data).slice(0, 4);
        if (!hash.equals(checksum)) {
            throw new BitcoinError_1.BitcoinError(BitcoinErrorCode_1.BitcoinErrorCode.Base58ChecksumFailed, {
                actual: hash,
                expected: checksum,
            });
        }
        return data;
    }
}
exports.Base58Check = Base58Check;
//# sourceMappingURL=Base58Check.js.map