Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sCrypt-Inc/go-scryptlib

An sCrypt SDK for the Go language
https://github.com/sCrypt-Inc/go-scryptlib

Last synced: about 2 months ago
JSON representation

An sCrypt SDK for the Go language

Awesome Lists containing this project

README

        

[![CI](https://github.com/sCrypt-Inc/go-scryptlib/actions/workflows/main.yml/badge.svg)](https://github.com/sCrypt-Inc/go-scryptlib/actions/workflows/main.yml)

# go-scryptlib
An [sCrypt](https://scrypt.io/) SDK for the Go language.

You can learn all about writing sCrypt smart contracts in the official [docs](https://scryptdoc.readthedocs.io/en/latest/intro.html).

## 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](https://scrypt.io/#download) or executing the following command, if you have an UNIX-like OS:
```sh
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:
```sh
curl -Ls https://scrypt.io/setup | sh -s -- -v 1.3.2
```

### SDK

For installing the SDK itself, run the following command
```sh
go get github.com/sCrypt-Inc/go-scryptlib
```

## Usage

### Compiling an sCrypt contract

To compile an sCrypt contract, we must first initialize a CompilerWrapper:
```go
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:
```go
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:
```go
desc, _ := compilerResult.ToDescWSourceMap()
```

This is the basis, that will be used to create a Contract struct, which represents our compiled contract.
```go
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.
```go
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.
```go
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.
```go
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](https://scrypt-ide.readthedocs.io/en/latest/testing.html#launch-debugger-command) by printing the launch debugger url.

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

## Testing

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