本文整理汇总了Java中com.google.bitcoin.core.Transaction.SigHash类的典型用法代码示例。如果您正苦于以下问题:Java SigHash类的具体用法?Java SigHash怎么用?Java SigHash使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SigHash类属于com.google.bitcoin.core.Transaction包,在下文中一共展示了SigHash类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addOnlyInputToTransaction
import com.google.bitcoin.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);
byte[] connectedPubKeyScript = prevOut.scriptPubKey.getProgram();
Sha256Hash hash = t.hashForSignature(0, connectedPubKeyScript, SigHash.ALL, false);
// Sign input
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(73);
bos.write(coinbaseOutKey.sign(hash).encodeToDER());
bos.write(SigHash.ALL.ordinal() + 1);
byte[] signature = bos.toByteArray();
Preconditions.checkState(prevOut.scriptPubKey.isSentToRawPubKey());
input.setScriptBytes(Script.createInputScript(signature));
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
示例2: signInput
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
private static byte[] signInput(NetworkParameters params, Transaction tx, int tx_input_index, String base58PrivKey, BitcoinScript script, SigHash sigHash) {
Log.d("SharedCoin", "SharedCoin signInput tx.getInputs().size " + tx.getInputs().size());
Log.d("SharedCoin", "SharedCoin signInput tx_input_index " + tx_input_index);
Log.d("SharedCoin", "SharedCoin signInput base58PrivKey " + base58PrivKey);
try {
ECKey key = new ECKey(Base58.decode(base58PrivKey), null);
Log.d("SharedCoin", "SharedCoin signInput key.toAddress " + key.toAddress(params).toString());
TransactionSignature transactionSignature = tx.calculateSignature(tx_input_index, key, null, script.getProgram(), SigHash.ALL, false);
byte[] signedScript = Script.createInputScript(transactionSignature.encodeToBitcoin(), key.getPubKey());
//ArrayUtils.reverse(signedScript);
String signedScriptHex = new String(Hex.encode(signedScript));
Log.d("SharedCoin", "SharedCoin signInput signedScriptHex " + signedScriptHex);
Log.d("SharedCoin", "SharedCoin signInput script.program hex " + new String(Hex.encode(script.getProgram())));
return signedScript;
} catch (Exception e) {
Log.d("SharedCoin", "SharedCoin signInput e " + e.getLocalizedMessage());
e.printStackTrace();
}
return null;
}
示例3: createTx
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
public static Transaction createTx(ECKey key, List<TransactionOutPoint> unspents, String topic, String message) throws Exception
{
//create new transaction
Transaction bulletin = new Transaction(Constants.NETWORK_PARAMETERS);
//add inputs and message outputs
addInputs(bulletin, unspents);
addBulletinOutputs(bulletin, topic, message);
//add change output to transaction
addChangeOutput(key, bulletin, unspents);
//sign the inputs
BasicKeyChain keychain = new BasicKeyChain();
keychain.importKey(key);
bulletin.signInputs(SigHash.ALL, false, keychain);
return bulletin;
}
示例4: createChannel
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
Transaction createChannel(ECKey peerKey, Transaction setupTransaction,
Transaction refundTransaction, BigInteger peerMinDeposit,
BigInteger minDeposit, BigInteger peerContribution,
BigInteger contribution, BigInteger initialFee, BigInteger feeStep,
int maxLifetime, int locktimeStep, int commitDepth) {
try {
refundTransaction.getInput(0).setScriptSig(
ScriptBuilder.createMultiSigInputScriptBytes(Arrays.asList(
refundTransaction.getInput(0).getScriptSig().getChunks().get(0).data,
refundTransaction.calculateSignature(
0,
getKey(),
refundTransaction.getInput(0)
.getConnectedOutput()
.getScriptPubKey(), SigHash.ALL,
false).encodeToBitcoin())));
this.connections.get(peerKey.getPubKey()).addChannel(
setupTransaction, refundTransaction, peerMinDeposit,
minDeposit, peerContribution, contribution, initialFee,
feeStep, maxLifetime, locktimeStep, commitDepth);
} catch (Exception e) {
// No exception handling for this demo
e.printStackTrace();
}
return refundTransaction;
}
示例5: expire
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
void expire() {
getParentPeer().wallet().addWalletTransaction(
new WalletTransaction(WalletTransaction.Pool.PENDING, this.refundTransaction));
getParentPeer().peerGroup().broadcastTransaction(this.refundTransaction);
this.redeemTransaction = new Transaction(getParentPeer().params());
this.redeemTransaction.addOutput(
this.refundTransaction.getOutput(this.iAmInitiator ? 0 : 1).getValue().subtract(this.initialFee),
getParentPeerKey().toAddress(getParentPeer().params()));
this.redeemTransaction.addInput(this.refundTransaction.getOutput(this.iAmInitiator ? 0 : 1));
this.redeemTransaction.getInput(0).setScriptSig(
new ScriptBuilder()
.data(this.redeemTransaction.calculateSignature(0, getParentPeerKey(), null,
this.refundTransaction.getOutput(this.iAmInitiator ? 0 : 1).getScriptBytes(), SigHash.ALL,
false)
.encodeToBitcoin())
.data(this.committedPreImage)
.build());
getParentPeer().wallet().addWalletTransaction(
new WalletTransaction(WalletTransaction.Pool.PENDING, this.redeemTransaction));
getParentPeer().peerGroup().broadcastTransaction(this.redeemTransaction);
this.state = State.CLOSING;
}
示例6: acceptPaymentFor
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
public void acceptPaymentFor(byte[] request, Transaction pendingTransaction) {
try {
byte[] mySignature = pendingTransaction.calculateSignature(0, getParentPeerKey(),
pendingTransaction.getInput(0).getConnectedOutput().getScriptPubKey(),
SigHash.ALL, false).encodeToBitcoin();
byte[] peerSignature = pendingTransaction.getInput(0).getScriptSig().getChunks().get(0).data;
// Since initiator's pubkey is always passed at top of stack, it has to be pushed second
pendingTransaction.getInput(0).setScriptSig(
ScriptBuilder.createMultiSigInputScriptBytes(Arrays.asList(
iAmInitiator ? mySignature : peerSignature,
iAmInitiator ? peerSignature : mySignature)));
this.pendingTransaction = pendingTransaction;
this.state = State.PENDING_COMMIT;
this.pendingRequest = request;
this.iSentLast = false;
} catch (Exception e) {
// No exception handling for this demo
e.printStackTrace();
}
}
示例7: addOnlyInputToTransaction
import com.google.bitcoin.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);
byte[] connectedPubKeyScript = prevOut.scriptPubKey.getProgram();
Sha256Hash hash = t.hashTransactionForSignature(0, connectedPubKeyScript, SigHash.ALL, false);
// Sign input
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(73);
bos.write(coinbaseOutKey.sign(hash).encodeToDER());
bos.write(SigHash.ALL.ordinal() + 1);
byte[] signature = bos.toByteArray();
Preconditions.checkState(prevOut.scriptPubKey.isSentToRawPubKey());
input.setScriptBytes(Script.createInputScript(signature));
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
示例8: addInputToTransaction
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
private void addInputToTransaction(Transaction t, TransactionOutPoint prevOut, byte[] prevOutScriptPubKey, ECKey sigKey) throws ScriptException {
TransactionInput input = new TransactionInput(params, t, new byte[]{}, prevOut);
t.addInput(input);
Sha256Hash hash = t.hashTransactionForSignature(0, prevOutScriptPubKey, SigHash.ALL, false);
// Sign input
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(73);
bos.write(sigKey.sign(hash).encodeToDER());
bos.write(SigHash.ALL.ordinal() + 1);
byte[] signature = bos.toByteArray();
input.setScriptBytes(Script.createInputScript(signature));
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
示例9: spendOutputFromPendingTransaction
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void spendOutputFromPendingTransaction() throws Exception {
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
BigInteger v1 = Utils.toNanoCoins(1, 0);
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
// First create our current transaction
ECKey k2 = new ECKey();
wallet.addKey(k2);
BigInteger v2 = toNanoCoins(0, 50);
Transaction t2 = new Transaction(params);
TransactionOutput o2 = new TransactionOutput(params, t2, v2, k2.toAddress(params));
t2.addOutput(o2);
SendRequest req = SendRequest.forTx(t2);
req.ensureMinRequiredFee = false;
wallet.completeTx(req);
// Commit t2, so it is placed in the pending pool
wallet.commitTx(t2);
assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
assertEquals(2, wallet.getTransactions(true).size());
// Now try to the spend the output.
ECKey k3 = new ECKey();
BigInteger v3 = toNanoCoins(0, 25);
Transaction t3 = new Transaction(params);
t3.addOutput(v3, k3.toAddress(params));
t3.addInput(o2);
t3.signInputs(SigHash.ALL, wallet);
// Commit t3, so the coins from the pending t2 are spent
wallet.commitTx(t3);
assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
assertEquals(3, wallet.getTransactions(true).size());
// Now the output of t2 must not be available for spending
assertFalse(o2.isAvailableForSpending());
}
示例10: spendOutputFromPendingTransaction
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void spendOutputFromPendingTransaction() throws Exception {
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
BigInteger v1 = Utils.toNanoCoins(1, 0);
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
// First create our current transaction
ECKey k2 = new ECKey();
wallet.addKey(k2);
BigInteger v2 = toNanoCoins(0, 50);
Transaction t2 = new Transaction(params);
TransactionOutput o2 = new TransactionOutput(params, t2, v2, k2.toAddress(params));
t2.addOutput(o2);
SendRequest req = SendRequest.forTx(t2);
req.ensureMinRequiredFee = false;
boolean complete = wallet.completeTx(req);
assertTrue(complete);
// Commit t2, so it is placed in the pending pool
wallet.commitTx(t2);
assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.ALL));
// Now try to the spend the output.
ECKey k3 = new ECKey();
BigInteger v3 = toNanoCoins(0, 25);
Transaction t3 = new Transaction(params);
t3.addOutput(v3, k3.toAddress(params));
t3.addInput(o2);
t3.signInputs(SigHash.ALL, wallet);
// Commit t3, so the coins from the pending t2 are spent
wallet.commitTx(t3);
assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
assertEquals(3, wallet.getPoolSize(WalletTransaction.Pool.ALL));
// Now the output of t2 must not be available for spending
assertFalse(o2.isAvailableForSpending());
}
示例11: signMyInputs
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
void signMyInputs(Transaction tx) {
// This is to sign only MY OWN inputs in a transaction
try {
for (int i = 0; i < tx.getInputs().size(); i++) {
TransactionInput in = tx.getInput(i);
TransactionOutput cOut = in.getConnectedOutput();
Script pubKey = cOut.getScriptPubKey();
if (cOut.isMine(this.vWallet)) {
TransactionSignature sig = tx.calculateSignature(i,
getKey(), pubKey, SigHash.ALL, false);
if (pubKey.isSentToAddress()
&& (pubKey.getToAddress(params).equals(getKey()
.toAddress(params)))) {
in.setScriptSig(new ScriptBuilder()
.data(sig.encodeToBitcoin())
.data(getKey().getPubKey()).build());
} else if (pubKey.isSentToRawPubKey()
&& (pubKey.getPubKey().equals(getKey().getPubKey()))) {
in.setScriptSig(new ScriptBuilder().data(
sig.encodeToBitcoin()).build());
}
}
}
} catch (Exception e) {
// No exception handling for this demo
e.printStackTrace();
}
}
示例12: close
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
Transaction close(Transaction closingTransaction) {
try {
byte[] mySignature = closingTransaction.calculateSignature(0, getParentPeerKey(),
this.setupTransaction.getOutput(0).getScriptPubKey(),
SigHash.ALL, false).encodeToBitcoin();
byte[] peerSignature = closingTransaction.getInput(0).getScriptSig().getChunks().get(0).data;
closingTransaction.getInput(0).setScriptSig(
ScriptBuilder.createMultiSigInputScriptBytes(Arrays.asList(
iAmInitiator ? mySignature : peerSignature,
iAmInitiator ? peerSignature : mySignature)));
} catch (Exception e) {
// No exception handling for this demo
e.printStackTrace();
}
getParentPeer().wallet().addWalletTransaction(
new WalletTransaction(Pool.PENDING, closingTransaction));
getParentPeer().peerGroup().broadcastTransaction(closingTransaction);
this.redeemTransaction = new Transaction(getParentPeer().params());
this.redeemTransaction.addOutput(
closingTransaction.getOutput(this.iAmInitiator ? 0 : 1).getValue().subtract(this.initialFee),
getParentPeerKey().toAddress(getParentPeer().params()));
this.redeemTransaction.addInput(closingTransaction.getOutput(this.iAmInitiator ? 0 : 1));
this.redeemTransaction.getInput(0).setScriptSig(
new ScriptBuilder()
.data(this.redeemTransaction.calculateSignature(0, getParentPeerKey(), null,
closingTransaction.getOutput(this.iAmInitiator ? 0 : 1).getScriptBytes(), SigHash.ALL, false)
.encodeToBitcoin())
.data(this.committedPreImage)
.build());
getParentPeer().wallet().addWalletTransaction(
new WalletTransaction(WalletTransaction.Pool.PENDING, this.redeemTransaction));
getParentPeer().peerGroup().broadcastTransaction(this.redeemTransaction);
this.state = State.CLOSING;
return closingTransaction;
}
示例13: spendOutputFromPendingTransaction
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void spendOutputFromPendingTransaction() throws Exception {
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
BigInteger v1 = Utils.toNanoCoins(1, 0);
sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
// First create our current transaction
ECKey k2 = new ECKey();
wallet.addKey(k2);
BigInteger v2 = toNanoCoins(0, 50);
Transaction t2 = new Transaction(params);
TransactionOutput o2 = new TransactionOutput(params, t2, v2, k2.toAddress(params));
t2.addOutput(o2);
SendRequest req = SendRequest.forTx(t2);
req.ensureMinRequiredFee = false;
boolean complete = wallet.completeTx(req);
assertTrue(complete);
// Commit t2, so it is placed in the pending pool
wallet.commitTx(t2);
assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.ALL));
// Now try to the spend the output.
ECKey k3 = new ECKey();
BigInteger v3 = toNanoCoins(0, 25);
Transaction t3 = new Transaction(params);
t3.addOutput(v3, k3.toAddress(params));
t3.addInput(o2);
t3.signInputs(SigHash.ALL, wallet);
// Commit t3, so the coins from the pending t2 are spent
wallet.commitTx(t3);
assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
assertEquals(3, wallet.getPoolSize(WalletTransaction.Pool.ALL));
// Now the output of t2 must not be available for spending
assertFalse(o2.isAvailableForSpending());
}
示例14: testCompleteTxWithExistingInputs
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
@Test
public void testCompleteTxWithExistingInputs() throws Exception {
// Tests calling completeTx with a SendRequest that already has a few inputs in it
// Make sure TestWithWallet isnt doing anything crazy.
assertEquals(0, wallet.getTransactions(true).size());
Address notMyAddr = new ECKey().toAddress(params);
// Generate a few outputs to us
StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
Transaction tx1 = createFakeTx(params, Utils.COIN, myAddress);
wallet.receiveFromBlock(tx1, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
Transaction tx2 = createFakeTx(params, Utils.COIN, myAddress); assertTrue(!tx1.getHash().equals(tx2.getHash()));
wallet.receiveFromBlock(tx2, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
Transaction tx3 = createFakeTx(params, CENT, myAddress);
wallet.receiveFromBlock(tx3, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 2);
SendRequest request1 = SendRequest.to(notMyAddr, CENT);
// If we just complete as-is, we will use one of the COIN outputs to get higher priority,
// resulting in a change output
wallet.completeTx(request1);
assertEquals(1, request1.tx.getInputs().size());
assertEquals(2, request1.tx.getOutputs().size());
assertEquals(CENT, request1.tx.getOutput(0).getValue());
assertEquals(Utils.COIN.subtract(CENT), request1.tx.getOutput(1).getValue());
// Now create an identical request2 and add an unsigned spend of the CENT output
SendRequest request2 = SendRequest.to(notMyAddr, CENT);
request2.tx.addInput(tx3.getOutput(0));
// Now completeTx will result in one input, one output
wallet.completeTx(request2);
assertEquals(1, request2.tx.getInputs().size());
assertEquals(1, request2.tx.getOutputs().size());
assertEquals(CENT, request2.tx.getOutput(0).getValue());
// Make sure it was properly signed
request2.tx.getInput(0).getScriptSig().correctlySpends(request2.tx, 0, tx3.getOutput(0).getScriptPubKey(), true);
// However, if there is no connected output, we will grab a COIN output anyway and add the CENT to fee
SendRequest request3 = SendRequest.to(notMyAddr, CENT);
request3.tx.addInput(new TransactionInput(params, request3.tx, new byte[]{}, new TransactionOutPoint(params, 0, tx3.getHash())));
// Now completeTx will result in two inputs, two outputs and a fee of a CENT
// Note that it is simply assumed that the inputs are correctly signed, though in fact the first is not
wallet.completeTx(request3);
assertEquals(2, request3.tx.getInputs().size());
assertEquals(2, request3.tx.getOutputs().size());
assertEquals(CENT, request3.tx.getOutput(0).getValue());
assertEquals(Utils.COIN.subtract(CENT), request3.tx.getOutput(1).getValue());
SendRequest request4 = SendRequest.to(notMyAddr, CENT);
request4.tx.addInput(tx3.getOutput(0));
// Now if we manually sign it, completeTx will not replace our signature
request4.tx.signInputs(SigHash.ALL, wallet);
byte[] scriptSig = request4.tx.getInput(0).getScriptBytes();
wallet.completeTx(request4);
assertEquals(1, request4.tx.getInputs().size());
assertEquals(1, request4.tx.getOutputs().size());
assertEquals(CENT, request4.tx.getOutput(0).getValue());
assertArrayEquals(scriptSig, request4.tx.getInput(0).getScriptBytes());
}
示例15: signNormal
import com.google.bitcoin.core.Transaction.SigHash; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private void signNormal(Transaction tx, JSONArray connected_scripts, ObjectSuccessCallback objectSuccessCallback) {
Log.d("SharedCoin", "SharedCoin signNormal connected_scripts " + connected_scripts.toString());
JSONArray signatures = new JSONArray();
try {
for (int index = 0; index < connected_scripts.size(); index++) {
SharedCoin.setTimeout(1);
JSONObject connectedScriptStuff = (JSONObject) connected_scripts.get(index);
/*
BitcoinScript connected_script = (BitcoinScript) connectedScriptStuff.get("connected_script");
if (connected_script == null) throw new Exception("Null connected script");
//*/
//*
String connected_script_hex = (String) connectedScriptStuff.get("connected_script_hex");
BitcoinScript connected_script = new BitcoinScript(Hex.decode(connected_script_hex.getBytes()));
//*/
BigInteger tx_input_index = SharedCoin.getBigIntegerFromLong(connectedScriptStuff, "tx_input_index");
String base58PrivKey = (String) connectedScriptStuff.get("priv_to_use");
byte[] signed_script = SharedCoin.signInput(this.sharedCoin.params, tx, tx_input_index.intValue(), base58PrivKey, connected_script, SigHash.ALL);
if (signed_script != null) {
JSONObject signature = new JSONObject();
signature.put("tx_input_index", connectedScriptStuff.get("tx_input_index"));
String signedScriptHex = new String(Hex.encode(signed_script));
signature.put("input_script", signedScriptHex);
signature.put("offer_outpoint_index", connectedScriptStuff.get("offer_outpoint_index"));
signatures.add(signature);
} else {
throw new Exception("Unknown error signing transaction");
}
}
Log.d("SharedCoin", "SharedCoin signNormal signatures " + signatures.toString());
objectSuccessCallback.onSuccess(signatures);
} catch (Exception e) {
objectSuccessCallback.onFail(e.getLocalizedMessage());
e.printStackTrace();
}
}