-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
246 lines (204 loc) · 7.79 KB
/
init.js
File metadata and controls
246 lines (204 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
/**
* git-mark-init - Initialize gitmark for a repository
*
* Usage:
* git mark-init [voucher-uri]
*
* If voucher-uri is provided, funds will be transferred from the voucher
* to the newly generated key. Change goes back to the voucher address.
*/
import fs from 'fs';
import os from 'os';
import path from 'path';
import { generateKeypair, getPublicKey } from './lib/keys.js';
import { setPrivateKey, setNetwork, isGitRepo, getPrivateKey } from './lib/config.js';
import { parseVoucher, createVoucher, loadVoucher, saveVoucher, INIT_AMOUNT, MIN_FEE } from './lib/voucher.js';
const args = process.argv.slice(2);
// Help
if (args.includes('--help') || args.includes('-h')) {
console.log(`
git-mark-init - Initialize gitmark for a repository
Usage:
git mark-init [options] [voucher-uri]
Arguments:
voucher TXO voucher URI or path to voucher file
Format: txo:<chain>:<txid>:<vout>?amount=<sats>&key=<key>
If a file path is given, the voucher is read from the file
and updated with the change output after use.
Options:
-h, --help Show this help message
-v, --version Show version number
--global Store key in global git config (default: local)
--force Overwrite existing key
Examples:
# Initialize with a voucher URI
git mark-init "txo:tbtc4:abc...def:0?amount=5000000&key=123...abc"
# Initialize from voucher file (file is updated with change)
git mark-init /path/to/faucet.voucher
# Just generate a key (no funding)
git mark-init
# Use global config
git mark-init --global
`);
process.exit(0);
}
// Version
if (args.includes('--version') || args.includes('-v')) {
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url)));
console.log(pkg.version);
process.exit(0);
}
// Parse flags
const useGlobal = args.includes('--global');
const force = args.includes('--force');
// Default faucet location
const DEFAULT_FAUCET = path.join(os.homedir(), '.gitmark', 'faucet.txt');
// Find voucher argument - can be a URI or a file path
const voucherArgRaw = args.find(a =>
!a.startsWith('-') &&
a !== '--global' &&
a !== '--force'
);
// Resolve voucher: if it's a file, read from it
let voucherArg = null;
let voucherFile = null;
if (voucherArgRaw) {
if (voucherArgRaw.startsWith('txo:') || voucherArgRaw.startsWith('urn:voucher:')) {
// Direct URI
voucherArg = voucherArgRaw;
} else if (fs.existsSync(voucherArgRaw)) {
// File path - read voucher from file
voucherFile = voucherArgRaw;
voucherArg = loadVoucher(voucherFile);
if (!voucherArg) {
console.error(`Error: Could not read voucher from file: ${voucherFile}`);
process.exit(1);
}
} else {
console.error(`Error: Invalid voucher. Must be a txo: URI or a file path.`);
process.exit(1);
}
} else if (fs.existsSync(DEFAULT_FAUCET)) {
// No voucher provided, but default faucet exists
voucherFile = DEFAULT_FAUCET;
voucherArg = loadVoucher(voucherFile);
if (voucherArg) {
console.log(`Using default faucet: ${DEFAULT_FAUCET}`);
}
}
async function main() {
console.log('=== git-mark-init ===\n');
// Check if in git repo (unless using global)
if (!useGlobal && !isGitRepo()) {
console.error('Error: Not in a git repository. Use --global for global config.');
process.exit(1);
}
// Check for existing key
const existingKey = getPrivateKey();
if (existingKey && !force) {
console.log('Existing key found. Use --force to overwrite.');
console.log(`Public key: ${getPublicKey(existingKey)}`);
process.exit(0);
}
// Generate new keypair
console.log('Generating new keypair...');
const { privateKey, publicKey } = generateKeypair();
console.log(`Public key: ${publicKey}`);
// Save to git config
const scope = useGlobal ? 'global' : 'local';
setPrivateKey(privateKey, useGlobal);
console.log(`Private key saved to ${scope} git config (nostr.privkey)`);
// If voucher provided, fund the new wallet
if (voucherArg) {
console.log('\nProcessing voucher...');
try {
const voucher = parseVoucher(voucherArg);
console.log(`Chain: ${voucher.chain}`);
console.log(`Voucher amount: ${voucher.amount} sats`);
// Calculate amounts
const userAmount = Math.min(INIT_AMOUNT, voucher.amount - MIN_FEE);
const changeAmount = voucher.amount - userAmount - MIN_FEE;
if (userAmount <= 0) {
throw new Error(`Voucher amount too low. Need at least ${INIT_AMOUNT + MIN_FEE} sats.`);
}
console.log(`\nTransaction plan:`);
console.log(` Input: ${voucher.amount} sats (from voucher)`);
console.log(` Output: ${userAmount} sats (to you)`);
if (changeAmount > 0) {
console.log(` Change: ${changeAmount} sats (back to voucher)`);
}
console.log(` Fee: ${MIN_FEE} sats`);
// Build transaction
const { buildTx } = await import('btctx');
const voucherPubkey = getPublicKey(voucher.privateKey);
const outputs = [{ pubkey: publicKey, amount: userAmount }];
// Add change output back to voucher address
if (changeAmount > 0) {
outputs.push({ pubkey: voucherPubkey, amount: changeAmount });
}
console.log('\nBuilding transaction...');
const { hex, txid } = await buildTx({
privateKey: voucher.privateKey,
publicKey: voucherPubkey,
txid: voucher.txid,
vout: voucher.vout,
inputAmount: voucher.amount,
outputs,
});
// Broadcast
console.log('Broadcasting transaction...');
const sendtx = (await import('sendtx')).default;
const broadcastTxid = await sendtx(hex, voucher.chain);
console.log(`Transaction broadcast: ${broadcastTxid}`);
// Create TXO file
const txoDir = '.well-known/txo';
const txoFile = path.join(txoDir, 'txo.json');
fs.mkdirSync(txoDir, { recursive: true });
const txoUri = `txo:${voucher.chain}:${broadcastTxid}:0?amount=${userAmount}&pubkey=${publicKey}`;
const txoData = [txoUri];
fs.writeFileSync(txoFile, JSON.stringify(txoData, null, 2));
console.log(`\nTXO file created: ${txoFile}`);
// Set network
setNetwork(voucher.chain, useGlobal);
// Update voucher with change output
if (changeAmount > 0) {
const newVoucher = createVoucher({
chain: voucher.chain,
txid: broadcastTxid,
vout: 1,
privateKey: voucher.privateKey,
amount: changeAmount,
});
// If loaded from file, save new voucher back to file
if (voucherFile) {
saveVoucher(newVoucher, voucherFile);
console.log(`\nVoucher file updated: ${voucherFile}`);
} else {
console.log(`\n--- New faucet voucher (share to replenish) ---`);
console.log(newVoucher);
}
} else if (voucherFile) {
// No change - voucher is exhausted
console.log(`\nWarning: Voucher exhausted (no change). File not updated.`);
}
console.log('\n=== Initialization complete! ===');
console.log(`\nYou can now use: git mark "your commit message"`);
} catch (error) {
console.error(`\nError processing voucher: ${error.message}`);
console.log('\nKey was saved. You can fund manually and create txo.json later.');
process.exit(1);
}
} else {
console.log('\nNo voucher provided. Key saved but wallet not funded.');
console.log('To fund manually:');
console.log(` 1. Send testnet4 coins to address derived from pubkey: ${publicKey}`);
console.log(' 2. Create .well-known/txo/txo.json with the TXO URI');
console.log('\nOr run again with a voucher:');
console.log(' git mark-init "txo:tbtc4:<txid>:<vout>?amount=<sats>&key=<key>"');
}
}
main().catch(error => {
console.error(`Fatal error: ${error.message}`);
process.exit(1);
});