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 1 commit
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
Prev Previous commit
Next Next commit
update readme
  • Loading branch information
hh committed Jan 8, 2023
commit 5e87c0d4412a32a8b663dadce95afaec4bdb5ecc
101 changes: 49 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ There are three ways to generate this file (named as `<contract_name>.json`):
# 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 @@ -77,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 @@ -98,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(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 @@ -199,13 +192,12 @@ Access Library by **SDK** :

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 @@ -222,7 +214,7 @@ 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 @@ -248,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 @@ -267,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 @@ -293,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 @@ -323,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 @@ -376,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 src/scryptTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type HashedSet = Flavor<Set<SupportedParamType>, 'HashedSet'>;
export type HashedMap = Flavor<Map<SupportedParamType, SupportedParamType>, 'HashedMap'>;


export function Int(n: number | bigint): Int {
export function Int(n: number | bigint | string): Int {
return BigInt(n);
}

Expand Down