溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java怎么實(shí)現(xiàn)Omni core USDT離線簽名交易

發(fā)布時(shí)間:2022-01-06 16:40:54 來(lái)源:億速云 閱讀:215 作者:iii 欄目:互聯(lián)網(wǎng)科技

本篇內(nèi)容主要講解“java怎么實(shí)現(xiàn)Omni core USDT離線簽名交易 ”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java怎么實(shí)現(xiàn)Omni core USDT離線簽名交易 ”吧!

背景:

    1.主要是對(duì)區(qū)塊鏈離線交易進(jìn)行utxo上鏈。

    2.對(duì)usdt地址下的全部幣進(jìn)行匯集交易,單獨(dú)由btc進(jìn)行手續(xù)費(fèi)支付

一、對(duì)于如何查看一個(gè)unspent 交易: 

      1.調(diào)用錢包的 unspent接口

      2.調(diào)用三方區(qū)塊鏈瀏覽器的接口 例如:(blockchain : https://blockchain.info/unspent?active=35nNMpckrvuaryxFSiUd6jLvLGXJCWUyAY)


 java怎么實(shí)現(xiàn)Omni core USDT離線簽名交易

拿到錢包地址的unspentUTXO即可進(jìn)行離線交易獲取簽名信息了 ,直接上代碼:

<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  發(fā)送的usdt地址
	    * @param @param fee 手續(xù)費(fèi)
	    * @param @param omniHex - usdt hex
 	    * @param @param unBtcUtxos - btc utxo
	    * @param @param unUsdtUtxos - usdt utxo 
	    * @param @return    參數(shù)  
	    * @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);
					// 如果需要找零 消費(fèi)列表總金額 - 已經(jīng)轉(zhuǎn)賬的金額 - 手續(xù)費(fèi)
					long value_btc = unBtcUtxos.stream().mapToLong(UnSpentUtxo::getValue).sum();
					long value_usdt = unUsdtUtxos.stream().mapToLong(UnSpentUtxo::getValue).sum();
					// 總輸入 - 手續(xù)費(fèi) - 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("傳輸參數(shù):">

獲取OmniHex:

     調(diào)用RPC: get_balance 、 omni_createpayload_simplesend 、 omni_createrawtx_opreturn

java怎么實(shí)現(xiàn)Omni core USDT離線簽名交易

到此,相信大家對(duì)“java怎么實(shí)現(xiàn)Omni core USDT離線簽名交易 ”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI