Search in sources :

Example 6 with SigHash

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);
}
Also used : SigHash(org.bitcoinj.core.Transaction.SigHash) ScriptBuilder2(it.unica.tcs.lib.script.ScriptBuilder2) ECKey(org.bitcoinj.core.ECKey) Test(org.junit.Test)

Example 7 with SigHash

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;
}
Also used : SigHash(org.bitcoinj.core.Transaction.SigHash) Sha256Hash(org.bitcoinj.core.Sha256Hash) ArrayList(java.util.ArrayList) ECKey(org.bitcoinj.core.ECKey) TransactionSignature(org.bitcoinj.crypto.TransactionSignature) ScriptChunk(org.bitcoinj.script.ScriptChunk)

Aggregations

ECKey (org.bitcoinj.core.ECKey)7 SigHash (org.bitcoinj.core.Transaction.SigHash)7 ScriptBuilder2 (it.unica.tcs.lib.script.ScriptBuilder2)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)1 Sha256Hash (org.bitcoinj.core.Sha256Hash)1 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)1 ScriptChunk (org.bitcoinj.script.ScriptChunk)1