示例#1
0
 def test_spend_p2wpkh(self):
     tx = Transaction([self.txin_spend], [self.txout2], has_segwit=True)
     sig = self.sk.sign_segwit_input(tx, 0, self.p2pkh_redeem_script,
                                     self.txin_spend_amount)
     pk = self.sk.get_public_key().to_hex()
     tx.witnesses = [Script([sig, pk])]
     self.assertEqual(tx.serialize(), self.spend_p2pkh_result)
示例#2
0
    def test_siganyonecanpay_none_send(self):
        """
        SIGHASH_NONE | SIGHASH_ANYONECANPAY:signs only the txin_index input
        """
        tx = Transaction([self.txin1_siganyonecanpay_none],
                         [self.txout1_siganyonecanpay_none],
                         has_segwit=True)
        pk = self.sk.get_public_key().to_hex()

        sig_signone = self.sk.sign_segwit_input(
            tx, 0, self.p2pkh_redeem_script,
            self.txin1_siganyonecanpay_none_amount,
            SIGHASH_NONE | SIGHASH_ANYONECANPAY)
        tx.witnesses = [Script([sig_signone, pk])]

        tx.inputs.append(self.txin2_siganyonecanpay_none)
        tx.outputs.append(self.txout2_siganyonecanpay_none)

        sig = self.sk.sign_segwit_input(tx, 1, self.p2pkh_redeem_script,
                                        self.txin2_siganyonecanpay_none_amount,
                                        SIGHASH_ALL)
        tx.witnesses.append(Script([sig, pk]))

        self.assertEqual(tx.serialize(),
                         self.test_siganyonecanpay_none_send_result)
示例#3
0
    def test_multiple_input_multiple_ouput(self):
        tx = Transaction(
            [self.txin1_multiple, self.txin2_multiple, self.txin3_multiple], [
                self.output1_multiple, self.output2_multiple,
                self.output3_multiple
            ],
            has_segwit=True)

        sig1 = self.sk1.sign_input(tx, 0, self.p2pkh_addr.to_script_pub_key())
        pk1 = self.sk1.get_public_key().to_hex()
        self.txin1_multiple.script_sig = Script([sig1, pk1])
        tx.witnesses = [Script([])]

        sig_p2sh1 = self.sk1.sign_segwit_input(tx, 1, self.p2wsh_redeem_script,
                                               self.txin2_multiple_amount)
        sig_p2sh2 = self.sk2.sign_segwit_input(tx, 1, self.p2wsh_redeem_script,
                                               self.txin2_multiple_amount)
        pk2 = self.p2wsh_redeem_script.to_hex()
        tx.witnesses.append(Script(['OP_0', sig_p2sh1, sig_p2sh2, pk2]))

        sig3 = self.sk1.sign_segwit_input(tx, 2,
                                          self.p2pkh_addr.to_script_pub_key(),
                                          self.txin3_multiple_amount)
        pk3 = self.sk1.get_public_key().to_hex()
        tx.witnesses.append(Script([sig3, pk3]))

        #print(tx.serialize())
        self.assertEqual(tx.serialize(),
                         self.multiple_input_multiple_ouput_result)
示例#4
0
    def test_spend_p2wsh(self):
        tx = Transaction([self.txin_spend], [self.txout2], has_segwit=True)
        sig1 = self.sk1.sign_segwit_input(tx, 0, self.p2wsh_redeem_script,
                                          self.txin_spend_amount)
        sig2 = self.sk2.sign_segwit_input(tx, 0, self.p2wsh_redeem_script,
                                          self.txin_spend_amount)

        pk = self.p2wsh_redeem_script.to_hex()
        tx.witnesses = [Script(['OP_0', sig1, sig2, pk])]
        #print(tx.serialize())
        self.assertEqual(tx.serialize(), self.spend_p2pkh_result)
示例#5
0
    def test_sigsingle_send(self):
        """
        SIGHASH_SINGLE:signs all inputs but only txin_index output
        """
        tx = Transaction([self.txin1_sigsingle], [self.txout1_sigsingle],
                         has_segwit=True)
        pk = self.sk.get_public_key().to_hex()

        sig_signone = self.sk.sign_segwit_input(tx, 0,
                                                self.p2pkh_redeem_script,
                                                self.txin1_sigsingle_amount,
                                                SIGHASH_SINGLE)
        tx.witnesses = [Script([sig_signone, pk])]

        tx.outputs.append(self.txout2_sigsingle)
        self.assertEqual(tx.serialize(), self.test_sigsingle_send_result)
示例#6
0
    def test_p2pkh_and_p2wpkh_to_p2pkh(self):
        tx = Transaction([self.txin_spend_p2pkh, self.txin_spend_p2wpkh],
                         [self.txout3],
                         has_segwit=True)
        # spend_p2pkh
        sig1 = self.sk.sign_input(tx, 0, self.p2pkh_addr.to_script_pub_key())
        pk1 = self.sk.get_public_key().to_hex()
        self.txin_spend_p2pkh.script_sig = Script([sig1, pk1])
        tx.witnesses = [Script([])]
        # spend_p2wpkh
        sig2 = self.sk.sign_segwit_input(tx, 1, self.p2pkh_redeem_script,
                                         self.txin_spend_p2wpkh_amount)
        pk2 = self.sk.get_public_key().to_hex()
        tx.witnesses.append(Script([sig2, pk2]))

        self.assertEqual(tx.serialize(), self.p2pkh_and_p2wpkh_to_p2pkh_result)
示例#7
0
    def test_signone_send(self):
        """
        SIGHASH_NONE:signs all of the inputs
        """
        # First, only txin1 and txout1 are added to the transaction.
        tx = Transaction([self.txin1_signone], [self.txout1_signone],
                         has_segwit=True)
        pk = self.sk.get_public_key().to_hex()

        sig_signone = self.sk.sign_segwit_input(tx, 0,
                                                self.p2pkh_redeem_script,
                                                self.txin1_signone_amount,
                                                SIGHASH_NONE)
        tx.witnesses = [Script([sig_signone, pk])]
        # Adding additional output signatures will not be affected
        tx.outputs.append(self.txout2_signone)

        self.assertEqual(tx.serialize(), self.test_signone_send_result)