ofek / bit

Bitcoin made easy.
https://ofek.dev/bit/
MIT License
1.24k stars 213 forks source link

Generating multisig addresses #81

Closed xplsek03 closed 5 years ago

xplsek03 commented 5 years ago

I'm working on escrow/multisig system. Bit is great and I'd be sad to not implement it just because it doesn't support generating multisig addresses. I understand this behavior might not be the priority (https://github.com/ofek/bit/issues/6), but here I go.

I plan to use couple of Multisig instances, which will share the same Key as _privatekey, as all the paths the trade could get. For this it should be enough to write function which will calculate the appropriate values for this universal Key: address, _publickey etc.. Am I thinking right or is it more complicated?

Thanks.

bjarnemagnussen commented 5 years ago

I am not sure I understand your question.

Bit does support multisignature and basic example code can be found in the docs at e.g. https://ofek.dev/bit/guide/keys.html#types

You can use the same PrivateKey for multiple instances of MultiSig that incorporate different public keys. For privacy reasons this is however rarely desirable to do. For signing you must use the instance of MultiSig, which incorporates the appropriate multi-signature contract.

xplsek03 commented 5 years ago

Oh, ok I rephrase that. What I mean is I would like to create multisig address like this one: www.cs.kent.edu/~ruttan/sysprog/lectures/multi-thread/multi-thread.html

and then, using this escrow address, and then I would like to run couple of multisig options on this address. Can Bit do this? I'm going to read the docs once more, maybe there is a better option how to do it.

bjarnemagnussen commented 5 years ago

Is the link you provided the correct one?

A multisig escrow in Bitcoin is typically something like a 2-of-3 signature contract, which is possible to do with Bit. But what do you mean by "multisig options"?

Can you provide an example of how you would like to use it?

xplsek03 commented 5 years ago

Yes, of course. What I am trying to achieve (for A, B and System) is: A will transfer the funds to a escrow address, just like the generated one in the link I sent. The funds will be locked for manipulation like this. Then, there will be couple of 2/3 multisig contracts 'binded' to this escrow address, eg. one of them will send funds to A, other one to B: that is what I meant by 'multisig options'.

Is this the escrow-multisig behaviour I can achieve with Bit? Never worked with bitcoin before, so it is possible I haven't grasped some crucial concept properly.

bjarnemagnussen commented 5 years ago

Please check the link you posted, as I cannot find anything directly Bitcoin specific in it.

Just to be sure we are on the same page: Bitcoin uses a UTXO system. If you don't know what that is I would recommend that you read up on that first. I found the book Mastering Bitcoin from Andreas Antonopolous very good.

With Bit you can create an escrow (2-of-3 multisig) address, e.g. {A, B, System}. A can then transfer funds to this escrow. Those funds can then be spent (and split) to any address(es) by either A+B, A+System or B+System.

However, each escrow address is "binded" to exactly one specific 2-of-3 multisig contract, e.g. {A, B, System}. You cannot use the same address for different 2-of-3 multisig contracts, e.g. {A', B, System} generates a completely different address.

xplsek03 commented 5 years ago

1/ sorry, wrong one: https://coinb.in/#newMultiSig 2/ I know what that is. Gonna read it anyway, thanks. 3/ Can you provide a brief example how to create escrow address then? I would be much obliged.

bjarnemagnussen commented 5 years ago

You create multisig addresses just as described in the docs, see also https://ofek.dev/bit/guide/transactions.html.

from bit import PrivateKey, MultiSig

# Create the System private key
system_key = PrivateKey()

# Define the public keys for A and B
a_key = "public key of A"  # use appropriate values...
b_key = "public key of B"  # use appropriate values...

# Create the 2-of-3 multisig
multisig = MultiSig(system_key, {a_key.public_key, b_key.public_key, system_key.public_key}, 2)

# Display escrow address
multisig.segwit_address

# Send out some received funds by creating a transaction signed by System
tx_signed_by_system = multisig.create_transaction([('SOME_ADDRESS', 10, 'USD')])

The tx_signed_by_system will then contain a spending transaction that does still also need to be signed by either A or B on their end.

xplsek03 commented 5 years ago

Now it's 100% clear. Thank you for your help.

ukor commented 3 years ago

Hello @bjarnemagnussen and @xplsek03 I will appreciate your help on this #142 issue 142