BTCGPU / bitcoinjs-lib

Bitcoin-related functions implemented in pure JavaScript
MIT License
1 stars 3 forks source link

How to sign transaction #11

Closed cenyG closed 3 years ago

cenyG commented 6 years ago

Hi, i'm trying to build tx for BTG using TransactionBuilder but have "16: mandatory-script-verify-flag-failed (Signature hash type missing or not understood)" , can help me ? Do I need a special way to sign tx? I do:

let txb = new bgold.TransactionBuilder(network)
let counter = 0
inputs.forEach((input) => {
    if (!!input.txid) {
        txb.addInput(input.txid, input.vout)
        counter++
    }
})
outputs.forEach((output) => {
    if (!!output.address) txb.addOutput(output.address,  bitcore.Unit.fromBTC(output.amount).toSatoshis())
})
const privatekey = bgold.ECPair.fromWIF(privkeys[0], network)
for (let i = 0; i < counter; i++) {
    txb.sign(i, privatekey)
}
return txb.build().toHex()
Vutov commented 6 years ago

Signing transaction for BTG is a little different then for BTC, because we initially wanted to merge the code to bitcoinjs-lib. It never happened, but anyway: You can see an example here - https://github.com/BTCGPU/bitcoinjs-lib/blob/master/test/bitcoingold.test.js#L12 Please note you need to add txb.enableBitcoinGold(true) txb.setVersion(2) and change the sign to txb.sign(0, keyPair, null, hashType, value). Also please keep in mind the fee. The value in the sign should be the original value including the fee. In the example there is no fee.

cenyG commented 6 years ago

@Vutov thank you for answer, it helps for a while , but now I have a new error ;<<< "message":"16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI) SIG operation) I made everything like in example. Do you have any ideas why it could be?

Vutov commented 6 years ago

Looks like bad signature - are you using Transaction.SIGHASH_ALL | Transaction.SIGHASH_FORKID ? Also if you are running regtest (may be network is not correct?) please make sure it has height over 2000 (to enable sigwit) and you are spending P2PKH UTXO

cenyG commented 6 years ago

I'm using Transaction.SIGHASH_ALL | Transaction.SIGHASH_FORKID and testnet not regtest. I checked tx outputs, they are not spended. I generate tx and decode it using https://btc.com/tools/tx/decode . May be you have some ideas whats wrong here ?

{
    "txid": "698675fbc48568ff6a093d734b597978325b61cd27d1127f522366669d236f89",
    "hash": "698675fbc48568ff6a093d734b597978325b61cd27d1127f522366669d236f89",
    "size": 339,
    "vsize": 339,
    "version": 2,
    "locktime": 0,
    "vin": [
        {
            "txid": "5bb9c797f7b6079167c3aede7bc5dbfa873d7eb5324ece05ab7f3c6e07c500ab",
            "vout": 1,
            "scriptSig": {
                "asm": "3045022100a493c86fd38e56afedd30d91b98dbdfbcfa03851049378aa14dc0fa598c70c1a022065ad73adc0fc6d07713396b648ef0f562c7627d8ffa3f67cded5e12ec205c3db41 02b70010948bc6ee94d35b36095b4bff12921fd82e3773a046fb1ee0d6967e4e0b",
                "hex": "483045022100a493c86fd38e56afedd30d91b98dbdfbcfa03851049378aa14dc0fa598c70c1a022065ad73adc0fc6d07713396b648ef0f562c7627d8ffa3f67cded5e12ec205c3db412102b70010948bc6ee94d35b36095b4bff12921fd82e3773a046fb1ee0d6967e4e0b"
            },
            "sequence": 4294967295
        },
        {
            "txid": "0e5c8db28caf8320241fe291731aa5b336be50ab45c2dd0ff8fdd69e0ed1aab8",
            "vout": 0,
            "scriptSig": {
                "asm": "30440220209be8aee84ec3922a79b78a356dc862a8b7aecfeda4744e863e1dd62b19f06402205c5e5bf0121cf6bad1530624d6e3eb65262ac05da3d50a869794c7874f5c673541 02b70010948bc6ee94d35b36095b4bff12921fd82e3773a046fb1ee0d6967e4e0b",
                "hex": "4730440220209be8aee84ec3922a79b78a356dc862a8b7aecfeda4744e863e1dd62b19f06402205c5e5bf0121cf6bad1530624d6e3eb65262ac05da3d50a869794c7874f5c6735412102b70010948bc6ee94d35b36095b4bff12921fd82e3773a046fb1ee0d6967e4e0b"
            },
            "sequence": 4294967295
        }
    ],
    "vout": [
        {
            "value": 19997600,
            "n": 0,
            "scriptPubKey": {
                "asm": "OP_DUP OP_HASH160 a6b06c99aa094a1769987a3b38dc9a98cd1808d4 OP_EQUALVERIFY OP_CHECKSIG",
                "hex": "76a914a6b06c99aa094a1769987a3b38dc9a98cd1808d488ac",
                "reqSigs": 1,
                "type": "pubkeyhash",
                "addresses": [
                    "1GCNX1nYACM2e147VMqnFWGN7ADkDoqy3Z"
                ]
            }
        }
    ]
}
h4x3rotab commented 6 years ago
addInput = function (txHash, vout, sequence, prevOutScript)

addInput takes 4 input parameters. The last 2 parameters can only be ignored when the txHash is a Transaction object. Comparing with @Vutov's sample code, I believe you forgot to specify the last two parameters, which ends up with a bad signature.

So I think the solution is to figure out the sequence number and the output script (pubkeyScript) from the UTXO and pass it to addInput. It will be very easy if the UTXO is just a P2PKH address. It's a bit harder if it's a P2SH or a nested SegWit address. There are a few examples in the unit tests.

Let's have a try and see if this is the core issue.

h4x3rotab commented 6 years ago

BTW, sometimes Coinbin for BTG may be helpful for development: https://h4x3rotab.github.io/coinbin/#settings