"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@polkadot/types");
const utils_1 = require("../../../src/utils");
const chai_1 = require("../../chai");
const bitcoinjs = __importStar(require("bitcoinjs-lib"));
const factory_1 = require("../../../src/factory");
describe("Bitcoin", () => {
    let registry;
    const createH256 = (hash) => {
        return registry.createType("H256", hash);
    };
    const createH160 = (hash) => {
        return registry.createType("H160", hash);
    };
    before(() => {
        registry = new types_1.TypeRegistry();
        registry.register(factory_1.getAPITypes());
    });
    describe("decodeBtcAddress", () => {
        it("should get correct hash for p2pkh address", () => {
            chai_1.assert.deepEqual(utils_1.decodeBtcAddress("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", bitcoinjs.networks.bitcoin), {
                p2pkh: "0x751e76e8199196d454941c45d1b3a323f1433bd6",
            });
        });
        it("should get correct hash for p2sh address", () => {
            chai_1.assert.deepEqual(utils_1.decodeBtcAddress("3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN", bitcoinjs.networks.bitcoin), {
                p2sh: "0xbcfeb728b584253d5f3f70bcb780e9ef218a68f4",
            });
        });
        it("should get correct hash for p2wpkh address", () => {
            // compare hex because type lib has trouble with this enum
            chai_1.assert.deepEqual(utils_1.decodeBtcAddress("bcrt1qjvmc5dtm4qxgtug8faa5jdedlyq4v76ngpqgrl", bitcoinjs.networks.regtest), {
                p2wpkhv0: "0x93378a357ba80c85f1074f7b49372df901567b53",
            });
            chai_1.assert.deepEqual(utils_1.decodeBtcAddress("tb1q45uq0q4v22fspeg3xjnkgf8a0v7pqgjks4k6sz", bitcoinjs.networks.testnet), {
                p2wpkhv0: "0xad380782ac529300e51134a76424fd7b3c102256",
            });
            chai_1.assert.deepEqual(utils_1.decodeBtcAddress("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", bitcoinjs.networks.bitcoin), {
                p2wpkhv0: "0xe8df018c7e326cc253faac7e46cdc51e68542c42",
            });
        });
        it("should get correct hash for p2wsh address", () => {
            chai_1.assert.deepEqual(utils_1.decodeBtcAddress("bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul", bitcoinjs.networks.bitcoin), {
                p2wshv0: "0xf513a6b2a03f11e15f921c0345bee24c63fffe638a73eca823ec58aaec5f38c6",
            });
        });
    });
    describe("encodeBtcAddress", () => {
        it("should encode correct p2pkh address from hash", () => {
            const mockAddress = {
                isP2pkh: true,
                asP2pkh: createH160("0x751e76e8199196d454941c45d1b3a323f1433bd6"),
                type: "P2pkh",
            };
            const encodedAddress = utils_1.encodeBtcAddress(mockAddress, bitcoinjs.networks.bitcoin);
            chai_1.assert.equal(encodedAddress, "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH");
        });
        it("should encode correct p2sh address from hash", () => {
            const mockAddress = {
                isP2sh: true,
                asP2sh: createH160("0xbcfeb728b584253d5f3f70bcb780e9ef218a68f4"),
                type: "P2sh",
            };
            const encodedAddress = utils_1.encodeBtcAddress(mockAddress, bitcoinjs.networks.bitcoin);
            chai_1.assert.equal(encodedAddress, "3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN");
        });
        it("should encode correct p2wpkh address from hash", () => {
            const mockAddress = {
                isP2wpkHv0: true,
                asP2wpkHv0: createH160("0x93378a357ba80c85f1074f7b49372df901567b53"),
                type: "P2wpkHv0",
            };
            const encodedAddress = utils_1.encodeBtcAddress(mockAddress, bitcoinjs.networks.regtest);
            chai_1.assert.equal(encodedAddress, "bcrt1qjvmc5dtm4qxgtug8faa5jdedlyq4v76ngpqgrl");
        });
        it("should encode correct p2wsh address from hash", () => {
            const mockAddress = {
                isP2wsHv0: true,
                asP2wsHv0: createH256("0xf513a6b2a03f11e15f921c0345bee24c63fffe638a73eca823ec58aaec5f38c6"),
                type: "P2wsHv0",
            };
            const encodedAddress = utils_1.encodeBtcAddress(mockAddress, bitcoinjs.networks.bitcoin);
            chai_1.assert.equal(encodedAddress, "bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul");
        });
    });
});
//# sourceMappingURL=bitcoin.test.js.map