"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.attestFromSolana = exports.attestFromEth = void 0;
const web3_js_1 = require("@solana/web3.js");
const ethers_contracts_1 = require("../ethers-contracts");
const solana_1 = require("../solana");
const tokenBridge_1 = require("../solana/tokenBridge");
const createNonce_1 = require("../utils/createNonce");
function attestFromEth(tokenBridgeAddress, signer, tokenAddress, overrides = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const bridge = ethers_contracts_1.Bridge__factory.connect(tokenBridgeAddress, signer);
        const v = yield bridge.attestToken(tokenAddress, createNonce_1.createNonce(), overrides);
        const receipt = yield v.wait();
        return receipt;
    });
}
exports.attestFromEth = attestFromEth;
function attestFromSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, mintAddress, commitment) {
    return __awaiter(this, void 0, void 0, function* () {
        const nonce = createNonce_1.createNonce().readUInt32LE(0);
        const transferIx = yield solana_1.createBridgeFeeTransferInstruction(connection, bridgeAddress, payerAddress);
        const messageKey = web3_js_1.Keypair.generate();
        const attestIx = tokenBridge_1.createAttestTokenInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, mintAddress, messageKey.publicKey, nonce);
        const transaction = new web3_js_1.Transaction().add(transferIx, attestIx);
        const { blockhash } = yield connection.getLatestBlockhash(commitment);
        transaction.recentBlockhash = blockhash;
        transaction.feePayer = new web3_js_1.PublicKey(payerAddress);
        transaction.partialSign(messageKey);
        return transaction;
    });
}
exports.attestFromSolana = attestFromSolana;
