@paytaca/opencode-plugin 0.1.5 → 0.1.7

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.
package/src/wallet.ts DELETED
@@ -1,310 +0,0 @@
1
- import { spawn, execSync } from 'child_process';
2
- import * as fs from 'fs';
3
- import * as os from 'os';
4
- import { promisify } from 'util';
5
- import * as path from 'path';
6
- import { WalletInfo } from './types';
7
-
8
- const execAsync = promisify(require('child_process').exec);
9
-
10
- function getGlobalNpmRoot(): string | null {
11
- try {
12
- return execSync('npm root -g', { encoding: 'utf8' }).trim();
13
- } catch {
14
- return null;
15
- }
16
- }
17
-
18
- function getPaytacaCommand(): string {
19
- // Priority 1: Local node_modules (via require.resolve)
20
- try {
21
- const paytacaCliPkg = require.resolve('paytaca-cli/package.json');
22
- return path.resolve(path.dirname(paytacaCliPkg), 'bin', 'paytaca.js');
23
- } catch {}
24
-
25
- // Priority 2: Local .bin symlink
26
- const localPaytaca = path.join(__dirname, '..', 'node_modules', '.bin', 'paytaca');
27
- if (fs.existsSync(localPaytaca)) {
28
- return localPaytaca;
29
- }
30
-
31
- // Priority 3: Global npm root
32
- const globalRoot = getGlobalNpmRoot();
33
- if (globalRoot) {
34
- const globalPaytaca = path.join(globalRoot, 'paytaca-cli', 'bin', 'paytaca.js');
35
- if (fs.existsSync(globalPaytaca)) {
36
- return globalPaytaca;
37
- }
38
- const scopedPaytaca = path.join(globalRoot, '@paytaca', 'opencode-plugin', 'node_modules', 'paytaca-cli', 'bin', 'paytaca.js');
39
- if (fs.existsSync(scopedPaytaca)) {
40
- return scopedPaytaca;
41
- }
42
- }
43
-
44
- // Priority 4: Common global installation paths
45
- const commonPaths = [
46
- '/usr/lib/node_modules/paytaca-cli/bin/paytaca.js',
47
- '/usr/local/lib/node_modules/paytaca-cli/bin/paytaca.js',
48
- '/opt/homebrew/lib/node_modules/paytaca-cli/bin/paytaca.js',
49
- ];
50
- for (const p of commonPaths) {
51
- if (fs.existsSync(p)) {
52
- return p;
53
- }
54
- }
55
-
56
- // Priority 5: which/where on PATH
57
- try {
58
- const which = process.platform === 'win32' ? 'where' : 'which';
59
- const result = execSync(`${which} paytaca`, { encoding: 'utf8' }).trim().split('\n')[0];
60
- if (result) {
61
- return result;
62
- }
63
- } catch {}
64
-
65
- // Priority 6: Bare command (rely on PATH at runtime)
66
- return 'paytaca';
67
- }
68
-
69
- const PAYTACA_CMD = getPaytacaCommand();
70
-
71
- export async function checkWallet(): Promise<WalletInfo> {
72
- try {
73
- const { stdout } = await execAsync(`"${PAYTACA_CMD}" wallet info`);
74
- const output = stdout.toString();
75
-
76
- // Extract wallet hash
77
- const hashMatch = output.match(/Wallet hash:\s*(.+)/i);
78
- const hash = hashMatch ? hashMatch[1].trim() : undefined;
79
-
80
- if (!hash) {
81
- return { exists: false };
82
- }
83
-
84
- // Extract address
85
- const addressMatch = output.match(/Address:\s*(.+)/i);
86
- const address = addressMatch ? addressMatch[1].trim() : undefined;
87
-
88
- // Extract balance
89
- const balanceMatch = output.match(/Balance:\s*(.+)/i);
90
- const balance = balanceMatch ? balanceMatch[1].trim() : undefined;
91
-
92
- return {
93
- exists: true,
94
- hash,
95
- address,
96
- balance
97
- };
98
- } catch (err) {
99
- return {
100
- exists: false
101
- };
102
- }
103
- }
104
-
105
- function addToPath(dir: string): void {
106
- if (dir && fs.existsSync(dir) && !process.env.PATH?.includes(dir)) {
107
- process.env.PATH = `${dir}${path.delimiter}${process.env.PATH}`;
108
- }
109
- }
110
-
111
- export function ensurePaytacaOnPath(): string | null {
112
- // Priority 1: Local node_modules .bin dir
113
- try {
114
- const paytacaCliPkg = require.resolve('paytaca-cli/package.json');
115
- const binDir = path.resolve(path.dirname(paytacaCliPkg), '..', '.bin');
116
- const paytacaBin = path.join(binDir, 'paytaca');
117
- if (fs.existsSync(paytacaBin)) {
118
- addToPath(binDir);
119
- return binDir;
120
- }
121
- } catch {}
122
-
123
- // Priority 2: Global npm root .bin dir
124
- const globalRoot = getGlobalNpmRoot();
125
- if (globalRoot) {
126
- const globalBinDir = path.resolve(globalRoot, '..', '.bin');
127
- const globalPaytaca = path.join(globalBinDir, 'paytaca');
128
- if (fs.existsSync(globalPaytaca)) {
129
- addToPath(globalBinDir);
130
- return globalBinDir;
131
- }
132
- }
133
-
134
- // Priority 3: Common global bin directories
135
- const commonBinDirs = [
136
- '/usr/local/bin',
137
- '/usr/bin',
138
- path.join(os.homedir(), '.npm-global', 'bin'),
139
- process.env.NVM_BIN,
140
- ].filter((p): p is string => !!p);
141
-
142
- for (const binDir of commonBinDirs) {
143
- const paytacaBin = path.join(binDir, 'paytaca');
144
- if (fs.existsSync(paytacaBin)) {
145
- addToPath(binDir);
146
- return binDir;
147
- }
148
- }
149
-
150
- return null;
151
- }
152
-
153
- export async function checkPaytacaCli(): Promise<boolean> {
154
- try {
155
- await execAsync(`"${PAYTACA_CMD}" --version`);
156
- return true;
157
- } catch {
158
- return false;
159
- }
160
- }
161
-
162
- export async function createWallet(): Promise<WalletInfo> {
163
- try {
164
- // Generate a new wallet using paytaca CLI
165
- // This will create a wallet and display the mnemonic
166
- const { stdout, stderr } = await execAsync(`"${PAYTACA_CMD}" wallet create --json`);
167
-
168
- let output = stdout.toString();
169
- if (!output && stderr) {
170
- output = stderr.toString();
171
- }
172
-
173
- // Try to parse JSON output if available
174
- try {
175
- const jsonOutput = JSON.parse(output);
176
- if (jsonOutput.mnemonic) {
177
- // Show warning to user
178
- console.log('\\n⚠️ IMPORTANT: Your wallet has been created!');
179
- console.log('\\nSAVE THIS RECOVERY PHRASE SECURELY:');
180
- console.log('═'.repeat(60));
181
- console.log(jsonOutput.mnemonic);
182
- console.log('═'.repeat(60));
183
- console.log('\\nWithout this phrase, you CANNOT recover your funds if you');
184
- console.log('lose access to this device. Write it down and store it safely.\\n');
185
-
186
- return {
187
- exists: true,
188
- hash: jsonOutput.hash,
189
- address: jsonOutput.address,
190
- balance: '0 BCH',
191
- mnemonic: jsonOutput.mnemonic
192
- };
193
- }
194
- } catch {
195
- // Fall through to regex parsing
196
- }
197
-
198
- // Parse from text output
199
- const hashMatch = output.match(/Wallet hash:\s*(.+)/i);
200
- const addressMatch = output.match(/Address:\s*(.+)/i);
201
- const mnemonicMatch = output.match(/Recovery phrase[\s\S]*?([a-z]+(?:\s+[a-z]+){11,23})/i);
202
-
203
- if (mnemonicMatch) {
204
- console.log('\\n⚠️ IMPORTANT: Your wallet has been created!');
205
- console.log('\\nSAVE THIS RECOVERY PHRASE SECURELY:');
206
- console.log('═'.repeat(60));
207
- console.log(mnemonicMatch[1]);
208
- console.log('═'.repeat(60));
209
- console.log('\\nWithout this phrase, you CANNOT recover your funds if you');
210
- console.log('lose access to this device. Write it down and store it safely.\\n');
211
- }
212
-
213
- return {
214
- exists: true,
215
- hash: hashMatch ? hashMatch[1].trim() : undefined,
216
- address: addressMatch ? addressMatch[1].trim() : undefined,
217
- balance: '0 BCH',
218
- mnemonic: mnemonicMatch ? mnemonicMatch[1] : undefined
219
- };
220
- } catch (err: any) {
221
- console.error('Failed to create wallet:', err.message || err);
222
- return {
223
- exists: false
224
- };
225
- }
226
- }
227
-
228
- export async function ensureWallet(): Promise<WalletInfo> {
229
- // First check if wallet exists
230
- const existingWallet = await checkWallet();
231
- if (existingWallet.exists) {
232
- return existingWallet;
233
- }
234
-
235
- // No wallet found - create one automatically
236
- console.log('🔧 No Paytaca wallet found. Creating a new wallet...');
237
- const newWallet = await createWallet();
238
-
239
- if (!newWallet.exists) {
240
- throw new Error(`Failed to automatically create wallet. Please create one manually: "${PAYTACA_CMD}" wallet create`);
241
- }
242
-
243
- // Wait a moment for wallet to be fully initialized
244
- await new Promise(resolve => setTimeout(resolve, 1000));
245
-
246
- // Verify the wallet was created
247
- const verifyWallet = await checkWallet();
248
- if (!verifyWallet.exists) {
249
- throw new Error(`Wallet creation verification failed. Please check "${PAYTACA_CMD}" wallet status.`);
250
- }
251
-
252
- return verifyWallet;
253
- }
254
-
255
- export async function importWallet(mnemonic: string): Promise<WalletInfo> {
256
- try {
257
- // Import wallet using provided mnemonic
258
- const { stdout } = await execAsync(`echo "${mnemonic}" | "${PAYTACA_CMD}" wallet import --stdin`);
259
- const output = stdout.toString();
260
-
261
- const hashMatch = output.match(/Wallet hash:\s*(.+)/i);
262
- const addressMatch = output.match(/Address:\s*(.+)/i);
263
-
264
- return {
265
- exists: true,
266
- hash: hashMatch ? hashMatch[1].trim() : undefined,
267
- address: addressMatch ? addressMatch[1].trim() : undefined,
268
- balance: undefined
269
- };
270
- } catch (err: any) {
271
- console.error('Failed to import wallet:', err.message || err);
272
- return {
273
- exists: false
274
- };
275
- }
276
- }
277
-
278
- export function extractWalletHash(output: string): string | undefined {
279
- const match = output.match(/Wallet hash:\s*(.+)/i);
280
- return match ? match[1].trim() : undefined;
281
- }
282
-
283
- export async function getReceivingAddress(): Promise<string | null> {
284
- try {
285
- const { stdout } = await execAsync(`"${PAYTACA_CMD}" wallet info`);
286
- const output = stdout.toString();
287
- const addressMatch = output.match(/Address:\s*(.+)/i);
288
- return addressMatch ? addressMatch[1].trim() : null;
289
- } catch {
290
- return null;
291
- }
292
- }
293
-
294
- export async function getWalletBalance(): Promise<{ bch: number; sats: number } | null> {
295
- try {
296
- const { stdout } = await execAsync(`"${PAYTACA_CMD}" wallet info`);
297
- const output = stdout.toString();
298
- const match = output.match(/Balance:\s*([\d.]+)\s*BCH/i);
299
- if (match) {
300
- const bch = parseFloat(match[1]);
301
- return {
302
- bch,
303
- sats: Math.floor(bch * 100000000)
304
- };
305
- }
306
- return null;
307
- } catch {
308
- return null;
309
- }
310
- }
package/tsconfig.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "commonjs",
5
- "lib": ["ES2020"],
6
- "outDir": "./dist",
7
- "rootDir": "./src",
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "skipLibCheck": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "declaration": true,
13
- "declarationMap": true,
14
- "sourceMap": true,
15
- "resolveJsonModule": true
16
- },
17
- "include": ["src/**/*"],
18
- "exclude": ["node_modules", "dist"]
19
- }