{"id":18246697,"url":"https://github.com/scrypt-inc/py-scryptlib","last_synced_at":"2025-04-04T14:31:20.008Z","repository":{"id":57464893,"uuid":"358246316","full_name":"sCrypt-Inc/py-scryptlib","owner":"sCrypt-Inc","description":"sCrypt SDK for Python","archived":false,"fork":false,"pushed_at":"2022-06-10T11:37:33.000Z","size":475,"stargazers_count":20,"open_issues_count":3,"forks_count":7,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-04T05:40:14.661Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sCrypt-Inc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-15T12:13:00.000Z","updated_at":"2024-06-16T17:57:39.000Z","dependencies_parsed_at":"2022-08-31T03:11:53.553Z","dependency_job_id":null,"html_url":"https://github.com/sCrypt-Inc/py-scryptlib","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sCrypt-Inc%2Fpy-scryptlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sCrypt-Inc%2Fpy-scryptlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sCrypt-Inc%2Fpy-scryptlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sCrypt-Inc%2Fpy-scryptlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sCrypt-Inc","download_url":"https://codeload.github.com/sCrypt-Inc/py-scryptlib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247194000,"owners_count":20899409,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T09:27:20.394Z","updated_at":"2025-04-04T14:31:19.987Z","avatar_url":"https://github.com/sCrypt-Inc.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# scryptlib-python\nA Python SDK for [sCrypt](https://scrypt.io/).\n\n[![Build Status](https://app.travis-ci.com/sCrypt-Inc/py-scryptlib.svg?branch=main)](https://travis-ci.com/sCrypt-Inc/py-scryptlib)\n\nYou can learn all about writing sCrypt smart contracts in the official [docs](https://scryptdoc.readthedocs.io/en/latest/intro.html).\n\n## Installation\n\n### Compiler\n\nTo use the SDK, you need to get a copy of the sCrypt compiler. You can get it either by downloading the [sCrypt IDE](https://scrypt.io/#download) or executing the following command, if you have an UNIX-like OS:\n```sh\ncurl -Ls https://scrypt.io/setup | sh -s --\n```\n\nThis will download the latest version of the compiler.\n\nYou can also download a specific version of the compiler using the `-v` flag:\n```sh\ncurl -Ls https://scrypt.io/setup | sh -s -- -v 1.15.1\n```\n\n### SDK\n\nYou can install the SDK as a Python package using `pip`, either directly from the project folder:\n\n```sh\npip install .\n```\n\n, or from the PyPI:\n\n```sh\npip install scryptlib\n```\n\n## Usage\n\nThe SDK is used to convert script templates, produced by the sCrypt compiler, to an object-based representation in Python. It allows for easy compilation, inspection and verification of smart contracts.\n\n### Compiling an sCrypt contract\n\nWe can compile an sCrypt conract source file like so:\n\n```python\nimport scryptlib\n\ncontract = './test/res/arraydemo.scrypt'\ncompiler_result = scryptlib.compile_contract(contract)\n```\n\nThis will leave us with a `CompilerResult` object, that contains all of the data, returned by the compiler.\nThe `compile_contract` method will try to automatically search for the compiler binary. You can also explicitly pass the path to the binary, using the `compiler_bin` parameter.\n\nIt is also possible to pass the sCrypt source code as a string object:\n\n```python\ncontract_source = '''\n    contract Equals {\n        int x;\n\n        constructor(int x) {\n            this.x = x;\n        }\n\n        public function equals(int y) {\n            require(this.x == y);\n        }\n\n    }\n'''\n\ncompiler_result = scryptlib.compile_contract(contract_source, from_string=True)\n```\n\nThe resulting **contract description file** will be written to `./out` by default. That may also be changed with the `out_dir` parameter.\nYou can access the contract description directly from the `CompilerResult` with its `to_desc()` method.\n\n### Evaluating a contract locally\n\nWe can evaluate any public function of the contract locally on our machine, before broadcasting it.\n\nFirst we need to create a class representation of the contract and instantiate it:\n\n```python\nimport scryptlib\nfrom scryptlib.types import Int\n\nEQUAL_VAL = 2021\n\ncontract = './test/res/equals.scrypt'\n\ncompiler_result = scryptlib.compile_contract(contract)\ndesc = compiler_result.to_desc()\n\nEquals = scryptlib.build_contract_class(desc)\ncontract_obj = Equals(Int(EQUAL_VAL))\n```\n\nAs we can see, the created class takes the contract parameters in the constructor. In the case of the `Equals` contract, that is an sCrypt `int` type, which we can represent in Python using an instance of `scryptlib.types.Int`.\n\nOnce we have an instance of the contract class, we can evaluate its public functions:\n\n```python\nverify_result = contract_obj.equals(Int(EQUAL_VAL)).verify()\nassert verify_result == True\n```\n\nFrom the example see, that we called the contracts public function, named `equals`. The actual call to `equals()` in Python reutrns an instance of `scryptlib.abi.FunctionCall`. That object in turn has a method, named `verify`, with which we can run the function calls unlocking script against the contracts locking script.\n`verify` can internaly create an input evaluation context for simple contracts, but once we start using more advanced constructs, like signatures, we can pass an instance of `bitconx.TxInputContext`, using the `tx_input_context` parameter.\n\npy-scryptlib leverages the [bitcoinx](https://github.com/kyuupichan/bitcoinX) library to deal with Bitcoin primitives.\n\nThe following is an example of a local evaluation of a P2PKH contract:\n\n```python\nimport pytest\nimport scryptlib\nfrom scryptlib.types import Sig, PubKey, Ripemd160\n\nimport bitcoinx\nfrom bitcoinx import SigHash, PrivateKey, pack_byte\n\n\nkey_priv = PrivateKey.from_arbitrary_bytes(b'test123')\nkey_pub = key_priv.public_key\npubkey_hash = key_pub.hash160()\n\ncontract = './test/res/p2pkh.scrypt'\n\ncompiler_result = scryptlib.compile_contract(contract)\ndesc = compiler_result.to_desc()\n\nP2PKH = scryptlib.build_contract_class(desc)\np2pkh_obj = P2PKH(Ripemd160(pubkey_hash))\n\ncontext = scryptlib.create_dummy_input_context()\n\nsighash_flag = SigHash(SigHash.ALL | SigHash.FORKID)\ninput_idx = 0\nutxo_satoshis = context.utxo.value\nsighash = context.tx.signature_hash(input_idx, utxo_satoshis, p2pkh_obj.locking_script, sighash_flag)\n\nsig = key_priv.sign(sighash, hasher=None)\nsig = sig + pack_byte(sighash_flag)\n\nverify_result = p2pkh_obj.unlock(Sig(sig), PubKey(key_pub)).verify(context)\nassert verify_result == True\n```\n\nYou can find many other examples under `test/test_contract_*`.\n\n## Testing\n\nThe SDK has a suite of unit tests, which we can run by executing the `pytest` command in the root of the project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscrypt-inc%2Fpy-scryptlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscrypt-inc%2Fpy-scryptlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscrypt-inc%2Fpy-scryptlib/lists"}