Skip to content

Commit 6863e2d

Browse files
author
BitgetLimited
committed
merkelTree validate
1 parent 257a716 commit 6863e2d

18 files changed

+3664
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
/src/test/

images/faild.png

92.5 KB
Loading

images/success.png

91.8 KB
Loading

images/流程图.jpg

105 KB
Loading

merkel_tree_bg.json

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"path": [
3+
{
4+
"auditId": "Au20221129",
5+
"balances": {
6+
"BTC": 0,
7+
"ETH": 0,
8+
"USDT": 7681.73476302
9+
},
10+
"encryptUid": "e1117ce2af7d7fbf4fe7a77cc9515e92f6cf11f59ab803cb749d64c426342f6d",
11+
"level": 11,
12+
"merkelLeaf": "64ced402edefa4da",
13+
"nonce": "5j01abmm722ak2wvlq75bntkz18idvfimwwhaypt66bjn0ptne81160et3ajv1z3",
14+
"role": 2
15+
},
16+
{
17+
"auditId": "Au20221129",
18+
"balances": {
19+
"BTC": 1000000.0001998,
20+
"ETH": 300000,
21+
"USDT": 1299991.9972
22+
},
23+
"level": 10,
24+
"merkelLeaf": "87bd27437fbf59c2",
25+
"role": 1
26+
},
27+
{
28+
"auditId": "Au20221129",
29+
"balances": {
30+
"BTC": 0,
31+
"ETH": 0,
32+
"USDT": 53051.10598709
33+
},
34+
"level": 9,
35+
"merkelLeaf": "d61c58070ee1dc12",
36+
"role": 2
37+
},
38+
{
39+
"auditId": "Au20221129",
40+
"balances": {
41+
"BTC": 11.98852944,
42+
"ETH": 157.31252,
43+
"USDT": 1099754333.25209158
44+
},
45+
"level": 8,
46+
"merkelLeaf": "81cc6c9125fa98cb",
47+
"role": 2
48+
},
49+
{
50+
"auditId": "Au20221129",
51+
"balances": {
52+
"BTC": 715.09237636,
53+
"ETH": 2100.0404595,
54+
"USDT": 2020155332.95708230
55+
},
56+
"level": 7,
57+
"merkelLeaf": "4ccb436f7883fe91",
58+
"role": 1
59+
},
60+
{
61+
"auditId": "Au20221129",
62+
"balances": {
63+
"BTC": 61400096.00294400,
64+
"ETH": 433168.81989999,
65+
"USDT": 1016785013.13079698
66+
},
67+
"level": 6,
68+
"merkelLeaf": "795423500e6d597d",
69+
"role": 1
70+
},
71+
{
72+
"auditId": "Au20221129",
73+
"balances": {
74+
"BTC": 286595751.08265201,
75+
"ETH": 26727776.90857214,
76+
"USDT": 2453648994.37423942
77+
},
78+
"level": 5,
79+
"merkelLeaf": "6050ca3d4e946783",
80+
"role": 2
81+
},
82+
{
83+
"auditId": "Au20221129",
84+
"balances": {
85+
"BTC": 8508725002.69311378,
86+
"ETH": 2146389444.08493589,
87+
"USDT": 15787098930.60614156
88+
},
89+
"level": 4,
90+
"merkelLeaf": "8ad038df7e82fce6",
91+
"role": 2
92+
},
93+
{
94+
"auditId": "Au20221129",
95+
"balances": {
96+
"BTC": 9944837661.61102144,
97+
"ETH": 10112560970.15270098,
98+
"USDT": 63861352068.33045056
99+
},
100+
"level": 3,
101+
"merkelLeaf": "e33150a464c294ed",
102+
"role": 2
103+
},
104+
{
105+
"auditId": "Au20221129",
106+
"balances": {
107+
"BTC": 420217093.46290786,
108+
"ETH": 221002355.98534612,
109+
"USDT": 6649023671.61392569
110+
},
111+
"level": 2,
112+
"merkelLeaf": "864f125992c7bae9",
113+
"role": 2
114+
},
115+
{
116+
"auditId": "Au20221129",
117+
"balances": {
118+
"BTC": 19222776331.93374469,
119+
"ETH": 12507420973.30443462,
120+
"USDT": 92889184629.59267820
121+
},
122+
"level": 1,
123+
"merkelLeaf": "ffedeaf82363b23c",
124+
"role": 3
125+
}
126+
],
127+
"self": {
128+
"auditId": "Au20221129",
129+
"balances": {
130+
"BTC": 0,
131+
"ETH": 5000,
132+
"USDT": 5560.49
133+
},
134+
"encryptUid": "b3e887893212aa3faea8923b3b4c8589131895e7f468d394c4c508e31e3c85bf",
135+
"level": 11,
136+
"merkelLeaf": "1fa8ef498dc505f6",
137+
"nonce": "wx9bzgdojl0hoz7ckgft0k1cdyly7qohglaphsmqkx0hv80hjlwf0igvnamdqbh7",
138+
"role": 1
139+
}
140+
}

pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.upex</groupId>
6+
<artifactId>proof-of-reserves</artifactId>
7+
<version>1.0.0</version>
8+
9+
<packaging>jar</packaging>
10+
11+
<name>proof-of-reserves</name>
12+
<url>https://www.bitget.com</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- 引用commons-lang3 -->
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-lang3</artifactId>
23+
<version>3.9</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>commons-collections</groupId>
28+
<artifactId>commons-collections</artifactId>
29+
<version>3.2.2</version>
30+
</dependency>
31+
32+
<!-- 引用fastjson -->
33+
<dependency>
34+
<groupId>com.alibaba</groupId>
35+
<artifactId>fastjson</artifactId>
36+
<version>1.2.83</version>
37+
</dependency>
38+
39+
</dependencies>
40+
41+
<build>
42+
<finalName>proof-of-reserves</finalName>
43+
<plugins>
44+
<plugin>
45+
<artifactId>maven-assembly-plugin</artifactId>
46+
<configuration>
47+
<appendAssemblyId>false</appendAssemblyId>
48+
<descriptorRefs>
49+
<descriptorRef>jar-with-dependencies</descriptorRef>
50+
</descriptorRefs>
51+
<archive>
52+
<manifest>
53+
<mainClass>com.upex.BgMerkleValidator</mainClass>
54+
</manifest>
55+
</archive>
56+
</configuration>
57+
<executions>
58+
<execution>
59+
<id>make-assembly</id>
60+
<phase>package</phase>
61+
<goals>
62+
<goal>assembly</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-compiler-plugin</artifactId>
70+
<configuration>
71+
<source>11</source>
72+
<target>11</target>
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.upex;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.upex.model.MerkleProof;
5+
import com.upex.util.CollectionUtils;
6+
import com.upex.util.StringUtils;
7+
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
13+
/**
14+
* 验证账户资产是否包含在BitGet发布的Merkle树中
15+
*
16+
* @author BitgetLimited
17+
*/
18+
public class BgMerkleValidator {
19+
/**
20+
* 定义文件路径:merkel_tree_bg.json
21+
**/
22+
private static final String MERKLE_TREE_BG_FILE_PATH = "merkel_tree_bg.json";
23+
24+
public static void main(String[] args) {
25+
System.out.println("Merkel tree path validation start");
26+
// 获取"merkel_tree_bg.json"文件内容
27+
String merkleJsonFile = getMerkleJsonFile();
28+
if (StringUtils.isBlank(merkleJsonFile)) {
29+
System.out.println("Merkel tree path validation failed, invalid merkle proof file");
30+
return;
31+
}
32+
33+
// 获得默克尔树证明对象
34+
MerkleProof merkleProof = JSONObject.parseObject(merkleJsonFile, MerkleProof.class);
35+
36+
// 默克尔树参数验证
37+
if(validate(merkleProof)){
38+
System.out.println("Consistent with the Merkle tree root hash. The verification succeeds");
39+
}else {
40+
System.out.println("Inconsistent with the Merkle tree root hash. The verification fails");
41+
}
42+
}
43+
44+
/**
45+
* 参数校验
46+
* @param merkleProof
47+
* @return
48+
* @author BitgetLimited
49+
* @date 2022/11/25 20:29
50+
*/
51+
private static boolean validate(MerkleProof merkleProof){
52+
53+
// self节点不能为空 并且 path节点也不能为空
54+
if(merkleProof.getSelf() == null || CollectionUtils.isEmpty(merkleProof.getPath())){
55+
return false;
56+
}
57+
58+
// 验证self数据一致性
59+
if(!merkleProof.getSelf().validateSelf()){
60+
return false;
61+
}
62+
63+
// 验证path参数验证
64+
if(!merkleProof.getPath().get(0).validatePath()){
65+
return false;
66+
}
67+
68+
if(merkleProof.getPath().get(0).getRole().intValue() == merkleProof.getSelf().getRole().intValue()){
69+
return false;
70+
}
71+
72+
return merkleProof.validate();
73+
}
74+
75+
76+
/**
77+
* 获取merkel_tree_bg.json文件内容
78+
*
79+
* @author BitgetLimited
80+
* @date 2022/11/25 16:53
81+
*/
82+
private static String getMerkleJsonFile() {
83+
StringBuilder builder = new StringBuilder();
84+
try {
85+
Files.readAllLines(Path.of(MERKLE_TREE_BG_FILE_PATH), StandardCharsets.UTF_8).forEach(builder::append);
86+
return builder.toString();
87+
} catch (IOException e) {
88+
throw new RuntimeException(MERKLE_TREE_BG_FILE_PATH + " file does not exist");
89+
}
90+
}
91+
}
92+
93+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.upex.constants;
2+
3+
/**
4+
* 默克尔树常量
5+
* @author BitgetLimited
6+
* @date 2022/11/25 23:03
7+
*/
8+
public class MerkelTreeConstants {
9+
10+
/**
11+
* 逗号
12+
*/
13+
public static final String COMMA = ",";
14+
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.upex.constants;
2+
3+
/**
4+
* @author BitgetLimited
5+
* @date 2020-11-18 16:14
6+
* @desc
7+
**/
8+
public class TreeNodeRoleConstants {
9+
/**
10+
* 空节点
11+
*/
12+
public static final Integer EMPTY_NODE = 0;
13+
14+
/**
15+
* 左节点
16+
*/
17+
public static final Integer LEFT_NODE = 1;
18+
19+
/**
20+
* 右节点
21+
*/
22+
public static final Integer RIGHT_NODE = 2;
23+
24+
/**
25+
* 根节点
26+
*/
27+
public static final Integer ROOT_NODE = 3;
28+
29+
}

0 commit comments

Comments
 (0)