Search in sources :

Example 1 with ScriptChunk

use of org.bitcoinj.script.ScriptChunk in project catena-java by alinush.

the class CatenaUtils method getCatenaTxData.

/**
 * Returns the OP_RETURN data in the specified Catena TX.
 *
 * @param tx
 * @return
 */
public static byte[] getCatenaTxData(Transaction tx) {
    Script pubKey = tx.getOutput(1).getScriptPubKey();
    checkState(pubKey.isOpReturn());
    ScriptChunk dataChunk = pubKey.getChunks().get(1);
    checkState(dataChunk.isPushData());
    return dataChunk.data;
}
Also used : Script(org.bitcoinj.script.Script) ScriptChunk(org.bitcoinj.script.ScriptChunk)

Example 2 with ScriptChunk

use of org.bitcoinj.script.ScriptChunk in project balzac by balzac-lang.

the class AbstractScriptBuilderWithVar method addVariableChunk.

private void addVariableChunk(String name) {
    byte[] data = (FREEVAR_PREFIX + name).getBytes();
    checkState(data.length < 256, "data too long: " + data.length);
    ScriptChunk chunk = new ScriptChunk(OP_PUSHDATA1, data);
    super.addChunk(chunk);
}
Also used : ScriptChunk(org.bitcoinj.script.ScriptChunk)

Example 3 with ScriptChunk

use of org.bitcoinj.script.ScriptChunk in project balzac by balzac-lang.

the class AbstractScriptBuilderWithVar method signaturePlaceholder.

@SuppressWarnings("unchecked")
public T signaturePlaceholder(String keyID, SigHash hashType, boolean anyoneCanPay) {
    checkNotNull(keyID, "'keyID' cannot be null");
    checkNotNull(hashType, "'hashType' cannot be null");
    SignatureUtil sig = new SignatureUtil(keyID, hashType, anyoneCanPay);
    String mapKey = sig.getUniqueKey();
    byte[] data = (SIGNATURE_PREFIX + mapKey).getBytes();
    checkState(data.length < 256, "data too long: " + data.length);
    ScriptChunk chunk = new ScriptChunk(OP_PUSHDATA1, data);
    super.addChunk(chunk);
    this.signatures.put(mapKey, sig);
    return (T) this;
}
Also used : ScriptChunk(org.bitcoinj.script.ScriptChunk)

Example 4 with ScriptChunk

use of org.bitcoinj.script.ScriptChunk in project balzac by balzac-lang.

the class AbstractScriptBuilderWithVar method append.

/**
 * Append the given script builder to this one.
 * If it contains some free variables or signatures placeholder, they are merged ensuring consistency.
 * @param <U> the concrete type of the given builder
 * @param append the script builder to append
 * @return this builder
 */
@SuppressWarnings("unchecked")
public <U extends AbstractScriptBuilderWithVar<U>> T append(AbstractScriptBuilderWithVar<U> append) {
    for (ScriptChunk ch : append.getChunks()) {
        if (isVariable(ch)) {
            // merge free variables
            String name = getVariableName(ch);
            // check they are consistent
            if (hasVariable(name)) {
                checkState(getType(name).equals(append.getType(name)), "Inconsitent state: variable '%s' is bound to type '%s' (this) and type '%s' (append)", name, this.getType(name), append.getType(name));
                if (isBound(name) && append.isBound(name)) {
                    checkState(getValue(name).equals(append.getValue(name)), "Inconsitent state: variable '%s' is bound to value '%s' (this) and value '%s' (append)", name, this.getValue(name), append.getValue(name));
                }
            }
            this.addVariable(name, append.getType(name));
            if (!isBound(name) && append.isBound(name)) {
                this.bindVariable(name, append.getValue(name));
            }
        } else if (isSignature(ch)) {
            // merge signatures
            String mapKey = getMapKey(ch);
            checkNotNull(append.signatures.containsKey(mapKey));
            if (this.signatures.containsKey(mapKey)) {
                // check they are consistent
                checkState(this.signatures.get(mapKey).equals(append.signatures.get(mapKey)), "Inconsitent state: sig placeholder '%s' is bound to '%s' (this) and '%s' (append)", mapKey, this.signatures.get(mapKey), append.signatures.get(mapKey));
            } else {
                this.signatures.put(mapKey, append.signatures.get(mapKey));
            }
            this.addChunk(ch);
        } else {
            this.addChunk(ch);
        }
    }
    return (T) this;
}
Also used : ScriptChunk(org.bitcoinj.script.ScriptChunk)

Example 5 with ScriptChunk

use of org.bitcoinj.script.ScriptChunk in project balzac by balzac-lang.

the class AbstractScriptBuilderWithVar method substituteAllBinding.

/*
     * Return a Script, binding the variables.
     * This builder is assumed to be ready
     */
private Script substituteAllBinding() {
    ScriptBuilder sb = new ScriptBuilder();
    for (ScriptChunk chunk : getChunks()) {
        if (isVariable(chunk)) {
            String name = getVariableName(chunk);
            Object obj = getValue(name);
            Class<?> expectedClass = getType(name);
            if (expectedClass.isInstance(obj)) {
                for (ScriptChunk ch : BitcoinUtils.toScript(obj).getChunks()) {
                    sb.addChunk(ch);
                }
            } else
                throw new IllegalArgumentException("expected class " + expectedClass.getName() + ", got " + obj.getClass().getName());
        } else {
            sb.addChunk(chunk);
        }
    }
    return sb.build();
}
Also used : ScriptBuilder(org.bitcoinj.script.ScriptBuilder) ScriptChunk(org.bitcoinj.script.ScriptChunk)

Aggregations

ScriptChunk (org.bitcoinj.script.ScriptChunk)8 ECKey (org.bitcoinj.core.ECKey)2 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)2 Script (org.bitcoinj.script.Script)2 ArrayList (java.util.ArrayList)1 ScriptException (org.bitcoinj.core.ScriptException)1 Sha256Hash (org.bitcoinj.core.Sha256Hash)1 Transaction (org.bitcoinj.core.Transaction)1 SigHash (org.bitcoinj.core.Transaction.SigHash)1 DeterministicKey (org.bitcoinj.crypto.DeterministicKey)1 ScriptBuilder (org.bitcoinj.script.ScriptBuilder)1 TransactionSigner (org.bitcoinj.signers.TransactionSigner)1 DecryptingKeyBag (org.bitcoinj.wallet.DecryptingKeyBag)1 KeyBag (org.bitcoinj.wallet.KeyBag)1 RedeemData (org.bitcoinj.wallet.RedeemData)1