Skip to content

Commit 8f1522d

Browse files
committed
Update docs for P2S and other small changes
1 parent 59e0b32 commit 8f1522d

9 files changed

Lines changed: 146 additions & 45 deletions

File tree

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ npm install -g cashc
2727
### Usage
2828

2929
```bash
30-
Usage: cashc [options] [source_file]
30+
Usage: cashc [options] <source_file>
31+
32+
Arguments:
33+
source_file The source file to compile.
3134

3235
Options:
33-
-V, --version Output the version number.
34-
-o, --output <path> Specify a file to output the generated artifact.
35-
-h, --hex Compile the contract to hex format rather than a full artifact.
36-
-A, --asm Compile the contract to ASM format rather than a full artifact.
37-
-c, --opcount Display the number of opcodes in the compiled bytecode.
38-
-s, --size Display the size in bytes of the compiled bytecode.
39-
-f, --format <format> Specify the format of the output. (choices: "json", "ts", default: "json")
40-
-?, --help Display help
36+
-V, --version Output the version number.
37+
-o, --output <path> Specify a file to output the generated artifact.
38+
-h, --hex Compile the contract to hex format rather than a full artifact.
39+
-A, --asm Compile the contract to ASM format rather than a full artifact.
40+
-c, --opcount Display the number of opcodes in the compiled bytecode.
41+
-s, --size Display the size in bytes of the compiled bytecode.
42+
-S, --skip-enforce-function-parameter-types Do not enforce function parameter types.
43+
-f, --format <format> Specify the format of the output. (choices: "json", "ts", default:
44+
"json")
45+
-?, --help Display help
4146
```
4247

4348
## The CashScript SDK

packages/cashc/README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ npm install -g cashc
2323

2424
### Usage
2525
```bash
26-
Usage: cashc [options] [source_file]
26+
Usage: cashc [options] <source_file>
27+
28+
Arguments:
29+
source_file The source file to compile.
2730

2831
Options:
29-
-V, --version Output the version number.
30-
-o, --output <path> Specify a file to output the generated artifact.
31-
-h, --hex Compile the contract to hex format rather than a full artifact.
32-
-A, --asm Compile the contract to ASM format rather than a full artifact.
33-
-c, --opcount Display the number of opcodes in the compiled bytecode.
34-
-s, --size Display the size in bytes of the compiled bytecode.
35-
-f, --format <format> Specify the format of the output. (choices: "json", "ts", default: "json")
36-
-?, --help Display help
32+
-V, --version Output the version number.
33+
-o, --output <path> Specify a file to output the generated artifact.
34+
-h, --hex Compile the contract to hex format rather than a full artifact.
35+
-A, --asm Compile the contract to ASM format rather than a full artifact.
36+
-c, --opcount Display the number of opcodes in the compiled bytecode.
37+
-s, --size Display the size in bytes of the compiled bytecode.
38+
-S, --skip-enforce-function-parameter-types Do not enforce function parameter types.
39+
-f, --format <format> Specify the format of the output. (choices: "json", "ts", default:
40+
"json")
41+
-?, --help Display help
3742
```

website/docs/compiler/artifacts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface Artifact {
1919
compiler: {
2020
name: string // Compiler used to compile this contract
2121
version: string // Compiler version used to compile this contract
22+
options?: CompilerOptions // Compiler options used to compile this contract
2223
}
2324
debug?: {
2425
bytecode: string // unlike `bytecode` property above, this is a hex-encoded binary string
@@ -57,4 +58,8 @@ interface RequireStatement {
5758
line: number; // line in the source code
5859
message: string; // custom message for failing `require` statement
5960
}
61+
62+
interface CompilerOptions {
63+
enforceFunctionParameterTypes?: boolean; // Enforce function parameter types (default: true)
64+
}
6065
```

website/docs/compiler/compiler.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,22 @@ npm install -g cashc
2424
The `cashc` CLI tool can be used to compile `.cash` files to JSON (or `.ts`) artifact files.
2525

2626
```bash
27-
Usage: cashc [options] [source_file]
27+
Usage: cashc [options] <source_file>
28+
29+
Arguments:
30+
source_file The source file to compile.
2831

2932
Options:
30-
-V, --version Output the version number.
31-
-o, --output <path> Specify a file to output the generated artifact.
32-
-h, --hex Compile the contract to hex format rather than a full artifact.
33-
-A, --asm Compile the contract to ASM format rather than a full artifact.
34-
-c, --opcount Display the number of opcodes in the compiled bytecode.
35-
-s, --size Display the size in bytes of the compiled bytecode.
36-
-f, --format <format> Specify the format of the output. (choices: "json", "ts", default: "json")
37-
-?, --help Display help
33+
-V, --version Output the version number.
34+
-o, --output <path> Specify a file to output the generated artifact.
35+
-h, --hex Compile the contract to hex format rather than a full artifact.
36+
-A, --asm Compile the contract to ASM format rather than a full artifact.
37+
-c, --opcount Display the number of opcodes in the compiled bytecode.
38+
-s, --size Display the size in bytes of the compiled bytecode.
39+
-S, --skip-enforce-function-parameter-types Do not enforce function parameter types.
40+
-f, --format <format> Specify the format of the output. (choices: "json", "ts", default:
41+
"json")
42+
-?, --help Display help
3843
```
3944

4045
:::tip
@@ -63,7 +68,7 @@ npm install cashc
6368

6469
### compileFile()
6570
```ts
66-
compileFile(sourceFile: PathLike): Artifact
71+
compileFile(sourceFile: PathLike, compilerOptions?: CompilerOptions): Artifact
6772
```
6873

6974
Compiles a CashScript contract from a source file. This compile method is handy when using Node.js with the contract source file available but you are doing quick compilations (for example for contract size comparisons) and you don't need the contract artifact file to be generated.
@@ -79,7 +84,7 @@ const P2PKH = compileFile(new URL('p2pkh.cash', import.meta.url));
7984

8085
### compileString()
8186
```ts
82-
compileString(sourceCode: string): Artifact
87+
compileString(sourceCode: string, compilerOptions?: CompilerOptions): Artifact
8388
```
8489

8590
Compiles a CashScript contract from a source code string. This compile method is handy in a browser compilation setting like the [CashScript Playground](https://playground.cashscript.org/) where testing contracts can be quickly compiled and discarded. The method is also useful if no source file is locally available (e.g. the source code is retrieved with a REST API).
@@ -91,3 +96,16 @@ const source = await result.text();
9196

9297
const P2PKH = compileString(source);
9398
```
99+
100+
### Compiler Options
101+
```ts
102+
interface CompilerOptions {
103+
enforceFunctionParameterTypes?: boolean;
104+
}
105+
```
106+
107+
The `enforceFunctionParameterTypes` option is used to enforce function parameter types, such as byte length of `bytes20` or `bytes32` types and `bool` values. By default, it is set to `true`.
108+
109+
If set to `false`, the compiler will not enforce function parameter types. This means that it is possible for `bytes20` values to have a different length at runtime than the expected 20 bytes. Or that `bool` values are not actually booleans, but integers.
110+
111+
This option is useful if you are certain that passing in incorrect function parameter types will not cause runtime vulnerabilities, and you want to save on the extra opcodes that are added to the script to enforce the types.

website/docs/compiler/grammar.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ expressionList
121121
122122
expression
123123
: '(' expression ')' # Parenthesised
124-
| typeName '(' castable=expression (',' size=expression)? ','? ')' # Cast
124+
| typeCast '(' castable=expression ','? ')' # Cast
125125
| functionCall # FunctionCallExpression
126126
| 'new' Identifier expressionList #Instantiation
127127
| expression '[' index=NumberLiteral ']' # TupleIndexOp
@@ -164,7 +164,15 @@ numberLiteral
164164
;
165165
166166
typeName
167-
: 'int' | 'bool' | 'string' | 'pubkey' | 'sig' | 'datasig' | Bytes
167+
: PrimitiveType
168+
| BoundedBytes
169+
| UnboundedBytes
170+
;
171+
172+
typeCast
173+
: PrimitiveType
174+
| UnboundedBytes
175+
| UnsafeCast
168176
;
169177
170178
VersionLiteral
@@ -192,8 +200,21 @@ ExponentPart
192200
: [eE] NumberPart
193201
;
194202
195-
Bytes
196-
: 'bytes' Bound? | 'byte'
203+
PrimitiveType
204+
: 'int'
205+
| 'bool'
206+
| 'string'
207+
| 'pubkey'
208+
| 'sig'
209+
| 'datasig'
210+
;
211+
212+
UnboundedBytes
213+
: 'bytes'
214+
;
215+
216+
BoundedBytes
217+
: 'bytes' Bound | 'byte'
197218
;
198219
199220
Bound
@@ -218,6 +239,13 @@ TxVar
218239
| 'tx.time'
219240
;
220241
242+
UnsafeCast
243+
: 'unsafe_int'
244+
| 'unsafe_bool'
245+
| 'unsafe_bytes' Bound?
246+
| 'unsafe_byte'
247+
;
248+
221249
NullaryOp
222250
: 'this.activeInputIndex'
223251
| 'this.activeBytecode'

website/docs/sdk/electrum-network-provider.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const provider = new ElectrumNetworkProvider('chipnet', { hostname });
5252

5353
## ElectrumNetworkProvider Methods
5454

55-
5655
### getUtxos()
5756
```ts
5857
async provider.getUtxos(address: string): Promise<Utxo[]>;
@@ -81,6 +80,16 @@ interface TokenDetails {
8180
```ts
8281
const userUtxos = await provider.getUtxos(userAddress)
8382
```
83+
### getUtxosForLockingBytecode()
84+
```ts
85+
async provider.getUtxosForLockingBytecode(lockingBytecode: Uint8Array | string): Promise<Utxo[]>;
86+
```
87+
Returns all UTXOs for a specific locking bytecode. Both confirmed and unconfirmed UTXOs are included.
88+
89+
#### Example
90+
```ts
91+
const utxos = await provider.getUtxosForLockingBytecode(lockingBytecode)
92+
```
8493

8594
### getBlockHeight()
8695
```ts

website/docs/sdk/instantiation.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ new Contract(
1414
constructorArgs: ConstructorArgument[],
1515
options : {
1616
provider: NetworkProvider,
17-
addressType?: 'p2sh20' | 'p2sh32',
17+
contractType?: 'p2sh20' | 'p2sh32' | 'p2s',
1818
}
1919
)
2020
```
2121

22-
A CashScript contract can be instantiated by providing an `Artifact` object, a list of constructor arguments, and optionally an options object configuring `NetworkProvider` and `addressType`.
22+
A CashScript contract can be instantiated by providing an `Artifact` object, a list of constructor arguments, and optionally an options object configuring `NetworkProvider` and `contractType`.
2323

24-
An `Artifact` object is the result of compiling a CashScript contract. Compilation can be done using the standalone [`cashc` CLI](/docs/compiler) or programmatically with the `cashc` NPM package (see [CashScript Compiler](/docs/compiler#javascript-compilation)).
24+
An `Artifact` object is the result of compiling a CashScript contract. Compilation can be done using the standalone [`cashc` CLI](/docs/compiler) or programmatically with the `cashc` NPM package (see [CashScript Compiler](/docs/compiler#javascript-compilation)).
2525

2626
:::tip
2727
If compilation is done using the `cashc` CLI with the <span style={{ display: 'inline-block' }}>`--format ts`</span> option to output TypeScript Artifacts, you will get explicit types and type checking for the constructor arguments and function arguments of the `Contract` class.
2828
:::
2929

3030
The `NetworkProvider` option is used to manage network operations for the CashScript contract. By default, a mainnet `ElectrumNetworkProvider` is used, but the network providers can be configured. See the docs on [NetworkProvider](/docs/sdk/network-provider).
3131

32-
The `addressType` option is used to choose between a `p2sh20` and `p2sh32` address type for the CashScript contract. By default `p2sh32` is used because it has increased cryptographic security — but it is not yet supported by all wallets.
32+
The `contractType` option is used to choose between a `p2sh20`, `p2sh32` or `p2s` contract type for the CashScript contract. By default `p2sh32` is used because it has increased cryptographic security — but it is not yet supported by all wallets.
3333

3434
:::caution
3535
p2sh32 was introduced because p2sh20 is cryptographically insecure for a large subset of smart contracts. For contracts holding large sums of BCH this provides an incentive to find a hash collision and hack the contract.
@@ -49,7 +49,7 @@ const P2PKH = compileFile(new URL('p2pkh.cash', import.meta.url));
4949
const contractArguments = [alicePkh]
5050

5151
const provider = new ElectrumNetworkProvider('chipnet');
52-
const options = { provider, addressType: 'p2sh20' }
52+
const options = { provider, contractType: 'p2sh20' }
5353
const contract = new Contract(P2PKH, contractArguments, options);
5454
```
5555

@@ -66,6 +66,10 @@ A contract's regular address (without token-support) can be retrieved through th
6666
Wallets will not allow you to send CashTokens to this address. For that you must use the [tokenAddress](#tokenaddress) below. Wallets which have not upgraded might not recognize this new address type.
6767
:::
6868

69+
:::info
70+
If you are using a `p2s` contract, the `address` member field does not exist on the contract object.
71+
:::
72+
6973
#### Example
7074
```ts
7175
console.log(contract.address)
@@ -78,17 +82,33 @@ contract.tokenAddress: string
7882

7983
A contract's token-supporting address can be retrieved through the `tokenAddress` member field.
8084

85+
:::info
86+
If you are using a `p2s` contract, the `tokenAddress` member field does not exist on the contract object.
87+
:::
88+
8189
#### Example
8290
```ts
8391
console.log(contract.tokenAddress)
8492
```
8593

94+
### lockingBytecode
95+
```ts
96+
contract.lockingBytecode: string
97+
```
98+
99+
Returns the contract's locking bytecode encoded as a hex string. This exists for all contract types, including `p2s`.
100+
101+
#### Example
102+
```ts
103+
console.log(contract.lockingBytecode)
104+
```
105+
86106
### bytecode
87107
```ts
88108
contract.bytecode: string
89109
```
90110

91-
Returns the contract's redeem script encoded as a hex string.
111+
Returns the contract's bytecode encoded as a hex string.
92112

93113
#### Example
94114
```ts
@@ -100,17 +120,17 @@ console.log(contract.bytecode)
100120
contract.bytesize: number
101121
```
102122

103-
The size of the contract's bytecode in bytes can be retrieved through the `bytesize` member field. This is useful to ensure that the contract is not too big, since Bitcoin Cash smart contracts can be 1,650 bytes at most.
123+
The size of the contract's bytecode in bytes can be retrieved through the `bytesize` member field. This is useful to ensure that the contract is not too big, since Bitcoin Cash smart contracts can be 10,000 bytes at most for P2SH contracts and 201 bytes for P2S contracts. See the [Script & Transaction Limits](/docs/compiler/script-limits) page for more information.
104124

105-
:::tip
125+
:::info
106126
Using `contract.bytesize` is the best way to get the size of contract bytecode, as it includes the constructor arguments.
107127
The size outputs of the `cashc` compiler are based on the bytecode without constructor arguments so are always an underestimate.
108128
:::
109129

110130
#### Example
111131
```ts
112132
// make sure the contract bytesize is within standardness limits
113-
assert(contract.bytesize <= 1650)
133+
assert(contract.bytesize <= 10_000)
114134
```
115135

116136
### opcount

website/docs/sdk/network-provider.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ interface TokenDetails {
5151
const userUtxos = await provider.getUtxos(userAddress)
5252
```
5353

54+
### getUtxosForLockingBytecode()
55+
```ts
56+
async provider.getUtxosForLockingBytecode(lockingBytecode: Uint8Array | string): Promise<Utxo[]>;
57+
```
58+
Returns all UTXOs for a specific locking bytecode. Both confirmed and unconfirmed UTXOs are included.
59+
60+
#### Example
61+
```ts
62+
const utxos = await provider.getUtxosForLockingBytecode(lockingBytecode)
63+
```
64+
5465
### getBlockHeight()
5566
```ts
5667
async provider.getBlockHeight(): Promise<number>;

website/docs/sdk/other-network-providers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ new FullStackNetworkProvider(network: Network, bchjs: BCHJS)
4747
The `FullStackNetworkProvider` uses [FullStack.cash][fullstack]' infrastructure to connect to the BCH network. FullStack.cash' offers dedicated infrastructure and support plans for larger projects. Both `network` and `bchjs` parameters are mandatory, where `bchjs` is an instance of FullStack.cash' [BCHJS][bchjs].
4848

4949
:::caution
50-
The `FullStackNetworkProvider` does not currently support CashTokens. If you want to use CashTokens, please use the `ElectrumNetworkProvider` instead.
50+
The `FullStackNetworkProvider` does not currently support CashTokens or the `getUtxosForLockingBytecode()` method. If you want to use these features, please use the `ElectrumNetworkProvider` instead.
5151
:::
5252

5353
#### Example
@@ -72,7 +72,7 @@ new BitcoinRpcNetworkProvider(network: Network, url: string, options?: any)
7272
The `BitcoinRpcNetworkProvider` uses a direct connection to a BCH node. Note that a regular node does not have indexing, so any address of interest (e.g. the contract address) need to be registered by the node *before* sending any funds to those addresses. Because of this it is recommended to use a different network provider unless you have a specific reason to use the RPC provider.
7373

7474
:::caution
75-
The `BitcoinRpcNetworkProvider` does not currently support CashTokens. If you want to use CashTokens, use the `ElectrumNetworkProvider` instead.
75+
The `BitcoinRpcNetworkProvider` does not currently support CashTokens or the `getUtxosForLockingBytecode()` method. If you want to use these features, please use the `ElectrumNetworkProvider` instead.
7676
:::
7777

7878
#### Example

0 commit comments

Comments
 (0)