0

I'm trying to sign a multisig transaction (3 of 4 signatories) in Go using some btcd packages.

The following function should sign the transaction:

func SignMultisigTransaction(rawTransaction []byte, privateKeys []*btcec.PrivateKey, scriptPubKey []byte, redeemScript []byte, inputTx []byte, amount int64) ([]byte, error) {
    tx, err := btcutil.NewTxFromBytes(rawTransaction)
    if err != nil {
        return nil, err
    }

    // generate signatures
    var signatures [][]byte
    for i, txIn := range tx.MsgTx().TxIn {
        for _, privateKey := range privateKeys {
            signature, err := txscript.RawTxInSignature(tx.MsgTx(), i, txIn.SignatureScript, txscript.SigHashAll, privateKey)
            if err != nil {
                return nil, err
            }

            signatures = append(signatures, signature)
        }

        builder := txscript.NewScriptBuilder().AddOp(txscript.OP_FALSE)
        for _, signature := range signatures {
            builder.AddData(signature)
        }

        builder.AddData(redeemScript)

        signatureScript, err := builder.Script()
        if err != nil {
            return nil, err
        }

        tx.MsgTx().TxIn[i].SignatureScript = signatureScript
    }

    buffer := &bytes.Buffer{}
    err = tx.MsgTx().Serialize(buffer)
    if err != nil {
        return nil, err
    }

    return buffer.Bytes(), nil
}

During the transaction verification program try to execute scriptPubKey of txOut[0] using the following function:

func ExecuteScriptPubKey(scriptPubKey []byte, signedTx *wire.MsgTx) error {
    flags := txscript.StandardVerifyFlags

    vm, err := txscript.NewEngine(scriptPubKey, signedTx, 0, flags, nil, nil, -1)
    if err != nil {
        return err
    }

    if err := vm.Execute(); err != nil {
        return err
    }

    return nil
}

The error raised by the Execute function is Error on execute transaction scriptPubKey: not all signatures empty on failed checkmultisig, it would seem like an error in the verification of signatures, what could be wrong with the function I mentioned above?

Example of unsigned raw transaction:

0100000001f08f31982996ae656e380bacf6b71b7c1b8bef425f38431e10f8a45f026fcffe0100000017a914da3a5983769d69895ca53d2c1999aeef7a562c3187ffffffff0210270000000000001976a9143dee47716e3cfa57df45113473a6312ebeaef31188acb1b7f5050000000017a914da3a5983769d69895ca53d2c1999aeef7a562c318700000000

public keys:

pubKey1 = "0276a38006fee262e9918c4a9210aaaa990c9a48bcd681505cb44121300d819194"
pubKey2 = "039fb9b10734d14ee0b9d5ff52da1bbf10df4196695ec80303270e62747bfd4c92"
pubKey3 = "037965ae9fa904982ba24562447b9d17a69ed18313bd2bf152fca0b5c9819a70ce"
pubKey4 = "0252d9b0a3ff888dafaacad28133e418c185d1446e60e5c0d633bd4f4e65e12568"

and signed transaction:

0100000001f08f31982996ae656e380bacf6b71b7c1b8bef425f38431e10f8a45f026fcffe01000000fd670100483045022100d08d3daafb28e76e255a0556d8aba6f771a75b6828e68f6d145cdfbc6429586702201d8d90783946440cb11d4f4e3ffb6c3ac00cb73c8022fd01d4255de935569d110147304402200880ff2d795dc2d9a88b0747af8b6d5527d601ee417dfa71150af40ab79131be02204948a8fa8840f62c08f86fd9cea8385b484dd0a7af8fced495dcb14a10b4234d0147304402205832623d6d08a497f9cb62c4d182de99045cbdb3e7dfcf2f0d5353ab9fddc3660220663e92e18a4edb28f9cab0257c7005f0d8179575bd3d7acc983bbdadc33710cf014c8b53210276a38006fee262e9918c4a9210aaaa990c9a48bcd681505cb44121300d81919421039fb9b10734d14ee0b9d5ff52da1bbf10df4196695ec80303270e62747bfd4c9221037965ae9fa904982ba24562447b9d17a69ed18313bd2bf152fca0b5c9819a70ce210252d9b0a3ff888dafaacad28133e418c185d1446e60e5c0d633bd4f4e65e1256854aeffffffff0210270000000000001976a9143dee47716e3cfa57df45113473a6312ebeaef31188acb1b7f5050000000017a914da3a5983769d69895ca53d2c1999aeef7a562c318700000000

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.