Java Code Examples for org.bitcoinj.script.Script#isSentToRawPubKey()

The following examples show how to use org.bitcoinj.script.Script#isSentToRawPubKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TradeWalletService.java    From bisq-core with GNU Affero General Public License v3.0 6 votes vote down vote up
private void signInput(Transaction transaction, TransactionInput input, int inputIndex) throws SigningException {
    checkNotNull(input.getConnectedOutput(), "input.getConnectedOutput() must not be null");
    Script scriptPubKey = input.getConnectedOutput().getScriptPubKey();
    checkNotNull(wallet);
    ECKey sigKey = input.getOutpoint().getConnectedKey(wallet);
    checkNotNull(sigKey, "signInput: sigKey must not be null. input.getOutpoint()=" + input.getOutpoint().toString());
    if (sigKey.isEncrypted())
        checkNotNull(aesKey);
    Sha256Hash hash = transaction.hashForSignature(inputIndex, scriptPubKey, Transaction.SigHash.ALL, false);
    ECKey.ECDSASignature signature = sigKey.sign(hash, aesKey);
    TransactionSignature txSig = new TransactionSignature(signature, Transaction.SigHash.ALL, false);
    if (scriptPubKey.isSentToRawPubKey()) {
        input.setScriptSig(ScriptBuilder.createInputScript(txSig));
    } else if (scriptPubKey.isSentToAddress()) {
        input.setScriptSig(ScriptBuilder.createInputScript(txSig, sigKey));
    } else {
        throw new SigningException("Don't know how to sign for this kind of scriptPubKey: " + scriptPubKey);
    }
}
 
Example 2
Source File: ElectrumMultiWallet.java    From java-stratum with Apache License 2.0 6 votes vote down vote up
private void doMarkKeysAsUsed(Transaction tx, KeyChainGroup keychain) {
    for (TransactionOutput o : tx.getOutputs()) {
        try {
            Script script = o.getScriptPubKey();
            if (script.isSentToRawPubKey()) {
                byte[] pubkey = script.getPubKey();
                keychain.markPubKeyAsUsed(pubkey);
            } else if (script.isSentToAddress()) {
                byte[] pubkeyHash = script.getPubKeyHash();
                keychain.markPubKeyHashAsUsed(pubkeyHash);
            } else if (script.isPayToScriptHash()) {
                Address a = Address.fromP2SHScript(tx.getParams(), script);
                keychain.markP2SHAddressAsUsed(a);
            }
        } catch (ScriptException e) {
            // Just means we didn't understand the output of this transaction: ignore it.
            log.warn("Could not parse tx output script: {}", e.toString());
        }
    }
}
 
Example 3
Source File: TradeWalletService.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
private void signInput(Transaction transaction, TransactionInput input, int inputIndex) throws SigningException {
    checkNotNull(input.getConnectedOutput(), "input.getConnectedOutput() must not be null");
    Script scriptPubKey = input.getConnectedOutput().getScriptPubKey();
    checkNotNull(wallet);
    ECKey sigKey = input.getOutpoint().getConnectedKey(wallet);
    checkNotNull(sigKey, "signInput: sigKey must not be null. input.getOutpoint()=" +
            input.getOutpoint().toString());
    if (sigKey.isEncrypted()) {
        checkNotNull(aesKey);
    }
    Sha256Hash hash = transaction.hashForSignature(inputIndex, scriptPubKey, Transaction.SigHash.ALL, false);
    ECKey.ECDSASignature signature = sigKey.sign(hash, aesKey);
    TransactionSignature txSig = new TransactionSignature(signature, Transaction.SigHash.ALL, false);
    if (scriptPubKey.isSentToRawPubKey()) {
        input.setScriptSig(ScriptBuilder.createInputScript(txSig));
    } else if (scriptPubKey.isSentToAddress()) {
        input.setScriptSig(ScriptBuilder.createInputScript(txSig, sigKey));
    } else {
        throw new SigningException("Don't know how to sign for this kind of scriptPubKey: " + scriptPubKey);
    }
}
 
