Package: enchaintesdk
Enchainté SDK -  PythonThis SDK offers all the features available in the Enchainté Toolset:Write messagesGet messages proofValidate proofGet messages detailsInstallationThe SDK can be installed with PIP as follows:$pipinstallenchaintesdkUsageThe following examples summarize how to access the different functionalities available:Prepare dataIn order to interact with the SDK data must be processed through the Message module.There are several ways to generate a Message:fromenchaintesdkimportMessage# From a dictd={'data':'Example Data'}Message.fromDict(d)# From a hash string (hex encoded 64-chars long string with no "0x" prefix)Message.fromHash('5ac706bdef87529b22c08646b74cb98baf310a46bd21ee420814b04c71fa42b1')# From a hex encoded string (with no "0x" prefix)Message.fromHex('0123456789abcdef')# From a stringMessage.fromString('Example Data')# From a bytesMessage.fromBytes(b'Example Data')# Retrieve the computed messageMessage.fromBytes(b'Example Data').getHash()Send messagesThis example shows how to send data to Enchainté.fromenchaintesdkimportEnchainteClient,Message,EnchainteSDKException,ConfigEnvimportosapi_key=os.getenv("ENCHAINTE_APIKEY",default='api_key')client=EnchainteClient(api_key,environment=ConfigEnv.TEST)messages=[Message.fromString('Example Data 1')]try:send_receipt=client.sendMessages(messages)print('sendReceipt status: ',send_receipt[0].status)exceptEnchainteSDKException:raiseGet messages statusThis example shows how to get all the details and status of messages:fromenchaintesdkimportEnchainteClient,Message,EnchainteSDKException,ConfigEnvimportosapiKey=os.getenv("ENCHAINTE_APIKEY",default='apiKey')client=EnchainteClient(apiKey,environment=ConfigEnv.TEST)messages=[Message.fromString('Example Data 1'),Message.fromString('Example Data 2'),Message.fromString('Example Data 3')]try:m_receipts=client.getMessages(messages)formrinm_receipts:print("MessageReceipt: {{anchor_id:{}, client:{}, message:{}, status:{}}}".format(mr.anchor,mr.client,mr.message,mr.status))exceptEnchainteSDKException:raiseWait for messages to processThis example shows how to wait for a message to be processed by Enchainté after sending i:fromenchaintesdkimportEnchainteClient,Message,EnchainteSDKException,ConfigEnvimportosapi_key=os.getenv("ENCHAINTE_APIKEY",default='api_key')client=EnchainteClient(api_key,environment=ConfigEnv.TEST)messages=[Message.fromString('Example Data 1')]try:send_receipt=client.sendMessages(messages)anchor=client.waitAnchor(send_receipt[0].anchor)print("Anchor: {{id:{}, blocks:{}, network:{}, root:{}, status:{}}}".format(anchor.id,anchor.block_roots,anchor.networks,anchor.root,anchor.status))exceptEnchainteSDKException:raiseGet and validate messages proofThis example shows how to get a proof for an array of messages and validate it:fromenchaintesdkimportEnchainteClient,Message,EnchainteSDKException,ConfigEnvimportosapiKey=os.getenv("ENCHAINTE_APIKEY",default='apiKey')client=EnchainteClient(apiKey,environment=ConfigEnv.TEST)messages=[Message.fromString('Example Data 1'),Message.fromString('Example Data 2'),Message.fromString('Example Data 3')]try:proof=client.getProof(messages)timestamp=client.verifyProof(proof)# or simply: timestamp = client.verifyMessages(messages)print('When were our messages sent to blockchain? :{}'.format(is_valid_boolean))exceptEnchainteSDKException:raiseFull exampleThis snippet shows a complete data cycle including: write, message status polling and proof retrieval and validation.#!/usr/bin/env python3fromenchaintesdkimportEnchainteClient,Message,EnchainteSDKExceptionimportrandomimporttimeimportosdefrandHex(l):''' Helper function to generate a random message.'''val=[int(random.uniform(0,256))forxinrange(0,l)]result=''forninval:result+=('%x'%n)returnresultdefmain():apiKey=os.getenv("API_KEY",default='apiKey')client=EnchainteClient(apiKey)try:messages=[Message.fromString(randHex(64))]send_receipt=client.sendMessages(messages)print('Message sent to Enchainté. Waiting for anchor to be processed ...')client.waitAnchor(send_receipt[0].anchor)print('Anchor retrieved. Getting Message proof ...')proof=client.getProof(messages)print('Verifying proof ...')timestamp=client.verifyProof(proof)iftimestamp<=0:print('Data not registered on the blockchain.')print('Success!')exceptEnchainteSDKExceptionase:print(e)if__name__=="__main__":# execute only if run as a scriptmain()