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

[Brc20 Commit Error with p2sh] when i use p2sh address call brc20 commit, i got error. #3307

Closed
weixuefeng opened this issue Jul 12, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@weixuefeng
Copy link
Contributor

when i use p2sh address call brc20 commit, i got raw transaction, but when i boardcast it to network, i got

mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)

there is my code

  var privateKeyHex =
        "2f609a828ce21a2d8dc6a0a53190f0b059203f6557104960bb7e2a5c015e2daa";
    var utxoList = [
      UTXO(
          txId:
              "2ca801f84d9979663e85b641c359f7c7e08941a8827ec9576c5ca77578f365d4",
          index: 1,
          amount: "99996992")
    ];
    var fromAddress = "2N3ETqqmibtVU8Zj1S6ohQqL46pf1hKRojw";
    var toAddress = "2N3ETqqmibtVU8Zj1S6ohQqL46pf1hKRojw";
    var utxos = UTXOList(data: utxoList);
    var minerFee = btcClient.calculateBrc20CommitMinerFee(
        fromAddress, toAddress, 1, utxos, "2");
    Log.d("minerFee: ${minerFee}");
    var raw = btcClient.signBrc20Commit(fromAddress, toAddress, privateKeyHex,
        brc20Ticker, brc20Amount, 1, utxos, brcInscribeAmount, minerFee);
    Log.d("commit raw: ${hex.encode(raw.encoded)}");
    

----
Bitcoin.SigningOutput signBrc20Commit(
      String fromAddress,
      String toAddress,
      String privateKeyHex,
      String brc20Ticker,
      String brc20Amount,
      int coinType,
      UTXOList utxos,
      int brcInscribeAmount,
      int minerFee) {
    var privateKey = PrivateKey.createWithData(privateKeyHex.toUint8List());
    var publicKey = privateKey.getPublicKeySecp256k1(true);
    var addressType = detectAddressType(fromAddress, coinType);
    // generate input
    var input = Bitcoin.SigningInput(
        isItBrcOperation: true,
        privateKey: [privateKeyHex.toUint8List()],
        amount: $fixnum.Int64(brcInscribeAmount));
    if (addressType == P2SH) {
      input.scripts.addAll(
          generateSigScriptForP2SH(privateKey.getPublicKeySecp256k1(true)));
    }

    // generate normal utxo
    UTXOSummary summary = _generateUtxoPramater(fromAddress, utxos, coinType);
    input.utxo.addAll(summary.unspentTransactions);

    // change amount
    var changeAmount = summary.totalAmount - minerFee - brcInscribeAmount;

    // make inscribe
    var outputInscribe = BitcoinScript.buildBRC20InscribeTransfer(
        brc20Ticker, brc20Amount, publicKey.data());
    var outputInscribeProto =
        Bitcoin.TransactionOutput.fromBuffer(outputInscribe.data());

    // generate out put
    var utxoPlan0 = Bitcoin.UnspentTransaction(
        amount: $fixnum.Int64(brcInscribeAmount),
        script: outputInscribeProto.script,
        variant: TransactionVariant.BRC20TRANSFER);

    // change out put
    var utxoPlan1 = Bitcoin.UnspentTransaction(
        script: BitcoinScript.lockScriptForAddress(fromAddress, coinType)
            .data()
            .toList(),
        amount: $fixnum.Int64(changeAmount));

    // make transaction plan
    var plan = Bitcoin.TransactionPlan(utxos: [utxoPlan0, utxoPlan1]);
    input.plan = plan;
    // sign transaction
    var signed = AnySigner.sign(input.writeToBuffer(), coinType);
    var signingOutput = Bitcoin.SigningOutput.fromBuffer(signed);
    return signingOutput;
  }
@weixuefeng weixuefeng added the bug Something isn't working label Jul 12, 2023
@lamafab
Copy link
Contributor

lamafab commented Jul 17, 2023

Hey, a couple of things:

EDIT:

    if (addressType == P2SH) {
      input.scripts.addAll(
          generateSigScriptForP2SH(privateKey.getPublicKeySecp256k1(true)));
    }

Generally, you should construct the BRC20 transactions as seen in the unit tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants