Java解析比特币钱包交易记录
可以通过比特币钱包命令导出相关的交易记录,如果你想统计某个地址或者某个用户的交易时,可以解析该文件。
技术很简单,因为导出文件为JSON格式,这里通过alibaba的fastjson来解析。
首先,通过命令导出最近两条交易记录:
1 |
. /cli listtransactions '*' 10 0 >>txid.txt
|
将导出的内容写入txid.txt文件,然后下载该文件
单条交易记录:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
{ "account": "[email protected]", "address": "1MNirm9f72WTHCgjox4WgtW2tvdBhEPCgG", "category": "receive", "amount": 9.00000000, "label": "[email protected]", "vout": 0, "confirmations": 883, "blockhash": "000000000000000001762f8a98297a5bfed2dd543fc0452673bd9c37b79f887c", "blockindex": 633, "blocktime": 1496374952, "txid": "6d6d17d968da1c2deb28e365501a051049d0dc1e61ef7efc176ffbe2f28cec70", "walletconflicts": [ ], "time": 1496374331, "timereceived": 1496374331, "bip125-replaceable": "no" } |
account,label保护隐私这里屏蔽
address交易的地址
category交易类型,receive为收,send为发
amount交易金额
confirmations区块链确认数
blockhash矿机计算出的hash
blockindex在钱包中的索引
blocktime时间
txid交易凭据
time交易时间
timereceived钱包接收时间
可以根据自己的需要进行解析、判断、统计,示例:
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
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class AnalysisBtcTxid {
public static String readTxtFile(String filePath) {
StringBuffer sb = new StringBuffer( "" );
try {
String encoding = "GBK" ;
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding); // 考虑到编码格�?
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null ;
while ((lineTxt = bufferedReader.readLine()) != null ) {
sb.append(lineTxt);
}
read.close();
} else {
System.out.println( "找不到指定的文件" );
}
} catch (Exception e) {
System.out.println( "读取文件内容出错" );
e.printStackTrace();
}
return sb.toString();
}
@SuppressWarnings ({ "rawtypes" , "unchecked" })
public static void main(String argv[]) {
// 读取文件,一行一行读取即可
String filePath = "D:\\txid.txt" ;
String json = readTxtFile(filePath);
List<Map> txids = JSON.parseObject(json, List. class );
System.out.println( "共计:" + txids.size() + " 交易记录" );
BigDecimal succ = new BigDecimal( "0.00000000" );
BigDecimal err = new BigDecimal( "0.00000000" );
BigDecimal noconfirm = new BigDecimal( "0.00000000" );
int index = 0 ;
for (Map m : txids) {
try {
String category = m.get( "category" ).toString();
String address = m.get( "address" ).toString();
if ( "receive" .equals(category) && "1MNirm9f72WTHCgjox4WgtW2tvdBhEPCgG" .equals(address)) {
index++;
String amount = m.get( "amount" ).toString();
String confirmations = m.get( "confirmations" ).toString();
if ( "0" .equals(confirmations)) {
noconfirm = noconfirm.add( new BigDecimal(amount));
System.out.println( "TXID\t" + m.get( "txid" ) + "\t" + m.get( "amount" ) + "\t确认中" );
} else if ( "-1" .equals(confirmations)) {
err = err.add( new BigDecimal(amount));
System.out.println( "TXID\t" + m.get( "txid" ) + "\t" + m.get( "amount" ) + "\t失败" );
} else {
succ = succ.add( new BigDecimal(amount));
System.out.println( "TXID\t" + m.get( "txid" ) + "\t" + m.get( "amount" ) + "\t成功" );
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println( "有效交易记录:" + index);
System.out.println( "未确认:" + noconfirm.toBigInteger());
System.out.println( "错误:" + err.toBigInteger());
System.out.println( "成功:" + succ.toBigInteger());
}
} |
刚打出的币,确认数为0,节点确认后,确认数开始增加,-1代表失败。
大家可以进群一起探讨学习技术,Java技术交流 582482022