Skip to content

Commit

Permalink
Add eth and eos sign and validate
Browse files Browse the repository at this point in the history
  • Loading branch information
pengpengliu committed Mar 26, 2019
1 parent f9dbee8 commit 86a0f82
Show file tree
Hide file tree
Showing 31 changed files with 311 additions and 1,591 deletions.
8 changes: 0 additions & 8 deletions core/src/main/java/META-INF/MANIFEST.MF

This file was deleted.

4 changes: 2 additions & 2 deletions core/src/main/java/org/bitcorej/chain/ChainState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface ChainState {
KeyPair generateKeyPair(String secret);
KeyPair generateKeyPair();

// Boolean validateTx(List from, List to, String rawTx);

Boolean validateTx(String rawTx, String requestTx);
Transaction decodeRawTransaction(String rawTx);
String signRawTransaction(String rawTx, List<String> keys);
}
10 changes: 10 additions & 0 deletions core/src/main/java/org/bitcorej/chain/ChainStateProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public KeyPair generateKeyPair() {
return provider.generateKeyPair();
}

@Override
public Boolean validateTx(String rawTx, String tx) {
return provider.validateTx(rawTx, tx);
}

@Override
public Transaction decodeRawTransaction(String rawTx) {
return null;
}

@Override
public String signRawTransaction(String rawTx, List<String> keys) {
return provider.signRawTransaction(rawTx, keys);
Expand Down
126 changes: 126 additions & 0 deletions core/src/main/java/org/bitcorej/chain/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.bitcorej.chain;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Transaction {

public class Input {
private String address;
private BigDecimal amount;

public Input(String address, BigDecimal amount) {
this.address = address;
this.amount = amount;
}
}

public class Output {
private String address;
private BigDecimal amount;
private String memo;

public Output(String address, BigDecimal amount, String memo) {
this.address = address;
this.amount = amount;
this.memo = memo;
}
}

private List<Input> from;
private List<Output> to;

private BigDecimal fee;

public Transaction() {}

public Transaction(String json) {
JSONObject jsonObject = new JSONObject(json);
JSONArray from = jsonObject.getJSONArray("from");
for (int i = 0; i < from.length(); i++) {
JSONObject input = from.getJSONObject(i);
this.addInput(new Input(input.getString("address"), input.has("amount") ? new BigDecimal(input.getString("amount")) : null));
}
JSONArray to = jsonObject.getJSONArray("to");
for (int i = 0; i < to.length(); i++) {
JSONObject output = to.getJSONObject(i);
this.addOutput(new Output(output.getString("address"), new BigDecimal(output.getString("amount")), output.has("memo") ? output.getString("memo") : ""));
}
this.fee = jsonObject.has("fee") ? new BigDecimal(jsonObject.getString("fee")) : null;
}

public List<Input> getFrom() {
return from;
}

public void setFrom(List<Input> from) {
this.from = from;
}

public void addInput(Input input) {
if (this.from == null)
this.from = new ArrayList<>();
this.from.add(input);
}

public List<Output> getTo() {
return to;
}

public void setTo(List<Output> to) {
this.to = to;
}

public void addOutput(Output output) {
if (this.to == null)
this.to = new ArrayList<>();
this.to.add(output);
}

public BigDecimal getFee() {
return fee;
}

public void setFee(BigDecimal fee) {
this.fee = fee;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Transaction)) return false;

Transaction tx = (Transaction)obj;
for (int i = 0; i < tx.getFrom().size(); i++) {
if (!tx.getFrom().get(i).address.equals(this.getFrom().get(i).address)) {
return false;
}
if (tx.getFrom().get(i).amount != null) {
if (tx.getFrom().get(i).amount.compareTo(this.getFrom().get(i).amount) != 0) {
return false;
}
}
}

for (int i = 0; i < tx.getTo().size(); i++) {
if (!tx.getTo().get(i).address.equals(this.getTo().get(i).address)) {
return false;
}
if (tx.getTo().get(i).amount.compareTo(this.getTo().get(i).amount) != 0) {
return false;
}
if (!tx.getTo().get(i).memo.equals(this.getTo().get(i).memo)) {
return false;
}
}

if (tx.getFee() != null) {
return tx.getFee().compareTo(getFee()) == 0;
} else {
return true;
}
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/org/bitcorej/chain/bch/BCHStateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public BCHStateProvider(Network network) {
super(network);
}

@Override
public Boolean validateTx(String rawTx, String tx) {
return null;
}

@Override
public org.bitcorej.chain.Transaction decodeRawTransaction(String rawTx) {
return null;
}

@Override
public String signRawTransaction(String rawTx, List<String> keys) {
Transaction tx = super.buildTransaction(rawTx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public KeyPair generateKeyPair() {
return this.generateKeyPair(new ECKey().getPrivateKeyAsHex());
}

@Override
public Boolean validateTx(String rawTx, String tx) {
return null;
}

@Override
public org.bitcorej.chain.Transaction decodeRawTransaction(String rawTx) {
return null;
}


protected Transaction buildTransaction(String json) {
JSONObject jsonObject = new JSONObject(json);
Expand Down
117 changes: 0 additions & 117 deletions core/src/main/java/org/bitcorej/chain/eos/Action.java

This file was deleted.

Loading

0 comments on commit 86a0f82

Please sign in to comment.