Skip to content

Commit ced1267

Browse files
committed
Add TypeScript build process
- Fix tsconfig file to compile to dist/ - Add JS example + update README - Update package.json to reflect these changes - Update package.json for npm publish
1 parent 8803e1d commit ced1267

11 files changed

+873
-140
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
src/compiler/grammar
22
test.ts
3+
dist/

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
.btcdeb_history
21
test.ts
2+
dist/
33

44
**/.DS_Store
55
/**/.project

LICENCE renamed to LICENSE

File renamed without changes.

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
[![Build Status](https://travis-ci.org/Bitcoin-com/cashscript.svg)](https://travis-ci.org/Bitcoin-com/cashscript)
44
[![Coverage Status](https://img.shields.io/codecov/c/github/Bitcoin-com/cashscript.svg)](https://codecov.io/gh/Bitcoin-com/cashscript/)
5-
![GitHub](https://img.shields.io/github/license/Bitcoin-com/cashscript.svg)
5+
[![NPM Version](https://img.shields.io/npm/v/cashscript.svg)](https://www.npmjs.com/package/cashscript)
6+
[![NPM Monthly Downloads](https://img.shields.io/npm/dm/cashscript.svg)](https://www.npmjs.com/package/cashscript)
7+
[![NPM License](https://img.shields.io/npm/l/cashscript.svg)](https://www.npmjs.com/package/cashscript)
68

79
CashScript is a high level language enabling basic smart contract functionality on Bitcoin Cash. We love the Ethereum development ecosystem, and with CashScript we want to bring part of that workflow into the Bitcoin Cash ecosystem.
810

@@ -42,9 +44,23 @@ The "Hello World" of cash contracts is defining the P2PKH pattern inside a cash
4244
Note that not all of these examples have been tested to work, and are also still a work in progress.
4345
4446
### Running the examples
45-
All `.ts` files in the [`examples/`](/examples) directory can be easily executed with `ts-node`.
47+
To run the examples, clone this repository.
4648
49+
```bash
50+
git clone [email protected]:Bitcoin-com/cashscript.git
51+
cd cashscript
4752
```
53+
54+
All `.ts` files in the [`examples/`](/examples) directory can then be easily executed with `ts-node`.
55+
56+
```bash
4857
npm install -g ts-node
4958
ts-node examples/p2pkh.ts
5059
```
60+
61+
All `.js` files can be easily executed with `node`. Be sure to compile the TypeScript source code first, so it can be used from JavaScript.
62+
63+
```bash
64+
yarn / npm install
65+
node examples/p2pkh.js
66+
```

examples/p2pkh.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { BITBOX } = require('bitbox-sdk');
2+
const path = require('path');
3+
const { Contract, Sig } = require('..');
4+
5+
(async () => {
6+
const network = 'testnet';
7+
const bitbox = new BITBOX({ restURL: 'https://trest.bitcoin.com/v2/' });
8+
9+
const rootSeed = bitbox.Mnemonic.toSeed('CashScript');
10+
const hdNode = bitbox.HDNode.fromSeed(rootSeed, network);
11+
const keypair = bitbox.HDNode.toKeyPair(hdNode);
12+
13+
const pk = bitbox.ECPair.toPublicKey(keypair);
14+
const pkh = bitbox.Crypto.hash160(pk);
15+
16+
const P2PKH = Contract.fromCashFile(path.join(__dirname, 'p2pkh.cash'), network);
17+
const instance = P2PKH.new(pkh);
18+
const contractBalance = await instance.getBalance();
19+
20+
console.log('contract address:', instance.address);
21+
console.log('contract balance:', contractBalance);
22+
23+
// Send to one output
24+
const tx = await instance.functions.spend(pk, new Sig(keypair, 0x01))
25+
.send(instance.address, 10000);
26+
27+
console.log('transaction details:', tx);
28+
29+
// Send to multiple outputs
30+
const tx2 = await instance.functions.spend(pk, new Sig(keypair, 0x01)).send([
31+
{ to: instance.address, amount: 10000 },
32+
{ to: instance.address, amount: 20000 },
33+
]);
34+
35+
console.log('transaction details:', tx2);
36+
})();

examples/p2pkh.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ import { Contract, Instance, Sig } from '..';
2929
console.log('transaction details:', tx);
3030

3131
// Send to multiple outputs
32-
const tx2 = await instance.functions.spend(pk, new Sig(keypair, 0x01)).send([
33-
{ to: instance.address, amount: 10000 },
34-
{ to: instance.address, amount: 20000 },
35-
]);
32+
const tx2: TxnDetailsResult = await instance.functions.spend(pk, new Sig(keypair, 0x01))
33+
.send([
34+
{ to: instance.address, amount: 10000 },
35+
{ to: instance.address, amount: 20000 },
36+
]);
3637

3738
console.log('transaction details:', tx2);
3839
})();

examples/transfer_with_timeout.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { BITBOX } from 'bitbox-sdk';
22
import { TxnDetailsResult } from 'bitcoin-com-rest';
33
import { ECPair, HDNode } from 'bitcoincashjs-lib';
44
import * as path from 'path';
5-
import { Contract, Sig } from '../src/sdk/cashscript-sdk';
6-
import { Instance } from '../src/sdk/Contract';
5+
import { Contract, Instance, Sig } from '..';
76

87
(async (): Promise<any> => {
98
const network: string = 'testnet';

package.json

+41-26
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
{
22
"name": "cashscript",
3+
"description": "⚖️ Easily write and interact with Cash Contracts on Bitcoin Cash",
34
"version": "0.1.0-beta.1",
4-
"description": "",
5-
"main": "index.ts",
6-
"directories": {
7-
"doc": "docs"
8-
},
9-
"files": [
10-
"src/",
11-
"index.ts"
12-
],
13-
"scripts": {
14-
"antlr": "antlr4ts -visitor -listener src/compiler/grammar/CashScript.g4",
15-
"test": "nyc mocha",
16-
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
17-
"lint": "eslint . --ext .ts"
18-
},
19-
"repository": {
20-
"type": "git",
21-
"url": "git+https://github.com/Bitcoin-com/cashscript.git"
22-
},
235
"author": "Rosco Kalis <[email protected]>",
24-
"contributors": [
25-
"Gabriel Cardona <[email protected]>"
26-
],
27-
"license": "MIT",
286
"bugs": {
297
"url": "https://github.com/Bitcoin-com/cashscript/issues"
308
},
31-
"homepage": "https://github.com/Bitcoin-com/cashscript#readme",
9+
"contributors": [
10+
"Gabriel Cardona <[email protected]>"
11+
],
3212
"dependencies": {
3313
"antlr4ts": "^0.5.0-alpha.3",
34-
"bitbox-sdk": "8.3.2",
14+
"bitbox-sdk": "^8.4.0",
3515
"bitcoincashjs-lib": "Bitcoin-com/bitcoincashjs-lib#v4.0.1",
3616
"delay": "^4.2.0"
3717
},
@@ -53,5 +33,40 @@
5333
"source-map-support": "^0.5.12",
5434
"ts-node": "^8.1.0",
5535
"typescript": "^3.4.5"
56-
}
36+
},
37+
"directories": {
38+
"doc": "docs",
39+
"example": "examples",
40+
"lib": "src",
41+
"test": "test"
42+
},
43+
"files": [
44+
"dist/"
45+
],
46+
"homepage": "https://github.com/Bitcoin-com/cashscript#readme",
47+
"keywords": [
48+
"bitcoin",
49+
"bitcoin cash",
50+
"cash contracts",
51+
"compiler",
52+
"programming language",
53+
"sdk",
54+
"smart contracts"
55+
],
56+
"license": "MIT",
57+
"main": "dist/index.js",
58+
"repository": {
59+
"type": "git",
60+
"url": "git+https://github.com/Bitcoin-com/cashscript.git"
61+
},
62+
"scripts": {
63+
"antlr": "antlr4ts -visitor -listener src/compiler/grammar/CashScript.g4",
64+
"build": "tsc",
65+
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
66+
"lint": "eslint . --ext .ts",
67+
"prepare": "npm run build",
68+
"prepublishOnly": "npm test && npm run lint",
69+
"test": "nyc mocha"
70+
},
71+
"types": "dist/index.d.ts"
5772
}

src/sdk/ABI.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Ast } from '../compiler/ast/AST';
22
import { Type } from '../compiler/ast/Type';
33
import { Script } from '../compiler/generation/Script';
4-
import pkg from '../../package.json';
4+
import * as pkg from '../../package.json';
55

66
export interface AbiParameter {
77
name: string;

tsconfig.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
1010
"typeRoots" : ["node_modules/@types", "src/lib"],
1111
"resolveJsonModule": true,
12-
"esModuleInterop": true,
13-
}
12+
"declaration": true,
13+
"outDir": "dist/",
14+
"moduleResolution": "node",
15+
},
16+
"include": ["index.ts"],
1417
}

0 commit comments

Comments
 (0)