litecore-lib-ltc 1.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.
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "litecore-lib-ltc",
3
+ "version": "1.0.0",
4
+ "main": "src/index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": ["litecoin", "crypto", "blockchain"],
9
+ "license": "MIT",
10
+ "description": "Litecoin utility functions for transaction creation, signing, and broadcasting.",
11
+ "dependencies": {
12
+ "axios": "^1.13.6",
13
+ "bitcoinjs-lib": "^7.0.1",
14
+ "ecpair": "^3.0.1",
15
+ "tiny-secp256k1": "^2.2.4"
16
+ },
17
+ "files": ["src/"]
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,76 @@
1
+ // src/index.js
2
+ const bitcoin = require('bitcoinjs-lib');
3
+ const { ECPairFactory } = require('ecpair');
4
+ const ecc = require('tiny-secp256k1');
5
+ const axios = require('axios');
6
+ const LITECOIN = require('./network');
7
+
8
+ const ECPair = ECPairFactory(ecc);
9
+
10
+ /**
11
+ * Build and broadcast a Litecoin transaction
12
+ * @param {string} wif - Sender's WIF private key
13
+ * @param {string} toAddress - Recipient's LTC address
14
+ * @param {number} amountSatoshis - Amount in litoshis (1 LTC = 1e8)
15
+ * @param {number} feeSatoshis - Fee in litoshis
16
+ */
17
+ async function sendLitecoin(wif, toAddress, amountSatoshis, feeSatoshis = 10000) {
18
+ const keyPair = ECPair.fromWIF(wif, LITECOIN);
19
+ const { address: fromAddress } = bitcoin.payments.p2pkh({
20
+ pubkey: keyPair.publicKey,
21
+ network: LITECOIN,
22
+ });
23
+
24
+ // 1. Fetch UTXOs (using Blockcypher as example)
25
+ const utxoRes = await axios.get(
26
+ `https://api.blockcypher.com/v1/ltc/main/addrs/${fromAddress}?unspentOnly=true`
27
+ );
28
+ const utxos = utxoRes.data.txrefs || [];
29
+
30
+ if (!utxos.length) throw new Error('No UTXOs available');
31
+
32
+ // 2. Build transaction
33
+ const psbt = new bitcoin.Psbt({ network: LITECOIN });
34
+ let inputTotal = 0;
35
+
36
+ for (const utxo of utxos) {
37
+ // Fetch raw tx hex for each input
38
+ const txRes = await axios.get(
39
+ `https://api.blockcypher.com/v1/ltc/main/txs/${utxo.tx_hash}?includeHex=true`
40
+ );
41
+ psbt.addInput({
42
+ hash: utxo.tx_hash,
43
+ index: utxo.tx_output_n,
44
+ nonWitnessUtxo: Buffer.from(txRes.data.hex, 'hex'),
45
+ });
46
+ inputTotal += utxo.value;
47
+ if (inputTotal >= amountSatoshis + feeSatoshis) break;
48
+ }
49
+
50
+ if (inputTotal < amountSatoshis + feeSatoshis) {
51
+ throw new Error('Insufficient funds');
52
+ }
53
+
54
+ // 3. Add outputs
55
+ psbt.addOutput({ address: toAddress, value: amountSatoshis });
56
+
57
+ const change = inputTotal - amountSatoshis - feeSatoshis;
58
+ if (change > 0) {
59
+ psbt.addOutput({ address: fromAddress, value: change });
60
+ }
61
+
62
+ // 4. Sign and finalize
63
+ psbt.signAllInputs(keyPair);
64
+ psbt.finalizeAllInputs();
65
+ const rawTx = psbt.extractTransaction().toHex();
66
+
67
+ // 5. Broadcast
68
+ const broadcastRes = await axios.post(
69
+ 'https://api.blockcypher.com/v1/ltc/main/txs/push',
70
+ { tx: rawTx }
71
+ );
72
+
73
+ return broadcastRes.data.tx.hash;
74
+ }
75
+
76
+ module.exports = { sendLitecoin };
package/src/network.js ADDED
@@ -0,0 +1,14 @@
1
+ // src/network.js
2
+ const LITECOIN = {
3
+ messagePrefix: '\x19Litecoin Signed Message:\n',
4
+ bech32: 'ltc',
5
+ bip32: {
6
+ public: 0x019da462,
7
+ private: 0x019d9cfe,
8
+ },
9
+ pubKeyHash: 0x30, // L addresses
10
+ scriptHash: 0x32, // M addresses
11
+ wif: 0xb0,
12
+ };
13
+
14
+ module.exports = LITECOIN;