@paimaexample/ord 0.3.109

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/index.js +181 -0
  2. package/package.json +10 -0
package/index.js ADDED
@@ -0,0 +1,181 @@
1
+ import BinWrapper from '@xhmikosr/bin-wrapper';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { execFile, spawn } from 'node:child_process';
5
+ import fs from 'node:fs';
6
+ import os from 'node:os';
7
+ import process from 'node:process';
8
+
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
+
11
+ const version = '0.23.3';
12
+ const base = `https://github.com/ordinals/ord/releases/download/${version}`;
13
+ const dest = path.join(__dirname, 'vendor');
14
+
15
+ const bin = new BinWrapper()
16
+ .src(`${base}/ord-${version}-x86_64-unknown-linux-gnu.tar.gz`, 'linux', 'x64')
17
+ .src(`${base}/ord-${version}-aarch64-apple-darwin.tar.gz`, 'darwin', 'arm64')
18
+ .src(`${base}/ord-${version}-x86_64-apple-darwin.tar.gz`, 'darwin', 'x64')
19
+ .dest(dest)
20
+ .use('ord');
21
+
22
+ export default bin;
23
+
24
+ export async function run(options = {}) {
25
+ const {
26
+ dataDir,
27
+ bitcoinDataDir,
28
+ chain = 'regtest',
29
+ rpcUrl = 'http://dev:devpassword@127.0.0.1:18443',
30
+ server = false,
31
+ serverPort = 8080,
32
+ verbose = false,
33
+ args = [],
34
+ } = options;
35
+
36
+ await bin.run(['--version']);
37
+
38
+ const dataDirPath = dataDir || fs.mkdtempSync(path.join(os.tmpdir(), 'ord-'));
39
+
40
+ if (!fs.existsSync(dataDirPath)) {
41
+ fs.mkdirSync(dataDirPath, { recursive: true });
42
+ }
43
+
44
+ // Parse RPC URL to extract components
45
+ const rpcUrlObj = new URL(rpcUrl);
46
+ const rpcUser = rpcUrlObj.username;
47
+ const rpcPass = rpcUrlObj.password;
48
+ const rpcHost = rpcUrlObj.hostname;
49
+ // Always include port explicitly - use from URL or default
50
+ const rpcPort = rpcUrlObj.port || (rpcUrlObj.protocol === 'https:' ? '443' : '80');
51
+ // Construct URL ensuring port is always included - ord expects full URL with protocol
52
+ const rpcUrlWithoutAuth = `${rpcUrlObj.protocol}//${rpcHost}:${rpcPort}`;
53
+
54
+ // Build ord command arguments
55
+ // Ord can use --bitcoin-rpc-url with --bitcoin-rpc-username and --bitcoin-rpc-password
56
+ // This avoids needing the cookie file
57
+ // Note: --bitcoin-rpc-url should be the full URL including protocol and port
58
+ // Use --chain parameter (defaults to regtest)
59
+ const ordArgs = [
60
+ '--chain', chain,
61
+ '--bitcoin-rpc-url', rpcUrlWithoutAuth,
62
+ '--bitcoin-rpc-username', rpcUser,
63
+ '--bitcoin-rpc-password', rpcPass,
64
+ '--data-dir', dataDirPath,
65
+ ];
66
+
67
+ if (server) {
68
+ ordArgs.push('server', '--http-port', String(serverPort));
69
+ }
70
+
71
+ // Add any additional args
72
+ ordArgs.push(...args);
73
+
74
+ const child = spawn(bin.path(), ordArgs, {
75
+ stdio: verbose ? 'inherit' : 'pipe',
76
+ });
77
+
78
+ if (verbose && child.stdout) {
79
+ child.stdout.on('data', (data) => {
80
+ console.log(`ord stdout: ${data}`);
81
+ });
82
+ }
83
+
84
+ if (child.stderr) {
85
+ child.stderr.on('data', (data) => {
86
+ console.error(`ord stderr: ${data}`);
87
+ });
88
+ }
89
+
90
+ child.on('close', (code) => {
91
+ if (code !== 0) {
92
+ console.log(`ord exited with code ${code}`);
93
+ }
94
+ });
95
+
96
+ return {
97
+ child,
98
+ dataDir: dataDirPath,
99
+ stop: () => child.kill(),
100
+ };
101
+ }
102
+
103
+ export async function runCommand(command, options = {}) {
104
+ const {
105
+ rpcUrl = 'http://dev:devpassword@127.0.0.1:18443',
106
+ dataDir,
107
+ bitcoinDataDir,
108
+ chain = 'regtest',
109
+ verbose = false,
110
+ serverUrl,
111
+ } = options;
112
+
113
+ await bin.run(['--version']);
114
+
115
+ // Use provided dataDir or create a temporary one
116
+ // Note: If dataDir is not provided, each command will use its own temp dir
117
+ const dataDirPath = dataDir;
118
+
119
+ if (dataDirPath && !fs.existsSync(dataDirPath)) {
120
+ fs.mkdirSync(dataDirPath, { recursive: true });
121
+ }
122
+
123
+ // Parse RPC URL to extract components
124
+ const rpcUrlObj = new URL(rpcUrl);
125
+ const rpcUser = rpcUrlObj.username;
126
+ const rpcPass = rpcUrlObj.password;
127
+ const rpcHost = rpcUrlObj.hostname;
128
+ // Always include port explicitly - use from URL or default
129
+ const rpcPort = rpcUrlObj.port || (rpcUrlObj.protocol === 'https:' ? '443' : '80');
130
+ // Construct URL ensuring port is always included
131
+ const rpcUrlWithoutAuth = `${rpcUrlObj.protocol}//${rpcHost}:${rpcPort}`;
132
+
133
+ // Build ord command arguments
134
+ // Ord can use --bitcoin-rpc-url with --bitcoin-rpc-username and --bitcoin-rpc-password
135
+ // This avoids needing the cookie file
136
+ // Use --chain parameter (defaults to regtest)
137
+ // Note: --http-port is only valid for 'ord server' command, not for CLI commands
138
+ const ordArgs = [
139
+ '--chain', chain,
140
+ '--bitcoin-rpc-url', rpcUrlWithoutAuth,
141
+ '--bitcoin-rpc-username', rpcUser,
142
+ '--bitcoin-rpc-password', rpcPass,
143
+ ...(dataDirPath ? ['--data-dir', dataDirPath] : []),
144
+ ...(bitcoinDataDir ? ['--bitcoin-data-dir', bitcoinDataDir] : []),
145
+ ];
146
+
147
+ // If serverUrl is provided and command is a wallet command, add --server-url flag
148
+ // Wallet commands need the ord server URL to query block count and other info
149
+ // Note: --server-url must come AFTER 'wallet', not before it
150
+ if (serverUrl && command.length > 0 && command[0] === 'wallet') {
151
+ // Add 'wallet' first, then --server-url, then the rest of the command
152
+ ordArgs.push('wallet', '--server-url', serverUrl, ...command.slice(1));
153
+ } else {
154
+ // Add the actual command as-is
155
+ ordArgs.push(...command);
156
+ }
157
+
158
+ // Set environment variables for ord CLI commands
159
+ // Note: ord also supports ORD_SERVER_URL environment variable, but --server-url flag is preferred
160
+ const env = { ...process.env };
161
+
162
+ return new Promise((resolve, reject) => {
163
+ const child = execFile(bin.path(), ordArgs, { env }, (error, stdout, stderr) => {
164
+ if (error) {
165
+ reject(error);
166
+ return;
167
+ }
168
+ resolve({ stdout, stderr });
169
+ });
170
+
171
+ if (verbose) {
172
+ child.stdout.on('data', (data) => {
173
+ console.log(`ord stdout: ${data}`);
174
+ });
175
+ child.stderr.on('data', (data) => {
176
+ console.error(`ord stderr: ${data}`);
177
+ });
178
+ }
179
+ });
180
+ }
181
+
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@paimaexample/ord",
3
+ "version": "0.3.109",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "dependencies": {
7
+ "@xhmikosr/bin-wrapper": "^5.0.0"
8
+ }
9
+ }
10
+