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, ConfigEnv
import os

api_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)
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, ConfigEnv
import os

apiKey = 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)
    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, ConfigEnv
import os

api_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))
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, ConfigEnv
import os

apiKey = 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))
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.3.tar.gz (24.6 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.3-py3-none-any.whl (40.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for enchaintesdk-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1985a35bfdfe79adb39298b8fc4b56af319803e63f20b53ac079d9bd1587a62e
MD5 f5dd402fb67934da838f21573ec7c04a
BLAKE2b-256 f982e3ccb06bf9aa3c2f2e9df96fcca10ce92c76cc7a4b714d5af8cd89bd3432

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for enchaintesdk-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2bcff2d2930e2f03dafc67ccdd4c55dcf3172d873f029085b0b7fb8956548c50
MD5 b20c71eb570eaccedac8a4d40ab01725
BLAKE2b-256 f44fc8b2497ab5d2fd07e28db2ef920fce46f3b7914fb22c5570c671f13bc3c7

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