Post
Topic
Board Development & Technical Discussion
Re: Assurance contracts
by
Mike Hearn
on 20/09/2012, 22:02:22 UTC
I've played around a bit with bitcoinj earlier today (while also brushing up my Java knowledge, it's been a while). From a quick look at the source, it seems that creating the contract transaction would already be possible, but the signing of inputs using anyonecanpay isn't possible yet?

If you look at Transaction.signInputs() there is a boolean hardcoded to false called anyoneCanPay. There's also an assert at the top checking you use SIGHASH_ALL, as I never tested the other flags.

I think it'd be some fairly simple modifications to add support. Basically you would extend Wallet.SendRequest to have a new parameter like "inputValue". By default it'd be unset/set to a dummy value like -1 and then obviously the input value would be >= output value as now. If explicitly set then wallet.completeTx() would add enough inputs to reach the target value and then call tx.signInputs(SigHash.ALL, this, true); where the last boolean param would be one you add that'd set anyoneCanPay.

One thing that needs careful thought is what happens if you can't make the input target value exactly. Adding change outputs won't work properly, so you'd need to first construct a transaction that gives you an output of exactly the right value, and then another transaction to pledge it. That would complicate the API somewhat if done automatically, so I'd suggest just throwing an exception in that case and expecting the app to handle it at a higher level.

If you want to discuss it further let's take the design talk to the bitcoinj mailing list.