"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishPackage = exports.getCoinBuildOutput = exports.publishCoin = void 0;
const sui_js_1 = require("@mysten/sui.js");
const publishCoin = (coreBridgePackageId, tokenBridgePackageId, vaa, signerAddress) => __awaiter(void 0, void 0, void 0, function* () {
    const build = exports.getCoinBuildOutput(coreBridgePackageId, tokenBridgePackageId, vaa);
    return exports.publishPackage(build, signerAddress);
});
exports.publishCoin = publishCoin;
const getCoinBuildOutput = (coreBridgePackageId, tokenBridgePackageId, vaa) => {
    const bytecode = "oRzrCwYAAAAKAQAIAggOAxYWBCwEBTAoB1iGAQjeAWAGvgLlAQqjBAUMqAQWAAMBCgELAgQAAAIAAgECAAMCDAEAAQAGAAEAAQgIAQEMAgkFBgADBwMEAQIDAgEHAggABwgBAAEIAAMJAAoCBwgBAQsCAQkAAQYIAQEFAQsCAQgAAgkABQRDT0lOCVR4Q29udGV4dBFXcmFwcGVkQXNzZXRTZXR1cARjb2luDmNyZWF0ZV93cmFwcGVkC2R1bW15X2ZpZWxkBGluaXQUcHJlcGFyZV9yZWdpc3RyYXRpb24PcHVibGljX3RyYW5zZmVyBnNlbmRlcgh0cmFuc2Zlcgp0eF9jb250ZXh0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAjVFfai/LOUUAUvqTWa3Rh8p5xhe/wYJfCRRBlwG9pGyCgLhAd8B" +
        Buffer.from(vaa, "hex").toString("base64").replace(/\=+$/, "") +
        "ACAQUBAAAAAAEJCwAHAAoBOAALAS4RAjgBAgA=";
    return {
        modules: [bytecode],
        dependencies: [
            sui_js_1.normalizeSuiAddress("0x1"),
            sui_js_1.normalizeSuiAddress("0x2"),
            tokenBridgePackageId,
            coreBridgePackageId,
        ],
    };
};
exports.getCoinBuildOutput = getCoinBuildOutput;
const publishPackage = (buildOutput, signerAddress) => __awaiter(void 0, void 0, void 0, function* () {
    // Publish contracts
    const tx = new sui_js_1.TransactionBlock();
    const [upgradeCap] = tx.publish({
        modules: buildOutput.modules.map((m) => Array.from(sui_js_1.fromB64(m))),
        dependencies: buildOutput.dependencies.map((d) => sui_js_1.normalizeSuiObjectId(d)),
    });
    // Transfer upgrade capability to recipient
    tx.transferObjects([upgradeCap], tx.pure(signerAddress));
    return tx;
});
exports.publishPackage = publishPackage;
