Bitcoin Forum
November 13, 2024, 07:56:12 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Don't know how to sign for this kind of scriptPubKey  (Read 133 times)
lichangyou (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 5


View Profile
July 26, 2021, 11:20:53 AM
Merited by NotATether (5)
 #1

when I send tx but catch this message "Don't know how to sign for this kind of scriptPubKey",I don't know what happen I
hope someone help me,my code is:

 public  String sign(String fromAddress, String toAddress, String privateKey, long amount, long fee, List<UTXO> utxos) throws Exception {
        NetworkParameters networkParameters = MainNetParams.get();
        Transaction transaction = new Transaction(networkParameters);
        String changeAddress = fromAddress;//找零地址
        Long changeAmount = 0L;
        Long utxoAmount = 0L;
        List<UTXO> needUtxos = new ArrayList<UTXO>();
        if (utxos == null || utxos.size() == 0) {
            throw new Exception("未消费列表为空");
        }
        //遍历未花费列表,组装合适的item
        for (UTXO utxo : utxos) {
            if (utxoAmount >= (amount + fee)) {
                break;
            } else {
                needUtxos.add(utxo);
                utxoAmount += utxo.getValue().value;
            }
        }
        transaction.addOutput(Coin.valueOf(amount), LegacyAddress.fromBase58(networkParameters, toAddress));
        //消费列表总金额 - 已经转账的金额 - 手续费 就等于需要返回给自己的金额了
        changeAmount = utxoAmount - (amount + fee);
        //余额判断
        if (changeAmount < 0) {
            throw new Exception("utxo余额不足");
        }
        //输出-转给自己(找零)
        if (changeAmount > 0) {
            transaction.addOutput(Coin.valueOf(changeAmount), LegacyAddress.fromBase58(networkParameters, changeAddress));
        }
        //输入未消费列表项
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(networkParameters, privateKey);
        ECKey ecKey = dumpedPrivateKey.getKey();
        for (UTXO utxo : needUtxos) {
            TransactionOutPoint outPoint = new TransactionOutPoint(networkParameters, utxo.getIndex(), utxo.getHash());
              transaction.addSignedInput(outPoint, utxo.getScript(), ecKey, Transaction.SigHash.ALL, true);
        }
        byte[] bytes = transaction.bitcoinSerialize();
        String hash = Hex.toHexString(bytes);
        log.info("fee:{},utxoAmount:{},changeAmount{}", fee, utxoAmount, changeAmount);
        return hash;
    }

the exception at the red color
BlackHatCoiner
Legendary
*
Offline Offline

Activity: 1694
Merit: 8336


Fiatheist


View Profile WWW
July 26, 2021, 02:14:36 PM
Merited by NotATether (2), ABCbits (1)
 #2

3. What version of programming language do you use (it looks like C# for me, but i'm not sure)?
It isn't C#, but Java and he probably uses the bitcoinj library.

But, OP it's true. You should specify your problem; by saying that it returns you an exception as an error, we can't understand much. Is this your code or have you copied it from somewhere else? You may be more assisted in here: https://bitcoin.stackexchange.com/questions/tagged/bitcoinj

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1186



View Profile WWW
July 27, 2021, 03:05:45 AM
 #3

How did you create that address? Perhaps it is a non-standard script which is why the error says it doesn't know how to sign it. If a custom p2sh what was your script?

NotATether
Legendary
*
Offline Offline

Activity: 1778
Merit: 7374


Top Crypto Casino


View Profile WWW
July 27, 2021, 03:47:26 PM
 #4

TransactionOutPoint.addSignedInput() does not support P2SH or P2WSH scripts so since you are using LegacyAddress class which allows for P2SH script input via the fromBase58() function make sure you are not passing any addresses into the function that are P2SH (begin with a "3").

███████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████

███████████████████████
.
BC.GAME
▄▄▀▀▀▀▀▀▀▄▄
▄▀▀░▄██▀░▀██▄░▀▀▄
▄▀░▐▀▄░▀░░▀░░▀░▄▀▌░▀▄
▄▀▄█▐░▀▄▀▀▀▀▀▄▀░▌█▄▀▄
▄▀░▀░░█░▄███████▄░█░░▀░▀▄
█░█░▀░█████████████░▀░█░█
█░██░▀█▀▀█▄▄█▀▀█▀░██░█
█░█▀██░█▀▀██▀▀█░██▀█░█
▀▄▀██░░░▀▀▄▌▐▄▀▀░░░██▀▄▀
▀▄▀██░░▄░▀▄█▄▀░▄░░██▀▄▀
▀▄░▀█░▄▄▄░▀░▄▄▄░█▀░▄▀
▀▄▄▀▀███▄███▀▀▄▄▀
██████▄▄▄▄▄▄▄██████
.
..CASINO....SPORTS....RACING..


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!