Skip to content

Commit

Permalink
Merge pull request #12 from lingting/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lingting authored May 12, 2021
2 parents 38e18b5 + 3535d25 commit 5f62368
Show file tree
Hide file tree
Showing 88 changed files with 7,609 additions and 596 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
eol = lf
* text eol=lf
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
```

# 结构

```text
├─virtual-currency-bitcoin
│ ├─src
Expand Down
54 changes: 2 additions & 52 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
<module>virtual-currency-tronscan</module>
<module>virtual-currency-bitcoin</module>
<module>virtual-currency-all</module>
<module>virtual-currency-tronscan-sdk</module>
</modules>

<name>virtual-currency</name>
<description>虚拟货币处理</description>
<url>https://github.com/lingting-gzm/VirtualCurrency</url>

<properties>
<revision>0.4.1</revision>
<revision>0.4.2</revision>
<scm.url>[email protected]:lingting-gzm/VirtualCurrency.git</scm.url>

<java.version>1.8</java.version>
Expand Down Expand Up @@ -142,46 +143,6 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>org.tron.tronj</groupId>
<artifactId>utils</artifactId>
<version>${tronj.version}</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.briandilley.jsonrpc4j</groupId>
<artifactId>jsonrpc4j</artifactId>
<version>${jsonrpc4j.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -207,17 +168,6 @@
</license>
</licenses>

<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>tron-maven</id>
<name>tron</name>
<url>https://dl.bintray.com/tronj/tronj</url>
</repository>
</repositories>

<profiles>
<profile>
<id>deploy</id>
Expand Down
4 changes: 2 additions & 2 deletions virtual-currency-all/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>virtual-currency</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.SignatureDecodeException;
import org.bitcoinj.core.TransactionConfidence;
import org.bitcoinj.core.TransactionInput;
import org.bitcoinj.core.TransactionOutPoint;
Expand All @@ -39,29 +41,33 @@
import org.bitcoinj.script.ScriptChunk;
import org.bitcoinj.script.ScriptPattern;
import org.bouncycastle.util.encoders.Hex;
import live.lingting.virtual.currency.bitcoin.endpoints.OmniEndpoints;
import live.lingting.virtual.currency.bitcoin.contract.OmniContract;
import live.lingting.virtual.currency.bitcoin.model.cupher.Balance;
import live.lingting.virtual.currency.bitcoin.endpoints.BitcoinCypherEndpoints;
import live.lingting.virtual.currency.bitcoin.endpoints.BitcoinEndpoints;
import live.lingting.virtual.currency.bitcoin.endpoints.BitcoinSochainEndpoints;
import live.lingting.virtual.currency.bitcoin.endpoints.BlockchainEndpoints;
import live.lingting.virtual.currency.bitcoin.endpoints.OmniEndpoints;
import live.lingting.virtual.currency.bitcoin.model.FeeAndSpent;
import live.lingting.virtual.currency.bitcoin.model.Unspent;
import live.lingting.virtual.currency.bitcoin.model.blockchain.LatestBlock;
import live.lingting.virtual.currency.bitcoin.model.blockchain.RawTransaction;
import live.lingting.virtual.currency.bitcoin.model.Unspent;
import live.lingting.virtual.currency.bitcoin.model.cypher.Balance;
import live.lingting.virtual.currency.bitcoin.model.omni.Balances;
import live.lingting.virtual.currency.bitcoin.model.omni.Domain;
import live.lingting.virtual.currency.bitcoin.model.omni.PushTx;
import live.lingting.virtual.currency.bitcoin.model.omni.TokenHistory;
import live.lingting.virtual.currency.bitcoin.model.omni.TransactionByHash;
import live.lingting.virtual.currency.bitcoin.properties.BitcoinProperties;
import live.lingting.virtual.currency.bitcoin.util.BitcoinUtils;
import live.lingting.virtual.currency.core.model.Account;
import live.lingting.virtual.currency.core.Contract;
import live.lingting.virtual.currency.core.Endpoints;
import live.lingting.virtual.currency.core.PlatformService;
import live.lingting.virtual.currency.core.enums.TransactionStatus;
import live.lingting.virtual.currency.core.enums.VirtualCurrencyPlatform;
import live.lingting.virtual.currency.core.model.Account;
import live.lingting.virtual.currency.core.model.TransactionInfo;
import live.lingting.virtual.currency.core.model.TransferParams;
import live.lingting.virtual.currency.core.model.TransferResult;
import live.lingting.virtual.currency.core.PlatformService;
import live.lingting.virtual.currency.core.util.AbiUtils;

/**
Expand Down Expand Up @@ -95,34 +101,32 @@ public class BitcoinServiceImpl implements PlatformService<BitcoinTransactionGen

private final BitcoinProperties properties;

private final Endpoints omniEndpoints;
private final NetworkParameters np;

private final BlockchainEndpoints blockchainEndpoints;

private final Endpoints bitcoinEndpoints;
private static final OmniEndpoints OMNI_ENDPOINTS = OmniEndpoints.MAINNET;

private final BitcoinCypherEndpoints cypherEndpoints;

public BitcoinServiceImpl(BitcoinProperties properties) {
this.properties = properties;
// 自定义 omni url
if (StrUtil.isNotBlank(properties.getOmniUrl())) {
omniEndpoints = properties::getOmniUrl;
}
// 未自定义 omni url
else {
omniEndpoints = OmniEndpoints.MAINNET;
}
// 自定义 bitcoin url
if (StrUtil.isNotBlank(properties.getOmniUrl())) {
bitcoinEndpoints = properties::getBitcoinUrl;
this.np = properties.getNp();

if (properties.getEndpoints() == BitcoinEndpoints.MAINNET) {
blockchainEndpoints = BlockchainEndpoints.MAINNET;
cypherEndpoints = BitcoinCypherEndpoints.MAINNET;
}
// 未自定义 bitcoin url
else {
bitcoinEndpoints = properties.getEndpoints();
blockchainEndpoints = BlockchainEndpoints.TEST;
cypherEndpoints = BitcoinCypherEndpoints.TEST;
}

}

@Override
public Optional<TransactionInfo> getTransactionByHash(String hash) throws Throwable {
RawTransaction rawTransaction = RawTransaction.of(bitcoinEndpoints, hash);
public Optional<TransactionInfo> getTransactionByHash(String hash) throws Exception {
RawTransaction rawTransaction = RawTransaction.of(blockchainEndpoints, hash);

if (rawTransaction == null || StrUtil.isBlank(rawTransaction.getHash())
|| CollectionUtil.isEmpty(rawTransaction.getOuts())) {
Expand Down Expand Up @@ -160,7 +164,7 @@ public Optional<TransactionInfo> getTransactionByHash(String hash) throws Throwa
return btcTransactionHandler(sumOut, outInfos, rawTransaction);
}

TransactionByHash response = request(STATIC_TRANSACTION_HASH, omniEndpoints, hash);
TransactionByHash response = request(STATIC_TRANSACTION_HASH, OMNI_ENDPOINTS, hash);
// 交易查询不到 或者 valid 为 false
if (response.getAmount() == null || !response.getValid()) {
return Optional.empty();
Expand Down Expand Up @@ -206,7 +210,7 @@ public Integer getDecimalsByContract(Contract contract) throws JsonProcessingExc
return CONTRACT_DECIMAL_CACHE.get(contract.getHash());
}

TokenHistory history = request(STATIC_TOKEN_HISTORY, omniEndpoints, contract.getHash());
TokenHistory history = request(STATIC_TOKEN_HISTORY, OMNI_ENDPOINTS, contract.getHash());
int decimals = getDecimalsByString(history.getTransactions().get(0).getAmount());
CONTRACT_DECIMAL_CACHE.put(contract.getHash(), decimals);
return decimals;
Expand All @@ -215,13 +219,13 @@ public Integer getDecimalsByContract(Contract contract) throws JsonProcessingExc
@Override
public BigInteger getBalanceByAddressAndContract(String address, Contract contract) throws JsonProcessingException {
if (contract == OmniContract.BTC) {
Balance balance = Balance.of(bitcoinEndpoints, address);
Balance balance = Balance.of(cypherEndpoints, address);
if (balance == null || StrUtil.isNotBlank(balance.getError()) || balance.getFinalBalance() == null) {
return BigInteger.ZERO;
}
return balance.getFinalBalance();
}
Balances balances = request(STATIC_BALANCES, omniEndpoints, address);
Balances balances = request(STATIC_BALANCES, OMNI_ENDPOINTS, address);
if (CollectionUtil.isEmpty(balances.getBalance())) {
return BigInteger.ZERO;
}
Expand Down Expand Up @@ -254,11 +258,10 @@ public BigDecimal getNumberByBalanceAndContract(BigInteger balance, Contract con

@Override
public BitcoinTransactionGenerate transactionGenerate(Account from, String to, Contract contract, BigDecimal value,
TransferParams params) throws Throwable {
TransferParams params) throws Exception {
if (value.compareTo(BigDecimal.ZERO) <= 0) {
return BitcoinTransactionGenerate.failed("转账金额必须大于0!");
}
NetworkParameters np = properties.getNp();
// BTC 转账数量
Coin btcAmount;
// 转账合约数量
Expand Down Expand Up @@ -287,7 +290,7 @@ public BitcoinTransactionGenerate transactionGenerate(Account from, String to, C
// 参数
params,
// 未使用余额
properties.getUnspent().apply(from.getAddress(), bitcoinEndpoints),
properties.getUnspent().apply(from.getAddress(), properties.getEndpoints()),
// 转账数量
btcAmount,
// 最小确认数
Expand All @@ -304,7 +307,8 @@ public BitcoinTransactionGenerate transactionGenerate(Account from, String to, C
tx.addOutput(btcAmount, toAddress);

// 找零输出
if (fs.getZero()) {
boolean zero = fs.getZero();
if (zero) {
tx.addOutput(
// 找零 = 输出数量 - 总手续费 - 转账数量
fs.getOutNumber().subtract(fs.getFee()).subtract(btcAmount),
Expand Down Expand Up @@ -345,14 +349,15 @@ public BitcoinTransactionGenerate transactionGenerate(Account from, String to, C
}

@Override
public BitcoinTransactionGenerate transactionSign(BitcoinTransactionGenerate generate) throws Throwable {
public BitcoinTransactionGenerate transactionSign(BitcoinTransactionGenerate generate)
throws SignatureDecodeException {
// 如果上一步失败则直接返回
if (!generate.getSuccess()) {
boolean error = !generate.getSuccess();
if (error) {
return generate;
}
org.bitcoinj.core.Transaction tx = generate.getBitcoin().getTransaction();
Account from = generate.getFrom();
NetworkParameters np = properties.getNp();

// 初始密钥
List<ECKey> keys = getEcKeysByFrom(from);
Expand Down Expand Up @@ -461,13 +466,14 @@ else if (ScriptPattern.isP2PKH(script)) {
}

@Override
public TransferResult transactionBroadcast(BitcoinTransactionGenerate generate) throws Throwable {
public TransferResult transactionBroadcast(BitcoinTransactionGenerate generate) throws Exception {
// 如果上一步失败则直接返回
if (!generate.getSuccess()) {
boolean error = !generate.getSuccess();
if (error) {
return TransferResult.failed(generate);
}
// 广播交易, 返回 交易hash
PushTx pushTx = properties.getBroadcastTransaction().apply(generate.getSignHex(), omniEndpoints);
PushTx pushTx = properties.getBroadcastTransaction().apply(generate.getSignHex(), OMNI_ENDPOINTS);
if (!pushTx.isSuccess()) {
if (pushTx.getE() != null) {
return TransferResult.failed(pushTx.getE());
Expand All @@ -477,9 +483,10 @@ public TransferResult transactionBroadcast(BitcoinTransactionGenerate generate)
return TransferResult.success(pushTx.getTxId());
}

@SneakyThrows
@Override
public boolean validate(String address) throws JsonProcessingException {
Balance balance = Balance.of(bitcoinEndpoints, address);
public boolean validate(String address) {
Balance balance = Balance.of(cypherEndpoints, address);
return StrUtil.isBlank(balance.getError());
}

Expand Down Expand Up @@ -511,7 +518,8 @@ public long sleepTime() {
*/
private <T> T request(Domain<T> domain, Endpoints endpoints, Object params) throws JsonProcessingException {
// 获取锁
if (properties.getLock().get()) {
boolean lock = properties.getLock().get();
if (lock) {
try {
// 执行请求方法
return domain.of(endpoints, params);
Expand All @@ -530,7 +538,7 @@ private <T> T request(Domain<T> domain, Endpoints endpoints, Object params) thro
* 解析原始交易数据, 返回结果
* @author lingting 2021-01-10 19:00
*/
private Optional<TransactionInfo> btcTransactionHandler(RawTransaction rawTransaction) throws Throwable {
private Optional<TransactionInfo> btcTransactionHandler(RawTransaction rawTransaction) throws Exception {
// 总输出数量
BigInteger sumOut = BigInteger.ZERO;
// 输出详情
Expand All @@ -545,7 +553,7 @@ private Optional<TransactionInfo> btcTransactionHandler(RawTransaction rawTransa
}

private Optional<TransactionInfo> btcTransactionHandler(BigInteger sumOut, Map<String, BigDecimal> outInfos,
RawTransaction rawTransaction) throws Throwable {
RawTransaction rawTransaction) throws Exception {
// 总输入数量
BigInteger sumIn = BigInteger.ZERO;
// 输入详情
Expand Down Expand Up @@ -578,7 +586,7 @@ private Optional<TransactionInfo> btcTransactionHandler(BigInteger sumOut, Map<S
// 计算确认数
else {
// 获取最新区块
LatestBlock block = LatestBlock.of(bitcoinEndpoints);
LatestBlock block = LatestBlock.of(blockchainEndpoints);
// 计算确认数
BigInteger confirmationNumber = block.getHeight().subtract(transactionInfo.getBlock());
transactionInfo.setStatus(
Expand All @@ -594,7 +602,7 @@ private Optional<TransactionInfo> btcTransactionHandler(BigInteger sumOut, Map<S
* @author lingting 2021-01-10 19:31
*/
private BigInteger statisticsDetails(BigInteger sumIn, Map<String, BigDecimal> inInfos, RawTransaction.Out out)
throws Throwable {
throws Exception {
// 统计输入数量
sumIn = sumIn.add(out.getValue());
// 存在统计详情
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
import org.bouncycastle.util.encoders.Hex;
import live.lingting.virtual.currency.core.model.Account;
import live.lingting.virtual.currency.core.Contract;
import live.lingting.virtual.currency.core.model.Account;
import live.lingting.virtual.currency.core.model.TransactionGenerate;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,4 @@ public enum BitcoinCypherEndpoints implements Endpoints {

private final String desc;

public static BitcoinCypherEndpoints of(Endpoints endpoints) {
if (endpoints == BitcoinEndpoints.MAINNET) {
return MAINNET;
}
return TEST;
}

}
Loading

0 comments on commit 5f62368

Please sign in to comment.