Example 4
Source File: KeyTimeCoinSelector.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    try {
        LinkedList<TransactionOutput> gathered = Lists.newLinkedList();
        Coin valueGathered = Coin.ZERO;
        for (TransactionOutput output : candidates) {
            if (ignorePending && !isConfirmed(output))
                continue;
            // Find the key that controls output, assuming it's a regular pay-to-pubkey or pay-to-address output.
            // We ignore any other kind of exotic output on the assumption we can't spend it ourselves.
            final Script scriptPubKey = output.getScriptPubKey();
            ECKey controllingKey;
            if (scriptPubKey.isSentToRawPubKey()) {
                controllingKey = wallet.findKeyFromPubKey(scriptPubKey.getPubKey());
            } else if (scriptPubKey.isSentToAddress()) {
                controllingKey = wallet.findKeyFromPubHash(scriptPubKey.getPubKeyHash());
            } else {
                log.info("Skipping tx output {} because it's not of simple form.", output);
                continue;
            }
            checkNotNull(controllingKey, "Coin selector given output as candidate for which we lack the key");
            if (controllingKey.getCreationTimeSeconds() >= unixTimeSeconds) continue;
            // It's older than the cutoff time so select.
            valueGathered = valueGathered.add(output.getValue());
            gathered.push(output);
            if (gathered.size() >= MAX_SIMULTANEOUS_INPUTS) {
                log.warn("Reached {} inputs, going further would yield a tx that is too large, stopping here.", gathered.size());
                break;
            }
        }
        return new CoinSelection(valueGathered, gathered);
    } catch (ScriptException e) {
        throw new RuntimeException(e);  // We should never have problems understanding scripts in our wallet.
    }
}
 
Example 5
Source File: KeyTimeCoinSelector.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    try {
        LinkedList<TransactionOutput> gathered = Lists.newLinkedList();
        Coin valueGathered = Coin.ZERO;
        for (TransactionOutput output : candidates) {
            if (ignorePending && !isConfirmed(output))
                continue;
            // Find the key that controls output, assuming it's a regular pay-to-pubkey or pay-to-address output.
            // We ignore any other kind of exotic output on the assumption we can't spend it ourselves.
            final Script scriptPubKey = output.getScriptPubKey();
            ECKey controllingKey;
            if (scriptPubKey.isSentToRawPubKey()) {
                controllingKey = wallet.findKeyFromPubKey(scriptPubKey.getPubKey());
            } else if (scriptPubKey.isSentToAddress()) {
                controllingKey = wallet.findKeyFromPubHash(scriptPubKey.getPubKeyHash());
            } else {
                log.info("Skipping tx output {} because it's not of simple form.", output);
                continue;
            }
            checkNotNull(controllingKey, "Coin selector given output as candidate for which we lack the key");
            if (controllingKey.getCreationTimeSeconds() >= unixTimeSeconds) continue;
            // It's older than the cutoff time so select.
            valueGathered = valueGathered.add(output.getValue());
            gathered.push(output);
            if (gathered.size() >= MAX_SIMULTANEOUS_INPUTS) {
                log.warn("Reached {} inputs, going further would yield a tx that is too large, stopping here.", gathered.size());
                break;
            }
        }
        return new CoinSelection(valueGathered, gathered);
    } catch (ScriptException e) {
        throw new RuntimeException(e);  // We should never have problems understanding scripts in our wallet.
    }
}
 
Example 6
Source File: UtxoTrieMgr.java    From jelectrum with MIT License 5 votes vote down vote up
public static byte[] getPublicKeyForTxOut(TransactionOutput out, NetworkParameters params)
{
    byte[] public_key=null;
    Script script = null;
        try
        {  
            script = out.getScriptPubKey();
            if (script.isSentToRawPubKey())
            {  
                byte[] key = out.getScriptPubKey().getPubKey();
                byte[] address_bytes = org.bitcoinj.core.Utils.sha256hash160(key);

                public_key = address_bytes;
            }
            else
            {  
                Address a = script.getToAddress(params);
                public_key = a.getHash160();
            }
        }
        catch(ScriptException e)
        { 
          public_key = figureOutStrange(script, out, params);
       }

  return public_key; 
}