def test_address_to_script(self): # bech32 native segwit # test vectors from BIP-0173 TODO self.assertEqual( address_to_script('MONA1Q4KPN6PSTHGD5UR894AUHJJ2G02WLGMP8KE08NE'), '0014ad833d060bba1b4e0ce5af797949487a9df46c27') self.assertEqual( address_to_script( 'mona1qp8f842ywwr9h5rdxyzggex7q3trvvvaarfssxccju52rj6htfzfsqr79j2' ), '002009d27aa88e70cb7a0da620908c9bc08ac6c633bd1a61036312e514396aeb4893' ) self.assertEqual(address_to_script('mona1sw50qpvnxy8'), '6002751e') self.assertEqual( address_to_script('mona1zw508d6qejxtdg4y5r3zarvaryvhm3vz7'), '5210751e76e8199196d454941c45d1b3a323') # base58 P2PKH self.assertEqual( address_to_script('MFMy9FwJsV6HiN5eZDqDETw4pw52q3UGrb'), '76a91451dadacc7021440cbe4ca148a5db563b329b4c0388ac') self.assertEqual( address_to_script('MVELZC3ks1Xk59kvKWuSN3mpByNwaxeaBJ'), '76a914e9fb298e72e29ebc2b89864a5e4ae10e0b84726088ac') # base58 P2SH self.assertEqual( address_to_script('PHjTKtgYLTJ9D2Bzw2f6xBB41KBm2HeGfg'), 'a9146449f568c9cd2378138f2636e1567112a184a9e887') self.assertEqual( address_to_script('3AqJ6Tn8qS8LKMDfi41AhuZiY6JbR6mt6E'), 'a9146449f568c9cd2378138f2636e1567112a184a9e887')
def parse_output(self, x) -> bytes: try: address = self.parse_address(x) return bfh(bitcoin.address_to_script(address)) except: script = self.parse_script(x) return bfh(script)
def parse_output(self, x) -> bytes: try: address = self.parse_address(x) return bfh(bitcoin.address_to_script(address)) except Exception: pass try: script = self.parse_script(x) return bfh(script) except Exception: pass raise Exception("Invalid address or script.")
def test_address_to_script(self): # bech32 native segwit # test vectors from BIP-0173 self.assertEqual( address_to_script( 'tmona1qfj8lu0rafk2mpvk7jj62q8eerjpex3xlcadtupkrkhh5a73htmhs68e55m' ), '00204c8ffe3c7d4d95b0b2de94b4a01f391c839344dfc75abe06c3b5ef4efa375eef' ) self.assertEqual( address_to_script( 'tmona1q0p29rfu7ap3duzqj5t9e0jzgqzwdtd97pa5rhuz4r38t5a6dknyqxmyyaz' ), '0020785451a79ee862de0812a2cb97c848009cd5b4be0f683bf0551c4eba774db4c8' ) # base58 P2PKH self.assertEqual( address_to_script('mptvgSbAs4iwxQ7JQZdEN6Urpt3dtjbawd'), '76a91466e0ef980c8ff8129e8d0f716b2ce1df2f97bbbf88ac') self.assertEqual( address_to_script('mrodaP7iH3B9ZXSptfGQXLKE3hfdjMdf7y'), '76a9147bd0d45ec256701811ebb38cfd2ba3d17576bf3e88ac') # base58 P2SH self.assertEqual( address_to_script('pJwLxfRRUhAaYJsKzKCk9cATAn8Do2SS7L'), 'a91492e825fa92f4aa873c6caf4b20f6c7e949b456a987') self.assertEqual( address_to_script('pHNnBm6ECsh5QsUyXMzdoAXV8qV68wj2M4'), 'a91481c75a711f23443b44d70b10ddf856e39a6b254d87')
def pin_changed(s): if len(s) < len(self.idxs): i = self.idxs[len(s)] addr = self.txdata['address'] if not constants.net.TESTNET: text = addr[:i] + '<u><b>' + addr[ i:i + 1] + '</u></b>' + addr[i + 1:] else: # pin needs to be created from mainnet address addr_mainnet = bitcoin.script_to_address( bitcoin.address_to_script(addr), net=constants.BitcoinMainnet) addr_mainnet = addr_mainnet[:i] + '<u><b>' + addr_mainnet[ i:i + 1] + '</u></b>' + addr_mainnet[i + 1:] text = str(addr) + '\n' + str(addr_mainnet) self.addrtext.setHtml(str(text)) else: self.addrtext.setHtml(_("Press Enter"))
from electrum_mona.transaction import Transaction from electrum_mona import paymentrequest from electrum_mona import paymentrequest_pb2 as pb2 from electrum_mona.bitcoin import address_to_script chain_file = 'mychain.pem' cert_file = 'mycert.pem' amount = 1000000 address = "18U5kpCAU4s8weFF8Ps5n8HAfpdUjDVF64" memo = "blah" out_file = "payreq" with open(chain_file, 'r') as f: chain = tlslite.X509CertChain() chain.parsePemList(f.read()) certificates = pb2.X509Certificates() certificates.certificate.extend(map(lambda x: str(x.bytes), chain.x509List)) with open(cert_file, 'r') as f: rsakey = tlslite.utils.python_rsakey.Python_RSAKey.parsePEM(f.read()) script = address_to_script(address) pr_string = paymentrequest.make_payment_request(amount, script, memo, rsakey) with open(out_file, 'wb') as f: f.write(pr_string) print("Payment request was written to file '%s'" % out_file)
def test_close_upfront_shutdown_script(self): alice_channel, bob_channel = create_test_channels() # create upfront shutdown script for bob, alice doesn't use upfront # shutdown script bob_uss_pub = lnutil.privkey_to_pubkey(os.urandom(32)) bob_uss_addr = bitcoin.pubkey_to_address('p2wpkh', bh2u(bob_uss_pub)) bob_uss = bfh(bitcoin.address_to_script(bob_uss_addr)) # bob commits to close to bob_uss alice_channel.config[HTLCOwner.REMOTE].upfront_shutdown_script = bob_uss # but bob closes to some receiving address, which we achieve by not # setting the upfront shutdown script in the channel config bob_channel.config[HTLCOwner.LOCAL].upfront_shutdown_script = b'' p1, p2, w1, w2, q1, q2 = self.prepare_peers(alice_channel, bob_channel) w1.network.config.set_key('dynamic_fees', False) w2.network.config.set_key('dynamic_fees', False) w1.network.config.set_key('fee_per_kb', 5000) w2.network.config.set_key('fee_per_kb', 1000) async def test(): async def close(): await asyncio.wait_for(p1.initialized, 1) await asyncio.wait_for(p2.initialized, 1) # bob closes channel with different shutdown script await p1.close_channel(alice_channel.channel_id) gath.cancel() async def main_loop(peer): async with peer.taskgroup as group: await group.spawn(peer._message_loop()) await group.spawn(peer.htlc_switch()) coros = [close(), main_loop(p1), main_loop(p2)] gath = asyncio.gather(*coros) await gath with self.assertRaises(UpfrontShutdownScriptViolation): run(test()) # bob sends the same upfront_shutdown_script has he announced alice_channel.config[HTLCOwner.REMOTE].upfront_shutdown_script = bob_uss bob_channel.config[HTLCOwner.LOCAL].upfront_shutdown_script = bob_uss p1, p2, w1, w2, q1, q2 = self.prepare_peers(alice_channel, bob_channel) w1.network.config.set_key('dynamic_fees', False) w2.network.config.set_key('dynamic_fees', False) w1.network.config.set_key('fee_per_kb', 5000) w2.network.config.set_key('fee_per_kb', 1000) async def test(): async def close(): await asyncio.wait_for(p1.initialized, 1) await asyncio.wait_for(p2.initialized, 1) await p1.close_channel(alice_channel.channel_id) gath.cancel() async def main_loop(peer): async with peer.taskgroup as group: await group.spawn(peer._message_loop()) await group.spawn(peer.htlc_switch()) coros = [close(), main_loop(p1), main_loop(p2)] gath = asyncio.gather(*coros) await gath with self.assertRaises(concurrent.futures.CancelledError): run(test())