• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java KeyBag类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.bitcoinj.wallet.KeyBag的典型用法代码示例。如果您正苦于以下问题:Java KeyBag类的具体用法?Java KeyBag怎么用?Java KeyBag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



KeyBag类属于org.bitcoinj.wallet包,在下文中一共展示了KeyBag类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: getConnectedKey

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
/**
 * Returns the ECKey identified in the connected output, for either pay-to-address scripts or pay-to-key scripts.
 * For P2SH scripts you can use {@link #getConnectedRedeemData(org.bitcoinj.wallet.KeyBag)} and then get the
 * key from RedeemData.
 * If the script form cannot be understood, throws ScriptException.
 *
 * @return an ECKey or null if the connected key cannot be found in the wallet.
 */
@Nullable
public ECKey getConnectedKey(KeyBag keyBag) throws ScriptException {
    TransactionOutput connectedOutput = getConnectedOutput();
    checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key");
    Script connectedScript = connectedOutput.getScriptPubKey();
    if (connectedScript.isSentToAddress()) {
        byte[] addressBytes = connectedScript.getPubKeyHash();
        return keyBag.findKeyFromPubHash(addressBytes);
    } else if (connectedScript.isSentToRawPubKey()) {
        byte[] pubkeyBytes = connectedScript.getPubKey();
        return keyBag.findKeyFromPubKey(pubkeyBytes);
    } else {
        throw new ScriptException("Could not understand form of connected output script: " + connectedScript);
    }
}
 
开发者ID:HashEngineering,项目名称:namecoinj,代码行数:24,代码来源:TransactionOutPoint.java


示例2: getConnectedRedeemData

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
/**
 * Returns the RedeemData identified in the connected output, for either pay-to-address scripts, pay-to-key
 * or P2SH scripts.
 * If the script forms cannot be understood, throws ScriptException.
 *
 * @return a RedeemData or null if the connected data cannot be found in the wallet.
 */
@Nullable
public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException {
    TransactionOutput connectedOutput = getConnectedOutput();
    checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key");
    Script connectedScript = connectedOutput.getScriptPubKey();
    if (connectedScript.isSentToAddress()) {
        byte[] addressBytes = connectedScript.getPubKeyHash();
        return RedeemData.of(keyBag.findKeyFromPubHash(addressBytes), connectedScript);
    } else if (connectedScript.isSentToRawPubKey()) {
        byte[] pubkeyBytes = connectedScript.getPubKey();
        return RedeemData.of(keyBag.findKeyFromPubKey(pubkeyBytes), connectedScript);
    } else if (connectedScript.isPayToScriptHash()) {
        byte[] scriptHash = connectedScript.getPubKeyHash();
        return keyBag.findRedeemDataFromScriptHash(scriptHash);
    } else {
        throw new ScriptException("Could not understand form of connected output script: " + connectedScript);
    }
}
 
开发者ID:HashEngineering,项目名称:namecoinj,代码行数:26,代码来源:TransactionOutPoint.java


示例3: signInputs

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
	//Dummy check. We will base our accept/reject criteria off of this.
    Scanner in = new Scanner(System.in);
    System.out.println("TransactionSigner: " + description + ", do you want to sign this transaxtion? [y/n]");
    System.out.println("Here we will send SMS message and wait for confirmation");
    
    if(in.equals("y") || in.equals("yes")){
 	return super.signInputs(propTx, keyBag);
    } else {
    	return false;
    }
}
 
开发者ID:JohnnyCryptoCoin,项目名称:speciebox,代码行数:14,代码来源:PluggableTransactionSigner.java


示例4: addOutputs

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
private static void addOutputs(final Transaction outputTransaction, final KeyBag bag) throws ScriptException {
    int numInputs = outputTransaction.getInputs().size();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = outputTransaction.getInput(i);
        Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
        RedeemData redeemData = txIn.getConnectedRedeemData(bag);
        checkNotNull(redeemData, "Transaction exists in wallet that we cannot redeem: %s", txIn.getOutpoint().getHash());
        txIn.setScriptSig(scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript));
    }
}
 
开发者ID:HashEngineering,项目名称:dashj,代码行数:11,代码来源:GenerateLowSTests.java


示例5: signInputs

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    if (missingSigsMode == Wallet.MissingSigsMode.USE_OP_ZERO)
        return true;

    int numInputs = propTx.partialTx.getInputs().size();
    byte[] dummySig = TransactionSignature.dummy().encodeToBitcoin();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = propTx.partialTx.getInput(i);
        if (txIn.getConnectedOutput() == null) {
            log.warn("Missing connected output, assuming input {} is already signed.", i);
            continue;
        }

        Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
        Script inputScript = txIn.getScriptSig();
        if (scriptPubKey.isPayToScriptHash() || scriptPubKey.isSentToMultiSig()) {
            int sigSuffixCount = scriptPubKey.isPayToScriptHash() ? 1 : 0;
            // all chunks except the first one (OP_0) and the last (redeem script) are signatures
            for (int j = 1; j < inputScript.getChunks().size() - sigSuffixCount; j++) {
                ScriptChunk scriptChunk = inputScript.getChunks().get(j);
                if (scriptChunk.equalsOpCode(0)) {
                    if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                        throw new MissingSignatureException();
                    } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                        txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, j - 1));
                    }
                }
            }
        } else {
            if (inputScript.getChunks().get(0).equalsOpCode(0)) {
                if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                    throw new ECKey.MissingPrivateKeyException();
                } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                    txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, 0));
                }
            }
        }
        // TODO handle non-P2SH multisig
    }
    return true;
}
 
开发者ID:guodroid,项目名称:okwallet,代码行数:43,代码来源:MissingSigResolutionSigner.java


示例6: signInputs

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    Transaction tx = propTx.partialTx;
    int numInputs = tx.getInputs().size();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = tx.getInput(i);
        TransactionOutput txOut = txIn.getConnectedOutput();
        if (txOut == null) {
            continue;
        }
        Script scriptPubKey = txOut.getScriptPubKey();
        if (!scriptPubKey.isPayToScriptHash()) {
            log.warn("CustomTransactionSigner works only with P2SH transactions");
            return false;
        }

        Script inputScript = checkNotNull(txIn.getScriptSig());

        try {
            // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when
            // we sign missing pieces (to check this would require either assuming any signatures are signing
            // standard output types or a way to get processed signatures out of script execution)
            txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey());
            log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i);
            continue;
        } catch (ScriptException e) {
            // Expected.
        }

        RedeemData redeemData = txIn.getConnectedRedeemData(keyBag);
        if (redeemData == null) {
            log.warn("No redeem data found for input {}", i);
            continue;
        }

        Sha256Hash sighash = tx.hashForSignature(i, redeemData.redeemScript, Transaction.SigHash.ALL, false);
        SignatureAndKey sigKey = getSignature(sighash, propTx.keyPaths.get(scriptPubKey));
        TransactionSignature txSig = new TransactionSignature(sigKey.sig, Transaction.SigHash.ALL, false);
        int sigIndex = inputScript.getSigInsertionIndex(sighash, sigKey.pubKey);
        inputScript = scriptPubKey.getScriptSigWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex);
        txIn.setScriptSig(inputScript);
    }
    return true;
}
 
开发者ID:guodroid,项目名称:okwallet,代码行数:45,代码来源:CustomTransactionSigner.java


示例7: getConnectedRedeemData

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
/**
 * Alias for getOutpoint().getConnectedRedeemData(keyBag)
 * @see TransactionOutPoint#getConnectedRedeemData(org.bitcoinj.wallet.KeyBag)
 */
@Nullable
public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException {
    return getOutpoint().getConnectedRedeemData(keyBag);
}
 
开发者ID:guodroid,项目名称:okwallet,代码行数:9,代码来源:TransactionInput.java


示例8: signInputs

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
@Override
public boolean signInputs(ProposedTransaction t, KeyBag keyBag) {
    return false;
}
 
开发者ID:Grant-Redmond,项目名称:cryptwallet,代码行数:5,代码来源:NopTransactionSigner.java


示例9: signTransaction

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
/**
 * <p>Given a send request containing transaction, attempts to sign it's inputs. This method expects transaction
 * to have all necessary inputs connected or they will be ignored.</p>
 * <p>Actual signing is done by pluggable {@link org.bitcoinj.signers.LocalTransactionSigner}
 * and it's not guaranteed that transaction will be complete in the end.</p>
 */
void signTransaction(BitSendRequest req) {
    lock.lock();
    try {
        Transaction tx = req.tx.getRawTransaction();
        List<TransactionInput> inputs = tx.getInputs();
        List<TransactionOutput> outputs = tx.getOutputs();
        Preconditions.checkState(inputs.size() > 0);
        Preconditions.checkState(outputs.size() > 0);

        KeyBag maybeDecryptingKeyBag = new DecryptingKeyBag(account, req.aesKey);

        int numInputs = tx.getInputs().size();
        for (int i = 0; i < numInputs; i++) {
            TransactionInput txIn = tx.getInput(i);
            if (txIn.getConnectedOutput() == null) {
                log.warn("Missing connected output, assuming input {} is already signed.", i);
                continue;
            }

            try {
                // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when
                // we sign missing pieces (to check this would require either assuming any signatures are signing
                // standard output types or a way to get processed signatures out of script execution)
                txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey());
                log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i);
                continue;
            } catch (ScriptException e) {
                // Expected.
            }

            Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
            RedeemData redeemData = txIn.getConnectedRedeemData(maybeDecryptingKeyBag);
            Preconditions.checkNotNull(redeemData, "Transaction exists in wallet that we cannot redeem: %s", txIn.getOutpoint().getHash());
            txIn.setScriptSig(scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript));
        }

        TransactionSigner.ProposedTransaction proposal = new TransactionSigner.ProposedTransaction(tx);
        TransactionSigner signer = new LocalTransactionSigner();
        if (!signer.signInputs(proposal, maybeDecryptingKeyBag)) {
            log.info("{} returned false for the tx", signer.getClass().getName());
        }

        // resolve missing sigs if any
        new MissingSigResolutionSigner(req.missingSigsMode).signInputs(proposal, maybeDecryptingKeyBag);
    } finally {
        lock.unlock();
    }
}
 
开发者ID:filipnyquist,项目名称:lbry-android,代码行数:55,代码来源:TransactionCreator.java


示例10: main

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
public static void main(final String[] argv) throws NoSuchAlgorithmException, IOException {
    final NetworkParameters params = new MainNetParams();
    final LocalTransactionSigner signer = new LocalTransactionSigner();
    final SecureRandom secureRandom = SecureRandom.getInstanceStrong();
    final ECKey key = new ECKey(secureRandom);
    final KeyBag bag = new KeyBag() {
        @Override
        public ECKey findKeyFromPubHash(byte[] pubkeyHash) {
            return key;
        }

        @Override
        public ECKey findKeyFromPubKey(byte[] pubkey) {
            return key;
        }

        @Override
        public RedeemData findRedeemDataFromScriptHash(byte[] scriptHash) {
            return null;
        }

    };

    // Generate a fictional output transaction we take values from, and
    // an input transaction for the test case

    final Transaction outputTransaction = new Transaction(params);
    final Transaction inputTransaction = new Transaction(params);
    final TransactionOutput output = new TransactionOutput(params, inputTransaction, Coin.ZERO, key.toAddress(params));

    inputTransaction.addOutput(output);
    outputTransaction.addInput(output);
    outputTransaction.addOutput(Coin.ZERO, new ECKey(secureRandom).toAddress(params));

    addOutputs(outputTransaction, bag);

    // Sign the transaction
    final ProposedTransaction proposedTransaction = new ProposedTransaction(outputTransaction);
    signer.signInputs(proposedTransaction, bag);
    final TransactionInput input = proposedTransaction.partialTx.getInput(0);

    input.verify(output);
    input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(),
        EnumSet.of(Script.VerifyFlag.DERSIG, Script.VerifyFlag.P2SH));

    final Script scriptSig = input.getScriptSig();
    final TransactionSignature signature = TransactionSignature.decodeFromBitcoin(scriptSig.getChunks().get(0).data, true, false);

    // First output a conventional low-S transaction with the LOW_S flag, for the tx_valid.json set
    System.out.println("[\"A transaction with a low-S signature.\"],");
    System.out.println("[[[\""
        + inputTransaction.getHashAsString() + "\", "
        + output.getIndex() + ", \""
        + scriptToString(output.getScriptPubKey()) + "\"]],\n"
        + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \""
        + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],");

    final BigInteger highS = HIGH_S_DIFFERENCE.subtract(signature.s);
    final TransactionSignature highSig = new TransactionSignature(signature.r, highS);
    input.setScriptSig(new ScriptBuilder().data(highSig.encodeToBitcoin()).data(scriptSig.getChunks().get(1).data).build());
    input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(),
        EnumSet.of(Script.VerifyFlag.P2SH));

    // A high-S transaction without the LOW_S flag, for the tx_valid.json set
    System.out.println("[\"A transaction with a high-S signature.\"],");
    System.out.println("[[[\""
        + inputTransaction.getHashAsString() + "\", "
        + output.getIndex() + ", \""
        + scriptToString(output.getScriptPubKey()) + "\"]],\n"
        + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \""
        + Script.VerifyFlag.P2SH.name() + "\"],");

    // Lastly a conventional high-S transaction with the LOW_S flag, for the tx_invalid.json set
    System.out.println("[\"A transaction with a high-S signature.\"],");
    System.out.println("[[[\""
        + inputTransaction.getHashAsString() + "\", "
        + output.getIndex() + ", \""
        + scriptToString(output.getScriptPubKey()) + "\"]],\n"
        + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \""
        + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],");
}
 
开发者ID:HashEngineering,项目名称:dashj,代码行数:82,代码来源:GenerateLowSTests.java


示例11: signInputs

import org.bitcoinj.wallet.KeyBag; //导入依赖的package包/类
/**
 * Signs given transaction's inputs.
 * Returns true if signer is compatible with given transaction (can do something meaningful with it).
 * Otherwise this method returns false
 */
boolean signInputs(ProposedTransaction propTx, KeyBag keyBag);
 
开发者ID:guodroid,项目名称:okwallet,代码行数:7,代码来源:TransactionSigner.java



注:本文中的org.bitcoinj.wallet.KeyBag类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java ApplicationEventType类代码示例发布时间:2022-05-22
下一篇:
Java AutoCompleteSettings类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap