Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/

dist/
dist.commonjs/

.npmignore
.prettierrc
tsconfig.json
eslint.config.mjs
.prettierrc

tsconfig.tsbuildinfo
.github/
10 changes: 10 additions & 0 deletions examples/ckb-ts-script/contracts/call-crypto-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TODO: Write this readme

This project was bootstrapped with [`create-ckb-js-vm-app`](https://github.com/nervosnetwork/ckb-js-vm).

## How to Run

```bash
pnpm build
pnpm start
```
33 changes: 33 additions & 0 deletions examples/ckb-ts-script/contracts/call-crypto-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "call-crypto-service",
"version": "0.1.0",
"description": "",
"author": "your name",
"license": "MIT",
"private": true,
"homepage": "https://github.com",
"repository": {
"type": "git",
"url": "https://github.com"
},
"type": "module",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.js",
"scripts": {
"start": "ckb-debugger --read-file dist/index.bc --bin node_modules/ckb-testtool/src/unittest/defaultScript/ckb-js-vm -- -r",
"build": "tsc --noEmit && esbuild --platform=neutral --minify --bundle --external:@ckb-js-std/bindings --target=es2022 src/index.ts --outfile=dist/index.js && ckb-debugger --read-file dist/index.js --bin node_modules/ckb-testtool/src/unittest/defaultScript/ckb-js-vm -- -c dist/index.bc",
"format": "prettier --write .",
"clean": "rimraf dist/*"
},
"devDependencies": {
"ckb-testtool": "~1.0.0",
"esbuild": "~0.25.0"
},
"dependencies": {
"@ckb-js-std/bindings": "~1.0.0",
"@ckb-js-std/core": "~1.0.0",
"@ckb-js-std/ipc": "^1.0.0",
"ckb-default-app": "link:"
}
}
53 changes: 53 additions & 0 deletions examples/ckb-ts-script/contracts/call-crypto-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as bindings from "@ckb-js-std/bindings";
import { HighLevel, bytesEq } from "@ckb-js-std/core";
import { Channel, RequestPacket, spawnCellServer } from "@ckb-js-std/ipc";

function runFunction(channel: Channel, payload: Object) {
let payloadHex = new bindings.TextEncoder().encode(JSON.stringify(payload));;
let res = channel.call(new RequestPacket(payloadHex));
if (res.errorCode() != 0) {
throw Error(`IPC Error: ${res.errorCode()}`);
}

let resPayload = new bindings.TextDecoder().decode(res.payload());
return Object.values(JSON.parse(resPayload))[0];
}

function resultOk(result: any): any {
let key = Object.keys(result)[0];
if (key != "Ok") {
throw (`res is not ok : ${JSON.stringify(result)}`);
}

return result[key];
}

function startService(): Channel {
const args = HighLevel.loadScript().args;
const codeHash = args.slice(35, 35 + 32);
const [readPipe, writePipe] = spawnCellServer(codeHash, bindings.SCRIPT_HASH_TYPE_DATA2, []);
return new Channel(readPipe, writePipe);
}

function ckbBlake2b(channel: Channel, data: number[]) {
let hasher_ctx = runFunction(channel, { "HasherNew": { "hash_type": "CkbBlake2b" } });
runFunction(channel, { "HasherUpdate": { "ctx": hasher_ctx, "data": data } });
let hash = new Uint8Array(resultOk(runFunction(channel, { "HasherFinalize": { "ctx": hasher_ctx, } })));

return hash;
}

function main() {
let channel = startService();

let hash = ckbBlake2b(channel, [0, 0, 0, 0]);
let witnesse = HighLevel.loadWitnessArgs(0, bindings.SOURCE_GROUP_INPUT).lock ?? new ArrayBuffer();

if (bytesEq(witnesse, hash.buffer) != true) {
return 1;
}

return 0;
}

bindings.exit(main());
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
/* The QuickJS supports ES2022 */
"module": "ESNext",
/* Use esbuild to bundle the code */
"moduleResolution": "Bundler",
"target": "ES2022",
"incremental": true,
"allowJs": true,
"importHelpers": false,
"declaration": true,
"declarationMap": true,
"experimentalDecorators": true,
"useDefineForClassFields": false,
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"alwaysStrict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*"]
}
13 changes: 13 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-child/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/

dist/
dist.commonjs/

.npmignore
.prettierrc
tsconfig.json
eslint.config.mjs
.prettierrc

tsconfig.tsbuildinfo
.github/
10 changes: 10 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-child/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TODO: Write this readme

