Exemplo n.º 1
0
    def __new__(cls, s):
        """from bech32 addr to """
        witver, data = decode(bitcointx.params.BECH32_HRP, s)
        if witver is None and data is None:
            raise Bech32Error('Bech32 decoding error')

        return cls.from_bytes(witver, data)
Exemplo n.º 2
0
    def test_from_data(self):
        test_bytes = unhexlify('751e76e8199196d454941c45d1b3a323f1433bd6')
        b = MockBech32Data.from_bytes(test_bytes, witver=0)
        self.assertEqual(b.bech32_witness_version, 0)
        self.assertEqual(b.__class__.bech32_witness_version, -1)
        self.assertEqual(
            str(b).upper(), 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')

        with self.assertRaises(ValueError):
            MockBech32Data.from_bytes(test_bytes, witver=-1)

        MockBech32Data.bech32_witness_version = 16

        with self.assertRaises(ValueError):
            MockBech32Data.from_bytes(test_bytes, witver=-1)

        with self.assertRaises(ValueError):
            MockBech32Data.from_bytes(test_bytes, witver=0)

        b = MockBech32Data.from_bytes(test_bytes, witver=16)
        self.assertEqual(b.bech32_witness_version, 16)
        self.assertEqual(MockBech32Data.bech32_witness_version, 16)

        witver, data = decode(MockBech32Data.bech32_hrp, str(b))
        self.assertEqual(data, test_bytes)
        self.assertEqual(witver, 16)
Exemplo n.º 3
0
    def __new__(cls, s):
        """from bech32 addr to """
        if cls.bech32_hrp is None:
            raise TypeError(
                'CBech32Data subclasses should define bech32_hrp attribute')
        witver, data = decode(cls.bech32_hrp, s)
        if witver is None and data is None:
            raise Bech32Error('Bech32 decoding error')

        return cls.from_bytes(data, witver=witver)
Exemplo n.º 4
0
    def __new__(cls: Type[T_CBech32Data], s: str) -> T_CBech32Data:
        """from bech32 addr to """
        if cls.bech32_hrp is None:
            raise TypeError(
                'CBech32Data subclasses should define bech32_hrp attribute')
        witver, data = decode(cls.bech32_hrp, s)
        if witver is None or data is None:
            assert witver is None and data is None
            raise Bech32Error('Bech32 decoding error')

        return cls.bech32_match_progam_and_version(data, witver)
Exemplo n.º 5
0
    def test_encode_decode(self):
        for exp_bin, exp_bech32 in load_test_vectors(
                'bech32_encode_decode.json'):
            exp_bin = unhexlify(exp_bin.encode('utf8'))
            witver = self.op_decode(exp_bin[0])
            hrp = exp_bech32[:exp_bech32.rindex('1')].lower()
            self.assertEqual(exp_bin[1], len(exp_bin[2:]))
            act_bech32 = encode(hrp, witver, exp_bin[2:])
            act_bin = decode(hrp, exp_bech32)

            self.assertEqual(act_bech32.lower(), exp_bech32.lower())
            self.assertEqual(to_scriptPubKey(*act_bin), bytes(exp_bin))
Exemplo n.º 6
0
    def test_encode_decode(self) -> None:
        for exp_bin_str, exp_bech32 in load_test_vectors('bech32_encode_decode.json'):
            exp_bin = unhexlify(exp_bin_str.encode('utf8'))
            witver = self.op_decode(exp_bin[0])
            hrp = exp_bech32[:exp_bech32.rindex('1')].lower()
            self.assertEqual(exp_bin[1], len(exp_bin[2:]))
            act_bech32 = encode(hrp, witver, exp_bin[2:])
            assert act_bech32 is not None
            act_bin = decode(hrp, exp_bech32)
            wv, wp = act_bin
            assert wv is not None
            assert wp is not None

            self.assertEqual(act_bech32.lower(), exp_bech32.lower())
            self.assertEqual(to_scriptPubKey(wv, wp), bytes(exp_bin))