Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Bitcoin deposit transaction detection #211

Open
r-czajkowski opened this issue Feb 1, 2024 · 1 comment
Open

Improve Bitcoin deposit transaction detection #211

r-czajkowski opened this issue Feb 1, 2024 · 1 comment
Labels
🔌 SDK TypeScript SDK Library

Comments

@r-czajkowski
Copy link
Contributor

We could improve it for the Ledger Live integration, as we should be able to get a Bitcoin transaction hash while broadcasting it in the staking flow.

Originally posted by @nkuba in #106 (comment)

@r-czajkowski
Copy link
Contributor Author

Example solution:

Ref: #106 (comment)

  async stake(fundingTxHash?: Hex): Promise<Hex> {
    if (!this.#signedMessage) {
      throw new Error("Sign message first")
    }

    const utxos = await this.#tbtcDeposit.detectFunding()

    if (utxos.length === 0) {
      throw new Error("Deposit not found yet")
    }

    // NEW CODE HERE: Find utxo by transaction hash 
    let utxo: BitcoinUtxo
    if (fundingTxHash) {
      const foundUtxo = utxos.find(({ transactionHash }) =>
        transactionHash.equals(fundingTxHash),
      )

      if (!foundUtxo) throw new Error("Deposit not found yet")

      utxo = foundUtxo
    }
  
    ;[utxo] = utxos

    const { transactionHash, outputIndex } = utxo

    const depositFundingTx = extractBitcoinRawTxVectors(
      await this.#bitcoinClient.getRawTransaction(transactionHash),
    )

    const { depositor: _, ...restDepositReceipt } =
      this.#tbtcDeposit.getReceipt()

    const revealDepositInfo = {
      fundingOutputIndex: outputIndex,
      ...restDepositReceipt,
    }

    // TODO: change it to the deposit details submission to the relayer bot.
    return this.#contracts.tbtcDepositor.initializeStake(
      depositFundingTx,
      revealDepositInfo,
      this.#staker,
      this.#referral,
    )
  }

@r-czajkowski r-czajkowski added the 🔌 SDK TypeScript SDK Library label Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌 SDK TypeScript SDK Library
Projects
None yet
Development

No branches or pull requests

1 participant