Ejemplo n.º 1
0
    def utxos(self):
        if self._outputs is None:
            import urllib.request
            import json
            url = network('utxo_url').format(address=self.address)

            req = urllib.request.Request(url)
            outputs = []
            try:
                with urllib.request.urlopen(req) as resp:
                    data = json.loads(resp.read().decode())
            except HTTPError as e:
                resp = e.read().decode()
                if resp == 'No free outputs to spend':
                    self._outputs = []
                else:
                    raise UpstreamError(resp)
            else:
                for item in data['unspent_outputs']:
                    out = Output(value=item['value'], script=hex_to_bytes(item['script']))
                    out.parent_id = hex_to_bytes(item['tx_hash_big_endian'])
                    out.tx_index = item['tx_output_n']
                    outputs.append(out)
                self._outputs = outputs
        return self._outputs
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
 def _receive(self, value: int):
     """Creates an output that sends to this address"""
     addr_type = self.type()
     output = Output(value=value, script=b'')
     if addr_type == ADDRESS.P2PKH:
         address = base58.decode(self.address).rjust(25, b'\x00')
         keyhash = address[1:-4]
         output.script = OP.DUP.byte + OP.HASH160.byte + push(keyhash) + OP.EQUALVERIFY.byte + OP.CHECKSIG.byte
     elif addr_type == ADDRESS.P2SH:
         address = base58.decode(self.address).rjust(25, b'\x00')
         scripthash = address[1:-4]
         output.script = OP.HASH160.byte + push(scripthash) + OP.EQUAL.byte
     elif addr_type in (ADDRESS.P2WPKH, ADDRESS.P2WSH):
         witness_version, witness_program = bech32.decode(network('hrp'), self.address)
         output.script = OP(bytes_to_int(witness_byte(witness_version))).byte + push(bytes(witness_program))
     else:
         raise ValidationError(f"Cannot create output of type {addr_type}")
     return output
Ejemplo n.º 4
0
    def get_utxos(self, address: str):
        from cryptotools.BTC.transaction import Output

        url = self._get_url(self.UTXO_URLS)
        req = request.Request(url.format(address=address))
        outputs = []
        try:
            with request.urlopen(req) as resp:
                data = json.loads(resp.read().decode())
        except HTTPError as e:
            resp = e.read().decode()
            raise UpstreamError(resp)
        else:
            for item in data:
                out = Output(value=item['value'], script=b"")
                out.parent_id = hex_to_bytes(item['txid'])
                out.tx_index = item['vout']
                outputs.append(out)
            return outputs
Ejemplo n.º 5
0
    def test_digest_p2wsh(self):
        ### NEEDS CODESEPERATOR
        # https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#native-p2wsh
        tx = Transaction.from_hex(
            '0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f8145e5acadf23f751864167f32e0963f788ac00000000'
        )

        ref = Output(
            10**9,
            hex_to_bytes('a9144733f37cf4db86fbc2efed2500b4f4e49f31202387'))
Ejemplo n.º 6
0
    def get_utxos(self, address):
        from cryptotools.BTC.transaction import Output

        url = self._get_url(self.UTXO_URLS)
        req = request.Request(url.format(address=address))
        outputs = []
        try:
            with request.urlopen(req) as resp:
                data = json.loads(resp.read().decode())
        except HTTPError as e:
            resp = e.read().decode()
            if resp == 'No free outputs to spend':
                return []
            else:
                raise UpstreamError(resp)
        else:
            for item in data['unspent_outputs']:
                out = Output(value=item['value'], script=hex_to_bytes(item['script']))
                out.parent_id = hex_to_bytes(item['tx_hash_big_endian'])
                out.tx_index = item['tx_output_n']
                outputs.append(out)
            return outputs
Ejemplo n.º 7
0
    def get_utxos(self, address: str) -> List['Output']:
        from cryptotools.BTC.transaction import Output

        response = self.rpc_call('scantxoutset', ['start', [f"addr({address})"]])['result']
        
        if not response['success']:
            raise UpstreamError(response['error'])
        outs = response['unspents']
        outputs = []
        for out in outs:
            sats = btc_to_satoshi(out['amount'])
            outputs.append(Output(value=sats, script=hex_to_bytes(out['scriptPubKey'])))
        return outputs
Ejemplo n.º 8
0
    def test_digest_p2wpkh(self):
        # https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#native-p2wpkh
        tx = Transaction.from_hex(
            '0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000'
        )
        ref = Output(
            6 * 10**8,
            hex_to_bytes('00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1'))

        tx.inputs[1]._referenced_output = ref

        sighash = sha256(sha256(tx.signature_form_segwit(1)))

        self.assertEqual(
            bytes_to_hex(sighash),
            'c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670')
        public = PublicKey.from_hex(
            '025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357'
        )

        sig = Signature.from_hex(
            '304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee'
        )
        self.assertTrue(sig.verify_hash(sighash, public))
