@greenlightcln/glsdk 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +200 -0
  2. package/package.json +58 -0
package/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # Greenlight SDK - Node.js Bindings
2
+
3
+ Node.js bindings for Blockstream's Greenlight SDK using [napi-rs](https://napi.rs/).
4
+
5
+ ## Prerequisites
6
+
7
+ - Node.js >= 16
8
+ - Protocol Buffers compiler (`protoc`)
9
+
10
+ ## Installation
11
+
12
+ ### From npm
13
+
14
+ ```bash
15
+ npm install @greenlightcln/glsdk
16
+ ```
17
+
18
+ ### Building from source
19
+
20
+ 1. Clone the Greenlight repository:
21
+
22
+ ```bash
23
+ git clone https://github.com/Blockstream/greenlight.git
24
+ cd greenlight
25
+ ```
26
+
27
+ 2. Navigate to the napi bindings directory:
28
+
29
+ ```bash
30
+ cd libs/gl-sdk-napi
31
+ ```
32
+
33
+ 3. Install dependencies:
34
+
35
+ ```bash
36
+ npm install
37
+ ```
38
+
39
+ 4. Build the package:
40
+
41
+ ```bash
42
+ npm run build
43
+ ```
44
+
45
+ This will compile the Rust code and generate the Node.js native module.
46
+
47
+ ## Supported Platforms
48
+
49
+ Prebuilt binaries are available for the following platforms:
50
+
51
+ | OS | Architecture | Target |
52
+ |---------|--------------|-----------------------------|
53
+ | Linux | x64 | x86_64-unknown-linux-gnu |
54
+ | Linux | arm64 | aarch64-unknown-linux-gnu |
55
+ | macOS | x64 | x86_64-apple-darwin |
56
+ | macOS | arm64 (M1/M2)| aarch64-apple-darwin |
57
+ | Windows | x64 | x86_64-pc-windows-msvc |
58
+
59
+ ### For Unsupported Platforms:
60
+
61
+ 1. Follow the instructions in the [Building from source](#building-from-source) section above.
62
+
63
+ 2. Then install the package from your local directory:
64
+
65
+ ```bash
66
+ cd /path/to/your/project
67
+ npm install /path/to/greenlight/libs/gl-sdk-napi
68
+ ```
69
+
70
+ ## Project Structure
71
+
72
+ ```
73
+ gl-sdk-napi/
74
+ ├── Cargo.toml # Rust dependencies and configuration
75
+ ├── package.json # Node.js package configuration
76
+ ├── src/
77
+ │ └── lib.rs # Rust implementation of Node.js bindings
78
+ ├── example.ts # TypeScript usage examples
79
+ └── tests/ # Test file/s
80
+ ```
81
+
82
+ ## Usage Example
83
+
84
+ **Async/Await**: All network operations are asynchronous. Always use await or handle returned promises properly to avoid unhandled rejections or unexpected behavior.
85
+
86
+
87
+ ```typescript
88
+ import { Scheduler, Signer, Node, Credentials, OnchainReceiveResponse } from '@greenlightcln/glsdk';
89
+
90
+ type Network = 'bitcoin' | 'regtest';
91
+
92
+ class GreenlightApp {
93
+ private credentials: Credentials | null = null;
94
+ private scheduler: Scheduler;
95
+ private signer: Signer;
96
+ private node: Node | null = null;
97
+
98
+ constructor(phrase: string, network: Network) {
99
+ this.scheduler = new Scheduler(network);
100
+ this.signer = new Signer(phrase);
101
+ const nodeId = this.signer.nodeId();
102
+ console.log(`✓ Signer created. Node ID: ${nodeId.toString('hex')}`);
103
+ }
104
+
105
+ async registerOrRecover(inviteCode?: string): Promise<void> {
106
+ try {
107
+ console.log('Attempting to register node...');
108
+ this.credentials = await this.scheduler.register(this.signer, inviteCode || '');
109
+ console.log('✓ Node registered successfully');
110
+ } catch (e: any) {
111
+ const match = e.message.match(/message: "([^"]+)"/);
112
+ console.error(`✗ Registration failed: ${match ? match[1] : e.message}`);
113
+ console.log('Attempting recovery...');
114
+ try {
115
+ this.credentials = await this.scheduler.recover(this.signer);
116
+ console.log('✓ Node recovered successfully');
117
+ } catch (recoverError) {
118
+ console.error('✗ Recovery failed:', recoverError);
119
+ throw recoverError;
120
+ }
121
+ }
122
+ }
123
+
124
+ createNode(): Node {
125
+ if (!this.credentials) { throw new Error('Must register/recover before creating node'); }
126
+ console.log('Creating node instance...');
127
+ this.node = new Node(this.credentials);
128
+ console.log('✓ Node created');
129
+ return this.node;
130
+ }
131
+
132
+ async getOnchainAddress(): Promise<OnchainReceiveResponse> {
133
+ if (!this.node) { this.createNode(); }
134
+ console.log('Generating on-chain address...');
135
+ return await this.node!.onchainReceive();
136
+ }
137
+
138
+ async cleanup(): Promise<void> {
139
+ if (this.node) {
140
+ await this.node.stop();
141
+ console.log('✓ Node stopped');
142
+ }
143
+ }
144
+ }
145
+
146
+ async function quickStart(): Promise<void> {
147
+ const phrase = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
148
+ const network: Network = 'regtest';
149
+ console.log('=== Greenlight SDK Demo ===');
150
+ const app = new GreenlightApp(phrase, network);
151
+ try {
152
+ await app.registerOrRecover();
153
+ app.createNode();
154
+ const address = await app.getOnchainAddress();
155
+ console.log(`✓ Bech32 Address: ${address.bech32}`);
156
+ console.log(`✓ P2TR Address: ${address.p2Tr}`);
157
+ } catch (e) {
158
+ console.error('✗ Error:', e);
159
+ } finally {
160
+ await app.cleanup();
161
+ }
162
+ }
163
+
164
+ quickStart();
165
+ ```
166
+
167
+ ## Development
168
+
169
+ ### Running Tests
170
+
171
+ ```bash
172
+ npm test
173
+ ```
174
+
175
+ ### Local npm Publishing
176
+ This workflow only builds for local platform. For multi-platform builds, use the GitHub Actions workflow which cross-compiles for all supported targets.
177
+
178
+ ```bash
179
+ # Clean previous builds
180
+ npm run clean
181
+
182
+ # Build the native binary for your platform
183
+ npm run build
184
+
185
+ # Preview what will be included in the package (Tarball Contents)
186
+ npm pack --dry-run
187
+
188
+ # Bump version (patch: 0.1.4 → 0.1.5, minor: 0.1.4 → 0.2.0, major: 0.1.4 → 1.0.0)
189
+ npm version patch/minor/major
190
+
191
+ # Publish to npm registry (requires authentication)
192
+ npm publish --access public
193
+ ```
194
+
195
+ ## Resources
196
+
197
+ - [Greenlight Documentation](https://blockstream.github.io/greenlight/)
198
+ - [napi-rs Documentation](https://napi.rs/)
199
+ - [Prebuilt Binary Support Matrix](https://github.com/napi-rs/napi-rs?tab=readme-ov-file#msrv)
200
+ - [Features Table](https://github.com/napi-rs/napi-rs?tab=readme-ov-file#features-table)
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@greenlightcln/glsdk",
3
+ "version": "0.0.0",
4
+ "description": "Node.js bindings for Blockstream Greenlight SDK",
5
+ "author": "The Greenlight Team",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "files": [
9
+ "index.js",
10
+ "index.d.ts",
11
+ "*.node",
12
+ "README.md",
13
+ "package.json"
14
+ ],
15
+ "keywords": [
16
+ "bitcoin",
17
+ "lightning",
18
+ "greenlight",
19
+ "blockstream",
20
+ "gl-sdk"
21
+ ],
22
+ "license": "MIT",
23
+ "napi": {
24
+ "binaryName": "gl-sdk",
25
+ "targets": [
26
+ "x86_64-unknown-linux-gnu",
27
+ "aarch64-unknown-linux-gnu",
28
+ "x86_64-apple-darwin",
29
+ "aarch64-apple-darwin",
30
+ "x86_64-pc-windows-msvc"
31
+ ]
32
+ },
33
+ "scripts": {
34
+ "clean": "rm -rf ../../target && rm -rf index.js && rm -rf index.d.ts && rm -rf *.node",
35
+ "build": "napi build --platform --release",
36
+ "test": "jest"
37
+ },
38
+ "devDependencies": {
39
+ "@napi-rs/cli": "2.18.4",
40
+ "@types/jest": "30.0.0",
41
+ "@types/node": "20.19.33",
42
+ "jest": "29.7.0",
43
+ "ts-jest": "29.4.6",
44
+ "typescript": "5.9.3"
45
+ },
46
+ "engines": {
47
+ "node": ">= 16"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://github.com/Blockstream/greenlight.git",
52
+ "directory": "libs/gl-sdk-napi"
53
+ },
54
+ "bugs": {
55
+ "url": "https://github.com/Blockstream/greenlight/issues"
56
+ },
57
+ "homepage": "https://github.com/Blockstream/greenlight#readme"
58
+ }