Bitcoin Forum

Bitcoin => Project Development => Topic started by: fbueller on May 16, 2014, 04:59:28 PM



Title: Script to sign multisig transactions using an Electrum wallet
Post by: fbueller on May 16, 2014, 04:59:28 PM
I wrote a command line script which prompts for a redeem script, raw transaction, and electrum seed or mnemonic, and adds creates signatures for any key that it finds. I wrote it largely out of frustration that you can't sign multi-signature transactions in the Electrum client. especially given how useful a master public key is for deriving multiple multisig addresses for users.

So, and I expect plenty of healthy suspicion from users about entering seeds, but there you go. The program works if you're offline anyway, once you can supply the JSON inputs string.

Code:
git clone https://github.com/Bit-Wasp/bitcoin-lib-php.git
cd bitcoin-lib-php/examples
php electrum_sign_multisig.php

If you're running Debian, you'll need to go: sudo apt-get install php5-gmp - or similar for Ubuntu.

The following is output from a transaction I just broadcast, I'm using this tool to sign transactions as an administrator on Bitwasp, since it derives keys from electrum for orders.
The raw transaction I pasted was partially signed using Bitcoin Core.
Code:
afk@jalapeno:~/git/bitcoin-lib-php/examples$ php electrum_sign_multisig.php 
Enter redeem script: 522103793882c7025f32d2bbdd07f145fbf16fea83df6352ef42e38d4137f5a24975cc2102e67b93adc52dfdfa181311610244d98811ecd0a26be65a10b720f417cb8997904104492368cd25892f3a6a618ce750e28f4f1c3c9fec4abd67287cb6450356abc0cfef3358cd39b5390c7ea7e053358c97463741d22d1e9cd2c2d3a528d137fb6bf553ae
Learned about 2 of 3 address: 33XfrAfvrihcpatpn2yw8A8mw2ARjhe3Ea

Enter a raw transaction to sign: 01000000011aa43825f69cf30167be512b03cf7a7e2c30e51e87202eb7157a658a2d8f77e000000000d40047304402201791527e0c93c0386d362f0bbf5223ffe993fbb820f8750541305a54622b4347022001f8df8c13ab07be2885f152a7425efefaf0ea3ef368d95936bac41af3a88c1e014c89522103793882c7025f32d2bbdd07f145fbf16fea83df6352ef42e38d4137f5a24975cc2102e67b93adc52dfdfa181311610244d98811ecd0a26be65a10b720f417cb8997904104492368cd25892f3a6a618ce750e28f4f1c3c9fec4abd67287cb6450356abc0cfef3358cd39b5390c7ea7e053358c97463741d22d1e9cd2c2d3a528d137fb6bf553aeffffffff02d8270000000000001976a914264cfe558e3cd797bdf83dfe72e03484f4e10a2288ac584d0000000000001976a914313513a1d8c37c0b19f9474bce59ed8c2c14839088ac00000000

Enter input data as JSON string (or 'x' to load this from webbtc.com): x

Enter electrum seed or mnemonic: ask cheat plain escape trouble moon grasp carve brown plant heart thought
Seed accepted.

Trying keys 0 to 30
Have 1 private keys we can sign with. Look for more? (y/n) y
Trying keys 30 to 60
Have 1 private keys we can sign with. Look for more? (y/n) n
Array
(
    [hex] => 01000000011aa43825f69cf30167be512b03cf7a7e2c30e51e87202eb7157a658a2d8f77e000000000fd1d010047304402201791527e0c93c0386d362f0bbf5223ffe993fbb820f8750541305a54622b4347022001f8df8c13ab07be2885f152a7425efefaf0ea3ef368d95936bac41af3a88c1e014830450221008206c3e8b5c53510c2716e78b6b1d36d882f6189752d51e6cdc722b1dacffb60022023954fe8620a798277fbef463d773ff9a8c317857ce5cb7af15647193f67cd97014c89522103793882c7025f32d2bbdd07f145fbf16fea83df6352ef42e38d4137f5a24975cc2102e67b93adc52dfdfa181311610244d98811ecd0a26be65a10b720f417cb8997904104492368cd25892f3a6a618ce750e28f4f1c3c9fec4abd67287cb6450356abc0cfef3358cd39b5390c7ea7e053358c97463741d22d1e9cd2c2d3a528d137fb6bf553aeffffffff02d8270000000000001976a914264cfe558e3cd797bdf83dfe72e03484f4e10a2288ac584d0000000000001976a914313513a1d8c37c0b19f9474bce59ed8c2c14839088ac00000000
    [complete] => true
)

