您好,登錄后才能下訂單哦!
本篇內容主要講解“java怎么實現Omni core USDT離線簽名交易 ”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java怎么實現Omni core USDT離線簽名交易 ”吧!
背景:
1.主要是對區塊鏈離線交易進行utxo上鏈。
2.對usdt地址下的全部幣進行匯集交易,單獨由btc進行手續費支付
一、對于如何查看一個unspent 交易:
1.調用錢包的 unspent接口
2.調用三方區塊鏈瀏覽器的接口 例如:(blockchain : https://blockchain.info/unspent?active=35nNMpckrvuaryxFSiUd6jLvLGXJCWUyAY)
拿到錢包地址的unspentUTXO即可進行離線交易獲取簽名信息了 ,直接上代碼:
<dependency> <groupId>org.bitcoinj</groupId> <artifactId>bitcoinj-tools</artifactId> <version>0.14.7</version> </dependency> <dependency> <groupId>org.bitcoinj</groupId> <artifactId>bitcoinj-examples</artifactId> <version>0.14.7</version> </dependency> <dependency> <groupId>org.bitcoinj</groupId> <artifactId>bitcoinj-core</artifactId> <version>0.14.7</version> </dependency>
UnspentUtxo:
package com.bscoin.coldwallet.cointype.common; import java.io.Serializable; public class UnSpentUtxo implements Serializable { private static final long serialVersionUID = -7417428486644921613L; private String hash; //未交易hash private long txN; private long value;//金額 private int height; //高度 private String script;//簽名 private String address;//錢包地址 public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } public long getTxN() { return txN; } public void setTxN(long txN) { this.txN = txN; } public long getValue() { return value; } public void setValue(long value) { this.value = value; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getScript() { return script; } public void setScript(String script) { this.script = script; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Transaction:
package com.bscoin.coldwallet.cointype.usdt; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.codec.binary.Hex; import org.apache.commons.configuration2.Configuration; import org.bitcoinj.core.Address; import org.bitcoinj.core.AddressFormatException; import org.bitcoinj.core.Coin; import org.bitcoinj.core.Context; import org.bitcoinj.core.DumpedPrivateKey; import org.bitcoinj.core.ECKey; import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.ScriptException; import org.bitcoinj.core.Sha256Hash; import org.bitcoinj.core.Transaction; import org.bitcoinj.core.TransactionOutPoint; import org.bitcoinj.core.UTXO; import org.bitcoinj.core.Utils; import org.bitcoinj.params.MainNetParams; import org.bitcoinj.params.TestNet3Params; import org.bitcoinj.script.Script; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON; import com.bscoin.coldwallet.cointype.common.ConfigUtil; import com.bscoin.coldwallet.cointype.common.UnSpentUtxo; import org.bitcoinj.core.TransactionConfidence; /** * @ClassName: RawTransaction * @author DHing * */ public class RawTransaction { private static Logger LOG = LoggerFactory.getLogger(RawTransaction.class); static NetworkParameters params; static { try { Configuration config = ConfigUtil.getInstance(); params = config.getBoolean("usdtcoin.testnet") ? TestNet3Params.get() : MainNetParams.get(); LOG.info("=== [USDT] usdtcoin client networkID:{} ===", params.getId()); } catch (Exception e) { LOG.info("=== [USDT] com.bscoin.coldwallet.cointype.usdt.RawTransaction:{} ===", e.getMessage(), e); } } /** * @Title: createRawTransaction * @param @param privBtcKey btc私鑰 * @param @param btcAddress 比特幣地址 * @param @param privUsdtKey usdt私鑰 * @param @param recevieUsdtAddr usdt接收地址 * @param @param formUsdtAddr 發送的usdt地址 * @param @param fee 手續費 * @param @param omniHex - usdt hex * @param @param unBtcUtxos - btc utxo * @param @param unUsdtUtxos - usdt utxo * @param @return 參數 * @return String 返回類型 * @throws */ public static String createRawTransaction(String privBtcKey, String btcAddress, String privUsdtKey, String recevieUsdtAddr, String formUsdtAddr, long fee, String omniHex, List<UnSpentUtxo> unBtcUtxos, List<UnSpentUtxo> unUsdtUtxos) { List<UTXO> btcUtxos = new ArrayList<UTXO>(); List<UTXO> usdtUtxos = new ArrayList<UTXO>(); try { if (!unBtcUtxos.isEmpty() && !unUsdtUtxos.isEmpty()) { // find a btc eckey info DumpedPrivateKey btcPrivateKey = DumpedPrivateKey.fromBase58(params, privBtcKey); ECKey btcKey = btcPrivateKey.getKey(); // a usdt eckey info DumpedPrivateKey usdtPrivateKey = DumpedPrivateKey.fromBase58(params, privUsdtKey); ECKey usdtKey = usdtPrivateKey.getKey(); // receive address Address receiveAddress = Address.fromBase58(params, recevieUsdtAddr); // create a transaction Transaction tx = new Transaction(params); // odd address Address oddAddress = Address.fromBase58(params, btcAddress); // 如果需要找零 消費列表總金額 - 已經轉賬的金額 - 手續費 long value_btc = unBtcUtxos.stream().mapToLong(UnSpentUtxo::getValue).sum(); long value_usdt = unUsdtUtxos.stream().mapToLong(UnSpentUtxo::getValue).sum(); // 總輸入 - 手續費 - 546 -546 = 找零金額 long leave = (value_btc + value_usdt) - fee - 1092; if (leave > 0) { tx.addOutput(Coin.valueOf(leave), oddAddress); } // usdt transaction tx.addOutput(Coin.valueOf(546), new Script(Utils.HEX.decode(omniHex))); // send to address tx.addOutput(Coin.valueOf(546), receiveAddress); // btc utxos is an array of inputs from my wallet for (UnSpentUtxo unUtxo : unBtcUtxos) { btcUtxos.add(new UTXO(Sha256Hash.wrap(unUtxo.getHash()), unUtxo.getTxN(), Coin.valueOf(unUtxo.getValue()), unUtxo.getHeight(), false, new Script(Utils.HEX.decode(unUtxo.getScript())), unUtxo.getAddress())); } // usdt utxos is an array of inputs from my wallet for (UnSpentUtxo unUtxo : unUsdtUtxos) { usdtUtxos.add(new UTXO(Sha256Hash.wrap(unUtxo.getHash()), unUtxo.getTxN(), Coin.valueOf(unUtxo.getValue()), unUtxo.getHeight(), false, new Script(Utils.HEX.decode(unUtxo.getScript())), unUtxo.getAddress())); } // create usdt utxo data for (UTXO utxo : usdtUtxos) { TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash()); tx.addSignedInput(outPoint, utxo.getScript(), usdtKey, Transaction.SigHash.ALL, true); } // create btc utxo data for (UTXO utxo : btcUtxos) { TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash()); tx.addSignedInput(outPoint, utxo.getScript(), btcKey, Transaction.SigHash.ALL, true); } Context context = new Context(params); tx.getConfidence().setSource(TransactionConfidence.Source.NETWORK); tx.setPurpose(Transaction.Purpose.USER_PAYMENT); LOG.info("=== [USDT] sign success,hash is :{} ===", tx.getHashAsString()); return new String(Hex.encodeHex(tx.bitcoinSerialize())); } } catch (Exception e) { LOG.info("=== com.bscoin.coldwallet.cointype.usdt.RawTransaction.createRawTransaction(String, String, String, String, String, long, String, List<UnSpentUtxo>, List<UnSpentUtxo>):{} ===", e.getMessage(), e); } return null; } public static void main(String[] args) { Map m = new HashMap(); List<UnSpentUtxo> us = new ArrayList<UnSpentUtxo>(); UnSpentUtxo u = new UnSpentUtxo(); u.setAddress("mvEtuEqYPMrLaKjJ5nTZ57vQAoYUtVmMaQ"); u.setHash("d235e908767d4bbf579e04ae768fa16298c8ccb2dc406f1cda90341477ccbb3f"); u.setHeight(1413239); u.setScript("76a914a1806613a51a81966779e2fa1537013cf4cd2b1788ac"); u.setTxN(0); u.setValue(300000); UnSpentUtxo u1 = new UnSpentUtxo(); u1.setAddress("mvEtuEqYPMrLaKjJ5nTZ57vQAoYUtVmMaQ"); u1.setHash("d74b16fd8e548e467bd1f4ce1214037fc6087bb7bf4f15cfa684d03d1cb2eda4"); u1.setHeight(1413334); u1.setScript("76a914a1806613a51a81966779e2fa1537013cf4cd2b1788ac"); u1.setTxN(1); u1.setValue(300000); us.add(u); us.add(u1); List<UnSpentUtxo> us2 = new ArrayList<UnSpentUtxo>(); UnSpentUtxo u3 = new UnSpentUtxo(); u3.setAddress("moUseQWZenTkU3a2bCZydth4CUUZqNY6Fk"); u3.setHash("bd6da7714f1eb5f36e62070bc8463f8d574b98083a0df872285d291417b3afe3"); u3.setHeight(1413334); u3.setScript("76a914575c4b21030d58d02c434fc36f66a866142e74ce88ac"); u3.setTxN(1); u3.setValue(546); us2.add(u3); m.put("btcUtxo", us); m.put("usdtUtxo", us2); m.put("omniHex", "6a146f6d6e6900000000000000010000000059682f00"); System.out.println("傳輸參數:">
獲取OmniHex:
調用RPC: get_balance 、 omni_createpayload_simplesend 、 omni_createrawtx_opreturn
到此,相信大家對“java怎么實現Omni core USDT離線簽名交易 ”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。