Skip to content

Commit d812d3b

Browse files
authored
Merge pull request #185 from CashScript/next
2 parents 01f9b9b + 59162f0 commit d812d3b

File tree

155 files changed

+12095
-2746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+12095
-2746
lines changed

.eslintrc.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const path = require('path');
33
module.exports = {
44
root: true,
55
parser: '@typescript-eslint/parser',
6-
plugins: ['@typescript-eslint'],
6+
plugins: ['@typescript-eslint', 'import'],
77
extends: ['airbnb-typescript/base'],
88
parserOptions: {
99
project: path.join(__dirname, 'tsconfig.json'),
10-
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
10+
ecmaVersion: 2021, // Allows for the parsing of modern ECMAScript features
1111
sourceType: 'module', // Allows for the use of imports
1212
extraFileExtensions: ['.cjs'],
1313
},
@@ -46,6 +46,7 @@ module.exports = {
4646
'max-classes-per-file': 0, // Multiple classes in one file are allowed (e.g. Errors)
4747
'@typescript-eslint/no-redeclare': 0, // I sometimes name variables an types the same
4848
'linebreak-style': 0, // Ignore linebreak lints https://stackoverflow.com/a/43008668/1129108
49-
'import/extensions': ['error', 'always'], // ESM requires file extensions
49+
'import/extensions': ['error', 'ignorePackages'], // ESM requires file extensions
50+
'@typescript-eslint/only-throw-error': 'error', // We should only throw Error objects
5051
},
5152
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,5 @@ typings/
105105

106106
# DynamoDB Local files
107107
.dynamodb/
108+
109+
manual-test.ts

DEVELOPMENT.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
We use yarn workspaces + lerna for monorepo management. So to get started, clone this repository and run `yarn` in the root directory to install all dependencies for all packages.
2+
3+
When updating code in one package, you can run `yarn build` in the root directory to build all packages so the changes get propagated to the other packages as well. If you're already in a package directory, you can run the following command to do so:
4+
5+
```bash
6+
pushd ../.. && yarn build && popd
7+
```
8+
9+
### Publishing a release
10+
11+
To publish a new release, we use `yarn update-version 'x.x.x'` in the root directory to bump the version before release, and then `yarn publish-all` in the root directory to publish the release. In case of a tagged release (such as `next`), we use `TESTS_USE_MOCKNET=true yarn publish-all --dist-tag <tag name>` to publish the release with the specified tag.
12+
113
## cashc
214

315
### Prerequisites
@@ -24,4 +36,40 @@ When updating the grammar file in `src/grammar/CashScript.g4`, we also need to m
2436

2537
```bash
2638
yarn antlr
27-
``````
39+
```
40+
41+
### Running `cashproof`
42+
43+
Most of the bytecode optimisations that the `cashc` compiler uses can be verified for correctness using the [`cashproof` tool](https://github.com/EyeOfPython/cashproof). This tool needs to be installed separately by installing its dependencies using `pip` and cloning its repository from GitHub.
44+
45+
From there, you can run `python <cashproof_path> [filenames]` to verify that the optimisations contained in these files are provably correct.
46+
47+
Example:
48+
```bash
49+
python <cashproof_path> packages/cashc/test/cashproof/0.1.2=0.2.0.equiv
50+
```
51+
52+
Note that if you want to run `cashproof` on the "main" CashScript optimisations file, you need to first extract the optimisations from the `cashc` compiler and save them in a separate file. This can be done using the following commands:
53+
54+
```bash
55+
cp packages/utils/src/cashproof-optimisations.ts opt.equiv && sed -i '' '/`/d' opt.equiv
56+
python <cashproof_path> opt.equiv
57+
```
58+
59+
## cashscript
60+
61+
### Running tests
62+
63+
By default, running tests in the `cashscript` package uses chipnet contracts, which requires the test accounts to have some chipnet BCH. To run the tests against a local "mock network", you can use the `TESTS_USE_MOCKNET` environment variable.
64+
65+
```bash
66+
# Run all tests using the mock network
67+
TESTS_USE_MOCKNET=true yarn test
68+
```
69+
70+
To run specific tests, you can use the `-t` flag to match the name mentioned in the `it` or `describe` block:
71+
72+
```bash
73+
# Run all tests in the 'Transaction Builder' describe block (test/e2e/transaction-builder/TransactionBuilder.test.ts)
74+
yarn test -t 'Transaction Builder'
75+
```

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# CashScript
22

3-
[![Build Status](https://travis-ci.org/Bitcoin-com/cashscript.svg)](https://travis-ci.org/Bitcoin-com/cashscript)
4-
[![Coverage Status](https://img.shields.io/codecov/c/github/Bitcoin-com/cashscript.svg)](https://codecov.io/gh/Bitcoin-com/cashscript/)
3+
[![Build Status](https://travis-ci.org/CashScript/cashscript.svg)](https://travis-ci.org/CashScript/cashscript)
4+
[![Coverage Status](https://img.shields.io/codecov/c/github/CashScript/cashscript.svg)](https://codecov.io/gh/CashScript/cashscript/)
55
[![NPM Version](https://img.shields.io/npm/v/cashscript.svg)](https://www.npmjs.com/package/cashscript)
66
[![NPM Monthly Downloads](https://img.shields.io/npm/dm/cashscript.svg)](https://www.npmjs.com/package/cashscript)
77
[![NPM License](https://img.shields.io/npm/l/cashscript.svg)](https://www.npmjs.com/package/cashscript)
88

99
CashScript is a high-level programming language for smart contracts on Bitcoin Cash. It offers a strong abstraction layer over Bitcoin Cash' native virtual machine, Bitcoin Script. Its syntax is based on Ethereum's smart contract language Solidity, but its functionality is very different since smart contracts on Bitcoin Cash differ greatly from smart contracts on Ethereum. For a detailed comparison of them, refer to the blog post [_Smart Contracts on Ethereum, Bitcoin and Bitcoin Cash_](https://kalis.me/smart-contracts-eth-btc-bch/).
1010

11-
This repository contains the code for the CashScript compiler & command line tool under [`packages/cashc/`](/packages/cashc). This repository also contains the code for the CashScript JavaScript SDK under [`packages/cashscript/`](/packages/cashscript). The source code of the [CashScript.org](https://cashscript.org) website is included under [`website/`](/website). Visit the website for a detailed [Documentation](https://cashscript.org/docs/) on the CashScript language and SDK.
11+
This repository contains the code for the CashScript compiler & command line tool under [`packages/cashc/`](/packages/cashc). This repository also contains the code for the CashScript TypeScript SDK under [`packages/cashscript/`](/packages/cashscript). The source code of the [CashScript.org](https://cashscript.org) website is included under [`website/`](/website). Visit the website for a detailed [Documentation](https://cashscript.org/docs/) on the CashScript language and SDK.
1212

1313
## The CashScript Language
1414

1515
CashScript is a high-level language that allows you to write Bitcoin Cash smart contracts in a straightforward and familiar way. Its syntax is inspired by Ethereum's Solidity language, but its functionality is different since the underlying systems have very different fundamentals. See the [language documentation](https://cashscript.org/docs/language/) for a full reference of the language.
1616

1717
## The CashScript Compiler
1818

19-
CashScript features a compiler as a standalone command line tool, called `cashc`. It can be installed through npm and used to compile `.cash` files into `.json` artifact files. These artifact files can be imported into the CashScript JavaScript SDK (or other SDKs in the future). The `cashc` NPM package can also be imported inside JavaScript files to compile `.cash` files without using the command line tool.
19+
CashScript features a compiler as a standalone command line tool, called `cashc`. It can be installed through npm and used to compile `.cash` files into `.json` artifact files. These artifact files can be imported into the CashScript TypeScript SDK (or other SDKs in the future). The `cashc` NPM package can also be imported inside JavaScript files to compile `.cash` files without using the command line tool.
2020

2121
### Installation
2222

@@ -94,16 +94,16 @@ The "Hello World" of CashScript contracts is defining the P2PKH pattern inside a
9494
To run the examples, clone this repository and navigate to the `examples/` directory. Since the examples depend on the SDK, be sure to run `npm install` or `yarn` inside the `examples/` directory, which installs all required packages.
9595

9696
```bash
97-
git clone git@github.com:Bitcoin-com/cashscript.git
97+
git clone git@github.com:CashScript/cashscript.git
9898
cd cashscript/examples
9999
npm install
100100
```
101101

102-
All `.ts` files in the [`examples/`](/examples) directory can then be executed with `ts-node-esm`.
102+
All `.ts` files in the [`examples/`](/examples) directory can then be executed with `tsx`.
103103

104104
```bash
105-
npm install -g ts-node
106-
ts-node-esm p2pkh.ts
105+
npm install -g tsx
106+
tsx p2pkh.ts
107107
```
108108

109109
All `.js` files can be executed with `node`.

examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ cd cashscript/examples
1212
yarn
1313
```
1414

15-
All `.ts` files can then be executed with `ts-node-esm`.
15+
All `.ts` files can then be executed with `tsx`.
1616

1717
```bash
18-
npm install -g ts-node
19-
ts-node-esm p2pkh.ts
18+
npm install -g tsx
19+
tsx p2pkh.ts
2020
```
2121

2222
All `.js` files can be executed with `node`.

examples/announcement.cash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma cashscript ^0.9.0;
1+
pragma cashscript ^0.10.0;
22

33
/* This is a contract showcasing covenants outside of regular transactional use.
44
* It enforces the contract to make an "announcement" on Memo.cash, and send the

examples/common-js.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import bip39 from 'bip39';
1111

1212
// Generate entropy from BIP39 mnemonic phrase and initialise a root HD-wallet node
1313
const seed = await bip39.mnemonicToSeed('CashScript Examples');
14-
const rootNode = deriveHdPrivateNodeFromSeed(seed, true);
14+
const rootNode = deriveHdPrivateNodeFromSeed(seed, { assumeValidity: true, throwErrors: true });
1515
const baseDerivationPath = "m/44'/145'/0'/0";
1616

1717
// Derive Alice's private key, public key, public key hash and address
@@ -20,4 +20,4 @@ if (typeof aliceNode === 'string') throw new Error();
2020
export const alicePub = secp256k1.derivePublicKeyCompressed(aliceNode.privateKey);
2121
export const alicePriv = aliceNode.privateKey;
2222
export const alicePkh = hash160(alicePub);
23-
export const aliceAddress = encodeCashAddress('bchtest', 'p2pkh', alicePkh);
23+
export const aliceAddress = encodeCashAddress({ prefix: 'bchtest', type: 'p2pkh', payload: alicePkh, throwErrors: true }).address;

examples/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { PriceOracle } from './PriceOracle.js';
1010

1111
// Generate entropy from BIP39 mnemonic phrase and initialise a root HD-wallet node
1212
const seed = await bip39.mnemonicToSeed('CashScript Examples');
13-
const rootNode = deriveHdPrivateNodeFromSeed(seed, true);
13+
const rootNode = deriveHdPrivateNodeFromSeed(seed, { assumeValidity: true, throwErrors: true });
1414
const baseDerivationPath = "m/44'/145'/0'/0";
1515

1616
// Derive Alice's private key, public key, public key hash and address
@@ -19,15 +19,15 @@ if (typeof aliceNode === 'string') throw new Error();
1919
export const alicePub = secp256k1.derivePublicKeyCompressed(aliceNode.privateKey) as Uint8Array;
2020
export const alicePriv = aliceNode.privateKey;
2121
export const alicePkh = hash160(alicePub);
22-
export const aliceAddress = encodeCashAddress('bchtest', 'p2pkhWithTokens', alicePkh);
22+
export const aliceAddress = encodeCashAddress({ prefix: 'bchtest', type: 'p2pkhWithTokens', payload: alicePkh, throwErrors: true }).address;
2323

2424
// Derive Bob's private key, public key, public key hash and address
2525
const bobNode = deriveHdPath(rootNode, `${baseDerivationPath}/1`);
2626
if (typeof bobNode === 'string') throw new Error();
2727
export const bobPub = secp256k1.derivePublicKeyCompressed(bobNode.privateKey) as Uint8Array;
2828
export const bobPriv = bobNode.privateKey;
2929
export const bobPkh = hash160(bobPub);
30-
export const bobAddress = encodeCashAddress('bchtest', 'p2pkhWithTokens', bobPkh);
30+
export const bobAddress = encodeCashAddress({ prefix: 'bchtest', type: 'p2pkhWithTokens', payload: bobPkh, throwErrors: true }).address;
3131

3232
// Initialise a price oracle with a private key
3333
const oracleNode = deriveHdPath(rootNode, `${baseDerivationPath}/2`);
@@ -36,4 +36,4 @@ export const oraclePub = secp256k1.derivePublicKeyCompressed(oracleNode.privateK
3636
export const oraclePriv = oracleNode.privateKey;
3737
export const oracle = new PriceOracle(oracleNode.privateKey);
3838
export const oraclePkh = hash160(oraclePub);
39-
export const oracleAddress = encodeCashAddress('bchtest', 'p2pkhWithTokens', oraclePkh);
39+
export const oracleAddress = encodeCashAddress({ prefix: 'bchtest', type: 'p2pkhWithTokens', payload: oraclePkh, throwErrors: true }).address;

examples/hodl_vault.cash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma cashscript ^0.9.0;
1+
pragma cashscript ^0.10.0;
22

33
// This contract forces HODLing until a certain price target has been reached
44
// A minimum block is provided to ensure that oracle price entries from before this block are disregarded

examples/mecenas.cash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma cashscript ^0.9.0;
1+
pragma cashscript ^0.10.0;
22

33
/* This is an unofficial CashScript port of Licho's Mecenas contract. It is
44
* not compatible with Licho's EC plugin, but rather meant as a demonstration

0 commit comments

Comments
 (0)