大家好,我是小a,不太经常写博客,比较菜,之前没做过utxo模型,这几天在疯狂弥补自己的不足-。-研究还不够深入,如有不正确地方还望指出,以后写博客我也尽量回去用英文,或者自己进行google翻译,现在越来越觉得英文很重要,认知到自己很多不足,希望大家可以一起学习英语,如果实在不行就边google边看意思,以及语法。
In fact, ltc belongs to the btc series. It is not difficult to find the source code. (In fact, the source code is the same -_-||, 1co’s altcoin), except that the network is different from the addressHeader, basically the same.。
The generated address is actually the same as btc, it is a network, it is very simple
.
// create address
//todo Here LitecoinNetParameters has a link in github as
[Ltc, doge's network description](https://github.com/dogecoin/libdohj/tree/master/core/src/main/java/org/libdohj/params)
LitecoinNetParameters networkParameters = LitecoinNetParameters.get();
ECKey ecKey = new ECKey();
LegacyAddress address = LegacyAddress.fromKey(networkParameters, ecKey);
String privateKey = ecKey.getPrivateKeyEncoded(networkParameters).toString();
System.out.println("address :" + address1.toBase58());
System.out.println("privateKey :" + privateKey);
Here I started to explain the offline signature. There is nothing different from btc. In addition to changing a network, I can’t think of it. It’s just a change.
.
// sign transaction
/**
* ltc offline sign
* @param changeAddress //todo The change address can be entered. Here I pass the change as a parameter, and the exchange and wallet are not recommended.
* @param toAddressList //todo The roll-out address is a collection, you can also change to a single
* @param outAmount //todo Transfer out the amount, here is not made a list, you want to utxo one-to-one correspondence can be their own transformation
* @param fee //todo fee
* @param inputUtxos //todo Mainly business parameters, you can package according to your business.
* @return Signed rawHex parameter,Can be used for broadcasting
* @throws Exception
*/
public String ltcSign(String changeAddress, List<String> toAddressList, long outAmount, long fee, List<BtcUtxo> inputUtxos) {
if (null == changeAddress) {
return "changeAddress is null";
}
if (CollectionUtils.isEmpty(inputUtxos)) {
return "utxos is null";
}
LitecoinNetParameters networkParameters = LitecoinNetParameters.get();
Transaction transaction = new Transaction(networkParameters);
if (toAddressList.size() > 0) { //todo Multiple receiving addresses
for (int i = 0; i <toAddressList.size() ; i++) {
transaction.addOutput(Coin.valueOf(outAmount), LegacyAddress.fromBase58(networkParameters, toAddressList.get(i)));
}
}
//todo Change address output (input total amount - total transfer amount - handling fee)
long inputTotalAmount = inputUtxos.stream().mapToLong(BtcUtxo::getValue).sum();
//todo Amount * toAddressList.size() is written because the amount has not been split into multiples. The amount of the transfer must be one-to-one with the address. Temporary use of a fixed amount * Total number of utxo
long changeAmount = inputTotalAmount - outAmount * toAddressList.size() -fee;
//todo If the change is greater than 0, it will be transferred to the change address. This has been changed to a parameter and can be changed to zero. For security advice, write a fixed address here. Avoid misuse.
if (changeAmount > 0) {
transaction.addOutput(Coin.valueOf(changeAmount), LegacyAddress.fromBase58(networkParameters, changeAddress));
}
for (BtcUtxo inputUtxo : inputUtxos) {
UTXO utxo = new UTXO(
Sha256Hash.wrap(inputUtxo.getTxid()),
Long.valueOf(inputUtxo.getOutputN()),
Coin.valueOf(inputUtxo.getValue()),
0,
false,
new Script(Hex.decode(inputUtxo.getScript())));
TransactionOutPoint outPoint = new TransactionOutPoint(networkParameters, utxo.getIndex(), utxo.getHash());
transaction.addSignedInput(
outPoint,
utxo.getScript(),
DumpedPrivateKey.fromBase58(networkParameters,inputUtxo.getPrivateKey()).getKey(),
Transaction.SigHash.ALL,
true);
}
String rawHex = Hex.toHexString(transaction.bitcoinSerialize());
log.info("fee:{},utxoAmount:{},changeAmount{}", fee, outAmount * toAddressList.size(), changeAmount);
return rawHex;
}
public static void main(String[] args) throws Exception {
LtcClient ltcClient = new LtcClient();
List<BtcUtxo> utxos = Lists.newArrayList();
BtcUtxo btcUtxo1 = new BtcUtxo()
.setTxid("d49eaa1b9a41bc713a08475e4c5c1af4e2c66e2860803a592d70dbc76182bbac")
.setOutputN("2")
.setValue(1810000)
.setScript("76a9149a32e6692d659e41a135d9e6fe0e3f591574e6cb88ac")
.setPrivateKey("your privateKey");
utxos.add(btcUtxo1); //todo success
BtcUtxo btcUtxo2 = new BtcUtxo()
.setTxid("d49eaa1b9a41bc713a08475e4c5c1af4e2c66e2860803a592d70dbc76182bbac")
.setOutputN("0")
.setValue(100000)
.setScript("76a914d0a9fb2847920a5a9abf3b1a32ee3c7bf1b00f9b88ac")
.setPrivateKey("your privateKey");
utxos.add(btcUtxo2); //todo success
String pk1 = "your privateKey";//todo LQT3XfhBHc5qjj2EnpaBd3tAp4JCa3JPvT
String pk2 = "your privateKey";//todo LZHHJMMrScT7ZQU45BNzvoZ5PMagWdbUS8
String pk3 = "your privateKey";//todo LZibmf4x2cUZinLUwAmtcbAZR1JGSjaXuk
String pk4 = "your privateKey";//todo LeFGSc8QC2aZacDBEkECookyKjBr76Lkz6
String testaddr = "your privateKey";//todo LNTV6tGkpoLmdd4D8M8CaMapnRysfsQGbX
//todo Transfer multiple addresses (same amount)
List<String> toAddressList = new ArrayList<>();
toAddressList.add("LZibmf4x2cUZinLUwAmtcbAZR1JGSjaXuk");
toAddressList.add("LQT3XfhBHc5qjj2EnpaBd3tAp4JCa3JPvT");
String rawHex = ltcClient.ltcSign("LNTV6tGkpoLmdd4D8M8CaMapnRysfsQGbX",toAddressList, 954000, 1000, utxos);
//todo raw hex is sign result, you push need this.
System.out.println(rawHex);
//todo push,Signature result submission broadcast,if return txid,You need to confirm if the block is greater than 0 or 6 to think that he is successful.
ltcClient.push(rawHex);
This is my many-to-many link to success.
Need to discuss can contact me, QQ 936710254 or WeChat: wr199611