Package: bpl-lib
BPL libA simple Python package for the Blockpool BlockchainThis is an easy-to-use Python package for interacting with a Blockpool Blockchain. This package provided the majority of the functionality provided byBPL-JSFeaturesAddressfrom_secretfrom_public_keyfrom_private_keyvalidateNetworkuseuse_customget_begin_epochget_versionTransaction Entitygenerateget_idget_hashsignsecond_signto_bytesto_dictfrom_dictverifysecond_verifyTransaction TypesTransfer TransactionVote TransactionSecond Signature TransactionMulti-Signature TransactionDelegate TransactionIPFS TransactionTimeget_timeget_real_timeget_slot_numberget_slot_timeInstallationpython-mpipinstall--no-cache-dir--index-urlhttps://test.pypi.org/simple/bpl-libNote: package has been uploaded to test.pypi. Some dependencies might not be able to be install (e.g. base58) due to this. If this occurs use:pipinstallbase58
pipinstallecdsaThenpython-mpipinstall--no-cache-dir--index-urlhttps://test.pypi.org/simple/bpl-libUsageThere are 6 main sub-packages in bpl-lib:bpl_lib.addressbpl_lib.cryptobpl_lib.helpersbpl_lib.networkbpl_lib.timebpl_lib.transactionsAddressesGenerating an AddressThere are currently 2 methods for generating an address:from_public_keyfrom_secretNote: Addresses are dependent on theversionof the network, therefore to generate an address you must first select a network (see network section).from_public_keyTo generate a unique Blockpool address from a given public key:frombpl_lib.addressimportAddressfrombpl_lib.networkimportNetworkNetwork.use("mainnet")public_key="03aacac6c98daaf3d433fe90e9295ce380916946f850bcdc6f6880ae6503ca1e40"address=Address.from_public_key(public_key)print(address)Printing:b'AdzCBJt2F2Q2RYL7vnp96QhTeGdDZNZGeJ'from_secretTo generate a unique Blockpool address from a given secret passphrase:frombpl_lib.addressimportAddressfrombpl_lib.networkimportNetworkNetwork.use("mainnet")address=Address.from_secret("secret passphrase")print(address)Printing:b'AdzCBJt2F2Q2RYL7vnp96QhTeGdDZNZGeJ'ValidationNote: Addresses are dependent on theversionof the network, therefore to validate an address you must first select a network (see network section).frombpl_lib.addressimportAddressfrombpl_lib.networkimportNetworkNetwork.use("mainnet")address="AdzCBJt2F2Q2RYL7vnp96QhTeGdDZNZGeJ"is_valid=Address.validate(address)print(is_valid)Printing:TrueCryptographyGenerating Keysfrombpl_lib.cryptoimportKeyskeys=Keys("secret passphrase").to_dict()print(keys)Printing:{"public_key":"03aacac6c98daaf3d433fe90e9295ce380916946f850bcdc6f6880ae6503ca1e40","private_key":"b6a2b12beb4179538bfb42423cce2e98ccdebcc684145ba977f2f80630eb278e"}Signaturesfrombpl_lib.cryptoimportSignature,sha256message=sha256("message".encode())signature=Signature("secret passphrase").sign(message)["signature"]print(signature)Printing:"30440220622b8edf8fc5cf4522a13489a9b710b1bf94b6e37722d2278a0069ae3c67088b0220206e202dcad8e4ee2100716ce0d2c7d08a685f983c21dfbccdd6ecec50268b6f"HashingThecryptosub-package also provides common hashing algorithms such as:sha1(bytes)sha256(bytes)ripemd160(bytes)hash160(bytes)hash256(bytes)HelpersThe helpers package contains useful contains and classes:TRANSACTION_TYPE- Enum Class containing all 6 transaction typesTRANSACTION_FEES- Transaction fees depending onTRANSACTION_TYPENETWORKS_DB- Networks database file locationNetworkThe networks sub-package is an interface for network configurations.  The networks sub-package makes use of a local SQLite database, which stores the network identifier, begin epoch time and network version. These setting / fields are require for calculations such as addresses and timestamps.Using a networkThere are current 2 methods that allows a client to use a network:useuse_customuseTheusemethod requires a networkidentifierand queries the local network database for the specified configuration. The method then stores the configuration in memory.frombpl_lib.networkimportNetworkNetwork.use("mainnet")print(Network.get_begin_epoch())print(Network.get_version())Printing:"2017-03-21 13:00:00"23use_customTheuse_custommethod requires a networkidentifier,begin_epochandversion. The method first inserts this custom configuration into the local network database, this will allow you to make use of the custom configuration in other applications. After that the method stores the custom configuration in memory.Note:identifieris used as the primary key in the database, this implies that the identifier for the network must be unique. If the identifier is not unique aBPLNetworkExceptionis raised.fromdatetimeimportdatetimefrombpl_lib.networkimportNetworkidentifier="test_use_custom_method"begin_epoch=datetime.strptime("2018-07-25 15:30:00","%Y-%m-%d%H:%M:%S")version=0x19Network.use_custom(identifier,begin_epoch,version)print(Network.get_begin_epoch())print(Network.get_version())Printing:"2018-07-25 15:30:00"25Accessing the current configurationAfter loading a network configuration into memory, there are 2 settings that can be accessed via theNetworkinterface:begin_epochversionAccessing begin_epochTo accessbegin_epochtheNetwork.get_begin_epochmethod must be used.frombpl_lib.networkimportNetworkNetwork.use("testnet")print(Network.get_begin_epoch())Printing:"2017-03-21 13:00:00"Accessing versionTo accessversiontheNetwork.get_versionmethod must be used.frombpl_lib.networkimportNetworkNetwork.use("testnet")print(Network.get_version())Printing:82TimeThe time sub-package contains 4 methods:get_time- returns the timestamp for the blockchainget_real_time- converts blockchain timestamp to datetimeget_slot_number- converts blockchain timestamp to slot numberget_slot_time- converts slot number to blockchain timestampget_timeTheget_timemethod converts a datetime object to a blockchain timestamp. The method has an optional argumenttime. Iftimeis not provided then the current time will be used. (See code)frombpl_lib.timeimportTimeprint(Time.get_time())Printing:42429391get_real_timeTheget_real_timemethod converts a blockchain timestamp into a datetime object. The method has an optional argumenttimestamp. Iftimestampis not provided then the current timestamp will be used. (See code)frombpl_lib.timeimportTimeprint(Time.get_real_time(42429391))Printing:"2018-07-25 14:56:31"get_slot_numberTheget_slot_numbermethod converts a blockchain timestamp into a slot number. The method has an optional argumenttimestamp. Iftimestampis not provided then the current timestamp will be used. (See code)frombpl_lib.timeimportTimeprint(Time.get_slot_number())Printing:5303721get_slot_timeTheget_slot_timemethod converts a slot number into a blockchain timestamp. The method has an optional argumentslot_number. Ifslot_numberis not provided then the current slot number will be used. (See code)frombpl_lib.timeimportTimeprint(Time.get_slot_time(5303721))Printing:42429768TransactionsEach transaction is built from the Transaction Entity (See Features). There are 2 currently possible ways of building a BPL transaction:Transaction.generateTransaction.from_dictBuidling a transaction using generateTransaction.generateautomatically calculates the timestamp for the transaction, therefore a network must be selected before a transaction can be built. (See network)frombpl_lib.transactions.TransferimportTransferfrombpl_lib.networkimportNetworkNetwork.use("mainnet")transaction=Transfer.generate("BCU4rocsgw2GNihtnzAgFzRfx7XebZRpRi",1000,"passphrase")print(transaction.to_dict())Printing:{'recipientId':'BCU4rocsgw2GNihtnzAgFzRfx7XebZRpRi','senderPublicKey':'02e012f0a7cac12a74bdc17d844cbc9f637177b470019c32a53cef94c7a56e2ea9','type':"<TRANSACTION_TYPE.TRANSFER: 0>",'id':'9bfa3aee9ed984f856c6268b0b03dd908d3541c4c94f614fdae5c66c587560b2','asset':{},'venderField':None,'fee':10000000,'signature':'3045022100faf1e2bb7388caf0ba4ca26d6bddf9ea39197365d369f63efe271ad183745a77022047865c97baa925369ee099594010f7e7912772febbb83bcb9512f9b2759ac97d','timestamp':42430405,'amount':1000,'signSignature':None}To see how each transaction is built see the documentation in the code.Security or ErrorsIf you discover a security vulnerability or error within this package, please emailmeor message me on theBPL discord.CreditsAlistair O'Brien