@paytaca/opencode-plugin 0.1.6 → 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,296 +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`);
167
-
168
- let output = stdout.toString();
169
- if (!output && stderr) {
170
- output = stderr.toString();
171
- }
172
-
173
- // Parse from text output
174
- const hashMatch = output.match(/Wallet hash:\s*(.+)/i);
175
- const addressMatch = output.match(/Address:\s*(.+)/i);
176
-
177
- // Extract mnemonic from numbered seed phrase list
178
- const phraseSection = output.match(/Seed phrase:\s*\n([\s\S]*?)(?=\n\s*Wallet hash)/i);
179
- let mnemonic = undefined;
180
- if (phraseSection) {
181
- const words = [];
182
- for (const line of phraseSection[1].trim().split('\n')) {
183
- const m = line.match(/^\s*\d+\.\s+(\w+)/);
184
- if (m) words.push(m[1]);
185
- }
186
- if (words.length >= 12) mnemonic = words.join(' ');
187
- }
188
-
189
- if (mnemonic) {
190
- console.log('\\n⚠️ IMPORTANT: Your wallet has been created!');
191
- console.log('\\nSAVE THIS RECOVERY PHRASE SECURELY:');
192
- console.log('═'.repeat(60));
193
- console.log(mnemonic);
194
- console.log('═'.repeat(60));
195
- console.log('\\nWithout this phrase, you CANNOT recover your funds if you');
196
- console.log('lose access to this device. Write it down and store it safely.\\n');
197
- }
198
-
199
- return {
200
- exists: true,
201
- hash: hashMatch ? hashMatch[1].trim() : undefined,
202
- address: addressMatch ? addressMatch[1].trim() : undefined,
203
- balance: '0 BCH',
204
- mnemonic
205
- };
206
- } catch (err: any) {
207
- console.error('Failed to create wallet:', err.message || err);
208
- return {
209
- exists: false
210
- };
211
- }
212
- }
213
-
214
- export async function ensureWallet(): Promise<WalletInfo> {
215
- // First check if wallet exists
216
- const existingWallet = await checkWallet();
217
- if (existingWallet.exists) {
218
- return existingWallet;
219
- }
220
-
221
- // No wallet found - create one automatically
222
- console.log('🔧 No Paytaca wallet found. Creating a new wallet...');
223
- const newWallet = await createWallet();
224
-
225
- if (!newWallet.exists) {
226
- throw new Error(`Failed to automatically create wallet. Please create one manually: "${PAYTACA_CMD}" wallet create`);
227
- }
228
-
229
- // Wait a moment for wallet to be fully initialized
230
- await new Promise(resolve => setTimeout(resolve, 1000));
231
-
232
- // Verify the wallet was created
233
- const verifyWallet = await checkWallet();
234
- if (!verifyWallet.exists) {
235
- throw new Error(`Wallet creation verification failed. Please check "${PAYTACA_CMD}" wallet status.`);
236
- }
237
-
238
- return verifyWallet;
239
- }
240
-
241
- export async function importWallet(mnemonic: string): Promise<WalletInfo> {
242
- try {
243
- // Import wallet using provided mnemonic
244
- const { stdout } = await execAsync(`echo "${mnemonic}" | "${PAYTACA_CMD}" wallet import --stdin`);
245
- const output = stdout.toString();
246
-
247
- const hashMatch = output.match(/Wallet hash:\s*(.+)/i);
248
- const addressMatch = output.match(/Address:\s*(.+)/i);
249
-
250
- return {
251
- exists: true,
252
- hash: hashMatch ? hashMatch[1].trim() : undefined,
253
- address: addressMatch ? addressMatch[1].trim() : undefined,
254
- balance: undefined
255
- };
256
- } catch (err: any) {
257
- console.error('Failed to import wallet:', err.message || err);
258
- return {
259
- exists: false
260
- };
261
- }
262
- }
263
-
264
- export function extractWalletHash(output: string): string | undefined {
265
- const match = output.match(/Wallet hash:\s*(.+)/i);
266
- return match ? match[1].trim() : undefined;
267
- }
268
-
269
- export async function getReceivingAddress(): Promise<string | null> {
270
- try {
271
- const { stdout } = await execAsync(`"${PAYTACA_CMD}" wallet info`);
272
- const output = stdout.toString();
273
- const addressMatch = output.match(/Address:\s*(.+)/i);
274
- return addressMatch ? addressMatch[1].trim() : null;
275
- } catch {
276
- return null;
277
- }
278
- }
279
-
280
- export async function getWalletBalance(): Promise<{ bch: number; sats: number } | null> {
281
- try {
282
- const { stdout } = await execAsync(`"${PAYTACA_CMD}" wallet info`);
283
- const output = stdout.toString();
284
- const match = output.match(/Balance:\s*([\d.]+)\s*BCH/i);
285
- if (match) {
286
- const bch = parseFloat(match[1]);
287
- return {
288
- bch,
289
- sats: Math.floor(bch * 100000000)
290
- };
291
- }
292
- return null;
293
- } catch {
294
- return null;
295
- }
296
- }
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
- }