示例#1
0
 def test_json_roundtrip(self):
     VALUES = [
         42,
         0,
         -42,
         2100000000000000,
         -2100000000000000,
         "basic string",
         "\u1111Unicode",
         "\U00010000Wide Unicode",
         "\x00\n\t\r\nEscape codes",
         "\"'\"Quotes",
         "",
         None,
         b"\x00\x01\xFFBinary data",
         b"",
         CBase58Data.from_bytes(b'\x00\x01\xFF', 42),
         P2SHBitcoinAddress.from_bytes(b'\x00\x01\xFF'),
         P2PKHBitcoinAddress.from_bytes(b'\x00\x01\xFF'),
         CMutableTxIn(COutPoint(b'\x00' * 16 + b'\xFF' * 16, 42),
                      CScript(b'\x00\x01\xFF'), 42),
         CMutableTxOut(42, CScript(b'\x00\x01\xFF')),
         CMutableTransaction([
             CMutableTxIn(COutPoint(b'\x00' * 32, 42),
                          CScript(b'\x00\x01\xFF'), 42),
             CMutableTxIn(COutPoint(b'\xFF' * 32, 42),
                          CScript(b'\xFF\x01\x00'), 43)
         ], [
             CMutableTxOut(42, CScript(b'\x00\x01\xFF')),
             CMutableTxOut(43, CScript(b'\xFF\x01\x00'))
         ], 42, 3),
         [
             1,
             b'\x00\x01\xFF',
             "List Test",
         ],
         {
             'a': 1,
             'key': b'\xFF\x01\x00',
             1: 'Dictionary Test'
         },
         [
             {
                 3: [
                     0,
                     1,
                     2,
                 ],
             },
             [
                 [
                     b'\xFFRecursion Test',
                 ],
             ],
         ],
     ]
     for value in VALUES:
         self.assertEqual(from_json(to_json(value)), value)
示例#2
0
def scriptpubkey2dict(scriptPubKey):
    s = cscript2dict(scriptPubKey)['script']
    if s[0]['opcode'] == "OP_HASH160":
        assert len(s) == 3, "unexpected script length"
        assert s[2]['opcode'] == 'OP_EQUAL', "unexpected scriptPubKey"
        return {
            'type': "P2SH_OR_P2PKWH",
            'address': str(CBase58Data.from_bytes(lx(s[1]['data']), 5))
        }
    elif s[0]['opcode'] == 'OP_DUP':
        assert s[1]['opcode'] == 'OP_HASH160', "unexpected scriptPubKey"
        assert s[3]['opcode'] == 'OP_EQUALVERIFY', "unexpected scriptPubKey"
        assert s[4]['opcode'] == 'OP_CHECKSIG', "unexpected scriptPubKey"
        return {
            'type': "P2PKH",
            'address': str(CBase58Data.from_bytes(lx(s[2]['data']), 0))
        }
    else:
        assert False, "unexpected scriptPubKey"
示例#3
0
    def encode_address(self):
        hash160 = str(self.hash_line.text())
        if len(hash160) != 40:
            self.address_line.setText("Hash160 must be 40 characters.")
            self.status_message("Hash160 must be 40 characters.", True)
            return
        try:
            i = int(hash160, 16)
        except ValueError:
            self.address_line.setText("Hash160 must contain only hex characters.")
            self.status_message("Hash160 must contain only hex characters.", True)
            return

        version = self.addr_version.value()
        addr = CBase58Data.from_bytes(hash160.decode("hex"), version)
        self.address_line.setText(str(addr))
        self.status_message('Encoded address "%s".' % str(addr))
示例#4
0
    def encode_address(self):
        hash160 = str(self.hash_line.text())
        if len(hash160) != 40:
            self.address_line.setText('Hash160 must be 40 characters.')
            self.status_message('Hash160 must be 40 characters.', True)
            return
        try:
            i = int(hash160, 16)
        except ValueError:
            self.address_line.setText('Hash160 must contain only hex characters.')
            self.status_message('Hash160 must contain only hex characters.', True)
            return

        version = self.addr_version.value()
        addr = CBase58Data.from_bytes(hash160.decode('hex'), version)
        self.address_line.setText(str(addr))
        self.status_message('Encoded address "%s".' % str(addr))
示例#5
0
 def test_json_roundtrip(self):
     VALUES = [
         42,
         0,
         -42,
         2100000000000000,
         -2100000000000000,
         "basic string",
         "\u1111Unicode",
         "\U00010000Wide Unicode",
         "\x00\n\t\r\nEscape codes",
         '"\'"Quotes',
         "",
         None,
         b"\x00\x01\xFFBinary data",
         b"",
         CBase58Data.from_bytes(b"\x00\x01\xFF", 42),
         P2SHBitcoinAddress.from_bytes(b"\x00\x01\xFF"),
         P2PKHBitcoinAddress.from_bytes(b"\x00\x01\xFF"),
         CMutableTxIn(COutPoint(b"\x00" * 16 + b"\xFF" * 16, 42), CScript(b"\x00\x01\xFF"), 42),
         CMutableTxOut(42, CScript(b"\x00\x01\xFF")),
         CMutableTransaction(
             [
                 CMutableTxIn(COutPoint(b"\x00" * 32, 42), CScript(b"\x00\x01\xFF"), 42),
                 CMutableTxIn(COutPoint(b"\xFF" * 32, 42), CScript(b"\xFF\x01\x00"), 43),
             ],
             [CMutableTxOut(42, CScript(b"\x00\x01\xFF")), CMutableTxOut(43, CScript(b"\xFF\x01\x00"))],
             42,
             3,
         ),
         [1, b"\x00\x01\xFF", "List Test"],
         {"a": 1, "key": b"\xFF\x01\x00", 1: "Dictionary Test"},
         [{3: [0, 1, 2]}, [[b"\xFFRecursion Test"]]],
     ]
     for value in VALUES:
         self.assertEqual(from_json(to_json(value)), value)
示例#6
0
 def test_CBase58Data_version(self):
     self.assertEqual(
         from_json(to_json(CBase58Data.from_bytes(b'\x00\x01\xFF',
                                                  42))).nVersion, 42)
示例#7
0
def encode_address(hash160, addr_version=0):
    """Encode hash160 into an address."""
    assert len(hash160) == 20, 'Invalid RIPEMD-160 hash'
    addr = CBase58Data.from_bytes(hash160, addr_version)
    return addr
示例#8
0
def encode_address(hash160, addr_version=0):
    """Encode hash160 into an address."""
    assert len(hash160) == 20, 'Invalid RIPEMD-160 hash'
    addr = CBase58Data.from_bytes(hash160, addr_version)
    return addr
示例#9
0
 def test_CBase58Data_version(self):
     self.assertEqual(from_json(to_json(CBase58Data.from_bytes(b"\x00\x01\xFF", 42))).nVersion, 42)