Skip to main content

Enchainté SDK for Python3

Project description

Enchainté SDK - Python

This SDK offers all the features available in the Enchainté Toolset:

  • Write messages
  • Get messages proof
  • Validate proof
  • Get messages details

Installation

The SDK can be installed with PIP as follows:

$ pip install enchaintesdk

Usage

The following examples summarize how to access the different functionalities available:

Prepare data

In order to interact with the SDK data must be processed through the Message module.

There are several ways to generate a Message:

from enchaintesdk import Message

# From a dict
d = {'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 string
Message.fromString('Example Data')

# From a bytes
Message.fromBytes(b'Example Data')

# Retrieve the computed message
Message.fromBytes(b'Example Data').getHash()

Send messages

This example shows how to send data to Enchainté.

from enchaintesdk import EnchainteClient, Message, EnchainteSDKException
import os

api_key = os.getenv("ENCHAINTE_APIKEY", default='api_key')
client = EnchainteClient(api_key)

messages = [Message.fromString('Example Data 1')]

try:
	send_receipt = client.sendMessages(messages)
    print('sendReceipt status: ', send_receipt[0].status)
except EnchainteSDKException:
	raise

Get messages status

This example shows how to get all the details and status of messages:

from enchaintesdk import EnchainteClient, Message, EnchainteSDKException
import os

apiKey = os.getenv("ENCHAINTE_APIKEY", default='apiKey')
client = EnchainteClient(apiKey)

messages = [
    Message.fromString('Example Data 1'),
    Message.fromString('Example Data 2'),
    Message.fromString('Example Data 3')
]

try:
	m_receipts = client.getMessages(messages)
    for mr in m_receipts:
        print("MessageReceipt: {{anchor_id: {}, client:{}, message:{}, status:{}}}".format(
            mr.anchor, mr.client, mr.message, mr.status))
except EnchainteSDKException:
	raise

Wait for messages to process

This example shows how to wait for a message to be processed by Enchainté after sending i:

from enchaintesdk import EnchainteClient, Message, EnchainteSDKException
import os

api_key = os.getenv("ENCHAINTE_APIKEY", default='api_key')
client = EnchainteClient(api_key)
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))
except EnchainteSDKException:
	raise

Get and validate messages proof

This example shows how to get a proof for an array of messages and validate it:

from enchaintesdk import EnchainteClient, Message, EnchainteSDKException
import os

apiKey = os.getenv("ENCHAINTE_APIKEY", default='apiKey')

client = EnchainteClient(apiKey)

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))
except EnchainteSDKException:
	raise

Full example

This snippet shows a complete data cycle including: write, message status polling and proof retrieval and validation.

#!/usr/bin/env python3

from enchaintesdk import EnchainteClient, Message, EnchainteSDKException
import random
import time
import os


def randHex(l):
    ''' Helper function to generate a random message.'''
    val = [int(random.uniform(0, 256)) for x in range(0, l)]
    result = ''
    for n in val:
        result += ('%x' % n)
    return result


def main():
    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)

        if timestamp <= 0:
            print('Data not registered on the blockchain.')

        print('Success!')

    except EnchainteSDKException as e:
        print(e)


if __name__ == "__main__":
    # execute only if run as a script
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

enchaintesdk-0.1.2.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enchaintesdk-0.1.2-py3-none-any.whl (40.4 kB view details)

Uploaded Python 3

File details

Details for the file enchaintesdk-0.1.2.tar.gz.

File metadata

  • Download URL: enchaintesdk-0.1.2.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for enchaintesdk-0.1.2.tar.gz
Algorithm Hash digest
SHA256 abe313f1c68979d9f6e12df167a12b248c3c24f1bfb145cf4bb4ba70b296a9c7
MD5 de74a39c441e146d0e22a188897d92b6
BLAKE2b-256 f0b8748872b2459f7536958b0fcc08558741247e672437bedb151234e054c42c

See more details on using hashes here.

File details

Details for the file enchaintesdk-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: enchaintesdk-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 40.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for enchaintesdk-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 78b386af4095883a63cca5dcaa5b4c49b1d3b5382cd7a439771c4d0ec9ffdee0
MD5 a638ece2c8c2629e0f4649704d66bbef
BLAKE2b-256 c4b311b922630042722898622c18c2fcf0389cca3dfb7cc92bd345cffe9190fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page