Java Code Examples for org.bitcoinj.script.ScriptBuilder#build()

The following examples show how to use org.bitcoinj.script.ScriptBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitcoinUtils.java    From balzac with Apache License 2.0 5 votes vote down vote up
public static Script toScript(Object obj) {

        if (obj instanceof Number)
            return new ScriptBuilder().number(((Number) obj).longValue()).build();

        else if (obj instanceof String)
            return new ScriptBuilder().data(((String) obj).getBytes(Charset.forName("UTF-8"))).build();

        else if (obj instanceof Hash)
            return new ScriptBuilder().data(((Hash) obj).getBytes()).build();

        else if (obj instanceof Boolean)
            return ((Boolean) obj) ? new ScriptBuilder().opTrue().build() : new ScriptBuilder().opFalse().build();

        else if (obj instanceof TransactionSignature)
            return new ScriptBuilder().data(((TransactionSignature) obj).encodeToBitcoin()).build();

        else if (obj instanceof Signature) {
            Signature sig = (Signature) obj;
            ScriptBuilder sb = new ScriptBuilder();
            sb.data(sig.getSignature());
            if (sig.getPubkey().isPresent())
                sb.data(sig.getPubkey().get().getBytes());
            return sb.build();
        }

        throw new IllegalArgumentException();
    }
 
Example 2
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 5 votes vote down vote up
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();
    }
 
Example 3
Source File: ScriptTools.java    From thunder with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Script produceScript (byte[] template, byte[]... parameters) {
    try {

        ScriptBuilder builder = new ScriptBuilder();

        int parameter = 0;
        for (byte chunk : template) {
            int op = chunk;
            if (op < 0) {
                op = op + 256;
            }
            if (op == 255) {
                builder.data(parameters[parameter]);
                parameter++;
            } else if (op == 0) {
                builder.data(new byte[0]);
            } else {
                builder.op(op);
            }
        }

        //Bug in bitcoinJ when dealing with OP_0. Gets solved by reserializing.
        Script s = builder.build();
        return new Script(s.getProgram());

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 4
Source File: ScriptTools.java    From thundernetwork with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Script produceScript (byte[] template, byte[]... parameters) {
    try {

        ScriptBuilder builder = new ScriptBuilder();

        int parameter = 0;
        for (byte chunk : template) {
            int op = chunk;
            if (op < 0) {
                op = op + 256;
            }

            if (op == 255) {
                builder.data(parameters[parameter]);
                parameter++;
            } else if (op == 0) {
                builder.data(new byte[0]);
            } else {
                builder.op(op);
            }
        }

        //Bug in bitcoinJ when dealing with OP_0. Gets solved by reserializing.
        Script s = builder.build();
        return new Script(s.getProgram());

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 5
Source File: Tools.java    From thunder with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Script getDummyScript () {
    ScriptBuilder builder = new ScriptBuilder();
    builder.smallNum(0);
    return builder.build();
}
 
Example 6
Source File: Tools.java    From thundernetwork with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Gets the dummy script.
 *
 * @return the dummy script
 */
public static Script getDummyScript () {
    ScriptBuilder builder = new ScriptBuilder();
    builder.smallNum(0);
    return builder.build();
}