如何在Go中签署多重签名交易

时间:2019-01-17 09:26:18

标签: bitcoin ecdsa bitcoind bitcoin-testnet

我正在尝试使用一些btcd包在Go中签署多重签名交易(4个签名者中的3个)。

以下功能应签署交易记录:

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
}

在交易验证程序期间,尝试使用以下功能执行txOut[0]的scriptPubKey:

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
}

Execute函数引发的错误是Error on execute transaction scriptPubKey: not all signatures empty on failed checkmultisig,这似乎是签名验证中的错误,我上面提到的函数可能出什么问题了?

未签名的原始交易的示例:

0100000001f08f31982996ae656e380bacf6b71b7c1b8bef425f38431e10f8a45f026fcffe0100000017a914da3a5983769d69895ca53d2c1999aeef7a562c3187ffffffff0210270000000000001976a9143dee47716e3cfa57df45113473a6312ebeaef31188acb1b7f5050000000017a914da3a5983769d69895ca53d2c1999aeef7a562c318700000000

公钥:

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

并签署了交易:

0100000001f08f31982996ae656e380bacf6b71b7c1b8bef425f38431e10f8a45f026fcffe01000000fd670100483045022100d08d3daafb28e76e255a0556d8aba6f771a75b6828e68f6d145cdfbc6429586702201d8d90783946440cb11d4f4e3ffb6c3ac00cb73c8022fd01d4255de935569d110147304402200880ff2d795dc2d9a88b0747af8b6d5527d601ee417dfa71150af40ab79131be02204948a8fa8840f62c08f86fd9cea8385b484dd0a7af8fced495dcb14a10b4234d0147304402205832623d6d08a497f9cb62c4d182de99045cbdb3e7dfcf2f0d5353ab9fddc3660220663e92e18a4edb28f9cab0257c7005f0d8179575bd3d7acc983bbdadc33710cf014c8b53210276a38006fee262e9918c4a9210aaaa990c9a48bcd681505cb44121300d81919421039fb9b10734d14ee0b9d5ff52da1bbf10df4196695ec80303270e62747bfd4c9221037965ae9fa904982ba24562447b9d17a69ed18313bd2bf152fca0b5c9819a70ce210252d9b0a3ff888dafaacad28133e418c185d1446e60e5c0d633bd4f4e65e1256854aeffffffff0210270000000000001976a9143dee47716e3cfa57df45113473a6312ebeaef31188acb1b7f5050000000017a914da3a5983769d69895ca53d2c1999aeef7a562c318700000000

谢谢!

0 个答案:

没有答案