}
public ProjectModel(LHProtos.ProjectDetails.Builder liveProto) {
this.proto = liveProto;
final LHProtos.Project.Builder wrapper = LHProtos.Project.newBuilder().setSerializedPaymentDetails(liveProto.build().toByteString());
Project project = unchecked(() -> new Project(wrapper.build()));
title.set(project.getTitle());
memo.set(project.getMemo());
goalAmount.set(project.getGoalAmount().value);
minPledgeAmount.set(recalculateMinPledgeAmount(goalAmount.longValue()));
if (liveProto.hasPaymentUrl()) {
String host = LHUtils.validateServerPath(liveProto.getPaymentUrl(), project.getID());
if (host == null)
throw new IllegalArgumentException("Server path not valid for Lighthouse protocol: " + liveProto.getPaymentUrl());
serverName.set(host);
}
// Connect the properties.
title.addListener(o -> proto.getExtraDetailsBuilder().setTitle(title.get()));
memo.addListener(o -> proto.setMemo(memo.get()));
// Just adjust the first output. GUI doesn't handle multioutput contracts right now (they're useless anyway).
goalAmount.addListener(o -> {
long value = goalAmount.longValue();
minPledgeAmount.set(recalculateMinPledgeAmount(value));
proto.getOutputsBuilder(0).setAmount(value);
});
minPledgeAmount.addListener(o -> proto.getExtraDetailsBuilder().setMinPledgeSize(minPledgeAmountProperty().get()));
serverName.addListener(o -> {
final String name = serverName.get();
if (name.isEmpty())
proto.clearPaymentUrl();
else
proto.setPaymentUrl(LHUtils.makeServerPath(name, LHUtils.titleToUrlString(title.get())));
});
Address addr = project.getOutputs().get(0).getAddressFromP2PKHScript(project.getParams());
if (addr == null)
throw new IllegalArgumentException("Output type is not a pay to address: " + project.getOutputs().get(0));
address.set(addr.toString());
address.addListener(o -> {
try {
Address addr2 = new Address(project.getParams(), address.get());
proto.getOutputsBuilder(0).setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(addr2).getProgram()));
} catch (AddressFormatException e) {
// Ignored: wait for the user to make it valid.
}
});