• 大小: 18KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Java
  • 标签: bitcoinj  jar  

资源简介

bitcoinj项目富含完整demo 此项目使用maven构建,不会使用maven的同学,查看项目pom.xml文件,并在http://mvnrepository.com/下载相应的依赖jar包. demo:bitcoinj签名交易 /** * @param unSpentBTCList 未花费utxo集合 * @param from 发送者地址 * @param to 接收者地址 * @param privateKey 私钥 * @param value 发送金额.单位:聪 * @param fee 旷工费.单位:聪 * @return 签名之后未广播的原生交易字符串 * @throws Exception */ public static String signBTCTransactionData(List unSpentBTCList, String from, String to, String privateKey, long value, long fee) throws Exception { NetworkParameters networkParameters = null; // networkParameters = MainNetParams.get(); //测试网络 networkParameters = TestNet3Params.get(); Transaction transaction = new Transaction(networkParameters); DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(networkParameters, privateKey); ECKey ecKey = dumpedPrivateKey.getKey(); long totalMoney = 0; List utxos = new ArrayList(); //遍历未花费列表,组装合适的item for (UnSpentBTC us : unSpentBTCList) { if (totalMoney >= (value + fee)) break; UTXO utxo = new UTXO(Sha256Hash.wrap(us.getTxid()), us.getVout(), Coin.valueOf(us.getSatoshis()), us.getHeight(), false, new Script(Hex.decode(us.getScriptPubKey()))); utxos.add(utxo); totalMoney += us.getSatoshis(); } transaction.addOutput(Coin.valueOf(value), Address.fromBase58(networkParameters, to)); // transaction. //消费列表总金额 - 已经转账的金额 - 手续费 就等于需要返回给自己的金额了 long balance = totalMoney - value - fee; //输出-转给自己 if (balance > 0) { transaction.addOutput(Coin.valueOf(balance), Address.fromBase58(networkParameters, from)); } //输入未消费列表项 for (UTXO utxo : utxos) { TransactionOutPoint outPoint = new TransactionOutPoint(networkParameters, utxo.getIndex(), utxo.getHash());

资源截图

代码片段和文件信息

package bitcoinj;

import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.script.script;
import org.spongycastle.util.encoders.Hex;

import java.util.ArrayList;
import java.util.List;


/**
 * @author mc qq:776355102
 * 
 * btc签名交易
 *
 */
public class SendRequest {

public static  void main(String[] args) throws Exception {
UnSpentBTC unSpentBTC = new UnSpentBTC();
unSpentBTC.setAddress(“mnzmwSmYy9z4a6MwU4bYb1zWH5iLZu3BBA“);
unSpentBTC.setSatoshis(54988192L);
unSpentBTC.setHeight(1383579);
unSpentBTC.setscriptPubKey(“76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac“);
unSpentBTC.setTxid(“75128d54a8607de5287773a14aa9ff88fa0e5797fed97e54fcb8a783b48bac7a“);
unSpentBTC.setVout(1);
List arrayList = new ArrayList();
arrayList.add(unSpentBTC);
String signBTCTransactionData = signBTCTransactionData(arrayList “mnzmwSmYy9z4a6MwU4bYb1zWH5iLZu3BBA“ “mkkz9NYnVLLey6vD5VrhY35mn8jVJdF5YC“ “cNpuiUKjawfxD9SxP2ycC44CusbfCvfDRYyVTzZgHbz3M1ENHczx“ 1000L 666L);
System.out.println(signBTCTransactionData);

}


