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;
}
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);
}
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;
}
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;
}
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();
}
Aggregations