Skip to content

Commit

Permalink
Add LUNA
Browse files Browse the repository at this point in the history
  • Loading branch information
pengpengliu committed Dec 25, 2019
1 parent a0e21e2 commit 4d281f3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/org/bitcorej/chain/ChainStateProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.bitcorej.chain.iost.IOSTStateProvider;
import org.bitcorej.chain.iris.IRISStateProvider;
import org.bitcorej.chain.ltc.LitecoinStateProvider;
import org.bitcorej.chain.luna.LUNAStateProvider;
import org.bitcorej.chain.mch.MCHStateProvider;
import org.bitcorej.chain.meta.METAStateProvider;
import org.bitcorej.chain.naka.NAKAStateProvider;
Expand Down Expand Up @@ -117,6 +118,9 @@ public class ChainStateProxy implements ChainState, UTXOState, USDTState {
CZZStateProvider czz = new CZZStateProvider(Network.MAIN);
services.put("CZZ", czz);
services.put("CZZ_MAIN", nrg);
LUNAStateProvider luna = new LUNAStateProvider();
services.put("LUNA", luna);
services.put("LUNA_MAIN", luna);
}

private ChainState provider;
Expand Down
59 changes: 59 additions & 0 deletions core/src/main/java/org/bitcorej/chain/luna/LUNAStateProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.bitcorej.chain.luna;

import org.apache.commons.codec.binary.Base64;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Sha256Hash;
import org.bitcorej.chain.cosmos.CosmosStateProvider;
import org.bitcorej.utils.ByteUtil;
import org.bitcorej.utils.NumericUtil;
import org.bouncycastle.util.encoders.Hex;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class LUNAStateProvider extends CosmosStateProvider {
public LUNAStateProvider() {
super.bech32AccAddr = "terra";
super.transferPrefix = "bank/MsgSend";
}

@Override
public String signRawTransaction(String rawTx, List<String> keys) {
JSONObject jsonObject = new JSONObject(rawTx);
int accountNumber = jsonObject.getInt("account_number");
String chainId = jsonObject.getString("chain_id");
String feeDenom = jsonObject.getJSONObject("fees").getString("denom");
String feeAmount = jsonObject.getJSONObject("fees").getString("amount");
String gas = jsonObject.getString("gas");
String memo = jsonObject.getString("memo");
int sequence = jsonObject.getInt("sequence");
String from = jsonObject.getString("from");
String to = jsonObject.getJSONObject("msg").getString("to");
String toDenom = jsonObject.getJSONObject("msg").getJSONArray("coins").getJSONObject(0).getString("denom");
String toAmount = jsonObject.getJSONObject("msg").getJSONArray("coins").getJSONObject(0).getString("amount");

String msg = "{\"account_number\":\"" + accountNumber + "\",\"chain_id\":\"" + chainId + "\",\"fee\":{\"amount\":[{\"amount\":\"" + feeAmount + "\",\"denom\":\"" + feeDenom + "\"}],\"gas\":\"" + gas +"\"},\"memo\":\"" + memo +"\",\"msgs\":[{\"type\":\"bank/MsgSend\",\"value\":{\"amount\":[{\"amount\":\"" + toAmount + "\",\"denom\":\"" + toDenom + "\"}],\"from_address\":\"" + from + "\",\"to_address\":\"" + to + "\"}}],\"sequence\":\"" + sequence + "\"}";
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
byte[] hash = digest.digest(
msg.getBytes(StandardCharsets.UTF_8));
String sha256hex = new String(Hex.encode(hash));

ECKey ecKey = ECKey.fromPrivate(NumericUtil.hexToBytes(keys.get(0)));
ECKey.ECDSASignature sig = ecKey.sign(Sha256Hash.wrap(sha256hex));

byte[] signature = ByteUtil.concat(sig.r.toByteArray(), sig.s.toByteArray());

String sigBase64 = Base64.encodeBase64String(ByteUtil.trimLeadingZeroes(signature));
String pub = Base64.encodeBase64String(ecKey.getPubKey());
return "{\"tx\":{\"msg\":[{\"type\":\"" + this.transferPrefix + "\",\"value\":{\"amount\":[{\"amount\":\"" + toAmount +"\",\"denom\":\"" + toDenom + "\"}],\"from_address\":\"" + from + "\",\"to_address\":\"" + to + "\"}}],\"fee\":{\"amount\":[{\"denom\":\"" + feeDenom + "\",\"amount\":\"" + feeAmount + "\"}],\"gas\":\"" + gas + "\"},\"signatures\":[{\"pub_key\":{\"type\":\"tendermint/PubKeySecp256k1\",\"value\":\"" + pub + "\"},\"signature\":\"" + sigBase64 + "\"}],\"memo\":\"" + memo + "\"},\"mode\":\"async\"}";
}
}

0 comments on commit 4d281f3

Please sign in to comment.