Skip to main content

Python wrapper around the BUTXO Equity compiler for Bytom protocol.

Project description

PY-EQUITY

Build Status PyPI License PyPI Version Coverage Status

Python wrapper around the BUTXO Equity compiler for Bytom protocol.

Installation

$ pip install py-equity

Development

We welcome pull requests. To get started, just fork this repo, clone it locally, and run:

$ pip install -e . -r requirements.txt

Quick Usage

from equity import Equity

LOCK_WITH_PUBLIC_KEY_SOURCE = """
    contract LockWithPublicKey(publicKey: PublicKey) locks valueAmount of valueAsset {
      clause spend(sig: Signature) {
        verify checkTxSig(publicKey, sig)
        unlock valueAmount of valueAsset
      }
    }
"""

LOCK_WITH_PUBLIC_KEY_PATH = "./LockWithPublicKey.equity"

# LOCK_WITH_PUBLIC_KEY_ARGS = [
#     "e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78"
# ]

equity = Equity("http://localhost:9888")

COMPILED = equity.compile_source(LOCK_WITH_PUBLIC_KEY_SOURCE,
                                 "e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78")

print(COMPILED["name"])
print(COMPILED["program"])
print(COMPILED["opcodes"])

print(COMPILED)

# Save compiled contract
equity.save()

OUTPUT

'LockWithPublicKey'
'20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0'
'0xe9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78 DEPTH 0xae7cac FALSE CHECKPREDICATE'

{'name': 'LockWithPublicKey', 'source': '\n    contract LockWithPublicKey(publicKey: PublicKey) locks valueAmount of valueAsset {\n      clause spend(sig: Signature) {\n        verify checkTxSig(publicKey, sig)\n        unlock valueAmount of valueAsset\n      }\n    }\n', 'program': '20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0', 'params': [{'name': 'publicKey', 'type': 'PublicKey'}], 'value': 'valueAmount of valueAsset', 'clause_info': [{'name': 'spend', 'params': [{'name': 'sig', 'type': 'Signature'}], 'values': [{'name': '', 'asset': 'valueAsset', 'amount': 'valueAmount'}]}], 'opcodes': '0xe9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78 DEPTH 0xae7cac FALSE CHECKPREDICATE', 'error': ''}

CLI

Command line interface, run the following command:

$ eqt --version

Example eqt:

$ eqt --file LockWithPublicKey.equity --args "e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78"

Get help:

$ eqt --help

From there, you can run eqt more commands, -s\--save to save your contract, -u\--url to change Bytom API url. by default (http://localhost:9888).

Testing

You can run the tests with:

$ pytest tests

Or use tox to run the complete suite against the full set of build targets, or pytest to run specific tests against a specific version of Python.

API

Class Equity()

Parameters

Optional:

  • String - url, Bytom API url, by default (http://localhost:9888).
  • String - api_key, Bytom API key.
  • Integer - timeout, request timeout, by default (1).

compile_source(): It is to compiling Bytom smart contract(Equity source).

Parameters

Object:

  • String - equity_source, Equity source code.

Optional:

  • Boolean/String/Integer/Array - *argv.
    • Boolean - boolean, boolean parameter.
    • String - string, string parameter.
    • Integer - integer, integer parameter.

Returns

Object:

  • String - name, contract name.
  • String - source, contract source code.
  • String - program, contract program.
  • Array - params, contract params.
  • String - value, contract value.
  • Array - clause_info, contract clause_info.
  • Array - values, contract values.
  • String - opcodes, contract opcodes.
  • String - error, contract error.

Example

LOCK_WITH_PUBLIC_KEY_SOURCE = """
    contract LockWithPublicKey(publicKey: PublicKey) locks valueAmount of valueAsset {
      clause spend(sig: Signature) {
        verify checkTxSig(publicKey, sig)
        unlock valueAmount of valueAsset
      }
    }
"""
equity = Equity("http://localhost:9888", timeout=5)
COMPILED = equity.compile_source(LOCK_WITH_PUBLIC_KEY_SOURCE,
                                 "e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78")

print(COMPILED)
Output
{'name': 'LockWithPublicKey', 'source': '\n    contract LockWithPublicKey(publicKey: PublicKey) locks valueAmount of valueAsset {\n      clause spend(sig: Signature) {\n        verify checkTxSig(publicKey, sig)\n        unlock valueAmount of valueAsset\n      }\n    }\n', 'program': '20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0', 'params': [{'name': 'publicKey', 'type': 'PublicKey'}], 'value': 'valueAmount of valueAsset', 'clause_info': [{'name': 'spend', 'params': [{'name': 'sig', 'type': 'Signature'}], 'values': [{'name': '', 'asset': 'valueAsset', 'amount': 'valueAmount'}]}], 'opcodes': '0xe9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78 DEPTH 0xae7cac FALSE CHECKPREDICATE', 'error': ''}

compile_file(): It is to compiling Bytom smart contract form Equity file.

Parameters

Object:

  • String - equity_source, Equity source code file(.equity or .eqt).

Optional:

  • Boolean/String/Integer/Array - *argv.
    • Boolean - boolean, boolean parameter.
    • String - string, string parameter.
    • Integer - integer, integer parameter.

Returns

Object:

  • String - name, contract name.
  • String - source, contract source code.
  • String - program, contract program.
  • Array - params, contract params.
  • String - value, contract value.
  • Array - clause_info, contract clause_info.
  • Array - values, contract values.
  • String - opcodes, contract opcodes.
  • String - error, contract error.

Example

LOCK_WITH_PUBLIC_KEY_PATH = "./LockWithPublicKey.equity"
equity = Equity("http://localhost:9888", timeout=5)
COMPILED = equity.compile_file(LOCK_WITH_PUBLIC_KEY_PATH,
                               "e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78")

print(COMPILED)
Output
{'name': 'LockWithPublicKey', 'source': '\n    contract LockWithPublicKey(publicKey: PublicKey) locks valueAmount of valueAsset {\n      clause spend(sig: Signature) {\n        verify checkTxSig(publicKey, sig)\n        unlock valueAmount of valueAsset\n      }\n    }\n', 'program': '20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0', 'params': [{'name': 'publicKey', 'type': 'PublicKey'}], 'value': 'valueAmount of valueAsset', 'clause_info': [{'name': 'spend', 'params': [{'name': 'sig', 'type': 'Signature'}], 'values': [{'name': '', 'asset': 'valueAsset', 'amount': 'valueAmount'}]}], 'opcodes': '0xe9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78 DEPTH 0xae7cac FALSE CHECKPREDICATE', 'error': ''}

save(): Save compiled equity source.

Parameters

Optional:

  • String - file_path, It is for full path with name.
  • String - dir_path, It is for only dir path.

Returns

Object:

  • String - name, contract name.

AUTHOR

MEHERET TESFAYE

LICENSE

AGPLv3+

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

py-equity-0.1.0.tar.gz (19.8 kB view hashes)

Uploaded Source

Built Distribution

py_equity-0.1.0-py3-none-any.whl (21.0 kB view hashes)

Uploaded Python 3

Supported by

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