This repository was archived by the owner on Aug 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
225 lines (175 loc) · 5.17 KB
/
Copy pathfunctions.js
File metadata and controls
225 lines (175 loc) · 5.17 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
import {
ethers,
parseEther,
keccak256,
Transaction,
} from "https://esm.sh/ethers@latest";
import { web3FromSource } from "https://esm.sh/@polkadot/extension-dapp@latest";
import bitcoin from "https://esm.sh/bitcoinjs-lib@latest";
export async function buildTx(from, to, value) {
const provider = new ethers.JsonRpcProvider(
"https://ethereum-sepolia-rpc.publicnode.com"
);
const transactionObject = {
to,
value: parseEther(value),
};
const unsignedTx = Transaction.from(transactionObject).unsignedSerialized;
const unsignedTxHash = keccak256(unsignedTx);
return unsignedTxHash;
}
export async function signPayloadPls(source, payload) {
const injector = await web3FromSource(source);
const signer = injector.signer;
const { signature } = await signer.signPayload(payload);
return signature;
}
async function fetchUTXOs(address) {
try {
const response = await fetch(
`https://blockstream.info/testnet/api/address/${address}/utxo`
);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Error fetching UTXOs:", error);
return [];
}
}
export async function buildUnsignedTransaction(from, to, amount) {
// try {
//const utxos = await fetchUTXOs(from);
// const psbt = new bitcoin.Psbt({ network: bitcoin.networks.testnet });
//console.log("Fetched UTXOs:", utxos);
// utxos.forEach((utxo) => {
// try {
// const hashBytes = hexToUint8Array(utxo.txid).reverse();
// psbt.addInput({
// hash: utxo.txid,
// index: utxo.vout,
// value: parseInt(utxo.value),
// });
// } catch (error) {
// console.error("Error adding input:", error);
// }
// });
// console.log("amount: ", amount);
// psbt.addOutput({
// address: to,
// value: parseInt(amount),
// });
// psbt.finalizeAllInputs();
// const unsignedTx = psbt.extractTransaction();
// return unsignedTx;
// } catch (error) {
// console.error("Error building transaction:", error);
// }
const tx = await createTransaction(from, to, parseInt(amount));
return tx;
}
export function hashFromUnsignedTx(unsignedTx) {
return unsignedTx.tosign[0];
}
// export async function getBitcoinBalance(address) {
// try {
// const response = await fetch(
// `https://blockstream.info/testnet/api/address/${address}/utxo`
// );
// if (!response.ok) {
// throw new Error(`HTTP error! Status: ${response.status}`);
// }
// const utxos = await response.json();
// // Calculate total balance from UTXOs
// let totalBalance = 0;
// utxos.forEach((utxo) => {
// totalBalance += utxo.value;
// });
// console.log(
// `Bitcoin address ${address} balance: ${totalBalance / 1e8} BTC`
// );
// return totalBalance / 1e8; // Convert satoshis to BTC
// } catch (error) {
// console.error("Error fetching balance:", error);
// return 0;
// }
// }
export async function getBitcoinBalance(address) {
try {
const response = await fetch(
`https://api.blockcypher.com/v1/bcy/test/addrs/${address}/balance`
);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
const balance = data.balance;
console.log(`Bitcoin address ${address} balance: ${balance / 1e8} BTC`);
return balance / 1e8; // Convert satoshis to BTC
} catch (error) {
console.error("Error fetching balance:", error);
return 0;
}
}
async function createTransaction(from, to, amount) {
try {
const url = "https://api.blockcypher.com/v1/bcy/test/txs/new";
const requestBody = {
inputs: [
{
addresses: [from],
},
],
outputs: [
{
addresses: [to],
value: amount, // amount in satoshis
},
],
};
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error("Failed to create transaction");
}
const responseData = await response.json();
return responseData;
} catch (error) {
console.error("Error creating transaction:", error);
}
}
async function submitTransaction(tx) {
try {
const url =
"https://api.blockcypher.com/v1/bcy/test/txs/send?token=065484743ce942a89148803e195b1ae2"; // Testnet endpoint
const requestBody = tx;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error("Failed to create transaction");
}
const responseData = await response.json();
return responseData;
} catch (error) {
console.error("Error creating transaction:", error);
}
}
export async function fillTxAndSubmit(unsignedTx, signature, pubkey) {
let tx = unsignedTx;
tx.signatures = [signature];
tx.pubkeys = [pubkey];
const res = await submitTransaction(tx);
console.log(res);
return res.tx.hash;
}