Skip to content

Commit

Permalink
merkelTree validate
Browse files Browse the repository at this point in the history
  • Loading branch information
BitgetLimited committed Dec 2, 2022
1 parent 257a716 commit 6863e2d
Show file tree
Hide file tree
Showing 18 changed files with 3,664 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
/src/test/
Binary file added images/faild.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/流程图.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions merkel_tree_bg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"path": [
{
"auditId": "Au20221129",
"balances": {
"BTC": 0,
"ETH": 0,
"USDT": 7681.73476302
},
"encryptUid": "e1117ce2af7d7fbf4fe7a77cc9515e92f6cf11f59ab803cb749d64c426342f6d",
"level": 11,
"merkelLeaf": "64ced402edefa4da",
"nonce": "5j01abmm722ak2wvlq75bntkz18idvfimwwhaypt66bjn0ptne81160et3ajv1z3",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 1000000.0001998,
"ETH": 300000,
"USDT": 1299991.9972
},
"level": 10,
"merkelLeaf": "87bd27437fbf59c2",
"role": 1
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 0,
"ETH": 0,
"USDT": 53051.10598709
},
"level": 9,
"merkelLeaf": "d61c58070ee1dc12",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 11.98852944,
"ETH": 157.31252,
"USDT": 1099754333.25209158
},
"level": 8,
"merkelLeaf": "81cc6c9125fa98cb",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 715.09237636,
"ETH": 2100.0404595,
"USDT": 2020155332.95708230
},
"level": 7,
"merkelLeaf": "4ccb436f7883fe91",
"role": 1
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 61400096.00294400,
"ETH": 433168.81989999,
"USDT": 1016785013.13079698
},
"level": 6,
"merkelLeaf": "795423500e6d597d",
"role": 1
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 286595751.08265201,
"ETH": 26727776.90857214,
"USDT": 2453648994.37423942
},
"level": 5,
"merkelLeaf": "6050ca3d4e946783",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 8508725002.69311378,
"ETH": 2146389444.08493589,
"USDT": 15787098930.60614156
},
"level": 4,
"merkelLeaf": "8ad038df7e82fce6",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 9944837661.61102144,
"ETH": 10112560970.15270098,
"USDT": 63861352068.33045056
},
"level": 3,
"merkelLeaf": "e33150a464c294ed",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 420217093.46290786,
"ETH": 221002355.98534612,
"USDT": 6649023671.61392569
},
"level": 2,
"merkelLeaf": "864f125992c7bae9",
"role": 2
},
{
"auditId": "Au20221129",
"balances": {
"BTC": 19222776331.93374469,
"ETH": 12507420973.30443462,
"USDT": 92889184629.59267820
},
"level": 1,
"merkelLeaf": "ffedeaf82363b23c",
"role": 3
}
],
"self": {
"auditId": "Au20221129",
"balances": {
"BTC": 0,
"ETH": 5000,
"USDT": 5560.49
},
"encryptUid": "b3e887893212aa3faea8923b3b4c8589131895e7f468d394c4c508e31e3c85bf",

This comment has been minimized.

Copy link
@oumcccm

oumcccm May 29, 2024

0x9b7d1bcd35eca7dfea0aefc52d1b9e26ccca59b4

"level": 11,
"merkelLeaf": "1fa8ef498dc505f6",
"nonce": "wx9bzgdojl0hoz7ckgft0k1cdyly7qohglaphsmqkx0hv80hjlwf0igvnamdqbh7",
"role": 1
}
}
77 changes: 77 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.upex</groupId>
<artifactId>proof-of-reserves</artifactId>
<version>1.0.0</version>

<packaging>jar</packaging>

<name>proof-of-reserves</name>
<url>https://www.bitget.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- 引用commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>

<!-- 引用fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>

</dependencies>

<build>
<finalName>proof-of-reserves</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.upex.BgMerkleValidator</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
93 changes: 93 additions & 0 deletions src/main/java/com/upex/BgMerkleValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.upex;

import com.alibaba.fastjson.JSONObject;
import com.upex.model.MerkleProof;
import com.upex.util.CollectionUtils;
import com.upex.util.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* 验证账户资产是否包含在BitGet发布的Merkle树中
*
* @author BitgetLimited
*/
public class BgMerkleValidator {
/**
* 定义文件路径:merkel_tree_bg.json
**/
private static final String MERKLE_TREE_BG_FILE_PATH = "merkel_tree_bg.json";

public static void main(String[] args) {
System.out.println("Merkel tree path validation start");
// 获取"merkel_tree_bg.json"文件内容
String merkleJsonFile = getMerkleJsonFile();
if (StringUtils.isBlank(merkleJsonFile)) {
System.out.println("Merkel tree path validation failed, invalid merkle proof file");
return;
}

// 获得默克尔树证明对象
MerkleProof merkleProof = JSONObject.parseObject(merkleJsonFile, MerkleProof.class);

// 默克尔树参数验证
if(validate(merkleProof)){
System.out.println("Consistent with the Merkle tree root hash. The verification succeeds");
}else {
System.out.println("Inconsistent with the Merkle tree root hash. The verification fails");
}
}

/**
* 参数校验
* @param merkleProof
* @return
* @author BitgetLimited
* @date 2022/11/25 20:29
*/
private static boolean validate(MerkleProof merkleProof){

// self节点不能为空 并且 path节点也不能为空
if(merkleProof.getSelf() == null || CollectionUtils.isEmpty(merkleProof.getPath())){
return false;
}

// 验证self数据一致性
if(!merkleProof.getSelf().validateSelf()){
return false;
}

// 验证path参数验证
if(!merkleProof.getPath().get(0).validatePath()){
return false;
}

if(merkleProof.getPath().get(0).getRole().intValue() == merkleProof.getSelf().getRole().intValue()){
return false;
}

return merkleProof.validate();
}


/**
* 获取merkel_tree_bg.json文件内容
*
* @author BitgetLimited
* @date 2022/11/25 16:53
*/
private static String getMerkleJsonFile() {
StringBuilder builder = new StringBuilder();
try {
Files.readAllLines(Path.of(MERKLE_TREE_BG_FILE_PATH), StandardCharsets.UTF_8).forEach(builder::append);
return builder.toString();
} catch (IOException e) {
throw new RuntimeException(MERKLE_TREE_BG_FILE_PATH + " file does not exist");
}
}
}


15 changes: 15 additions & 0 deletions src/main/java/com/upex/constants/MerkelTreeConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.upex.constants;

/**
* 默克尔树常量
* @author BitgetLimited
* @date 2022/11/25 23:03
*/
public class MerkelTreeConstants {

/**
* 逗号
*/
public static final String COMMA = ",";

}
29 changes: 29 additions & 0 deletions src/main/java/com/upex/constants/TreeNodeRoleConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.upex.constants;

/**
* @author BitgetLimited
* @date 2020-11-18 16:14
* @desc
**/
public class TreeNodeRoleConstants {
/**
* 空节点
*/
public static final Integer EMPTY_NODE = 0;

/**
* 左节点
*/
public static final Integer LEFT_NODE = 1;

/**
* 右节点
*/
public static final Integer RIGHT_NODE = 2;

/**
* 根节点
*/
public static final Integer ROOT_NODE = 3;

}
Loading

5 comments on commit 6863e2d

@Gabo139
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tdg

@oumcccm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tdg

1GUdTRtcCzLuHdbjVi7Nv4LcmFt4LURpz6

@GetitSE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vad är det här? Jag förstår inte hur det fungerar

@oumcccm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tdg

@oumcccm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

Please sign in to comment.