def test_from_nonstd_scriptPubKey(self): """CBitcoinAddress.from_scriptPubKey() with non-standard scriptPubKeys""" # Bad P2SH scriptPubKeys # non-canonical pushdata scriptPubKey = CScript(x('a94c14000000000000000000000000000000000000000087')) with self.assertRaises(CBitcoinAddressError): CBitcoinAddress.from_scriptPubKey(scriptPubKey) # Bad P2PKH scriptPubKeys # Missing a byte scriptPubKey = CScript(x('76a914000000000000000000000000000000000000000088')) with self.assertRaises(CBitcoinAddressError): CBitcoinAddress.from_scriptPubKey(scriptPubKey) # One extra byte scriptPubKey = CScript(x('76a914000000000000000000000000000000000000000088acac')) with self.assertRaises(CBitcoinAddressError): CBitcoinAddress.from_scriptPubKey(scriptPubKey) # One byte changed scriptPubKey = CScript(x('76a914000000000000000000000000000000000000000088ad')) with self.assertRaises(CBitcoinAddressError): CBitcoinAddress.from_scriptPubKey(scriptPubKey)
def T(serialized1, serialized2, are_equal): script1 = CScript(x(serialized1)) script2 = CScript(x(serialized2)) if are_equal: self.assertEqual(script1, script2) else: self.assertNotEqual(script1, script2)
def test_tokenize_roundtrip(self): def T(serialized_script, expected_tokens, test_roundtrip=True): serialized_script = x(serialized_script) script_obj = CScript(serialized_script) actual_tokens = list(script_obj) self.assertEqual(actual_tokens, expected_tokens) if test_roundtrip: recreated_script = CScript(actual_tokens) self.assertEqual(recreated_script, serialized_script) T('', []) # standard pushdata T('00', [b'']) T('0100', [b'\x00']) T('4b' + 'ff'*0x4b, [b'\xff'*0x4b]) # non-optimal pushdata T('4c00', [b''], False) T('4c04deadbeef', [x('deadbeef')], False) T('4d0000', [b''], False) T('4d0400deadbeef', [x('deadbeef')], False) T('4e00000000', [b''], False) T('4e04000000deadbeef', [x('deadbeef')], False) # numbers T('4f', [OP_1NEGATE]) T('51', [0x1]) T('52', [0x2]) T('53', [0x3]) T('54', [0x4]) T('55', [0x5]) T('56', [0x6]) T('57', [0x7]) T('58', [0x8]) T('59', [0x9]) T('5a', [0xa]) T('5b', [0xb]) T('5c', [0xc]) T('5d', [0xd]) T('5e', [0xe]) T('5f', [0xf]) # some opcodes T('9b', [OP_BOOLOR]) T('9a9b', [OP_BOOLAND, OP_BOOLOR]) T('ff', [OP_INVALIDOPCODE]) T('fafbfcfd', [CScriptOp(0xfa), CScriptOp(0xfb), CScriptOp(0xfc), CScriptOp(0xfd)]) # all three types T('512103e2a0e6a91fa985ce4dda7f048fca5ec8264292aed9290594321aa53d37fdea32410478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc345552ae', [1, x('03e2a0e6a91fa985ce4dda7f048fca5ec8264292aed9290594321aa53d37fdea32'), x('0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455'), 2, OP_CHECKMULTISIG])
def test_bloom_create_insert_key(self): filter = CBloomFilter(2, 0.001, 0, CBloomFilter.UPDATE_ALL) pubkey = x('045B81F0017E2091E2EDCD5EECF10D5BDD120A5514CB3EE65B8447EC18BFC4575C6D5BF415E54E03B1067934A0F0BA76B01C6B9AB227142EE1D543764B69D901E0') pubkeyhash = reddcoin.core.Hash160(pubkey) filter.insert(pubkey) filter.insert(pubkeyhash) self.assertEqual(filter.serialize(), x('038fc16b080000000000000001'))
def test_from_invalid_scriptPubKey(self): """CBitcoinAddress.from_scriptPubKey() with invalid scriptPubKeys""" # We should raise a CBitcoinAddressError, not any other type of error # Truncated P2SH scriptPubKey = CScript(x('a91400000000000000000000000000000000000000')) with self.assertRaises(CBitcoinAddressError): CBitcoinAddress.from_scriptPubKey(scriptPubKey) # Truncated P2PKH scriptPubKey = CScript(x('76a91400000000000000000000000000000000000000')) with self.assertRaises(CBitcoinAddressError): CBitcoinAddress.from_scriptPubKey(scriptPubKey)
def test_from_valid_pubkey(self): """Create P2PKHBitcoinAddress's from valid pubkeys""" def T(pubkey, expected_str_addr): addr = P2PKHBitcoinAddress.from_pubkey(pubkey) self.assertEqual(str(addr), expected_str_addr) T(x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71'), '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8') T(x('0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455'), '1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T') T(CPubKey(x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71')), '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8') T(CPubKey(x('0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455')), '1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T')
def test_create_from_string(self): """Create CBitcoinAddress's from strings""" def T(str_addr, expected_bytes, expected_nVersion, expected_class): addr = CBitcoinAddress(str_addr) self.assertEqual(addr.to_bytes(), expected_bytes) self.assertEqual(addr.nVersion, expected_nVersion) self.assertEqual(addr.__class__, expected_class) T('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', x('62e907b15cbf27d5425399ebf6f0fb50ebb88f18'), 0, P2PKHBitcoinAddress) T('37k7toV1Nv4DfmQbmZ8KuZDQCYK9x5KpzP', x('4266fc6f2c2861d7fe229b279a79803afca7ba34'), 5, P2SHBitcoinAddress)
def T(hex_scriptpubkey, expected_str_address): scriptPubKey = CScript(x(hex_scriptpubkey)) addr = P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey) self.assertEqual(str(addr), expected_str_address) # now test that CBitcoinAddressError is raised with accept_non_canonical_pushdata=False with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey, accept_bare_checksig=False)
def T(serialized_script, expected_tokens, test_roundtrip=True): serialized_script = x(serialized_script) script_obj = CScript(serialized_script) actual_tokens = list(script_obj) self.assertEqual(actual_tokens, expected_tokens) if test_roundtrip: recreated_script = CScript(actual_tokens) self.assertEqual(recreated_script, serialized_script)
def test_from_invalid_pubkeys(self): """Create P2PKHBitcoinAddress's from invalid pubkeys""" # first test with accept_invalid=True def T(invalid_pubkey, expected_str_addr): addr = P2PKHBitcoinAddress.from_pubkey(invalid_pubkey, accept_invalid=True) self.assertEqual(str(addr), expected_str_addr) T(x(''), '1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E') T(x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72'), '1L9V4NXbNtZsLjrD3nkU7gtEYLWRBWXLiZ') # With accept_invalid=False we should get CBitcoinAddressError's with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey(x('')) with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey(x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72')) with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey(CPubKey(x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72')))
def test_repr(self): def T(script, expected_repr): actual_repr = repr(script) self.assertEqual(actual_repr, expected_repr) T( CScript([]), 'CScript([])') T( CScript([1]), 'CScript([1])') T( CScript([1, 2, 3]), 'CScript([1, 2, 3])') T( CScript([1, x('7ac977d8373df875eceda362298e5d09d4b72b53'), OP_DROP]), "CScript([1, x('7ac977d8373df875eceda362298e5d09d4b72b53'), OP_DROP])") T(CScript(x('0001ff515261ff')), "CScript([x(''), x('ff'), 1, 2, OP_NOP, OP_INVALIDOPCODE])") # truncated scripts T(CScript(x('6101')), "CScript([OP_NOP, x('')...<ERROR: PUSHDATA(1): truncated data>])") T(CScript(x('614bff')), "CScript([OP_NOP, x('ff')...<ERROR: PUSHDATA(75): truncated data>])") T(CScript(x('614c')), "CScript([OP_NOP, <ERROR: PUSHDATA1: missing data length>])") T(CScript(x('614c0200')), "CScript([OP_NOP, x('00')...<ERROR: PUSHDATA1: truncated data>])")
def test_from_non_canonical_scriptPubKey(self): def T(hex_scriptpubkey, expected_str_address): scriptPubKey = CScript(x(hex_scriptpubkey)) addr = P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey) self.assertEqual(str(addr), expected_str_address) # now test that CBitcoinAddressError is raised with accept_non_canonical_pushdata=False with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey, accept_non_canonical_pushdata=False) T('76a94c14000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2') T('76a94d1400000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2'), T('76a94e14000000000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2') # make sure invalid scripts raise CBitcoinAddressError with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_scriptPubKey(x('76a94c14'))
def test_to_p2sh_scriptPubKey(self): def T(redeemScript, expected_hex_bytes): redeemScript = CScript(redeemScript) actual_script = redeemScript.to_p2sh_scriptPubKey() self.assertEqual(b2x(actual_script), expected_hex_bytes) T([], 'a914b472a266d0bd89c13706a4132ccfb16f7c3b9fcb87') T([1,x('029b6d2c97b8b7c718c325d7be3ac30f7c9d67651bce0c929f55ee77ce58efcf84'),1,OP_CHECKMULTISIG], 'a91419a7d869032368fd1f1e26e5e73a4ad0e474960e87') T([b'\xff'*517], 'a9140da7fa40ebf248dfbca363c79921bdd665fed5ba87') with self.assertRaises(ValueError): CScript([b'a' * 518]).to_p2sh_scriptPubKey()
def test_create_insert_serialize(self): filter = CBloomFilter(3, 0.01, 0, CBloomFilter.UPDATE_ALL) def T(elem): """Filter contains elem""" elem = x(elem) filter.insert(elem) self.assertTrue(filter.contains(elem)) def F(elem): """Filter does not contain elem""" elem = x(elem) self.assertFalse(filter.contains(elem)) T('99108ad8ed9bb6274d3980bab5a85c048f0950c8') F('19108ad8ed9bb6274d3980bab5a85c048f0950c8') T('b5a2c786d9ef4658287ced5914b37a1b4aa32eee') T('b9300670b4c5366e95b2699e8b18bc75e5f729c5') self.assertEqual(filter.serialize(), x('03614e9b050000000000000001'))
def test_create_insert_serialize_with_tweak(self): # Same test as bloom_create_insert_serialize, but we add a nTweak of 100 filter = CBloomFilter(3, 0.01, 2147483649, CBloomFilter.UPDATE_ALL) def T(elem): """Filter contains elem""" elem = x(elem) filter.insert(elem) self.assertTrue(filter.contains(elem)) def F(elem): """Filter does not contain elem""" elem = x(elem) self.assertFalse(filter.contains(elem)) T('99108ad8ed9bb6274d3980bab5a85c048f0950c8') F('19108ad8ed9bb6274d3980bab5a85c048f0950c8') T('b5a2c786d9ef4658287ced5914b37a1b4aa32eee') T('b9300670b4c5366e95b2699e8b18bc75e5f729c5') self.assertEqual(filter.serialize(), x('03ce4299050000000100008001'))
def test_from_bare_checksig_scriptPubKey(self): def T(hex_scriptpubkey, expected_str_address): scriptPubKey = CScript(x(hex_scriptpubkey)) addr = P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey) self.assertEqual(str(addr), expected_str_address) # now test that CBitcoinAddressError is raised with accept_non_canonical_pushdata=False with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey, accept_bare_checksig=False) # compressed T('21000000000000000000000000000000000000000000000000000000000000000000ac', '14p5cGy5DZmtNMQwTQiytBvxMVuTmFMSyU') # uncompressed T('410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ac', '1QLFaVVt99p1y18zWSZnespzhkFxjwBbdP') # non-canonical encoding T('4c21000000000000000000000000000000000000000000000000000000000000000000ac', '14p5cGy5DZmtNMQwTQiytBvxMVuTmFMSyU') # odd-lengths are *not* accepted with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_scriptPubKey(x('2200000000000000000000000000000000000000000000000000000000000000000000ac'))
def T(serialized, b): script = CScript(x(serialized)) self.assertEqual(script.is_push_only(), b)
def T(hex_script): invalid_script = CScript(x(hex_script)) self.assertFalse(invalid_script.is_push_only())
def T(serialized, b): script = CScript(x(serialized)) self.assertEqual(script.is_valid(), b)
def T(elem): """Filter contains elem""" elem = x(elem) filter.insert(elem) self.assertTrue(filter.contains(elem))
def T(serialized): with self.assertRaises(CScriptInvalidError): list(CScript(x(serialized)))
def T(hex_script, expected_result): script = CScript(x(hex_script)) self.assertEqual(script.has_canonical_pushes(), expected_result)
def T(hex_script): script = CScript(x(hex_script)) self.assertEqual(script.has_canonical_pushes(), False)
def T(data, expected): data = x(data) expected = x(expected) serialized_data = CScriptOp.encode_op_pushdata(data) self.assertEqual(serialized_data, expected)
def F(elem): """Filter does not contain elem""" elem = x(elem) self.assertFalse(filter.contains(elem))
def T(hex_scriptpubkey, expected_str_address, expected_class): scriptPubKey = CScript(x(hex_scriptpubkey)) addr = CBitcoinAddress.from_scriptPubKey(scriptPubKey) self.assertEqual(str(addr), expected_str_address) self.assertEqual(addr.__class__, expected_class)
def T(serialized, b): script = CScript(x(serialized)) self.assertEqual(script.is_unspendable(), b)
def T(expected, seed, data): self.assertEqual(MurmurHash3(seed, x(data)), expected)
def T(hex_pubkey, is_valid, is_fullyvalid, is_compressed): key = CPubKey(x(hex_pubkey)) self.assertEqual(key.is_valid, is_valid) self.assertEqual(key.is_fullyvalid, is_fullyvalid) self.assertEqual(key.is_compressed, is_compressed)