afk@jalapeno:~/git/bitcoin-lib-php/examples$ bitcoind sendrawtransaction 01000000011aa43825f69cf30167be512b03cf7a7e2c30e51e87202eb7157a658a2d8f77e000000000fd1d010047304402201791527e0c93c0386d362f0bbf5223ffe993fbb820f8750541305a54622b4347022001f8df8c13ab07be2885f152a7425efefaf0ea3ef368d95936bac41af3a88c1e014830450221008206c3e8b5c53510c2716e78b6b1d36d882f6189752d51e6cdc722b1dacffb60022023954fe8620a798277fbef463d773ff9a8c317857ce5cb7af15647193f67cd97014c89522103793882c7025f32d2bbdd07f145fbf16fea83df6352ef42e38d4137f5a24975cc2102e67b93adc52dfdfa181311610244d98811ecd0a26be65a10b720f417cb8997904104492368cd25892f3a6a618ce750e28f4f1c3c9fec4abd67287cb6450356abc0cfef3358cd39b5390c7ea7e053358c97463741d22d1e9cd2c2d3a528d137fb6bf553aeffffffff02d8270000000000001976a914264cfe558e3cd797bdf83dfe72e03484f4e10a2288ac584d0000000000001976a914313513a1d8c37c0b19f9474bce59ed8c2c14839088ac00000000
98af6ce3abafbfe4fac55cdde6a7482945038af0f3b4ca67e6fe9973fc08f697


How it works:
First prompt for the redeemScript. Decode, check that it's valid. If not, prompt again.

Ask for a raw transaction to sign. Paste raw transaction here, it decodes, checks that it's valid. If the JSON inputs are pasted here, it'll prevent the next prompt from popping up.

Ask for JSON inputs, or offer to download them from webbtc. If they were already supplied earlier, this is skipped.

Ask for electrum seed (hex seed) or 12 word mnemonic. It'll generate the secret exponent, and master public key, then derive the first 30 public keys for the wallet. If any match keys in the redeemScript, the corresponding private key is generated and used in the wallet. It'll then ask if you think this is OK, or should it scan for more keys. Maybe you own all the keys, maybe just one. It'll stop if you reach the number of signatures required.

Next is where the magic happens, it creates a wallet with private key/scripthash data, and builds up signatures wherever it can.

It'll return complete = TRUE or FALSE. TRUE means it's signed fully, but false means it's not signed fully.


Hopefully someone will find this as useful as I do.


Title: Re: Script to sign multisig transactions using an Electrum wallet
Post by: fauxik on March 19, 2015, 07:25:11 AM
Hello!
That sounds you have some experience with Bitwasp.
Can I ask you some question?

I testing out Bitwasp and I do not know what I supposed to do.
If buyer raise a dispute - there Bitwasp asking for "Sign with private key: m/0'/0/6" from admin
but I do not know where can be those private key found.
So the question is, how to dump private keys from Electrum when I never used Electrum wallet ?
 (I used a bip32.org to generate main admin public key ... so must I somehow import the main admin bip32 key into Electrum ? or what?)
I was looked into examples and find there file bip32.php, but I not sure how to fill those script to work correctly:

$master = BIP32::master_key(bin2hex(mcrypt_create_iv(64, \MCRYPT_DEV_URANDOM)));
But I do not know how I may rewrite those master for my purpose, how or where I may put there my bip32 private hex, master seed or what?
I am not sure, but there I may somehow put my admin bip32 private into $master ? But I unfortunatelly I do not know how.

and another value is $def = "0'/0"; -- so this I may rewrite to $def = "0'/0/6"; ?

Please it is possible to help me?
I would very much help appreciated.