Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrypt-ts: remove scryptType class, use type #211

Merged
merged 67 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
4f6227c
add error log
Nov 17, 2022
be8178d
don't clean if output sourceMap
Nov 17, 2022
458214d
add bsv type
Nov 17, 2022
266f872
fix compileAsync
Nov 17, 2022
cadd53a
rename to isGenesis
Nov 18, 2022
720f0dc
pump version to 1.1.0-beta2
Nov 18, 2022
aa68904
add dependencies
Nov 23, 2022
e55a716
remove types/bsv
Dec 3, 2022
dc72584
update setInputScript
Dec 3, 2022
45ccc74
remove log
Dec 3, 2022
50bfd74
1.1.0-beta.4
Dec 3, 2022
21e6b96
update bsv
Dec 3, 2022
ae732c9
1.1.0-beta.5
Dec 3, 2022
3c05705
update bsv.d.ts
Dec 3, 2022
c42455b
1.1.0-beta.6
Dec 3, 2022
d635412
update bsv
Dec 4, 2022
3624991
update package-lock.json
Dec 4, 2022
40bd338
1.1.0-beta.7
Dec 4, 2022
d5c9f08
optimize cli
Dec 5, 2022
89af021
update bsv
Dec 5, 2022
10adf97
1.1.0-beta.8
Dec 5, 2022
ffc81e6
delete no use file
Dec 5, 2022
67663e7
don't exit when no compiler foud
Dec 5, 2022
42458be
1.1.0-beta.9
Dec 5, 2022
5e2b25e
Fix findcompile
Dec 5, 2022
6fab7d5
1.1.0-beta.10
Dec 5, 2022
f8f4244
bind scrypt compile version
Dec 5, 2022
fc3fab7
1.1.0-beta.11
Dec 5, 2022
9bac5f6
update bsv
Dec 5, 2022
dbceec1
rename
Dec 5, 2022
ac1696b
1.1.0-beta.12
Dec 5, 2022
95cb3cc
compile before publish
Dec 5, 2022
9817423
Fix buildtype error
Dec 6, 2022
84a9f3e
update bsv
Dec 6, 2022
502b439
add test
Dec 6, 2022
16b222d
1.1.0-beta.13
Dec 6, 2022
06e23b9
update bsv
Dec 7, 2022
a0c1437
remove FORKID from sighash flag
Dec 7, 2022
a3e87c0
add md5 function
Dec 8, 2022
3a3ad85
1.1.0-beta.14
Dec 8, 2022
60553e5
Fix ci
Dec 8, 2022
f297345
1.1.0-beta.14
Dec 8, 2022
4e6062a
Fix ci
Dec 8, 2022
00c64db
remove ScryptType class, use type
Dec 18, 2022
abbd3e9
2.0.0-beta.1
Dec 18, 2022
2fb2154
exclude findCompiler
Dec 18, 2022
bd89240
don't run boilerplate now
Dec 18, 2022
090de0d
refactor: add deserializer
Dec 19, 2022
16d6723
Fix export
Dec 19, 2022
5ee3cb7
2.0.0-beta.2
Dec 19, 2022
9878177
use param type, not argument type
Dec 20, 2022
58ac86b
2.0.0-beta.3
Dec 20, 2022
ee5d056
desc files are only contractname.json now
msinkec Dec 29, 2022
6d271a4
Renamed all occurences of desc to artifacts.
msinkec Dec 30, 2022
b6da9f8
update findKeyIndex
Jan 4, 2023
e35091a
2.0.0-beta.4
Jan 4, 2023
1be3cc1
Organized imports and some other fixes to make linting pass.
msinkec Jan 5, 2023
636c32b
Merge pull request #215 from msinkec/scrypt-ts
zhfnjust Jan 5, 2023
bb1c9fe
2.0.0-beta.5
Jan 6, 2023
736c9db
auto calc keyIndex
Jan 7, 2023
0a168e5
2.0.0-beta.6
Jan 7, 2023
b7e1d56
rename to getSortedItem
Jan 7, 2023
b64e19c
Fix genLaunchConfigFile
Jan 7, 2023
f2bcaa0
mirror fix
Jan 7, 2023
f83ad19
run bp test
Jan 8, 2023
5e87c0d
update readme
Jan 8, 2023
820ae9d
update change log
Jan 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"src/test/**",
"**/*.d.ts",
"**/*.test.ts",
"src/index.ts"
"src/index.ts",
"src/findCompiler.ts"
],
"sourceMap": true,
"reporter": [
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ script:
- cd boilerplate
- npm install
- npm link scryptlib # use this version
#- npm ls scryptlib # double check the latest version is used
# #- npm ls scryptlib # double check the latest version is used
- npm test
- npx scryptlib contracts/demo.scrypt
- npx scryptlib compile contracts/demo.scrypt
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# CHANGELOG



## 2.0.0

- remove scryptType class, use type
- add bsv typescript defined
- firstCall rename to isGenesis
- auto complete transation sig and sighashpreimage.
- optimize cli
- remove FORKID from sighash flag
- auto calc keyIndex
- Fix genLaunchConfigFile
- rename desc files to contractname.json now

see [PR #211](https://github.com/sCrypt-Inc/scryptlib/pull/211)

*Release Date: 2023/01/08*



## 1.0.9

- Fix big int issue [#206](https://github.com/sCrypt-Inc/scryptlib/issues/206)
Expand Down
121 changes: 60 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ A smart contract is compiled to a locking script template. A contract function c

By using `scryptlib`, both scripts can be obtained with ease.

## Contract Description File
## Contract Artifact File

The compiler output results in a JSON file. It’s a representation used to build locking and unlocking scripts. We call this file a [**contract description file**](docs/counter_debug_desc.json).
The compiler output results in a JSON file. It’s a representation used to build locking and unlocking scripts. We call this file a [**contract artifact file**](docs/counter_debug.json).

There are three ways to generate this file (named as `xxx_desc.json`):
There are three ways to generate this file (named as `<contract_name>.json`):

1. Use [**sCrypt VS Code extension**](https://marketplace.visualstudio.com/items?itemName=bsv-scrypt.sCrypt) to compile manually;
2. Use the function `compile` programmatically:
Expand All @@ -35,7 +35,7 @@ There are three ways to generate this file (named as `xxx_desc.json`):
path: contractFilePath // the file path of the contract
},
{
desc: true // set this flag to be `true` to get the description file output
artifact: true // set this flag to be `true` to get the artifact file output
asm: true // set this flag to be `true` to get the asm file output
optimize: false //set this flag to be `true` to get optimized asm opcode
sourceMap: true //set this flag to be `true` to get source map
Expand Down Expand Up @@ -64,8 +64,10 @@ There are three ways to generate this file (named as `xxx_desc.json`):
```sh
# install compiler binary
npx scryptlib download
# the latest compiler may be incompatible with the current scryptlib
npx scryptlib download latest
# compiling contract
npx scryptlib your_directory/your_scrypt.scrypt
npx scryptlib compile your_directory/your_scrypt.scrypt
```

## Types
Expand All @@ -75,18 +77,18 @@ All basic types of the **sCrypt** language have their corresponding javascript c

| Types (scrypt) | scryptlib (javascript/typescript)|
| :---------: | :--------: |
| `int` | `new Int(1)` or `number` or `bigint` |
| `bool` | `new Bool(true)` or `boolean` |
| `bytes` | `new Bytes('0001')` or `new String("hello world 😊")`|
| `PubKey` | `new PubKey('0001')`|
| `PrivKey` | `new PrivKey(1)`|
| `Sig` | `new Sig('0001')`|
| `Ripemd160` | `new Ripemd160('0001')`|
| `Sha1` | `new Sha1('0001')`|
| `Sha256` | `new Sha256('0001')`|
| `SigHashType` | `new SigHashType('01')`|
| `SigHashPreimage` | `new SigHashPreimage('010001')`|
| `OpCodeType` | `new OpCodeType('76')`|
| `int` | `Int(1)` or `number` or `bigint` |
| `bool` | `Bool(true)` or `boolean` |
| `bytes` | `Bytes('0001')` or `stringToBytes("hello world 😊")`|
| `PubKey` | `PubKey('0001')`|
| `PrivKey` | `PrivKey(1)`|
| `Sig` | `Sig('0001')`|
| `Ripemd160` | `Ripemd160('0001')`|
| `Sha1` | `Sha1('0001')`|
| `Sha256` | `Sha256('0001')`|
| `SigHashType` | `SigHashType('01')`|
| `SigHashPreimage` | `SigHashPreimage('010001')`|
| `OpCodeType` | `OpCodeType('76')`|

### 2. Array Types

Expand All @@ -96,76 +98,69 @@ scryptlib uses javascript array to represent the array types of the **sCrypt** l

[[1, 3, 1]] // represent `int[1][3]` in **sCrypt** language

[new Bytes("00"), new Bytes("00"), new Bytes("00")] // represent `bytes[3]` in **sCrypt** language
[Bytes("00"), Bytes("00"), Bytes("00")] // represent `bytes[3]` in **sCrypt** language

```



### 3. Structure and Type Aliases

Composite types, including structs and type aliases, are dynamically generated by `buildTypeClasses`. When creating a structure, all members must specify values. Use dot to access structure members.
The structure in sCrypt needs to be represented by object in **SDK**. When creating a structure, all members must specify values. Use `.` to access structure members.

A type alias needs to be represented by a value corresponding to the original type

Structure and type aliases defined in sCrypt:

```javascript
```ts
/*Person is structure and Male, Female are type aliases */
struct Person {
bytes addr;
bool isMale;
int age;
}

struct Block {
bytes hash;
bytes header;
int time;
}

type Male = Person;
type Female = Person;

contract Main {
Person person;
int x;

contract PersonContract {
Male man;
Female woman;
...

}

```

Access Structure and type aliases by **SDK** :


```typescript

const PersonContract = buildContractClass(loadDescription('person_desc.json'));
const PersonContract = buildContractClass(loadArtifact('person.json'));

/*Person is structure and Male, Female are type aliases */
const { Person, Male, Female } = buildTypeClasses(PersonContract);

let man = new Person({
let man = {
isMale: true,
age: 14,
addr: new Bytes("68656c6c6f20776f726c6421")
});
age: 14n,
addr: Bytes("68656c6c6f20776f726c6421")
};

man.age = 20;
man.age = 20n;

let woman = new Female({
let woman = {
isMale: false,
age: 18,
addr: new Bytes("68656c6c6f20776f726c6421")
});
age: 18n,
addr: Bytes("68656c6c6f20776f726c6421")
};

woman.addr = new Bytes("")
woman.addr = Bytes("")

const instance = new PersonContract(man, woman);
```


### 4. Library

Library is another composite types. When the constructor parameter of the contract contains library, we need to create library through sdk.
Library is another composite types. When the constructor parameter of the contract contains library, we have to pass an array according to the constructor parameter of the library.

Library defined in sCrypt:

Expand Down Expand Up @@ -195,15 +190,14 @@ Access Library by **SDK** :

```typescript

const Test = buildContractClass(loadDescription('test_desc.json'));
const Test = buildContractClass(loadArtifact('test.json'));

const { L } = buildTypeClasses(Test);

let test = new Test(1, new L(1, 2));
let l = [1n, 2n];

let test = new Test(1n, l);
```

As you can see, creating a library instance is similar to creating a contract instance. Sometimes the constructor parameters of the library may be generic types. At this time, the sdk will deduce the generic type based on the constructor arguments you pass.
Sometimes the constructor parameters of the library may be generic types. At this time, the sdk will deduce the generic type based on the constructor arguments you pass.

## Deploy a Contract and Call Its Function

Expand All @@ -214,13 +208,13 @@ Both **deploying a contract** and **calling a contract function** are achieved b

There are 2 steps.
### 1. Get Locking and Unlocking Script
You can use the description file to build a reflected contract class in Javascript/TypeScript like this:
You can use the artifact file to build a reflected contract class in Javascript/TypeScript like this:
```typescript
const MyContract = buildContractClass(JSON.parse(descFileContent));
const MyContract = buildContractClass(JSON.parse(artifactFileContent));
```
To create an instance of the contract class, for example:
```typescript
const instance = new MyContract(1234, true, ...parameters);
const instance = new MyContract(1234n, true, ...parameters);
```
To get the locking script, use:
```typescript
Expand All @@ -246,9 +240,14 @@ const unlockingScriptHex = unlockingScript.toHex();
A useful method `verify(txContext)` is provided for each contract function call. It would execute the function call with the given context locally. The `txContext` argument provides some context information of the current transaction, **needed only if signature is checked inside the contract**.
```typescript
{
tx?: any; // current transaction represented in bsv.Transaction object
tx?: bsv.Transaction; // current transaction represented in bsv.Transaction object
inputIndex?: number; // input index, default value: 0
/**
* @deprecated no need any more
*/
inputSatoshis?: number; // input amount in satoshis
opReturn?: string; // contract state in ASM format
opReturnHex?: string; // contract state in hex format
}
```
It returns an object:
Expand All @@ -265,7 +264,7 @@ It usually appears in unit tests, like:
const context = { tx, inputIndex, inputSatoshis };

// 1) set context per verify()
const funcCall = instance.someFunc(new Sig('0123456'), new Bytes('aa11ff'), ...parameters);
const funcCall = instance.someFunc(Sig('0123456'), Bytes('aa11ff'), ...parameters);
const result = funcCall.verify(context);
// 2) alternatively, context can be set at instance level and all following verify() will use it
instance.txContext = context;
Expand All @@ -291,7 +290,7 @@ contract Counter {
Use the initial state to instantiate the contract and read the state by accessing the properties of the contract instance.

```typescript
const instance = new Counter(0);
const instance = new Counter(0n);

let state = instance.counter;
// update state
Expand Down Expand Up @@ -321,7 +320,7 @@ You can also access the state of the contract by accessing the properties of the
```typescript

instance.counter++;
instance.person.name = new Bytes('0001');
instance.person.name = Bytes('0001');

```

Expand Down Expand Up @@ -349,7 +348,7 @@ In addition to using a constructor to create a contract, you can also use a raw
```typescript
const axios = require('axios');

const Counter = buildContractClass(loadDesc("counter_debug_desc.json"));
const Counter = buildContractClass(loadArtifact("counter_debug.json"));
let response = await axios.get("https://api.whatsonchain.com/v1/bsv/test/tx/7b9bc5c67c91a3caa4b3212d3a631a4b61e5c660f0369615e6e3a969f6bef4de/hex")
// constructor from raw Transaction.
let counter = Counter.fromTransaction(response.data, 0/** output index**/);
Expand All @@ -374,9 +373,9 @@ import 'core-js/features/array';



let demo = new Demo("11111111111111111111111111111111111", 1);
let demo = new Demo(Int("11111111111111111111111111111111111"), 1n);

let result = demo.add(new Int("11111111111111111111111111111111112")).verify();
let result = demo.add(Int("11111111111111111111111111111111112")).verify();

console.assert(result.success, result.error)
```
2 changes: 1 addition & 1 deletion VERSIONLOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contract Description Version
# Contract Artifact Version


## version = 1
Expand Down
Loading