本文整理汇总了Java中org.bitcoinj.crypto.TransactionSignature类的典型用法代码示例。如果您正苦于以下问题:Java TransactionSignature类的具体用法?Java TransactionSignature怎么用?Java TransactionSignature使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TransactionSignature类属于org.bitcoinj.crypto包,在下文中一共展示了TransactionSignature类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateSigScript
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
public Script calculateSigScript(Transaction tx, int inOffset, Wallet w) {
assert(inOffset >= 0);
//get key by pubkeyhash from wallet
ECKey key = w.findKeyFromPubHash(this.pubkeyHash);
assert(key != null);
TransactionSignature ts = tx.calculateSignature(inOffset, key, redeemScript, SigHash.ALL, false);
ScriptBuilder sb = new ScriptBuilder();
byte[] sigEncoded = ts.encodeToBitcoin();
sb.data(sigEncoded);
assert(TransactionSignature.isEncodingCanonical(sigEncoded));
sb.data(key.getPubKey());
sb.data(redeemScript.getProgram());
return sb.build();
}
示例2: createP2SHMultiSigInputScript
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/**
* Create a program that satisfies a pay-to-script hashed OP_CHECKMULTISIG program.
* If given signature list is null, incomplete scriptSig will be created with OP_0 instead of signatures
*/
public static Script createP2SHMultiSigInputScript(@Nullable List<TransactionSignature> signatures,
Script multisigProgram) {
List<byte[]> sigs = new ArrayList<>();
if (signatures == null) {
// create correct number of empty signatures
int numSigs = multisigProgram.getNumberOfSignaturesRequiredToSpend();
for (int i = 0; i < numSigs; i++)
sigs.add(new byte[]{});
} else {
for (TransactionSignature signature : signatures) {
sigs.add(signature.encodeToBitcoin());
}
}
return createMultiSigInputScriptBytes(sigs, multisigProgram.getProgram());
}
示例3: isInputStandard
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
if (chunk.data != null && !chunk.isShortestPossiblePushData())
return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
if (chunk.isPushData()) {
ECDSASignature signature;
try {
signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
} catch (IllegalArgumentException x) {
// Doesn't look like a signature.
signature = null;
}
if (signature != null) {
if (!TransactionSignature.isEncodingCanonical(chunk.data))
return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
if (!signature.isCanonical())
return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
}
}
}
return RuleViolation.NONE;
}
示例4: addSignedInput
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/**
* Adds a new and fully signed input for the given parameters. Note that this method is <b>not</b> thread safe
* and requires external synchronization. Please refer to general documentation on Bitcoin scripting and contracts
* to understand the values of sigHash and anyoneCanPay: otherwise you can use the other form of this method
* that sets them to typical defaults.
*
* @throws ScriptException if the scriptPubKey is not a pay to address or pay to pubkey script.
*/
public TransactionInput addSignedInput(TransactionOutPoint prevOut, Script scriptPubKey, ECKey sigKey,
SigHash sigHash, boolean anyoneCanPay) throws ScriptException {
// Verify the API user didn't try to do operations out of order.
checkState(!outputs.isEmpty(), "Attempting to sign tx without outputs.");
TransactionInput input = new TransactionInput(params, this, new byte[]{}, prevOut);
addInput(input);
Sha256Hash hash = hashForSignature(inputs.size() - 1, scriptPubKey, sigHash, anyoneCanPay);
ECKey.ECDSASignature ecSig = sigKey.sign(hash);
TransactionSignature txSig = new TransactionSignature(ecSig, sigHash, anyoneCanPay);
if (scriptPubKey.isSentToRawPubKey())
input.setScriptSig(ScriptBuilder.createInputScript(txSig));
else if (scriptPubKey.isSentToAddress())
input.setScriptSig(ScriptBuilder.createInputScript(txSig, sigKey));
else
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Don't know how to sign for this kind of scriptPubKey: " + scriptPubKey);
return input;
}
示例5: signTransaction
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
private List<TransactionSignature> signTransaction(Transaction tx) throws CoinbleskException {
final List<TransactionInput> inputs = tx.getInputs();
final List<TransactionSignature> signatures = new ArrayList<>(inputs.size());
for (int i = 0; i < inputs.size(); ++i) {
TransactionInput txIn = inputs.get(i);
TransactionOutput prevTxOut = txIn.getConnectedOutput();
byte[] sentToHash = prevTxOut.getScriptPubKey().getPubKeyHash();
TimeLockedAddress tla = findTimeLockedAddressByHash(sentToHash);
if (tla == null) {
throw new CoinbleskException(String.format(Locale.US,
"Could not sign input (index=%d, pubKeyHash=%s)",
i, org.bitcoinj.core.Utils.HEX.encode(sentToHash)));
}
byte[] redeemScript = tla.createRedeemScript().getProgram();
TransactionSignature signature = tx.calculateSignature(
i, multisigClientKey, redeemScript, Transaction.SigHash.ALL, false);
signatures.add(signature);
}
return signatures;
}
示例6: createP2SHMultiSigInputScript
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/**
* Create a program that satisfies a pay-to-script hashed OP_CHECKMULTISIG program.
* If given signature list is null, incomplete scriptSig will be created with OP_0 instead of signatures
*/
public static Script createP2SHMultiSigInputScript(@Nullable List<TransactionSignature> signatures,
Script multisigProgram) {
List<byte[]> sigs = new ArrayList<byte[]>();
if (signatures == null) {
// create correct number of empty signatures
int numSigs = multisigProgram.getNumberOfSignaturesRequiredToSpend();
for (int i = 0; i < numSigs; i++)
sigs.add(new byte[]{});
} else {
for (TransactionSignature signature : signatures) {
sigs.add(signature.encodeToBitcoin());
}
}
return createMultiSigInputScriptBytes(sigs, multisigProgram.getProgram());
}
示例7: isInputStandard
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
if (chunk.data != null && !chunk.isShortestPossiblePushData())
return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
if (chunk.isPushData()) {
ECDSASignature signature;
try {
signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
} catch (RuntimeException x) {
// Doesn't look like a signature.
signature = null;
}
if (signature != null) {
if (!TransactionSignature.isEncodingCanonical(chunk.data))
return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
if (!signature.isCanonical())
return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
}
}
}
return RuleViolation.NONE;
}
示例8: addSignedInput
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/**
* Adds a new and fully signed input for the given parameters. Note that this method is <b>not</b> thread safe
* and requires external synchronization. Please refer to general documentation on Bitcoin scripting and contracts
* to understand the values of sigHash and anyoneCanPay: otherwise you can use the other form of this method
* that sets them to typical defaults.
*
* @throws ScriptException if the scriptPubKey is not a pay to address or pay to pubkey script.
*/
public TransactionInput addSignedInput(TransactionOutPoint prevOut, Script scriptPubKey, ECKey sigKey,
SigHash sigHash, boolean anyoneCanPay) throws ScriptException {
// Verify the API user didn't try to do operations out of order.
checkState(!outputs.isEmpty(), "Attempting to sign tx without outputs.");
TransactionInput input = new TransactionInput(params, this, new byte[]{}, prevOut);
addInput(input);
Sha256Hash hash = hashForSignature(inputs.size() - 1, scriptPubKey, sigHash, anyoneCanPay);
ECKey.ECDSASignature ecSig = sigKey.sign(hash);
TransactionSignature txSig = new TransactionSignature(ecSig, sigHash, anyoneCanPay);
if (scriptPubKey.isSentToRawPubKey())
input.setScriptSig(ScriptBuilder.createInputScript(txSig));
else if (scriptPubKey.isSentToAddress())
input.setScriptSig(ScriptBuilder.createInputScript(txSig, sigKey));
else
throw new ScriptException("Don't know how to sign for this kind of scriptPubKey: " + scriptPubKey);
return input;
}
示例9: createFakeTxWithChangeAddress
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
/**
* Create a fake TX of sufficient realism to exercise the unit tests. Two outputs, one to us, one to somewhere
* else to simulate change. There is one random input.
*/
public static Transaction createFakeTxWithChangeAddress(NetworkParameters params, Coin value, Address to, Address changeOutput) {
Transaction t = new Transaction(params);
TransactionOutput outputToMe = new TransactionOutput(params, t, value, to);
t.addOutput(outputToMe);
TransactionOutput change = new TransactionOutput(params, t, valueOf(1, 11), changeOutput);
t.addOutput(change);
// Make a previous tx simply to send us sufficient coins. This prev tx is not really valid but it doesn't
// matter for our purposes.
Transaction prevTx = new Transaction(params);
TransactionOutput prevOut = new TransactionOutput(params, prevTx, value, to);
prevTx.addOutput(prevOut);
// Connect it.
t.addInput(prevOut).setScriptSig(ScriptBuilder.createInputScript(TransactionSignature.dummy()));
// Fake signature.
// Serialize/deserialize to ensure internal state is stripped, as if it had been read from the wire.
return roundTripTransaction(params, t);
}
示例10: addOnlyInputToTransaction
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
private void addOnlyInputToTransaction(Transaction t, TransactionOutPointWithValue prevOut, long sequence) throws ScriptException {
TransactionInput input = new TransactionInput(params, t, new byte[]{}, prevOut.outpoint);
input.setSequenceNumber(sequence);
t.addInput(input);
if (prevOut.scriptPubKey.getChunks().get(0).equalsOpCode(OP_TRUE)) {
input.setScriptSig(new ScriptBuilder().op(OP_1).build());
} else {
// Sign input
checkState(prevOut.scriptPubKey.isSentToRawPubKey());
Sha256Hash hash = t.hashForSignature(0, prevOut.scriptPubKey, SigHash.ALL, false);
input.setScriptSig(ScriptBuilder.createInputScript(
new TransactionSignature(coinbaseOutKey.sign(hash), SigHash.ALL, false))
);
}
}
示例11: testCanonicalSigs
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
@Test
public void testCanonicalSigs() throws Exception {
// Tests the canonical sigs from Bitcoin Core unit tests
InputStream in = getClass().getResourceAsStream("sig_canonical.json");
// Poor man's JSON parser (because pulling in a lib for this is overkill)
while (in.available() > 0) {
while (in.available() > 0 && in.read() != '"') ;
if (in.available() < 1)
break;
StringBuilder sig = new StringBuilder();
int c;
while (in.available() > 0 && (c = in.read()) != '"')
sig.append((char)c);
assertTrue(TransactionSignature.isEncodingCanonical(HEX.decode(sig.toString())));
}
in.close();
}
示例12: testNonCanonicalSigs
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
@Test
public void testNonCanonicalSigs() throws Exception {
// Tests the noncanonical sigs from Bitcoin Core unit tests
InputStream in = getClass().getResourceAsStream("sig_noncanonical.json");
// Poor man's JSON parser (because pulling in a lib for this is overkill)
while (in.available() > 0) {
while (in.available() > 0 && in.read() != '"') ;
if (in.available() < 1)
break;
StringBuilder sig = new StringBuilder();
int c;
while (in.available() > 0 && (c = in.read()) != '"')
sig.append((char)c);
try {
final String sigStr = sig.toString();
assertFalse(TransactionSignature.isEncodingCanonical(HEX.decode(sigStr)));
} catch (IllegalArgumentException e) {
// Expected for non-hex strings in the JSON that we should ignore
}
}
in.close();
}
示例13: testCreatedSigAndPubkeyAreCanonical
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
@Test
public void testCreatedSigAndPubkeyAreCanonical() throws Exception {
// Tests that we will not generate non-canonical pubkeys or signatures
// We dump failed data to error log because this test is not expected to be deterministic
ECKey key = new ECKey();
if (!ECKey.isPubKeyCanonical(key.getPubKey())) {
log.error(Utils.HEX.encode(key.getPubKey()));
fail();
}
byte[] hash = new byte[32];
new Random().nextBytes(hash);
byte[] sigBytes = key.sign(Sha256Hash.wrap(hash)).encodeToDER();
byte[] encodedSig = Arrays.copyOf(sigBytes, sigBytes.length + 1);
encodedSig[sigBytes.length] = Transaction.SigHash.ALL.byteValue();
if (!TransactionSignature.isEncodingCanonical(encodedSig)) {
log.error(Utils.HEX.encode(sigBytes));
fail();
}
}
示例14: getSignatureObject
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
@Override
public ChannelSignatures getSignatureObject (Channel channel, Transaction channelTransaction) {
ChannelSignatures channelSignatures = new ChannelSignatures();
channelSignatures.channelSignatures = Tools.getChannelSignatures(channel, channelTransaction);
List<Transaction> paymentTransactions = getPaymentTransactions(channelTransaction.getHash(), channel.channelStatus, channel.keyServer, channel.keyClient);
List<TransactionSignature> signatureList = new ArrayList<>();
int index = 2;
for (Transaction t : paymentTransactions) {
TransactionSignature sig = Tools.getSignature(t, 0, channelTransaction.getOutput(index).getScriptBytes(), channel.getKeyServer());
signatureList.add(sig);
index++;
}
channelSignatures.paymentSignatures = signatureList;
return channelSignatures;
}
示例15: testSigning
import org.bitcoinj.crypto.TransactionSignature; //导入依赖的package包/类
@Test
public void testSigning() throws AddressFormatException {
NetworkParameters netParams = TestNet3Params.get();
HexBinaryAdapter adapter = new HexBinaryAdapter();
Context context = Context.getOrCreate(netParams);
byte[] bx = adapter.unmarshal("0100000001bd5ee90ffe5eedd67c09c3bb348dd7dc1308800eb221b1c92dda651010519ba3010000006a4730440220467868c0b2ed001a915cca5269b928698bee8aba4fe454e1d775070d9e4041cb02205d1c979dbc75e5dc656c4e9d5969d716a383797bd5ad5df79a13d0d6e3f51ccb012102403adb7674f25212bc8cf4a97797154a4980c60e9f328c90300b71a8a04389c7ffffffff024088db60000000001976a914990628d3670f439a5f9e0dfa6492b8bbf3b3fa1b88ac76cf6edd050000001976a914b679378d01ee7203a454bca2ad25698ef23a056388ac00000000");
org.bitcoinj.core.Transaction testbx = new org.bitcoinj.core.Transaction(netParams, bx);
org.bitcoinj.core.Transaction tx = new org.bitcoinj.core.Transaction(netParams);
tx.addOutput(org.bitcoinj.core.Coin.SATOSHI.multiply(testbx.getOutput(0).getValue().value - 50000l), new Address(netParams, "mobDb19geJ66kkQnsSYvN9PNEKNDiNBHEp"));
System.out.println(testbx.getOutput(0));
tx.addInput(testbx.getOutput(0));
String seckey = "3EC95EBFEDCF77373BABA0DE345A0962E51344CD2D0C8DBDF93AEFD0B66BE240";
byte[] privkey = Hex.decode(seckey);
ECKey ecPriv = ECKey.fromPrivate(privkey);
Sha256Hash hash2 = tx.hashForSignature(0, testbx.getOutput(0).getScriptPubKey().getProgram(), Transaction.SigHash.ALL, false);
ECKey.ECDSASignature ecSig = ecPriv.sign(hash2);
TransactionSignature txSig = new TransactionSignature(ecSig, Transaction.SigHash.ALL, false);
Script inputScript = ScriptBuilder.createInputScript(txSig, ECKey.fromPublicOnly(ecPriv.getPubKey()));
tx.getInput(0).setScriptSig(inputScript);
String hexBin = DatatypeConverter.printHexBinary(tx.bitcoinSerialize());
System.out.println(hexBin);
tx.getInput(0).verify(testbx.getOutput(0));
// SUCCESSFULLY BROADCAST WOO!
}