bitcoinj好是好但是需要下载钱包的数据
这个一下可能就20分钟,然后本地还会有300m的节点数据。
如果是android的钱包的话,300m就是比较不可以接受的呢 因为还要不断更新数据。
最好的方法就是使用Insight的api
Insight的api有查询地址的utxo
还有send rawtx交易的功能
android只需要组装rawtx即可,因为他是在本地组装的所以没有泄漏数据的危险。
Insight有js的客户端但是没有java的
看了两天bitcoinj的代码发现使用bitcoinj的原生代码和Insight的数据是可以组装成rawtx的
public static void main(String[] args){
NetworkParameters params = getParams();
PeerGroup peerGroup = new PeerGroup(params);
//Script ss = ScriptBuilder.createOutputScript(Address.fromBase58(null,"3DRXLvePoV6bcT2XksVNNCX5XrG3udgepG"));
byte[] zijie = Utils.parseAsHexOrBase58("76a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac");
Script ss01 = new Script(zijie);
UTXO utxo = new UTXO(
Sha256Hash.wrap("27f5f35b447e219d0b3a3ac55f9dc6aacf2d4145fbd930207ef5b7710c47d883"),
2,
Coin.valueOf((long) 9.12695200),
503962,
false,
ss01);
System.out.println(utxo.toString());
send(
peerGroup,
"2e07613d26fae6e3dea0681244223c4daf2af404337d37b9cd23937f5433b837",
"n4gYQANuxnytXnGRm9R8i1vTf86P6kPLAb",
100,
new ArrayList<UTXO>() {{
add(utxo);
}}
);
}
/**
*
*
* @param peerGroup
* @param privKey 私钥
* @param address 地址
* @param satoshis
* @return
*/
public static Transaction send(PeerGroup peerGroup,
String privKey,
String address,
long satoshis,List<UTXO> utxos) {
NetworkParameters params = getParams();
ECKey key = ECKey.fromPrivate(Numeric.toBigInt(privKey));
//String to an address
Address address2 = Address.fromBase58(params, address);
Transaction tx = new Transaction(params);
//value is a sum of all inputs, fee is 4013
tx.addOutput(Coin.valueOf(satoshis), address2);
//utxos is an array of inputs from my wallet
for(UTXO utxo : utxos)
{
TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
//YOU HAVE TO CHANGE THIS
tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);
}
tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
tx.setPurpose(Transaction.Purpose.USER_PAYMENT);
System.out.println(tx.getHashAsString());
//peerGroup.broadcastTransaction(tx);
return null;
}