-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
228 lines (187 loc) · 7.91 KB
/
index.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>fractal -UniSat钱包 批量 拆分/归集 UTXO 工具</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f5;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #fff;
border-radius: 8px;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
width: 600px;
}
h2 {
color: #4a90e2;
}
label {
font-weight: bold;
margin-top: 10px;
display: block;
}
input[type="text"], textarea {
width: 90%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background-color: #4a90e2;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #357ab8;
}
select {
padding: 10px;
border-radius: 4px;
}
p {
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<h2>Fractal - UniSat钱包 拆分 UTXO 工具</h2>
<!-- 连接钱包按钮 -->
<button id="connectWallet">连接 UniSat 钱包</button>
<p id="walletAddress"></p>
<!-- 输入批量拆分信息 -->
<label for="recipients">接收地址 (一行一个地址):</label>
<textarea id="recipients" placeholder="每行一个接收地址" rows="6"></textarea>
<label for="amount">每个地址的金额 (FB):</label>
<input type="text" id="amount" placeholder="输入金额">
<label for="gasfee">gas费:</label>
<input type="text" id="gasfee" placeholder="输入gas">
<button id="splitUtxo">拆分 UTXO 并发送</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@zhigang1992/[email protected]/bitcoinjs.min.js"></script>
<script src="https://unisat.io/api/unisat.js"></script>
<script src="https://mempool.space/mempool.js"></script>
<script type="module">
import { Buffer } from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
let walletAddress = '';
let confirmedBalance = 0;
let inputUtxo = [];
async function getFilteredUTXOs(btcaddress) {
try {
const response = await fetch("https://mempool.fractalbitcoin.io/api/address/" + btcaddress + "/utxo");
const data = await response.json();
// Sort by value in descending order
const sortedData = data.sort((a, b) => b.value - a.value);
// Filter out UTXOs with value less than 10000
const filteredData = sortedData.filter(utxo => utxo.value > 10000);
return filteredData ? filteredData[0] : [];
} catch (error) {
console.error('Error:', error);
return [];
}
}
// 连接 UniSat 钱包
document.getElementById('connectWallet').addEventListener('click', async () => {
try {
walletAddress = await window.unisat.requestAccounts();
document.getElementById('walletAddress').innerText = `钱包地址: ${walletAddress[0]}`;
$('#connectWallet').text("点击切换钱包");
unisat.getBalance().then(balance => {
confirmedBalance = balance.confirmed;
});
inputUtxo = await getFilteredUTXOs(walletAddress[0]);
} catch (err) {
console.error("钱包连接失败:", err);
}
});
// 拆分
document.getElementById('splitUtxo').addEventListener('click', async () => {
if (!inputUtxo) {
alert("没有可用余额");
return;
}
const recipientList = document.getElementById('recipients').value.trim().split("\n");
const amountStr = document.getElementById('amount').value.trim(); // 获取金额字符串
const gasfee = document.getElementById('gasfee').value.trim();
// 验证用户输入是否有效
if (recipientList.length === 0 || isNaN(amountStr) || parseFloat(amountStr) <= 0 || parseFloat(gasfee) <= 0) {
alert("请输入有效的接收地址,金额,脚本公钥或者Gas费有误");
return;
}
const amount = Math.round(parseFloat(amountStr) * 1e8); // 转换为聪并四舍五入
// 构建输出列表
const outputs = recipientList.map(address => ({
address: address.trim(),
value: amount
}));
try {
const psbt = new bitcoinjs.Psbt();
//添加所有 UTXO 作为输入
psbt.addInput({
hash: inputUtxo.txid,
index: inputUtxo.vout,
sequence: 0xFFFFFFFD, // 启用 RBF
witnessUtxo: {
script: Buffer.from(bitcoinjs.address.toOutputScript(walletAddress[0]).toString('hex'), 'hex'), //脚本公钥,在https://mempool.fractalbitcoin.io网站找
value: inputUtxo.value
}
});
const inputSizeP2TR = 57; // P2TR 输入大小
const outputSizeP2TR = 43; // P2TR 输出大小
const baseTransactionSize = 10; // 固定开销
let outUtxoNum = recipientList.length + 1;
const transactionSize = baseTransactionSize + (inputSizeP2TR) + (outputSizeP2TR * (outUtxoNum));
// 计算矿工费
const transactionFee = transactionSize * gasfee;
const ts_self = inputUtxo.value - transactionFee - (recipientList.length * amount);
if (ts_self < 0) {
alert("每个地址最大可转:" + Math.floor((inputUtxo.value - transactionFee) / (recipientList.length + 1)) / 1e8 );
return;
}
//计算剩余转给自己
if (ts_self >= 0) {
psbt.addOutput({
address: walletAddress[0], // 接收方地址
value: ts_self, // 输出金额(聪)
});
}
//逐个添加输出,添加调试信息
outputs.forEach((output, index) => {
psbt.addOutput({
address: output.address, // 接收方地址
value: output.value, // 输出金额(聪)
});
});
// 将 PSBT 转换为 Base64 格式
const psbtBase64 = psbt.toBase64();
// 将 PSBT 转换为 Hex
const psbtHex = Buffer.from(psbtBase64, 'base64').toString('hex');
//签名
const signedPsbtHex = await window.unisat.signPsbt(psbtHex);
//广播交易
let res = await window.unisat.pushPsbt(signedPsbtHex);
alert("成功");
} catch (err) {
console.error("失败:", err);
}
});
</script>
</body>
</html>