Package: scryptlib
scryptlib-pythonA Python SDK forsCrypt.You can learn all about writing sCrypt smart contracts in the officialdocs.InstallationCompilerTo use the SDK, you need to get a copy of the sCrypt compiler. You can get it either by downloading thesCrypt IDEor executing the following command, if you have an UNIX-like OS:curl-Lshttps://scrypt.io/setup|sh-s--This will download the latest version of the compiler.You can also download a specific version of the compiler using the-vflag:curl-Lshttps://scrypt.io/setup|sh-s---v1.15.1SDKYou can install the SDK as a Python package usingpip, either directly from the project folder:pipinstall., or from the PyPI:pipinstallscryptlibUsageThe 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.Compiling an sCrypt contractWe can compile an sCrypt conract source file like so:importscryptlibcontract='./test/res/arraydemo.scrypt'compiler_result=scryptlib.compile_contract(contract)This will leave us with aCompilerResultobject, that contains all of the data, returned by the compiler.
Thecompile_contractmethod will try to automatically search for the compiler binary. You can also explicitly pass the path to the binary, using thecompiler_binparameter.It is also possible to pass the sCrypt source code as a string object:contract_source='''contract Equals {int x;constructor(int x) {this.x = x;}public function equals(int y) {require(this.x == y);}}'''compiler_result=scryptlib.compile_contract(contract_source,from_string=True)The resultingcontract description filewill be written to./outby default. That may also be changed with theout_dirparameter.
You can access the contract description directly from theCompilerResultwith itsto_desc()method.Evaluating a contract locallyWe can evaluate any public function of the contract locally on our machine, before broadcasting it.First we need to create a class representation of the contract and instantiate it:importscryptlibfromscryptlib.typesimportIntEQUAL_VAL=2021contract='./test/res/equals.scrypt'compiler_result=scryptlib.compile_contract(contract)desc=compiler_result.to_desc()Equals=scryptlib.build_contract_class(desc)contract_obj=Equals(Int(EQUAL_VAL))As we can see, the created class takes the contract parameters in the constructor. In the case of theEqualscontract, that is an sCryptinttype, which we can represent in Python using an instance ofscryptlib.types.Int.Once we have an instance of the contract class, we can evaluate its public functions:verify_result=contract_obj.equals(Int(EQUAL_VAL)).verify()assertverify_result==TrueFrom the example see, that we called the contracts public function, namedequals. The actual call toequals()in Python reutrns an instance ofscryptlib.abi.FunctionCall. That object in turn has a method, namedverify, with which we can run the function calls unlocking script against the contracts locking script.verifycan internaly create an input evaluation context for simple contracts, but once we start using more advanced constructs, like signatures, we can pass an instance ofbitconx.TxInputContext, using thetx_input_contextparameter.py-scryptlib leverages thebitcoinxlibrary to deal with Bitcoin primitives.The following is an example of a local evaluation of a P2PKH contract:importpytestimportscryptlibfromscryptlib.typesimportSig,PubKey,Ripemd160importbitcoinxfrombitcoinximportSigHash,PrivateKey,pack_bytekey_priv=PrivateKey.from_arbitrary_bytes(b'test123')key_pub=key_priv.public_keypubkey_hash=key_pub.hash160()contract='./test/res/p2pkh.scrypt'compiler_result=scryptlib.compile_contract(contract)desc=compiler_result.to_desc()P2PKH=scryptlib.build_contract_class(desc)p2pkh_obj=P2PKH(Ripemd160(pubkey_hash))context=scryptlib.create_dummy_input_context()sighash_flag=SigHash(SigHash.ALL|SigHash.FORKID)input_idx=0utxo_satoshis=context.utxo.valuesighash=context.tx.signature_hash(input_idx,utxo_satoshis,p2pkh_obj.locking_script,sighash_flag)sig=key_priv.sign(sighash,hasher=None)sig=sig+pack_byte(sighash_flag)verify_result=p2pkh_obj.unlock(Sig(sig),PubKey(key_pub)).verify(context)assertverify_result==TrueYou can find many other examples undertest/test_contract_*.TestingThe SDK has a suite of unit tests, which we can run by executing thepytestcommand in the root of the project.