def test_private_keys(self): privateKey = Address.PrivateKeyFromWIF( b'\x80', '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ' ) # has bitcoin main net address version! self.assertEqual( privateKey, b"\x0c\x28\xfc\xa3\x86\xc7\xa2'`\x0b/\xe5\x0b|\xae\x11\xec\x86\xd3\xbf\x1f\xbeG\x1b\xe8\x98'\xe1\x9dr\xaa\x1d" ) testNetWIF = Address.PrivateKeyToWIF( privateKey, b'\xef') # litecoin testnet private key address version self.assertEqual( testNetWIF, '91gGn1HgSap6CbU12F6z3pJri26xzp7Ay1VW6NHCoEayNXwRpu2')
def signAndSend(self, unsignedTransactionHex, privateKeys, maximumSignedSize): #print('\t\tunsignedTransactionHex =', unsignedTransactionHex.__repr__()) #print('\t\tprivateKeys =', privateKeys.__repr__()) #print('\t\tmaximumSignedSize =', maximumSignedSize.__repr__()) #return None ## lowest level transaction send interface signingResult = self._rpcHost.call('signrawtransaction', unsignedTransactionHex) if signingResult['complete'] != True: privateKeys_WIF = [] for privateKey in privateKeys: privateKeys_WIF.append(Address.PrivateKeyToWIF(privateKey, self._privateKeyAddressVersion)) signingResult = self._rpcHost.call('signrawtransaction', signingResult['hex'], None, privateKeys_WIF) if signingResult['complete'] != True: raise SigningFailed("RPC call to signrawtransaction did not set 'complete' to True") signedHex = signingResult['hex'] byteSize = len(signedHex) / 2 if byteSize > maximumSignedSize: raise MaximumSignedSizeExceeded() try: txID = self._rpcHost.call('sendrawtransaction', signedHex) except RPC.RPCFailureException as e: raise ExceptionReportedToUser('RPC error sending signed transaction: ' + str(e)) with open(self._submittedTransactionsFileName, mode='a') as f: f.write(txID) f.write('\n') f.write(signedHex) f.write('\n') return txID
def addKeyPairAndReturnPubKeyHash(self): privateKey = self._keyGenerator.generatePrivateKey() privateKeyWIF = Address.PrivateKeyToWIF(privateKey, self._privateKeyAddressVersion) pubKeyHash = self._keyGenerator.privateKeyToPubKeyHash(privateKey) self._privateKeys.append(privateKey) self._pubKeyHashes.append(pubKeyHash) with open(self._fileName, mode='a') as f: f.write(privateKeyWIF) f.write('\n') return pubKeyHash
def test_bad_address(self): version = b'\x6f' self.assertRaisesRegexp(Address.BadAddress, 'invalid base58 character encountered', Address.ToPubKeyHash, version, 'm$5DS9xE4PBBhB5eHKRCNaEzekgspzxATY') self.assertRaisesRegexp(Address.BadAddress, 'checksum mismatch', Address.ToPubKeyHash, version, 'm') self.assertRaisesRegexp(Address.BadAddress, 'checksum mismatch', Address.ToPubKeyHash, version, 'mh5DS9xE4PBBhB6eHKRCNaEzekgspzxATY') pubKeyHash = b'\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1babcdefghi' address = Address.FromPubKeyHash(b'\x01', pubKeyHash) self.assertRaisesRegexp(Address.BadAddress, 'incorrect version byte', Address.ToPubKeyHash, b'\x02', address)
def __init__(self, fileName, privateKeyAddressVersion, keyGenerator=None): if keyGenerator is None: keyGenerator = DefaultKeyGenerator() self._privateKeyAddressVersion = privateKeyAddressVersion self._keyGenerator = keyGenerator self._fileName = fileName self._privateKeys = [] self._pubKeyHashes = [] if os.path.exists(fileName): with open(fileName, mode='r') as f: lines = f.readlines() for line in lines: privateKeyWIF = line.strip() privateKey = Address.PrivateKeyFromWIF(self._privateKeyAddressVersion, privateKeyWIF) self._privateKeys.append(privateKey) pubKeyHash = self._keyGenerator.privateKeyToPubKeyHash(privateKey) self._pubKeyHashes.append(pubKeyHash)
def getUnspent(self): ## lowest level getUnspent interface result = [] allUnspent = self._rpcHost.call('listunspent') for output in allUnspent: if not 'address' in output: ## is this check required? continue filtered = {} for key in ('txid', 'vout', 'scriptPubKey'): filtered[key] = output[key] filtered['address'] = Address.ToPubKeyHash(self._addressVersion, output['address']) amount_FloatFromJSON = output['amount'] amount_TenthsOfSatoshis = long(amount_FloatFromJSON * 1e9) # round to nearest, after conversion to integer amount_Satoshis = (amount_TenthsOfSatoshis + 5) // 10 filtered['amount'] = amount_Satoshis result.append(filtered) return result
def test(self): version = b'\x6f' # note old control address suffix here pubKeyHash = b'SWB\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00extrad' address = Address.FromPubKeyHash(version, pubKeyHash) assert type(address) is str self.assertEqual(address, 'mo7cukkECVHPxAEApxTJAQnbRj8xSHUzkH') pubKeyHash2 = Address.ToPubKeyHash(version, address) assert type(pubKeyHash2) is type(b'') self.assertEqual(pubKeyHash, pubKeyHash2) pubKeyHash = b'SWB\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0bxtrada' address = Address.FromPubKeyHash(version, pubKeyHash) self.assertEqual(address, 'mo7cukopgWjYZqRxZ5gxbYWXqHrLS9tKsx') pubKeyHash2 = Address.ToPubKeyHash(version, address) self.assertEqual(pubKeyHash, pubKeyHash2) pubKeyHash = b'\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1babcdefghi' address = Address.FromPubKeyHash(version, pubKeyHash) self.assertEqual(address, 'mh5DS9xE4PBBhB5eHKRCNaEzekgspzxATY') pubKeyHash2 = Address.ToPubKeyHash(version, address) self.assertEqual(pubKeyHash, pubKeyHash2)
def getNewNonSwapBillAddress(self): return Address.ToPubKeyHash(self._addressVersion, self._rpcHost.call('getnewaddress'))
def addressFromEndUserFormat(self, address): return Address.ToPubKeyHash(self._addressVersion, address)
def formatAddressForEndUser(self, pubKeyHash): return Address.FromPubKeyHash(self._addressVersion, pubKeyHash)
from __future__ import print_function import sys, argparse, binascii, traceback, struct import csv from os import path scriptPath = path.dirname(path.abspath(__file__)) sys.path.append(path.join(scriptPath, 'module')) sys.dont_write_bytecode = True from SwapBill import Address pubKeyHash = binascii.unhexlify(sys.argv[1].encode('ascii')) print(Address.FromPubKeyHash(b'\x6f', pubKeyHash))
def CheckRoundTrip(self, pubKeyHash, version=b'\x6f'): version = b'\x6f' address = Address.FromPubKeyHash(version, pubKeyHash) pubKeyHash2 = Address.ToPubKeyHash(version, address) self.assertEqual(pubKeyHash, pubKeyHash2)
from __future__ import print_function import sys from os import path scriptPath = path.dirname(path.abspath(__file__)) sys.path.append(path.join(scriptPath, 'module')) sys.dont_write_bytecode = True from SwapBill import Wallet, Address keyGen = Wallet.DefaultKeyGenerator() privateKey = keyGen.generatePrivateKey() print(Address.PrivateKeyToWIF( privateKey, b'\x80')) # bitcoin mainnet private key address version pubKeyHash = keyGen.privateKeyToPubKeyHash(privateKey) address = Address.FromPubKeyHash(b'\x00', pubKeyHash) # bitcoin mainnet address version assert Address.ToPubKeyHash(b'\x00', address) == pubKeyHash print(address)
def addressFromEndUserFormat(self, address): return Address.ToPubKeyHash( b'\x6f', address) # litecoin testnet address version
def formatAddressForEndUser(self, pubKeyHash): return Address.FromPubKeyHash( b'\x6f', pubKeyHash) # litecoin testnet address version