本篇内容主要讲解“java怎么实现Omni core USDT离线签名交易 ”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“java怎么实现Omni core USDT离线签名交易 ”吧!
背景:
Background:
1.主要是对区块链离线交易进行utxo上链。
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 1.
2.对usdt地址下的全部币进行汇集交易,单独由btc进行手续费支付
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 2. Merger transactions for all currencies under the Usdt address, to be paid separately by btc
一、对于如何查看一个unspent 交易:
i. For how to view an unspent transaction: & nbsp;
1.调用钱包的 unspent接口
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 1. Unspent interface to call wallets
2.调用三方区块链浏览器的接口 例如:(blockchain : https://blockchain.info/unspent?active=35nNMpckrvuaryxFSiUd6jLvLGXJCWUyAY)
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 2. For example: (blockchain: https://blockchain.info/unspent?active=35nNpckrvuaryxFSiUd6jLGXCWuyAY)
& nbsp;
拿到钱包地址的unspentUTXO即可进行离线交易获取签名信息了 ,直接上代码:
UnspentUTXO with wallet address can do offline transactions to get signature information & nbsp; direct to code:
<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; 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); } } 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:
Get OmniHex:
调用RPC: get_balance 、 omni_createpayload_simplesend 、 omni_createrawtx_opreturn
& nbsp; & nbsp; & nbsp; call RPC: get_balance, omni_createpayload_simplesend, & nbsp; omni_createrawtx_open
到此,相信大家对“java怎么实现Omni core USDT离线签名交易 ”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论