@@ -143,18 +143,18 @@ public BlockchainIterator getBlockchainIterator() {
143
143
/**
144
144
* 查找钱包地址对应的所有UTXO
145
145
*
146
- * @param address 钱包地址
146
+ * @param pubKeyHash 钱包公钥Hash
147
147
* @return
148
148
*/
149
- public TXOutput [] findUTXO (String address ) throws Exception {
150
- Transaction [] unspentTxs = this .findUnspentTransactions (address );
149
+ public TXOutput [] findUTXO (byte [] pubKeyHash ) throws Exception {
150
+ Transaction [] unspentTxs = this .findUnspentTransactions (pubKeyHash );
151
151
TXOutput [] utxos = {};
152
152
if (unspentTxs == null || unspentTxs .length == 0 ) {
153
153
return utxos ;
154
154
}
155
155
for (Transaction tx : unspentTxs ) {
156
156
for (TXOutput txOutput : tx .getOutputs ()) {
157
- if (txOutput .canBeUnlockedWith ( address )) {
157
+ if (txOutput .isLockedWithKey ( pubKeyHash )) {
158
158
utxos = ArrayUtils .add (utxos , txOutput );
159
159
}
160
160
}
@@ -166,11 +166,11 @@ public TXOutput[] findUTXO(String address) throws Exception {
166
166
/**
167
167
* 查找钱包地址对应的所有未花费的交易
168
168
*
169
- * @param address 钱包地址
169
+ * @param pubKeyHash 钱包公钥Hash
170
170
* @return
171
171
*/
172
- private Transaction [] findUnspentTransactions (String address ) throws Exception {
173
- Map <String , int []> allSpentTXOs = this .getAllSpentTXOs (address );
172
+ private Transaction [] findUnspentTransactions (byte [] pubKeyHash ) throws Exception {
173
+ Map <String , int []> allSpentTXOs = this .getAllSpentTXOs (pubKeyHash );
174
174
Transaction [] unspentTxs = {};
175
175
176
176
// 再次遍历所有区块中的交易输出
@@ -188,7 +188,7 @@ private Transaction[] findUnspentTransactions(String address) throws Exception {
188
188
}
189
189
190
190
// 保存不存在 allSpentTXOs 中的交易
191
- if (transaction .getOutputs ()[outIndex ].canBeUnlockedWith ( address )) {
191
+ if (transaction .getOutputs ()[outIndex ].isLockedWithKey ( pubKeyHash )) {
192
192
unspentTxs = ArrayUtils .add (unspentTxs , transaction );
193
193
}
194
194
}
@@ -201,11 +201,11 @@ private Transaction[] findUnspentTransactions(String address) throws Exception {
201
201
/**
202
202
* 从交易输入中查询区块链中所有已被花费了的交易输出
203
203
*
204
- * @param address 钱包地址
204
+ * @param pubKeyHash 钱包公钥Hash
205
205
* @return 交易ID以及对应的交易输出下标地址
206
206
* @throws Exception
207
207
*/
208
- private Map <String , int []> getAllSpentTXOs (String address ) throws Exception {
208
+ private Map <String , int []> getAllSpentTXOs (byte [] pubKeyHash ) throws Exception {
209
209
// 定义TxId ——> spentOutIndex[],存储交易ID与已被花费的交易输出数组索引值
210
210
Map <String , int []> spentTXOs = new HashMap <>();
211
211
for (BlockchainIterator blockchainIterator = this .getBlockchainIterator (); blockchainIterator .hashNext (); ) {
@@ -217,7 +217,7 @@ private Map<String, int[]> getAllSpentTXOs(String address) throws Exception {
217
217
continue ;
218
218
}
219
219
for (TXInput txInput : transaction .getInputs ()) {
220
- if (txInput .canUnlockOutputWith ( address )) {
220
+ if (txInput .usesKey ( pubKeyHash )) {
221
221
String inTxId = Hex .encodeHexString (txInput .getTxId ());
222
222
int [] spentOutIndexArray = spentTXOs .get (inTxId );
223
223
if (spentOutIndexArray == null ) {
@@ -237,11 +237,11 @@ private Map<String, int[]> getAllSpentTXOs(String address) throws Exception {
237
237
/**
238
238
* 寻找能够花费的交易
239
239
*
240
- * @param address 钱包地址
241
- * @param amount 花费金额
240
+ * @param pubKeyHash 钱包公钥Hash
241
+ * @param amount 花费金额
242
242
*/
243
- public SpendableOutputResult findSpendableOutputs (String address , int amount ) throws Exception {
244
- Transaction [] unspentTXs = this .findUnspentTransactions (address );
243
+ public SpendableOutputResult findSpendableOutputs (byte [] pubKeyHash , int amount ) throws Exception {
244
+ Transaction [] unspentTXs = this .findUnspentTransactions (pubKeyHash );
245
245
int accumulated = 0 ;
246
246
Map <String , int []> unspentOuts = new HashMap <>();
247
247
for (Transaction tx : unspentTXs ) {
@@ -252,7 +252,7 @@ public SpendableOutputResult findSpendableOutputs(String address, int amount) th
252
252
253
253
TXOutput txOutput = tx .getOutputs ()[outId ];
254
254
255
- if (txOutput .canBeUnlockedWith ( address ) && accumulated < amount ) {
255
+ if (txOutput .isLockedWithKey ( pubKeyHash ) && accumulated < amount ) {
256
256
accumulated += txOutput .getValue ();
257
257
258
258
int [] outIds = unspentOuts .get (txId );
0 commit comments