    /**
     * @param unSpentBTCList 未花费utxo集合
     * @param from 发送者地址
     * @param to 接收者地址
     * @param privateKey 私钥
     * @param value 发送金额.单位:聪
     * @param fee 旷工费.单位:聪
     * @return  签名之后未广播的原生交易字符串
     * @throws Exception
     */
    public static String signBTCTransactionData(List unSpentBTCList String from String to String privateKey long value long fee) throws Exception {
        NetworkParameters networkParameters = null;
//            networkParameters = MainNetParams.get();
           
            //测试网络
            networkParameters = TestNet3Params.get();
        Transaction transaction = new Transaction(networkParameters);
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.frombase58(networkParameters privateKey);

        ECKey ecKey = dumpedPrivateKey.getKey();

        long totalMoney = 0;
        List utxos = new ArrayList();
        //遍历未花费列表,组装合适的item
        for (UnSpentBTC us : unSpentBTCList) {
            if (totalMoney >= (value + fee))
                break;
            UTXO utxo = new UTXO(Sha256Hash.wrap(us.getTxid()) us.getVout() Coin.valueOf(us.getSatoshis())
                    us.getHeight() false new script(Hex.decode(us.getscriptPubKey())));
            utxos.add(utxo);
            totalMoney += us.getSatoshis();
        }

        transaction.addOutput(Coin.valueOf(value) Address.frombase58(networkParameters to));
        // transaction.

        //消费列表总金额 - 已经转账的金额 - 手续费 就等于需要返回给自己的金额了
        long balance = totalMoney - value - fee;
        //输出-转给自己
        if (balance > 0) {
            transaction.addOutput(Coin.valueOf(balance) Address.frombase58(networkParameters from));
        }
        //输入未消费列表项
        for (UTXO utxo : utxos) {
            Tra

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-09 09:55  bitcoinj\
     文件        1431  2018-08-09 09:55  bitcoinj\.classpath
     文件         560  2018-08-09 09:55  bitcoinj\.project
     目录           0  2018-08-09 09:55  bitcoinj\.settings\
     文件         243  2018-08-09 09:55  bitcoinj\.settings\org.eclipse.jdt.core.prefs
     文件          90  2018-08-09 09:55  bitcoinj\.settings\org.eclipse.m2e.core.prefs
     文件        4170  2018-08-09 10:03  bitcoinj\pom.xml
     目录           0  2018-08-09 09:55  bitcoinj\src\
     目录           0  2018-08-09 09:55  bitcoinj\src\main\
     目录           0  2018-08-30 16:20  bitcoinj\src\main\java\
     目录           0  2018-09-10 09:40  bitcoinj\src\main\java\bitcoinj\
     文件        3519  2018-09-10 09:50  bitcoinj\src\main\java\bitcoinj\SendRequest.java
     文件        2546  2018-09-10 09:42  bitcoinj\src\main\java\bitcoinj\UnSpentBTC.java
     目录           0  2018-09-10 09:51  bitcoinj\src\main\resources\
     目录           0  2018-08-09 09:55  bitcoinj\src\test\
     目录           0  2018-09-10 09:40  bitcoinj\src\test\java\
     目录           0  2018-08-09 10:03  bitcoinj\src\test\java\message\
     文件        3155  2018-09-10 09:45  bitcoinj\src\test\java\message\Message.java
     目录           0  2018-09-10 09:51  bitcoinj\src\test\resources\
     目录           0  2018-08-09 09:55  bitcoinj\target\
     目录           0  2018-08-30 16:20  bitcoinj\target\classes\
     目录           0  2018-08-16 10:23  bitcoinj\target\classes\meta-INF\
     文件         104  2018-09-10 09:23  bitcoinj\target\classes\meta-INF\MANIFEST.MF
     目录           0  2018-08-16 10:23  bitcoinj\target\classes\meta-INF\maven\
     目录           0  2018-08-16 10:23  bitcoinj\target\classes\meta-INF\maven\com.cmc\
     目录           0  2018-08-16 10:23  bitcoinj\target\classes\meta-INF\maven\com.cmc\bitcoinj\
     文件         210  2018-09-10 09:23  bitcoinj\target\classes\meta-INF\maven\com.cmc\bitcoinj\pom.properties
     文件        4170  2018-09-10 09:23  bitcoinj\target\classes\meta-INF\maven\com.cmc\bitcoinj\pom.xml
     目录           0  2018-09-10 09:40  bitcoinj\target\classes\bitcoinj\
     文件        5044  2018-09-10 09:50  bitcoinj\target\classes\bitcoinj\SendRequest.class
     文件        2883  2018-09-10 09:42  bitcoinj\target\classes\bitcoinj\UnSpentBTC.class
............此处省略3个文件信息

评论

共有 条评论

相关资源