use of org.bitcoinj.core.Transaction.SigHash in project balzac by balzac-lang.
the class ScriptBuilder2Test method test_serialize_signature5.
@Test
public void test_serialize_signature5() {
ECKey key = new ECKey();
SigHash hashType = SigHash.NONE;
ScriptBuilder2 sb = new ScriptBuilder2();
sb.number(15);
sb.signaturePlaceholder(key, hashType, false);
String expected = "15 [sig," + ECKeyStore.getUniqueID(key) + ",*0]";
String actual = sb.serialize();
assertEquals(expected, actual);
}
use of org.bitcoinj.core.Transaction.SigHash in project balzac by balzac-lang.
the class AbstractScriptBuilderWithVar method setAllSignatures.
/**
* Replace all the signatures placeholder with the actual signatures.
* Each placeholder is already associated with the key and the modifiers.
* @param tx the transaction to be signed
* @param inputIndex the index of the input that will contain this script
* @param outScript the redeemed output script
* @return a <b>copy</b> of this builder
* @throws KeyStoreException if an error occurs retrieving private keys
*/
@SuppressWarnings("unchecked")
public T setAllSignatures(ECKeyStore keystore, Transaction tx, int inputIndex, byte[] outScript) throws KeyStoreException {
List<ScriptChunk> newChunks = new ArrayList<>();
for (ScriptChunk chunk : getChunks()) {
ScriptBuilder2 sb = new ScriptBuilder2();
if (isSignature(chunk)) {
String mapKey = getMapKey(chunk);
SignatureUtil sig = this.signatures.get(mapKey);
checkState(keystore != null, "keystore must be set to retrieve the private keys");
checkState(keystore.containsKey(sig.keyID), "key " + sig.keyID + " not found on the specified keystore");
ECKey key = keystore.getKey(sig.keyID);
SigHash hashType = sig.hashType;
boolean anyoneCanPay = sig.anyoneCanPay;
// check the key is correct when P2PKH
// Script s = new Script(outScript);
// if (s.isSentToAddress()) {
// checkState(Arrays.equals(s.getPubKeyHash(), key.getPubKeyHash()));
// }
TransactionSignature txSig = tx.calculateSignature(inputIndex, key, outScript, hashType, anyoneCanPay);
Sha256Hash hash = tx.hashForSignature(inputIndex, outScript, (byte) txSig.sighashFlags);
boolean isValid = ECKey.verify(hash.getBytes(), txSig, key.getPubKey());
checkState(isValid);
checkState(txSig.isCanonical());
sb.data(txSig.encodeToBitcoin());
} else {
sb.addChunk(chunk);
}
newChunks.addAll(sb.getChunks());
}
super.getChunks().clear();
super.getChunks().addAll(newChunks);
this.signatures.clear();
return (T) this;
}
Aggregations