Categorygithub.com/sCrypt-Inc/go-scryptlib
modulepackage
0.1.0
Repository: https://github.com/scrypt-inc/go-scryptlib.git
Documentation: pkg.go.dev

# README

go-scryptlib

An sCrypt SDK for the Go language.

You can learn all about writing sCrypt smart contracts in the official docs.

Installation

Compiler

To use the SDK, you need to get a copy of the sCrypt compiler. You can get it either by downloading the sCrypt IDE or executing the following command, if you have an UNIX-like OS:

curl -Ls https://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 -v flag:

curl -Ls https://scrypt.io/setup | sh -s -- -v 1.3.2

SDK

For installing the SDK itself, run the following command

go get github.com/sCrypt-Inc/go-scryptlib

Usage

Compiling an sCrypt contract

To compile an sCrypt contract, we must first initialize a CompilerWrapper:

compilerBin, _ := FindCompiler()

compilerWrapper := CompilerWrapper {
        CompilerBin: compilerBin,
        OutDir: "./out",
        HexOut: true,
        Debug: true,
        Desc: true,
        Stack: true,
        Optimize: false,
        CmdArgs: "",
        Cwd: "./",
    }

Once that is initialized, we can compile the contract:

compilerResult, _ := compilerWrapper.CompileContractFile("./test/res/demo.scrypt")

This leaves us with a struct of type CompilerResult. This step also outputs results of the compiler, and a contract description file in the "./out" directory, which we passed as a parameter to the CompilerWrapper.

From the compiler results we can derive an in-memory representation of the contract description tree:

desc, _ := compilerResult.ToDescWSourceMap()

This is the basis, that will be used to create a Contract struct, which represents our compiled contract.

contractDemo, _ := NewContractFromDesc(desc)

Then we can set the values for the contracts constructor. This is needed to create a valid locking script for out contract.

x := Int{big.NewInt(7)}
y := Int{big.NewInt(4)}
constructorParams := map[string]ScryptType {
    "x": x,
    "y": y,
}

contractDemo.SetConstructorParams(constructorParams)

fmt.Println(contractDemo.GetLockingScript())

The same is true for our contracts public functions. Because our contract can contain many public functions, we use the functions name for referencing.

sumCorrect := Int{big.NewInt(11)}
addParams := map[string]ScryptType {
    "z": sumCorrect,
}

contractDemo.SetPublicFunctionParams("add", addParams)

fmt.Println(contractDemo.GetUnlockingScript("add"))

We can then localy check, if a public function calls successfully evaluates.

success, err := contractDemo.EvaluatePublicFunction("add")

The above method call will use the parameter values, that we set in the previous steps.

Launch sCrypt IDE debugger

After executing the EvaluatePublicFunction function, you can start the debugger with the launch debugger command by printing the launch debugger url.

// calling genLaunchConfig to generate launch debugger url
url := contractDemo.genLaunchConfig()
fmt.Println(url)

Testing

Run go test -v in the root of this project.

Patch

Some tests with @state decorator fail, you need to patch libsv/go-bt manually.

  1. Copy 0001-fix-parser-sCrypt-state-fail to $GOPATH/pkg/mod/github.com/libsv/go-bt/[email protected]
cp patch/0001-fix-parser-sCrypt-state-fail.patch $GOPATH/pkg/mod/github.com/libsv/go-bt/[email protected]
  1. Modify directory permissions
cd $GOPATH/pkg/mod/github.com/libsv/go-bt/[email protected] && chmod -R a+w .
  1. Applying Patch
git apply 0001-fix-parser-sCrypt-state-fail.patch

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Construct a map for resolving alias types from the alias section of the contract description file.
No description provided by the author
No description provided by the author
Drops length prefix of serialized sCrypt type.
No description provided by the author
Factor array declaration string to array type and sizes.
No description provided by the author
No description provided by the author
Turns hierarchical sCrypt type into a single dimensional slice of ScryptType values.
If data is Struct or a list of ScryptTypes, then hash (SHA256) every element of the flattened structure, concat the resulting hashes and hash again into a single hash.
No description provided by the author
Returns true if the passed Array sCrypt types are of the same structure.
Check if string is of an array type.
Check if string is a basic sCrypt type.
No description provided by the author
Returns true if the passed Library sCrypt types are of the same structure.
Returns true if the passed Struct sCrypt types are of the same structure.
No description provided by the author
No description provided by the author
No description provided by the author
Creates a new instance of Contract type.
Creates a new instance of Contract type from the contracts description tree.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
* * * @param type eg.
No description provided by the author
No description provided by the author
Retruns array declaration string for given type name and sizes.
Retruns array declaration string for given type name and sizes.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author