var m = Object.defineProperty;
var T = (c, e, t) => e in c ? m(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
var a = (c, e, t) => T(c, typeof e != "symbol" ? e + "" : e, t);
import { E as g, C as h } from "./platform-DkmFvjoG.mjs";
import { I as v, C as M } from "./contract-D8VzaUQJ.mjs";
import { r as S } from "./chain-Dq8acwHq.mjs";
import { ac as w, aR as r, aU as u, L as x, d0 as I, a3 as C, aV as A, aW as G } from "./address-CrYNZ_Nj.mjs";
import { c as F } from "./create-CRa9FZnZ.mjs";
import { E as U } from "./unsignedTransaction-C0V0UbsS.mjs";
class y {
  constructor(e, t, b, f) {
    a(this, "network");
    a(this, "chain");
    a(this, "provider");
    a(this, "contracts");
    a(this, "chainId");
    a(this, "coreAddress");
    a(this, "core");
    a(this, "coreIface");
    this.network = e, this.chain = t, this.provider = b, this.contracts = f, this.chainId = w.get(e, t), this.coreIface = d.createInterface();
    const n = this.contracts.coreBridge;
    if (!n)
      throw new Error("Core bridge address not found");
    this.coreAddress = n, this.core = d.connect(n, b);
  }
  async getMessageFee() {
    return await this.core.messageFee.staticCall();
  }
  async getGuardianSetIndex() {
    return Number(await this.core.getCurrentGuardianSetIndex.staticCall());
  }
  async getGuardianSet(e) {
    const t = await this.core.getGuardianSet(e);
    return {
      index: e,
      keys: t[0],
      expiry: t[1]
    };
  }
  static async fromRpc(e, t) {
    const [b, f] = await g.chainFromRpc(e), n = t[f];
    if (n.network !== b)
      throw new Error(`Network mismatch: ${n.network} != ${b}`);
    return new y(b, f, e, n.contracts);
  }
  async *publishMessage(e, t, b, f) {
    const n = new r(e).toString(), s = await this.getMessageFee(), i = await this.core.publishMessage.populateTransaction(b, t, f, {
      value: s
    });
    yield this.createUnsignedTx(u(i, n), "WormholeCore.publishMessage");
  }
  async *verifyMessage(e, t) {
    const b = new r(e).toString(), f = await this.core.parseAndVerifyVM.populateTransaction(x(t));
    yield this.createUnsignedTx(u(f, b), "WormholeCore.verifyMessage");
  }
  async parseTransaction(e) {
    const t = await this.provider.getTransactionReceipt(e);
    return t === null ? [] : t.logs.filter((b) => b.address === this.coreAddress).map((b) => {
      const { topics: f, data: n } = b, s = this.coreIface.parseLog({
        topics: f.slice(),
        data: n
      });
      if (s === null)
        return;
      const i = new r(s.args.sender);
      return {
        chain: this.chain,
        emitter: i.toUniversalAddress(),
        sequence: s.args.sequence
      };
    }).filter(I);
  }
  async parseMessages(e) {
    const t = await this.provider.getTransactionReceipt(e);
    if (t === null)
      throw new Error("Could not get transaction receipt");
    const b = await this.getGuardianSetIndex();
    return t.logs.filter((f) => f.address === this.coreAddress).map((f) => {
      const { topics: n, data: s } = f, i = this.coreIface.parseLog({
        topics: n.slice(),
        data: s
      });
      if (i === null)
        return null;
      const l = new r(i.args.sender);
      return F("Uint8Array", {
        guardianSet: b,
        // TODO: should we get this from the contract on init?
        timestamp: 0,
        // TODO: Would need to get the full block to get the timestamp
        emitterChain: this.chain,
        emitterAddress: l.toUniversalAddress(),
        consistencyLevel: Number(i.args.consistencyLevel),
        sequence: BigInt(i.args.sequence),
        nonce: Number(i.args.nonce),
        signatures: [],
        payload: C.decode(i.args.payload)
      });
    }).filter((f) => !!f);
  }
  createUnsignedTx(e, t, b = !1) {
    return new U(A(e, this.chainId), this.network, this.chain, t, b);
  }
}
const p = [
  {
    anonymous: !1,
    inputs: [
      {
        indexed: !1,
        internalType: "address",
        name: "previousAdmin",
        type: "address"
      },
      {
        indexed: !1,
        internalType: "address",
        name: "newAdmin",
        type: "address"
      }
    ],
    name: "AdminChanged",
    type: "event"
  },
  {
    anonymous: !1,
    inputs: [
      {
        indexed: !0,
        internalType: "address",
        name: "beacon",
        type: "address"
      }
    ],
    name: "BeaconUpgraded",
    type: "event"
  },
  {
    anonymous: !1,
    inputs: [
      {
        indexed: !0,
        internalType: "address",
        name: "oldContract",
        type: "address"
      },
      {
        indexed: !0,
        internalType: "address",
        name: "newContract",
        type: "address"
      }
    ],
    name: "ContractUpgraded",
    type: "event"
  },
  {
    anonymous: !1,
    inputs: [
      {
        indexed: !0,
        internalType: "uint32",
        name: "index",
        type: "uint32"
      }
    ],
    name: "GuardianSetAdded",
    type: "event"
  },
  {
    anonymous: !1,
    inputs: [
      {
        indexed: !0,
        internalType: "address",
        name: "sender",
        type: "address"
      },
      {
        indexed: !1,
        internalType: "uint64",
        name: "sequence",
        type: "uint64"
      },
      {
        indexed: !1,
        internalType: "uint32",
        name: "nonce",
        type: "uint32"
      },
      {
        indexed: !1,
        internalType: "bytes",
        name: "payload",
        type: "bytes"
      },
      {
        indexed: !1,
        internalType: "uint8",
        name: "consistencyLevel",
        type: "uint8"
      }
    ],
    name: "LogMessagePublished",
    type: "event"
  },
  {
    anonymous: !1,
    inputs: [
      {
        indexed: !0,
        internalType: "address",
        name: "implementation",
        type: "address"
      }
    ],
    name: "Upgraded",
    type: "event"
  },
  {
    stateMutability: "payable",
    type: "fallback"
  },
  {
    inputs: [],
    name: "chainId",
    outputs: [
      {
        internalType: "uint16",
        name: "",
        type: "uint16"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "evmChainId",
    outputs: [
      {
        internalType: "uint256",
        name: "",
        type: "uint256"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "getCurrentGuardianSetIndex",
    outputs: [
      {
        internalType: "uint32",
        name: "",
        type: "uint32"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "uint32",
        name: "index",
        type: "uint32"
      }
    ],
    name: "getGuardianSet",
    outputs: [
      {
        components: [
          {
            internalType: "address[]",
            name: "keys",
            type: "address[]"
          },
          {
            internalType: "uint32",
            name: "expirationTime",
            type: "uint32"
          }
        ],
        internalType: "struct Structs.GuardianSet",
        name: "",
        type: "tuple"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "getGuardianSetExpiry",
    outputs: [
      {
        internalType: "uint32",
        name: "",
        type: "uint32"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes32",
        name: "hash",
        type: "bytes32"
      }
    ],
    name: "governanceActionIsConsumed",
    outputs: [
      {
        internalType: "bool",
        name: "",
        type: "bool"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "governanceChainId",
    outputs: [
      {
        internalType: "uint16",
        name: "",
        type: "uint16"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "governanceContract",
    outputs: [
      {
        internalType: "bytes32",
        name: "",
        type: "bytes32"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "initialize",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
  },
  {
    inputs: [],
    name: "isFork",
    outputs: [
      {
        internalType: "bool",
        name: "",
        type: "bool"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "address",
        name: "impl",
        type: "address"
      }
    ],
    name: "isInitialized",
    outputs: [
      {
        internalType: "bool",
        name: "",
        type: "bool"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [],
    name: "messageFee",
    outputs: [
      {
        internalType: "uint256",
        name: "",
        type: "uint256"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "address",
        name: "emitter",
        type: "address"
      }
    ],
    name: "nextSequence",
    outputs: [
      {
        internalType: "uint64",
        name: "",
        type: "uint64"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedVM",
        type: "bytes"
      }
    ],
    name: "parseAndVerifyVM",
    outputs: [
      {
        components: [
          {
            internalType: "uint8",
            name: "version",
            type: "uint8"
          },
          {
            internalType: "uint32",
            name: "timestamp",
            type: "uint32"
          },
          {
            internalType: "uint32",
            name: "nonce",
            type: "uint32"
          },
          {
            internalType: "uint16",
            name: "emitterChainId",
            type: "uint16"
          },
          {
            internalType: "bytes32",
            name: "emitterAddress",
            type: "bytes32"
          },
          {
            internalType: "uint64",
            name: "sequence",
            type: "uint64"
          },
          {
            internalType: "uint8",
            name: "consistencyLevel",
            type: "uint8"
          },
          {
            internalType: "bytes",
            name: "payload",
            type: "bytes"
          },
          {
            internalType: "uint32",
            name: "guardianSetIndex",
            type: "uint32"
          },
          {
            components: [
              {
                internalType: "bytes32",
                name: "r",
                type: "bytes32"
              },
              {
                internalType: "bytes32",
                name: "s",
                type: "bytes32"
              },
              {
                internalType: "uint8",
                name: "v",
                type: "uint8"
              },
              {
                internalType: "uint8",
                name: "guardianIndex",
                type: "uint8"
              }
            ],
            internalType: "struct Structs.Signature[]",
            name: "signatures",
            type: "tuple[]"
          },
          {
            internalType: "bytes32",
            name: "hash",
            type: "bytes32"
          }
        ],
        internalType: "struct Structs.VM",
        name: "vm",
        type: "tuple"
      },
      {
        internalType: "bool",
        name: "valid",
        type: "bool"
      },
      {
        internalType: "string",
        name: "reason",
        type: "string"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedUpgrade",
        type: "bytes"
      }
    ],
    name: "parseContractUpgrade",
    outputs: [
      {
        components: [
          {
            internalType: "bytes32",
            name: "module",
            type: "bytes32"
          },
          {
            internalType: "uint8",
            name: "action",
            type: "uint8"
          },
          {
            internalType: "uint16",
            name: "chain",
            type: "uint16"
          },
          {
            internalType: "address",
            name: "newContract",
            type: "address"
          }
        ],
        internalType: "struct GovernanceStructs.ContractUpgrade",
        name: "cu",
        type: "tuple"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedUpgrade",
        type: "bytes"
      }
    ],
    name: "parseGuardianSetUpgrade",
    outputs: [
      {
        components: [
          {
            internalType: "bytes32",
            name: "module",
            type: "bytes32"
          },
          {
            internalType: "uint8",
            name: "action",
            type: "uint8"
          },
          {
            internalType: "uint16",
            name: "chain",
            type: "uint16"
          },
          {
            components: [
              {
                internalType: "address[]",
                name: "keys",
                type: "address[]"
              },
              {
                internalType: "uint32",
                name: "expirationTime",
                type: "uint32"
              }
            ],
            internalType: "struct Structs.GuardianSet",
            name: "newGuardianSet",
            type: "tuple"
          },
          {
            internalType: "uint32",
            name: "newGuardianSetIndex",
            type: "uint32"
          }
        ],
        internalType: "struct GovernanceStructs.GuardianSetUpgrade",
        name: "gsu",
        type: "tuple"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedRecoverChainId",
        type: "bytes"
      }
    ],
    name: "parseRecoverChainId",
    outputs: [
      {
        components: [
          {
            internalType: "bytes32",
            name: "module",
            type: "bytes32"
          },
          {
            internalType: "uint8",
            name: "action",
            type: "uint8"
          },
          {
            internalType: "uint256",
            name: "evmChainId",
            type: "uint256"
          },
          {
            internalType: "uint16",
            name: "newChainId",
            type: "uint16"
          }
        ],
        internalType: "struct GovernanceStructs.RecoverChainId",
        name: "rci",
        type: "tuple"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedSetMessageFee",
        type: "bytes"
      }
    ],
    name: "parseSetMessageFee",
    outputs: [
      {
        components: [
          {
            internalType: "bytes32",
            name: "module",
            type: "bytes32"
          },
          {
            internalType: "uint8",
            name: "action",
            type: "uint8"
          },
          {
            internalType: "uint16",
            name: "chain",
            type: "uint16"
          },
          {
            internalType: "uint256",
            name: "messageFee",
            type: "uint256"
          }
        ],
        internalType: "struct GovernanceStructs.SetMessageFee",
        name: "smf",
        type: "tuple"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedTransferFees",
        type: "bytes"
      }
    ],
    name: "parseTransferFees",
    outputs: [
      {
        components: [
          {
            internalType: "bytes32",
            name: "module",
            type: "bytes32"
          },
          {
            internalType: "uint8",
            name: "action",
            type: "uint8"
          },
          {
            internalType: "uint16",
            name: "chain",
            type: "uint16"
          },
          {
            internalType: "uint256",
            name: "amount",
            type: "uint256"
          },
          {
            internalType: "bytes32",
            name: "recipient",
            type: "bytes32"
          }
        ],
        internalType: "struct GovernanceStructs.TransferFees",
        name: "tf",
        type: "tuple"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "encodedVM",
        type: "bytes"
      }
    ],
    name: "parseVM",
    outputs: [
      {
        components: [
          {
            internalType: "uint8",
            name: "version",
            type: "uint8"
          },
          {
            internalType: "uint32",
            name: "timestamp",
            type: "uint32"
          },
          {
            internalType: "uint32",
            name: "nonce",
            type: "uint32"
          },
          {
            internalType: "uint16",
            name: "emitterChainId",
            type: "uint16"
          },
          {
            internalType: "bytes32",
            name: "emitterAddress",
            type: "bytes32"
          },
          {
            internalType: "uint64",
            name: "sequence",
            type: "uint64"
          },
          {
            internalType: "uint8",
            name: "consistencyLevel",
            type: "uint8"
          },
          {
            internalType: "bytes",
            name: "payload",
            type: "bytes"
          },
          {
            internalType: "uint32",
            name: "guardianSetIndex",
            type: "uint32"
          },
          {
            components: [
              {
                internalType: "bytes32",
                name: "r",
                type: "bytes32"
              },
              {
                internalType: "bytes32",
                name: "s",
                type: "bytes32"
              },
              {
                internalType: "uint8",
                name: "v",
                type: "uint8"
              },
              {
                internalType: "uint8",
                name: "guardianIndex",
                type: "uint8"
              }
            ],
            internalType: "struct Structs.Signature[]",
            name: "signatures",
            type: "tuple[]"
          },
          {
            internalType: "bytes32",
            name: "hash",
            type: "bytes32"
          }
        ],
        internalType: "struct Structs.VM",
        name: "vm",
        type: "tuple"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "uint32",
        name: "nonce",
        type: "uint32"
      },
      {
        internalType: "bytes",
        name: "payload",
        type: "bytes"
      },
      {
        internalType: "uint8",
        name: "consistencyLevel",
        type: "uint8"
      }
    ],
    name: "publishMessage",
    outputs: [
      {
        internalType: "uint64",
        name: "sequence",
        type: "uint64"
      }
    ],
    stateMutability: "payable",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "uint256",
        name: "numGuardians",
        type: "uint256"
      }
    ],
    name: "quorum",
    outputs: [
      {
        internalType: "uint256",
        name: "numSignaturesRequiredForQuorum",
        type: "uint256"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "_vm",
        type: "bytes"
      }
    ],
    name: "submitContractUpgrade",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "_vm",
        type: "bytes"
      }
    ],
    name: "submitNewGuardianSet",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "_vm",
        type: "bytes"
      }
    ],
    name: "submitRecoverChainId",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "_vm",
        type: "bytes"
      }
    ],
    name: "submitSetMessageFee",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes",
        name: "_vm",
        type: "bytes"
      }
    ],
    name: "submitTransferFees",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
  },
  {
    inputs: [
      {
        internalType: "bytes32",
        name: "hash",
        type: "bytes32"
      },
      {
        components: [
          {
            internalType: "bytes32",
            name: "r",
            type: "bytes32"
          },
          {
            internalType: "bytes32",
            name: "s",
            type: "bytes32"
          },
          {
            internalType: "uint8",
            name: "v",
            type: "uint8"
          },
          {
            internalType: "uint8",
            name: "guardianIndex",
            type: "uint8"
          }
        ],
        internalType: "struct Structs.Signature[]",
        name: "signatures",
        type: "tuple[]"
      },
      {
        components: [
          {
            internalType: "address[]",
            name: "keys",
            type: "address[]"
          },
          {
            internalType: "uint32",
            name: "expirationTime",
            type: "uint32"
          }
        ],
        internalType: "struct Structs.GuardianSet",
        name: "guardianSet",
        type: "tuple"
      }
    ],
    name: "verifySignatures",
    outputs: [
      {
        internalType: "bool",
        name: "valid",
        type: "bool"
      },
      {
        internalType: "string",
        name: "reason",
        type: "string"
      }
    ],
    stateMutability: "pure",
    type: "function"
  },
  {
    inputs: [
      {
        components: [
          {
            internalType: "uint8",
            name: "version",
            type: "uint8"
          },
          {
            internalType: "uint32",
            name: "timestamp",
            type: "uint32"
          },
          {
            internalType: "uint32",
            name: "nonce",
            type: "uint32"
          },
          {
            internalType: "uint16",
            name: "emitterChainId",
            type: "uint16"
          },
          {
            internalType: "bytes32",
            name: "emitterAddress",
            type: "bytes32"
          },
          {
            internalType: "uint64",
            name: "sequence",
            type: "uint64"
          },
          {
            internalType: "uint8",
            name: "consistencyLevel",
            type: "uint8"
          },
          {
            internalType: "bytes",
            name: "payload",
            type: "bytes"
          },
          {
            internalType: "uint32",
            name: "guardianSetIndex",
            type: "uint32"
          },
          {
            components: [
              {
                internalType: "bytes32",
                name: "r",
                type: "bytes32"
              },
              {
                internalType: "bytes32",
                name: "s",
                type: "bytes32"
              },
              {
                internalType: "uint8",
                name: "v",
                type: "uint8"
              },
              {
                internalType: "uint8",
                name: "guardianIndex",
                type: "uint8"
              }
            ],
            internalType: "struct Structs.Signature[]",
            name: "signatures",
            type: "tuple[]"
          },
          {
            internalType: "bytes32",
            name: "hash",
            type: "bytes32"
          }
        ],
        internalType: "struct Structs.VM",
        name: "vm",
        type: "tuple"
      }
    ],
    name: "verifyVM",
    outputs: [
      {
        internalType: "bool",
        name: "valid",
        type: "bool"
      },
      {
        internalType: "string",
        name: "reason",
        type: "string"
      }
    ],
    stateMutability: "view",
    type: "function"
  },
  {
    stateMutability: "payable",
    type: "receive"
  }
], o = " ", q = (c) => c.length > 1;
class d extends h {
  constructor(...e) {
    q(e) ? super(...e) : super(p, o, e[0]);
  }
  getDeployTransaction(e) {
    return super.getDeployTransaction(e || {});
  }
  deploy(e) {
    return super.deploy(e || {});
  }
  connect(e) {
    return super.connect(e);
  }
  static createInterface() {
    return new v(p);
  }
  static connect(e, t) {
    return new M(e, p, t);
  }
}
a(d, "bytecode", o), a(d, "abi", p);
const W = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Implementation__factory: d
}, Symbol.toStringTag, { value: "Module" }));
S(G, "WormholeCore", y);
export {
  y as EvmWormholeCore,
  W as ethers_contracts
};