Ejemplo n.º 9
0
    def test_digest_p2sh_p2wsh(self):
        # https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#p2sh-p2wsh
        tx = Transaction.from_hex(
            '010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000'
        )
        ref = Output(
            int(9.87654321 * 10**8),
            hex_to_bytes('a9149993a429037b5d912407a71c252019287b8d27a587'))

        inp = tx.inputs[0]
        inp._referenced_output = ref
        inp.script = push(
            hex_to_bytes(
                '0020a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1bb8a09062c35f0dcb54'
            ))
        inp.witness = [
            hex_to_bytes(
                '56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae'
            )
        ]

        sighash = tx.sighash(0, hashcode=SIGHASH.ALL)
        self.assertEqual(
            bytes_to_hex(sighash),
            '185c0be5263dce5b4bb50a047973c1b6272bfbd0103a89444597dc40b248ee7c')

        sighash = tx.sighash(0, hashcode=SIGHASH.NONE)
        self.assertEqual(
            bytes_to_hex(sighash),
            'e9733bc60ea13c95c6527066bb975a2ff29a925e80aa14c213f686cbae5d2f36')

        sighash = tx.sighash(0, hashcode=SIGHASH.SINGLE)
        self.assertEqual(
            bytes_to_hex(sighash),
            '1e1f1c303dc025bd664acb72e583e933fae4cff9148bf78c157d1e8f78530aea')

        sighash = tx.sighash(0, hashcode=SIGHASH.ALL_ANYONECANPAY)
        self.assertEqual(
            bytes_to_hex(sighash),
            '2a67f03e63a6a422125878b40b82da593be8d4efaafe88ee528af6e5a9955c6e')

        sighash = tx.sighash(0, hashcode=SIGHASH.NONE_ANYONECANPAY)
        self.assertEqual(
            bytes_to_hex(sighash),
            '781ba15f3779d5542ce8ecb5c18716733a5ee42a6f51488ec96154934e2c890a')

        sighash = tx.sighash(0, hashcode=SIGHASH.SINGLE_ANYONECANPAY)
        self.assertEqual(
            bytes_to_hex(sighash),
            '511e8e52ed574121fc1b654970395502128263f62662e076dc6baf05c2e6a99b')

        signatures = [
            hex_to_bytes(
                '304402206ac44d672dac41f9b00e28f4df20c52eeb087207e8d758d76d92c6fab3b73e2b0220367750dbbe19290069cba53d096f44530e4f98acaa594810388cf7409a1870ce01'
            ),
            hex_to_bytes(
                '3044022068c7946a43232757cbdf9176f009a928e1cd9a1a8c212f15c1e11ac9f2925d9002205b75f937ff2f9f3c1246e547e54f62e027f64eefa2695578cc6432cdabce271502'
            ),
            hex_to_bytes(
                '3044022059ebf56d98010a932cf8ecfec54c48e6139ed6adb0728c09cbe1e4fa0915302e022007cd986c8fa870ff5d2b3a89139c9fe7e499259875357e20fcbb15571c76795403'
            ),
            hex_to_bytes(
                '3045022100fbefd94bd0a488d50b79102b5dad4ab6ced30c4069f1eaa69a4b5a763414067e02203156c6a5c9cf88f91265f5a942e96213afae16d83321c8b31bb342142a14d16381'
            ),
            hex_to_bytes(
                '3045022100a5263ea0553ba89221984bd7f0b13613db16e7a70c549a86de0cc0444141a407022005c360ef0ae5a5d4f9f2f87a56c1546cc8268cab08c73501d6b3be2e1e1a8a0882'
            ),
            hex_to_bytes(
                '30440220525406a1482936d5a21888260dc165497a90a15669636d8edca6b9fe490d309c022032af0c646a34a44d1f4576bf6a4a74b67940f8faa84c7df9abe12a01a11e2b4783'
            )
        ]
        inp.witness = [b''] + signatures + inp.witness
        self.assertEqual(
            tx.hex(),
            '0100000000010136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000023220020a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1bb8a09062c35f0dcb54ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac080047304402206ac44d672dac41f9b00e28f4df20c52eeb087207e8d758d76d92c6fab3b73e2b0220367750dbbe19290069cba53d096f44530e4f98acaa594810388cf7409a1870ce01473044022068c7946a43232757cbdf9176f009a928e1cd9a1a8c212f15c1e11ac9f2925d9002205b75f937ff2f9f3c1246e547e54f62e027f64eefa2695578cc6432cdabce271502473044022059ebf56d98010a932cf8ecfec54c48e6139ed6adb0728c09cbe1e4fa0915302e022007cd986c8fa870ff5d2b3a89139c9fe7e499259875357e20fcbb15571c76795403483045022100fbefd94bd0a488d50b79102b5dad4ab6ced30c4069f1eaa69a4b5a763414067e02203156c6a5c9cf88f91265f5a942e96213afae16d83321c8b31bb342142a14d16381483045022100a5263ea0553ba89221984bd7f0b13613db16e7a70c549a86de0cc0444141a407022005c360ef0ae5a5d4f9f2f87a56c1546cc8268cab08c73501d6b3be2e1e1a8a08824730440220525406a1482936d5a21888260dc165497a90a15669636d8edca6b9fe490d309c022032af0c646a34a44d1f4576bf6a4a74b67940f8faa84c7df9abe12a01a11e2b4783cf56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae00000000'
        )
        self.assertTrue(tx.verify())