This project was bootstrapped with [`create-ckb-js-vm-app`](https://github.com/nervosnetwork/ckb-js-vm).

## How to Run

```bash
pnpm build
pnpm start
```
33 changes: 33 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-child/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "ipc-child",
"version": "0.1.0",
"description": "",
"author": "your name",
"license": "MIT",
"private": true,
"homepage": "https://github.com",
"repository": {
"type": "git",
"url": "https://github.com"
},
"type": "module",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.js",
"scripts": {
"start": "ckb-debugger --read-file dist/index.bc --bin node_modules/ckb-testtool/src/unittest/defaultScript/ckb-js-vm -- -r",
"build": "tsc --noEmit && esbuild --platform=neutral --minify --bundle --external:@ckb-js-std/bindings --target=es2022 src/index.ts --outfile=dist/index.js && ckb-debugger --read-file dist/index.js --bin node_modules/ckb-testtool/src/unittest/defaultScript/ckb-js-vm -- -c dist/index.bc",
"format": "prettier --write .",
"clean": "rimraf dist/*"
},
"devDependencies": {
"ckb-testtool": "~1.0.0",
"esbuild": "~0.25.0"
},
"dependencies": {
"@ckb-js-std/bindings": "~1.0.0",
"@ckb-js-std/core": "~1.0.0",
"@ckb-js-std/ipc": "^1.0.0",
"ckb-default-app": "link:"
}
}
22 changes: 22 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-child/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { log } from "@ckb-js-std/core";
import {
runServer,
RequestHandler,
RequestPacket,
ResponsePacket,
} from "@ckb-js-std/ipc";

class Serve implements RequestHandler {
serve(req: RequestPacket): ResponsePacket {
log.debug("[SERVER]: receive request %s", req.toString());
return new ResponsePacket(0, new Uint8Array([42]));
}
}

function main() {
runServer(new Serve());
}

log.setLevel(log.LogLevel.Debug);
log.debug("[SERVER]: start server");
main();
24 changes: 24 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-child/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
/* The QuickJS supports ES2022 */
"module": "ESNext",
/* Use esbuild to bundle the code */
"moduleResolution": "Bundler",
"target": "ES2022",
"incremental": true,
"allowJs": true,
"importHelpers": false,
"declaration": true,
"declarationMap": true,
"experimentalDecorators": true,
"useDefineForClassFields": false,
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"alwaysStrict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}
7 changes: 7 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-child/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*"]
}
13 changes: 13 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-parent/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/

dist/
dist.commonjs/

.npmignore
.prettierrc
tsconfig.json
eslint.config.mjs
.prettierrc

tsconfig.tsbuildinfo
.github/
10 changes: 10 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-parent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TODO: Write this readme

This project was bootstrapped with [`create-ckb-js-vm-app`](https://github.com/nervosnetwork/ckb-js-vm).

## How to Run

```bash
pnpm build
pnpm start
```
33 changes: 33 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-parent/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "ipc-parent",
"version": "0.1.0",
"description": "",
"author": "your name",
"license": "MIT",
"private": true,
"homepage": "https://github.com",
"repository": {
"type": "git",
"url": "https://github.com"
},
"type": "module",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.js",
"scripts": {
"start": "ckb-debugger --read-file dist/index.bc --bin node_modules/ckb-testtool/src/unittest/defaultScript/ckb-js-vm -- -r",
"build": "tsc --noEmit && esbuild --platform=neutral --minify --bundle --external:@ckb-js-std/bindings --target=es2022 src/index.ts --outfile=dist/index.js && ckb-debugger --read-file dist/index.js --bin node_modules/ckb-testtool/src/unittest/defaultScript/ckb-js-vm -- -c dist/index.bc",
"format": "prettier --write .",
"clean": "rimraf dist/*"
},
"devDependencies": {
"ckb-testtool": "~1.0.0",
"esbuild": "~0.25.0"
},
"dependencies": {
"@ckb-js-std/bindings": "~1.0.0",
"@ckb-js-std/core": "~1.0.0",
"@ckb-js-std/ipc": "^1.0.0",
"ckb-default-app": "link:"
}
}
28 changes: 28 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-parent/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as bindings from "@ckb-js-std/bindings";
import { HighLevel, log } from "@ckb-js-std/core";
import { Channel, RequestPacket, spawnCellServer } from "@ckb-js-std/ipc";

function main() {
const args = HighLevel.loadScript().args;
let codeHash = args.slice(35, 35 + 32);
let jsVmCodeHash = args.slice(35 + 32, 35 + 32 + 32);
let serverCellLocation = bindings.hex.encode(codeHash) + "04";
// spawn server
let [readPipe, writePipe] = spawnCellServer(
jsVmCodeHash,
bindings.SCRIPT_HASH_TYPE_DATA2,
["-t", serverCellLocation],
);

let channel = new Channel(readPipe, writePipe);
let req = new RequestPacket(new Uint8Array([1, 2, 3]));
for (let i = 0; i < 3; i++) {
log.debug("[CLIENT]: send request %s", req.toString());
let res = channel.call(req);
log.debug("[CLIENT]: receive response %s", res.toString());
console.assert(res.payload()[0] === 42);
}
}

log.setLevel(log.LogLevel.Debug);
main();
24 changes: 24 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-parent/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
/* The QuickJS supports ES2022 */
"module": "ESNext",
/* Use esbuild to bundle the code */
"moduleResolution": "Bundler",
"target": "ES2022",
"incremental": true,
"allowJs": true,
"importHelpers": false,
"declaration": true,
"declarationMap": true,
"experimentalDecorators": true,
"useDefineForClassFields": false,
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"alwaysStrict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}
7 changes: 7 additions & 0 deletions examples/ckb-ts-script/contracts/ipc-parent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*"]
}
Binary file added examples/ckb-ts-script/deps/ckb-crypto-service
Binary file not shown.
Loading