本文整理汇总了Java中com.google.bitcoin.script.ScriptBuilder类的典型用法代码示例。如果您正苦于以下问题:Java ScriptBuilder类的具体用法?Java ScriptBuilder怎么用?Java ScriptBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ScriptBuilder类属于com.google.bitcoin.script包,在下文中一共展示了ScriptBuilder类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPayment
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
/**
* Generates a Payment message based on the information in the PaymentRequest.
* Provide transactions built by the wallet.
* If the PaymentRequest did not specify a payment_url, returns null.
* @param txns list of transactions to be included with the Payment message.
* @param refundAddr will be used by the merchant to send money back if there was a problem.
* @param memo is a message to include in the payment message sent to the merchant.
*/
public @Nullable Protos.Payment getPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
throws IOException {
if (!paymentDetails.hasPaymentUrl())
return null;
Protos.Payment.Builder payment = Protos.Payment.newBuilder();
if (paymentDetails.hasMerchantData())
payment.setMerchantData(paymentDetails.getMerchantData());
if (refundAddr != null) {
Protos.Output.Builder refundOutput = Protos.Output.newBuilder();
refundOutput.setAmount(totalValue.longValue());
refundOutput.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(refundAddr).getProgram()));
payment.addRefundTo(refundOutput);
}
if (memo != null) {
payment.setMemo(memo);
}
for (Transaction txn : txns) {
txn.verify();
ByteArrayOutputStream o = new ByteArrayOutputStream();
txn.bitcoinSerialize(o);
payment.addTransactions(ByteString.copyFrom(o.toByteArray()));
}
return payment.build();
}
示例2: provideRefundSignature
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
/**
* <p>When the servers signature for the refund transaction is received, call this to verify it and sign the
* complete refund ourselves.</p>
*
* <p>If this does not throw an exception, we are secure against the loss of funds and can safely provide the server
* with the multi-sig contract to lock in the agreement. In this case, both the multisig contract and the refund
* transaction are automatically committed to wallet so that it can handle broadcasting the refund transaction at
* the appropriate time if necessary.</p>
*/
public synchronized void provideRefundSignature(byte[] theirSignature) throws VerificationException {
checkNotNull(theirSignature);
checkState(state == State.WAITING_FOR_SIGNED_REFUND);
TransactionSignature theirSig = TransactionSignature.decodeFromBitcoin(theirSignature, true);
if (theirSig.sigHashMode() != Transaction.SigHash.NONE || !theirSig.anyoneCanPay())
throw new VerificationException("Refund signature was not SIGHASH_NONE|SIGHASH_ANYONECANPAY");
// Sign the refund transaction ourselves.
final TransactionOutput multisigContractOutput = multisigContract.getOutput(0);
try {
multisigScript = multisigContractOutput.getScriptPubKey();
} catch (ScriptException e) {
throw new RuntimeException(e); // Cannot happen: we built this ourselves.
}
TransactionSignature ourSignature =
refundTx.calculateSignature(0, myKey, multisigScript, Transaction.SigHash.ALL, false);
// Insert the signatures.
Script scriptSig = ScriptBuilder.createMultiSigInputScript(ourSignature, theirSig);
log.info("Refund scriptSig: {}", scriptSig);
log.info("Multi-sig contract scriptPubKey: {}", multisigScript);
TransactionInput refundInput = refundTx.getInput(0);
refundInput.setScriptSig(scriptSig);
refundInput.verify(multisigContractOutput);
state = State.SAVE_STATE_IN_WALLET;
}
示例3: p2shAddress
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
@Test
public void p2shAddress() throws Exception {
// Test that we can construct P2SH addresses
Address mainNetP2SHAddress = new Address(MainNetParams.get(), "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
assertEquals(mainNetP2SHAddress.version, MainNetParams.get().p2shHeader);
assertTrue(mainNetP2SHAddress.isP2SHAddress());
Address testNetP2SHAddress = new Address(TestNet3Params.get(), "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
assertEquals(testNetP2SHAddress.version, TestNet3Params.get().p2shHeader);
assertTrue(testNetP2SHAddress.isP2SHAddress());
// Test that we can determine what network a P2SH address belongs to
NetworkParameters mainNetParams = Address.getParametersFromAddress("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
assertEquals(MainNetParams.get().getId(), mainNetParams.getId());
NetworkParameters testNetParams = Address.getParametersFromAddress("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
assertEquals(TestNet3Params.get().getId(), testNetParams.getId());
// Test that we can convert them from hashes
byte[] hex = Hex.decode("2ac4b0b501117cc8119c5797b519538d4942e90e");
Address a = Address.fromP2SHHash(mainParams, hex);
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString());
Address b = Address.fromP2SHHash(testParams, Hex.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString());
Address c = Address.fromP2SHScript(mainParams, ScriptBuilder.createP2SHOutputScript(hex));
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString());
}
示例4: addCoinbaseTransaction
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
/** Adds a coinbase transaction to the block. This exists for unit tests. */
void addCoinbaseTransaction(byte[] pubKeyTo, BigInteger value) {
unCacheTransactions();
transactions = new ArrayList<Transaction>();
Transaction coinbase = new Transaction(params);
// A real coinbase transaction has some stuff in the scriptSig like the extraNonce and difficulty. The
// transactions are distinguished by every TX output going to a different key.
//
// Here we will do things a bit differently so a new address isn't needed every time. We'll put a simple
// counter in the scriptSig so every transaction has a different hash.
coinbase.addInput(new TransactionInput(params, coinbase, new byte[]{(byte) txCounter, (byte) (txCounter++ >> 8)}));
coinbase.addOutput(new TransactionOutput(params, coinbase, value,
ScriptBuilder.createOutputScript(new ECKey(null, pubKeyTo)).getProgram()));
transactions.add(coinbase);
coinbase.setParent(this);
coinbase.length = coinbase.bitcoinSerialize().length;
adjustLength(transactions.size(), coinbase.length);
}
示例5: createNextBlock
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
private Block createNextBlock(Block baseBlock, int nextBlockHeight, TransactionOutPointWithValue prevOut,
BigInteger additionalCoinbaseValue) throws ScriptException {
Integer height = blockToHeightMap.get(baseBlock.getHash());
if (height != null)
Preconditions.checkState(height == nextBlockHeight - 1);
BigInteger coinbaseValue = Utils.toNanoCoins(50, 0).shiftRight(nextBlockHeight / params.getSubsidyDecreaseBlockCount())
.add((prevOut != null ? prevOut.value.subtract(BigInteger.ONE) : BigInteger.ZERO))
.add(additionalCoinbaseValue == null ? BigInteger.ZERO : additionalCoinbaseValue);
Block block = baseBlock.createNextBlockWithCoinbase(coinbaseOutKeyPubKey, coinbaseValue);
if (prevOut != null) {
Transaction t = new Transaction(params);
// Entirely invalid scriptPubKey to ensure we aren't pre-verifying too much
t.addOutput(new TransactionOutput(params, t, BigInteger.valueOf(0), new byte[] {OP_PUSHDATA1 - 1 }));
t.addOutput(new TransactionOutput(params, t, BigInteger.valueOf(1),
ScriptBuilder.createOutputScript(new ECKey(null, coinbaseOutKeyPubKey)).getProgram()));
// Spendable output
t.addOutput(new TransactionOutput(params, t, BigInteger.ZERO, new byte[] {OP_1}));
addOnlyInputToTransaction(t, prevOut);
block.addTransaction(t);
block.solve();
}
return block;
}
示例6: empty
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
@Test
public void empty() throws Exception {
// Check the base case of a wallet with one key and no transactions.
Wallet wallet1 = roundTrip(myWallet);
assertEquals(0, wallet1.getTransactions(true).size());
assertEquals(BigInteger.ZERO, wallet1.getBalance());
assertArrayEquals(myKey.getPubKey(),
wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
assertArrayEquals(myKey.getPrivKeyBytes(),
wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
assertEquals(myKey.getCreationTimeSeconds(),
wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
assertEquals(mScriptCreationTime,
wallet1.getWatchedScripts().get(0).getCreationTimeSeconds());
assertEquals(1, wallet1.getWatchedScripts().size());
assertEquals(ScriptBuilder.createOutputScript(myWatchedKey.toAddress(params)),
wallet1.getWatchedScripts().get(0));
assertEquals(WALLET_DESCRIPTION, wallet1.getDescription());
}
示例7: addSignedInput
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的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 {
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;
}
示例8: buildRawTx
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
private static String buildRawTx() {
ScriptBuilder builder = new ScriptBuilder();
builder.op(OP_DEPTH).op(OP_1).op(OP_NUMEQUAL).op(OP_IF)
.data("name of nakakamoto".getBytes()).op(OP_DROP)
.op(OP_RIPEMD160).op(OP_RIPEMD160)
.data(doublehash160("satoshi".getBytes())).op(OP_EQUAL)
.op(OP_ELSE).op(OP_DUP).op(OP_HASH160)
.data(doublehash160("Haha".getBytes())).op(OP_EQUALVERIFY)
.op(OP_CHECKSIG).op(OP_ENDIF);
Script outputScript = builder.build();
Transaction tx1 = new Transaction(MainNetParams.get());
tx1.addInput(new TransactionInput(MainNetParams.get(), tx1,
new byte[] {}));
ECKey key = new ECKey();
tx1.addOutput(Bitcoins.toSatoshiEndBully(), key);
Transaction tx2 = new Transaction(MainNetParams.get());
tx2.addInput(tx1.getOutput(0));
tx2.addOutput(Bitcoins.toSatoshiEndBully(), outputScript);
tx2.addOutput(Bitcoins.toSatoshiEndBully(), key);
System.out.println(tx2);
String rawTx = BaseEncoding.base16().encode(
tx2.unsafeBitcoinSerialize());
return rawTx;
}
示例9: empty
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
@Test
public void empty() throws Exception {
// Check the base case of a wallet with one key and no transactions.
Wallet wallet1 = roundTrip(myWallet);
assertEquals("Wrong number of keys", 0, wallet1.getTransactions(true).size());
assertEquals("Wrong balance", BigInteger.ZERO, wallet1.getBalance());
assertArrayEquals("Wrong pubkey", myKey.getPubKey(),
wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
assertArrayEquals("Wrong privkey", myKey.getPrivKeyBytes(),
wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
assertEquals("Wring creation time", myKey.getCreationTimeSeconds(),
wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
assertEquals(mScriptCreationTime,
wallet1.getWatchedScripts().get(0).getCreationTimeSeconds());
assertEquals(1, wallet1.getWatchedScripts().size());
assertEquals(ScriptBuilder.createOutputScript(myWatchedKey.toAddress(params)),
wallet1.getWatchedScripts().get(0));
assertEquals(WALLET_DESCRIPTION, wallet1.getDescription());
}
示例10: OpenTx
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
public OpenTx(LotteryTx commitTx, ECKey sk, List<byte[]> pks, Address address,
byte[] secret, BigInteger fee, boolean testnet) throws VerificationException {
NetworkParameters params = getNetworkParameters(testnet);
int noPlayers = commitTx.getOutputs().size();
tx = new Transaction(params);
BigInteger value = BigInteger.ZERO;
for (TransactionOutput out : commitTx.getOutputs()) {
tx.addInput(out);
value = value.add(out.getValue());
}
tx.addOutput(value.subtract(fee), address);
for (int k = 0; k < noPlayers; ++k) {
byte[] sig = sign(k, sk).encodeToBitcoin();
tx.getInput(k).setScriptSig(new ScriptBuilder()
.data(sig)
.data(sk.getPubKey())
.data(sig) // wrong signature in a good format
.data(pks.get(k))
.data(secret)
.build());
tx.getInput(k).verify();
}
tx.verify();
}
示例11: buildRawTx
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
private static String buildRawTx() {
ProtoBlue puf = ProtoBlue.newBuilder().setBkbcValue(200855)
.setProtoType(Type.TEST).setExchangeType(ExType.BTC_TWD)
.setVersion(VerType.TEST1).build();
System.out.println(puf);
ScriptBuilder builder = new ScriptBuilder();
builder.op(OP_RETURN).data(puf.toByteArray());
Script outputScript = builder.build();
Transaction tx1 = new Transaction(MainNetParams.get());
tx1.addInput(new TransactionInput(MainNetParams.get(), tx1,
new byte[] {}));
ECKey key = new ECKey();
tx1.addOutput(Bitcoins.toSatoshiEndBully(), key);
Transaction tx2 = new Transaction(MainNetParams.get());
tx2.addInput(tx1.getOutput(0));
tx2.addOutput(Bitcoins.toSatoshiEndBully(), key);
tx2.addOutput(Bitcoins.toSatoshiEndBully(), outputScript);
System.out.println(tx2);
String rawTx = BaseEncoding.base16().encode(
tx2.unsafeBitcoinSerialize());
return rawTx;
}
示例12: createNextBlock
import com.google.bitcoin.script.ScriptBuilder; //导入依赖的package包/类
private Block createNextBlock(Block baseBlock, int nextBlockHeight, TransactionOutPointWithValue prevOut,
BigInteger additionalCoinbaseValue) throws ScriptException {
Integer height = blockToHeightMap.get(baseBlock.getHash());
if (height != null)
Preconditions.checkState(height == nextBlockHeight - 1);
BigInteger coinbaseValue = Utils.toNanoCoins(50, 0).shiftRight(nextBlockHeight / params.getSubsidyDecreaseBlockCount())
.add((prevOut != null ? prevOut.value.subtract(BigInteger.ONE) : BigInteger.ZERO))
.add(additionalCoinbaseValue == null ? BigInteger.ZERO : additionalCoinbaseValue);
Block block = baseBlock.createNextBlockWithCoinbase(coinbaseOutKeyPubKey, coinbaseValue);
if (prevOut != null) {
Transaction t = new Transaction(params);
// Entirely invalid scriptPubKey to ensure we aren't pre-verifying too much
t.addOutput(new TransactionOutput(params, t, BigInteger.valueOf(0), new byte[] {OP_PUSHDATA1 - 1 }));
t.addOutput(new TransactionOutput(params, t, BigInteger.valueOf(1),
ScriptBuilder.createOutputScript(new ECKey(null, coinbaseOutKeyPubKey)).getProgram()));
// Spendable output
t.addOutput(new TransactionOutput(params, t, BigInteger.ZERO, new byte[] {OP_1, uniquenessCounter++}));
addOnlyInputToTransaction(t, prevOut);
block.addTransaction(t);
block.solve();
}
return block;
}