Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify signature against public key sighash #54

Open
zebpay-peswani opened this issue Oct 8, 2020 · 2 comments
Open

verify signature against public key sighash #54

zebpay-peswani opened this issue Oct 8, 2020 · 2 comments

Comments

@zebpay-peswani
Copy link

zebpay-peswani commented Oct 8, 2020

I am working on trezor multisig support
I've a raw transaction 2of3 multisig signed by 1 cosigner

0100000001f479528049ee4b235548a7116f5ca31c607728af3d4ac8f102c5be13e93dcca900000000b40047304402201b83bd2209bc101736f60eeec3bcc1f79d45019d1829af97f5bd3210e8d8d786022020954c15372268d299b5463e1335595221e16222aee0291509a2098a0eb7fe80414c69522103556117b3f9a92f2997529af415c2c07185b872ddf1439995eca7f3acb65c58822103917f2527fc394ae2da48ce0148d29e5b756c4d9d180a6762bbe7abe215b29a562103f03364c624889c99059902524bcd8e2b24fb09cceb2e0f895d6475ef4913676553aeffffffff02a08601000000000017a914fd80725877f902f4491a6dc733fe788c376aeff8875033f4050000000017a9149aba36d2c19b08781230a4301f364610a163fc2d8700000000

Signed hex

304402201b83bd2209bc101736f60eeec3bcc1f79d45019d1829af97f5bd3210e8d8d786022020954c15372268d299b5463e1335595221e16222aee0291509a2098a0eb7fe80

public keys involved

['03556117b3f9a92f2997529af415c2c07185b872ddf1439995eca7f3acb65c5882', '03917f2527fc394ae2da48ce0148d29e5b756c4d9d180a6762bbe7abe215b29a56', '03f03364c624889c99059902524bcd8e2b24fb09cceb2e0f895d6475ef49136765']

transaction details

I want to verify from which public key signed the transaction out of 3 public key

I tried with the below code in electrumg

    tx = Transaction(raw_tx)
    sighash = Hash(bfh(tx.serialize_preimage(0)))

    pubkeys, x_pubkeys = tx.get_sorted_pubkeys(tx.inputs()[0])

    print(pubkeys)
    print(x_pubkeys)

    order = ecdsa.ecdsa.generator_secp256k1.order()
    r, s = ecdsa.util.sigdecode_der(bfh(sig), order)
    sig_string = ecdsa.util.sigencode_string(r, s, order)
    compressed = True

    for recid in range(4):
        print(recid)
        public_key = MyVerifyingKey.from_signature(sig_string, recid, sighash, curve=SECP256k1)
        print(public_key)
        pubkey = bh2u(point_to_ser(public_key.pubkey.point, compressed))
        print(pubkey)
        # print(public_key.verify_digest(sig_string, sighash, sigdecode=ecdsa.util.sigdecode_string))
    print(tx.signature_count())
    print(sighash.hex())

The output of this comes

1
02e4ee5e437084cc41095ef383803d06268068ce602a95d2e1cf7f5f5584a7d4b4
2
028b7b3b8a52b866166e34ae1194cbf60d3e2f553393410585ae4488cfb7a86796
3
027a9e3a118516cf8892b3c078ad6073f539c40e8301382f74ddc66de32783df2d

As you can see no public key match with original public keys. they all start with 02 and my public keys start with 03

Please help me.

@zebpay-peswani zebpay-peswani changed the title verify signature against public key verify signature against public key sighash Oct 8, 2020
@h4x3rotab
Copy link
Member

Hi Peswani, I'm quite surprised because it's our first time to see people using Electrum as a Python library. Could you give us more information like how you run the code, so that we can easily reproduce and help figure it out?

Btw, though we do support Electrum in long term, we have another library bgoldjs-lib which is much more developer friendly. It's based on javascript, though. I'm also very happy to help if you want to know how it could be done with bgoldjs-lib.

@zebpay-peswani
Copy link
Author

@h4x3rotab Thanks for the information. I am working on trezor support. elecrtumg does not support it as of now .Because trezor library is in python so I've choose electrumg for referencing. I've created my own UI as well for the wallet. my issue is how can I know that the signature belong to a particular public key in case of multisig. for this I have referenced electrumg code.
The setup is simple you need to download the electrumg code and install all the dependencies. Create a new python file in lib folder and paste below code

from ecdsa import ecdsa, util
from lib.transaction import Transaction, bh2u, SECP256k1

from lib.bitcoin import Hash, MyVerifyingKey, i2o_ECPublicKey, point_to_ser
from lib.util import bfh

raw_tx = '0100000001f479528049ee4b235548a7116f5ca31c607728af3d4ac8f102c5be13e93dcca900000000b40047304402201b83bd2209bc101736f60eeec3bcc1f79d45019d1829af97f5bd3210e8d8d786022020954c15372268d299b5463e1335595221e16222aee0291509a2098a0eb7fe80414c69522103556117b3f9a92f2997529af415c2c07185b872ddf1439995eca7f3acb65c58822103917f2527fc394ae2da48ce0148d29e5b756c4d9d180a6762bbe7abe215b29a562103f03364c624889c99059902524bcd8e2b24fb09cceb2e0f895d6475ef4913676553aeffffffff02a08601000000000017a914fd80725877f902f4491a6dc733fe788c376aeff8875033f4050000000017a9149aba36d2c19b08781230a4301f364610a163fc2d8700000000'
sig = '304402201b83bd2209bc101736f60eeec3bcc1f79d45019d1829af97f5bd3210e8d8d786022020954c15372268d299b5463e1335595221e16222aee0291509a2098a0eb7fe80'


tx = Transaction(raw_tx)

sighash = Hash(bfh(tx.serialize_preimage(0)))

pubkeys, x_pubkeys = tx.get_sorted_pubkeys(tx.inputs()[0])
print(sighash.hex())
print(pubkeys)
# print(x_pubkeys)

order = ecdsa.generator_secp256k1.order()
r, s = util.sigdecode_der(bfh(sig), order)
sig_string = util.sigencode_string(r, s, order)
compressed = True

for recid in range(4):
    print(recid)
    public_key = MyVerifyingKey.from_signature(sig_string, recid, sighash, curve=SECP256k1)
    #key = i2o_ECPublicKey(public_key.pubkey, compressed=True)
    #print("=========" + key.hex() + "============")
    pubkey = bh2u(point_to_ser(public_key.pubkey.point, compressed))
    print(pubkey)
    # print(public_key.verify_digest(sig_string, sighash, sigdecode=ecdsa.util.sigdecode_string))
print(tx.signature_count())
print(sighash.hex())

The above code take reference from Transaction class in lib\transaction.py folder exact 649 line number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants