本文整理汇总了Java中org.bitcoinj.core.Transaction.SigHash类的典型用法代码示例。如果您正苦于以下问题:Java SigHash类的具体用法?Java SigHash怎么用?Java SigHash使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SigHash类属于org.bitcoinj.core.Transaction包,在下文中一共展示了SigHash类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateSigScript
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的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: addOnlyInputToTransaction
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的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))
);
}
}
示例3: addOnlyInputToTransaction
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的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))
);
}
}
示例4: calcSigHashValue
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
/** Calculates the byte used in the protocol to represent the combination of mode and anyoneCanPay. */
public static int calcSigHashValue(Transaction.SigHash mode, boolean anyoneCanPay) {
Preconditions.checkArgument(SigHash.ALL == mode || SigHash.NONE == mode || SigHash.SINGLE == mode); // enforce compatibility since this code was made before the SigHash enum was updated
int sighashFlags = mode.value;
if (anyoneCanPay)
sighashFlags |= Transaction.SigHash.ANYONECANPAY.value;
return sighashFlags;
}
示例5: isEncodingCanonical
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
/**
* Returns true if the given signature is has canonical encoding, and will thus be accepted as standard by
* Bitcoin Core. DER and the SIGHASH encoding allow for quite some flexibility in how the same structures
* are encoded, and this can open up novel attacks in which a man in the middle takes a transaction and then
* changes its signature such that the transaction hash is different but it's still valid. This can confuse wallets
* and generally violates people's mental model of how Bitcoin should work, thus, non-canonical signatures are now
* not relayed by default.
*/
public static boolean isEncodingCanonical(byte[] signature) {
// See Bitcoin Core's IsCanonicalSignature, https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
// A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
// Where R and S are not negative (their first byte has its highest bit not set), and not
// excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
// in which case a single 0 byte is necessary and even required).
if (signature.length < 9 || signature.length > 73)
return false;
int hashType = (signature[signature.length-1] & 0xff) & ~Transaction.SigHash.ANYONECANPAY.value; // mask the byte to prevent sign-extension hurting us
if (hashType < Transaction.SigHash.ALL.value || hashType > Transaction.SigHash.SINGLE.value)
return false;
// "wrong type" "wrong length marker"
if ((signature[0] & 0xff) != 0x30 || (signature[1] & 0xff) != signature.length-3)
return false;
int lenR = signature[3] & 0xff;
if (5 + lenR >= signature.length || lenR == 0)
return false;
int lenS = signature[5+lenR] & 0xff;
if (lenR + lenS + 7 != signature.length || lenS == 0)
return false;
// R value type mismatch R value negative
if (signature[4-2] != 0x02 || (signature[4] & 0x80) == 0x80)
return false;
if (lenR > 1 && signature[4] == 0x00 && (signature[4+1] & 0x80) != 0x80)
return false; // R value excessively padded
// S value type mismatch S value negative
if (signature[6 + lenR - 2] != 0x02 || (signature[6 + lenR] & 0x80) == 0x80)
return false;
if (lenS > 1 && signature[6 + lenR] == 0x00 && (signature[6 + lenR + 1] & 0x80) != 0x80)
return false; // S value excessively padded
return true;
}
示例6: sigHashMode
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
public Transaction.SigHash sigHashMode() {
final int mode = sighashFlags & 0x1f;
if (mode == Transaction.SigHash.NONE.value)
return Transaction.SigHash.NONE;
else if (mode == Transaction.SigHash.SINGLE.value)
return Transaction.SigHash.SINGLE;
else
return Transaction.SigHash.ALL;
}
示例7: getMultisigInputScript
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
public static Script getMultisigInputScript (ECDSASignature client, ECDSASignature server) {
ArrayList<TransactionSignature> signList = new ArrayList<TransactionSignature>();
signList.add(new TransactionSignature(client, SigHash.ALL, false));
signList.add(new TransactionSignature(server, SigHash.ALL, false));
Script inputScript = ScriptBuilder.createMultiSigInputScript(signList);
/*
* Seems there is a bug here,
* https://groups.google.com/forum/#!topic/bitcoinj/A9R8TdUsXms
*/
Script workaround = new Script(inputScript.getProgram());
return workaround;
}
示例8: getMultisigInputScript
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
/**
* Gets the multisig input script.
*
* @param client the client
* @param server the server
* @return the multisig input script
*/
public static Script getMultisigInputScript (ECDSASignature client, ECDSASignature server) {
ArrayList<TransactionSignature> signList = new ArrayList<TransactionSignature>();
signList.add(new TransactionSignature(client, SigHash.ALL, false));
signList.add(new TransactionSignature(server, SigHash.ALL, false));
Script inputScript = ScriptBuilder.createMultiSigInputScript(signList);
/*
* Seems there is a bug here,
* https://groups.google.com/forum/#!topic/bitcoinj/A9R8TdUsXms
*/
Script workaround = new Script(inputScript.getProgram());
return workaround;
}
示例9: microPayment_failsOnWrongAddressType
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void microPayment_failsOnWrongAddressType() throws Exception {
final ECKey senderKey = new ECKey();
accountService.createAccount(senderKey);
final ECKey receiverKey = new ECKey();
accountService.createAccount(receiverKey);
TimeLockedAddress tla = accountService.createTimeLockedAddress(senderKey, validLockTime)
.getTimeLockedAddress();
walletService.addWatching(tla.getAddress(params));
Transaction goodInput = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, tla.getAddress(params));
// Bad input is a pay 2 public key utxo from sender, that the server for some reason watches.
Transaction badInput = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, senderKey.toAddress(params));
watchAndMineTransactions(goodInput, badInput);
Transaction tx = new Transaction(params);
tx.addOutput(P2PKOutput(tx, serverKey, 500L, params));
tx.addInput(goodInput.getOutput(0));
tx.addInput(badInput.getOutput(0));
signInput(tx, 0, tla.createRedeemScript(), senderKey);
TransactionSignature sig = tx.calculateSignature(1, senderKey, badInput.getOutput(0).getScriptPubKey(), SigHash.ALL, false);
tx.getInput(1).setScriptSig(new ScriptBuilder().data(sig.encodeToBitcoin()).build());
SignedDTO dto = buildRequestDTO(senderKey, receiverKey, tx, 500L);
sendAndExpect4xxError(dto, "Transaction must spent P2SH addresses");
}
示例10: microPayment_failsOnSpentUTXOs
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void microPayment_failsOnSpentUTXOs() throws Exception {
final ECKey senderKey = new ECKey();
accountService.createAccount(senderKey);
final ECKey receiverKey = new ECKey();
accountService.createAccount(receiverKey);
TimeLockedAddress tla = accountService.createTimeLockedAddress(senderKey, validLockTime).getTimeLockedAddress();
Transaction fundingTx = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, tla.getAddress(params));
watchAndMineTransactions(fundingTx);
// Spend the funding tx
Account accountSender = accountService.getByClientPublicKey(senderKey.getPubKey());
final ECKey serverPrivateKey = ECKey.fromPrivateAndPrecalculatedPublic(accountSender.serverPrivateKey(),
accountSender.serverPublicKey());
Transaction spendingTx = new Transaction(params);
spendingTx.addInput(fundingTx.getOutput(0));
spendingTx.addOutput(anyP2PKOutput(spendingTx));
TransactionSignature clientSig = spendingTx.calculateSignature(0, senderKey, tla.createRedeemScript(),
SigHash.ALL, false);
TransactionSignature serverSig = spendingTx.calculateSignature(0, serverPrivateKey, tla.createRedeemScript(),
SigHash.ALL, false);
Script scriptSig = tla.createScriptSigBeforeLockTime(clientSig, serverSig);
spendingTx.getInput(0).setScriptSig(scriptSig);
walletService.getWallet().maybeCommitTx(spendingTx);
PaymentChannel channel = new PaymentChannel(params, tla.getAddress(params), senderKey, serverKey)
.addInputs(tla, fundingTx.getOutput(0)).addToServerOutput(1337L);
SignedDTO dto = buildRequestDTO(senderKey, receiverKey, channel.buildTx(), 1337L);
sendAndExpect4xxError(dto, "Input is already spent");
}
示例11: TransactionSignature
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
/** Constructs a signature with the given components and SIGHASH_ALL. */
public TransactionSignature(BigInteger r, BigInteger s) {
this(r, s, Transaction.SigHash.ALL.value);
}
示例12: anyoneCanPay
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
public boolean anyoneCanPay() {
return (sighashFlags & Transaction.SigHash.ANYONECANPAY.value) != 0;
}
示例13: testCreateMultiSigInputScript
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void testCreateMultiSigInputScript() {
// Setup transaction and signatures
ECKey key1 = DumpedPrivateKey.fromBase58(PARAMS, "cVLwRLTvz3BxDAWkvS3yzT9pUcTCup7kQnfT2smRjvmmm1wAP6QT").getKey();
ECKey key2 = DumpedPrivateKey.fromBase58(PARAMS, "cTine92s8GLpVqvebi8rYce3FrUYq78ZGQffBYCS1HmDPJdSTxUo").getKey();
ECKey key3 = DumpedPrivateKey.fromBase58(PARAMS, "cVHwXSPRZmL9adctwBwmn4oTZdZMbaCsR5XF6VznqMgcvt1FDDxg").getKey();
Script multisigScript = ScriptBuilder.createMultiSigOutputScript(2, Arrays.asList(key1, key2, key3));
byte[] bytes = HEX.decode("01000000013df681ff83b43b6585fa32dd0e12b0b502e6481e04ee52ff0fdaf55a16a4ef61000000006b483045022100a84acca7906c13c5895a1314c165d33621cdcf8696145080895cbf301119b7cf0220730ff511106aa0e0a8570ff00ee57d7a6f24e30f592a10cae1deffac9e13b990012102b8d567bcd6328fd48a429f9cf4b315b859a58fd28c5088ef3cb1d98125fc4e8dffffffff02364f1c00000000001976a91439a02793b418de8ec748dd75382656453dc99bcb88ac40420f000000000017a9145780b80be32e117f675d6e0ada13ba799bf248e98700000000");
Transaction transaction = PARAMS.getDefaultSerializer().makeTransaction(bytes);
TransactionOutput output = transaction.getOutput(1);
Transaction spendTx = new Transaction(PARAMS);
Address address = Address.fromBase58(PARAMS, "n3CFiCmBXVt5d3HXKQ15EFZyhPz4yj5F3H");
Script outputScript = ScriptBuilder.createOutputScript(address);
spendTx.addOutput(output.getValue(), outputScript);
spendTx.addInput(output);
Sha256Hash sighash = spendTx.hashForSignature(0, multisigScript, SigHash.ALL, false);
ECKey.ECDSASignature party1Signature = key1.sign(sighash);
ECKey.ECDSASignature party2Signature = key2.sign(sighash);
TransactionSignature party1TransactionSignature = new TransactionSignature(party1Signature, SigHash.ALL, false);
TransactionSignature party2TransactionSignature = new TransactionSignature(party2Signature, SigHash.ALL, false);
// Create p2sh multisig input script
Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(party1TransactionSignature, party2TransactionSignature), multisigScript);
// Assert that the input script contains 4 chunks
assertTrue(inputScript.getChunks().size() == 4);
// Assert that the input script created contains the original multisig
// script as the last chunk
ScriptChunk scriptChunk = inputScript.getChunks().get(inputScript.getChunks().size() - 1);
Assert.assertArrayEquals(scriptChunk.data, multisigScript.getProgram());
// Create regular multisig input script
inputScript = ScriptBuilder.createMultiSigInputScript(ImmutableList.of(party1TransactionSignature, party2TransactionSignature));
// Assert that the input script only contains 3 chunks
assertTrue(inputScript.getChunks().size() == 3);
// Assert that the input script created does not end with the original
// multisig script
scriptChunk = inputScript.getChunks().get(inputScript.getChunks().size() - 1);
Assert.assertThat(scriptChunk.data, IsNot.not(equalTo(multisigScript.getProgram())));
}
示例14: checkSignature
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
public static boolean checkSignature (Transaction transaction, int index, Script outputToSpend, ECKey key, TransactionSignature signature) {
Sha256Hash hash = transaction.hashForSignature(index, outputToSpend.getProgram(), SigHash.ALL, false);
return key.verify(hash, signature);
}
示例15: getSignature
import org.bitcoinj.core.Transaction.SigHash; //导入依赖的package包/类
public static TransactionSignature getSignature (Transaction transactionToSign, int index, byte[] outputToSpend, ECKey key) {
Sha256Hash hash = transactionToSign.hashForSignature(index, outputToSpend, SigHash.ALL, false);
TransactionSignature signature = new TransactionSignature(key.sign(hash).toCanonicalised(), SigHash.ALL, false);
return signature;
}