def test_digest_p2sh_p2wpkh(self):
        # https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#p2sh-p2wpkh
        tx = Transaction.from_hex(
            '0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000'
        )
        ref = Output(
            10 * 10**8,
            hex_to_bytes('a9144733f37cf4db86fbc2efed2500b4f4e49f31202387'))

        inp = tx.inputs[0]
        inp._referenced_output = ref
        inp.script = serialize(
            hex_to_bytes('001479091972186c449eb1ded22b78e40d009bdf0089'))

        sighash = tx.sighash(0)
        self.assertEqual(
            bytes_to_hex(sighash),
            '64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6')
        sig = Signature.from_hex(
            '3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb'
        )
        pub = PublicKey.from_hex(
            '03ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a26873'
        )

        self.assertTrue(sig.verify_hash(sighash, pub))
Exemplo n.º 2
0
 def serialize_witness(self):
     if not self.segwit:
         return b'\x00'
     result = var_int(len(self.witness))
     for stack_item in self.witness:
         result += serialize(stack_item)
     return result
Exemplo n.º 3
0
    def signature_form_segwit(self, i, hashcode=SIGHASH.ALL):
        """Segwit version of signature_form"""
        # https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification
        tx = deepcopy(self)

        ref = tx.inputs[i].ref()

        nversion = tx._version[::-1]
        hashprevouts = sha256(sha256(concat((inp.outpoint() for inp in tx.inputs)))) if not hashcode.is_anyonecanpay() else pad(0, 32)
        hashsequence = sha256(sha256(concat(inp._sequence[::-1] for inp in tx.inputs))) if hashcode == SIGHASH.ALL else pad(0, 32)
        outpoint = tx.inputs[i].outpoint()
        scriptcode = serialize(tx.inputs[i].scriptcode())
        value = ref._value[::-1]
        nsequence = tx.inputs[i]._sequence[::-1]

        if not (hashcode.is_single() or hashcode.is_none()):
            hashoutputs = sha256(sha256(concat(out._value[::-1] + push(out.script) for out in tx.outputs)))
        elif hashcode.is_single() and i < len(tx.outputs):
            hashoutputs = sha256(sha256(tx.outputs[i]._value[::-1] + push(tx.outputs[i].script)))
        else:
            hashoutputs = pad(0, 32)

        nlocktime = tx._lock_time[::-1]
        sighash = pad(hashcode.value, 4)[::-1]

        return concat([nversion, hashprevouts, hashsequence, outpoint, scriptcode, value, nsequence, hashoutputs, nlocktime, sighash])
Exemplo n.º 4
0
 def serialize(self):
     return self.output[::-1] + self._index[::-1] + serialize(self.script) + self._sequence[::-1]
Exemplo n.º 5
0
 def serialize(self):
     return self._value[::-1] + serialize(self.script)