ofek / bit

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

Making multisig Transaction #142

Closed ukor closed 3 years ago

ukor commented 3 years ago

I am having an issue signing and broadcasting a transaction, I followed the solution at #81 but still no luck.

Here is my code:

from bit import (
    PrivateKey,
    PrivateKeyTestnet,
    MultiSig,
    MultiSigTestnet,
)
from bit.network import NetworkAPI
from bit.transaction import calc_txid

system_key = PrivateKeyTestnet(system_wif)
user_key = PrivateKeyTestnet(user_wif)
wallet = MultiSigTestnet(
    user_key,
    [user_key.public_key, system_key.public_key],
    2,
)

address = wallet.address

After sending some testnet coins to address. Here is my transaction code.

Transaction

tx = wallet.create_transaction([('2MyELqPy73sejRziKWaAisjFNsryZamWxbX', '0.0001', 'btc')])

tx1 = system_key.sign_transaction(tx)

tx2 = user_key.sign_transaction(tx1)

NetworkAPI.broadcast_tx_testnet(tx2)

tx_id = calc_txid(tx2)

I get the error

    ConnectionError": "Transaction broadcast failed, or Unspents were already used."

wallet.get_unspent() returns

[
Unspent(amount=83000, confirmations=136, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='7d3d8407ad2da5538deddf3aaca26ba3314a36d6e81bba58e1db20cefa4f0dee', txindex=1, segwit=False, sequence=4294967295),

Unspent(amount=84000, confirmations=51, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='dd20d76e0c763c986d0c95c2f267e8476717974a9864db1345696a04a59a29d5', txindex=1, segwit=False, sequence=4294967295),

Unspent(amount=100000, confirmations=136, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='facb42759bdd9f5c04e38100c124e36a3b5ea9afd5f50d23d09da16b0158622d', txindex=0, segwit=False, sequence=4294967295),

Unspent(amount=10000, confirmations=136, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='572d9457ba49fe8496b5b8e4b5a5ec91316052dd281e7ca83bbbf3369cf00c65', txindex=1, segwit=False, sequence=4294967295),

Unspent(amount=83000, confirmations=136, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='3087a757e1ea5b91ea433af2788a072573e04ed99ed38d1bcecb235884cb37cf', txindex=0, segwit=False, sequence=4294967295),

Unspent(amount=1248173, confirmations=137, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='b96a89a17c52cb3c08349676178834d563ce61b3ed9bc2e844f5229f8756aea1', txindex=1, segwit=False, sequence=4294967295),

Unspent(amount=10000, confirmations=136, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='afb1c676639b825d72cce61c72a778d58cf4804063bcae39bed147ffa54f6074', txindex=0, segwit=False, sequence=4294967295),

Unspent(amount=100000, confirmations=136, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='c59155be109d94aa3f4480c212110ba9a4ba4d80bf89d8c688314bf73df2daac', txindex=0, segwit=False, sequence=4294967295),

Unspent(amount=10000, confirmations=51, script='a914d03723f90bb3a9533a484bf15f352501520408af87', txid='b9af63ad0b8b95b39900b55ea197938f98eff4b5a80131de6998fe558c67de02', txindex=0, segwit=False, sequence=4294967295)

]

What am I doing wrong?

bjarnemagnussen commented 3 years ago

Can you post the hex representation of both tx1 and tx2 here?

ukor commented 3 years ago

Tx1

0100000009a1ae56879f22f544e8c29bedb361ce63d5348817769634083ccb527ca1896ab9010000009200483045022100c26acdc9b3a53e3c587fd64225b30f031bb0d2f85ae9d4b359d481aebc3fcebe0220504a238138f51d53685750c9a701323bb193c5bceb88fb6543fffe0a030f866b01475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffee0d4fface20dbe158ba1be8d6364a31a36ba2ac3adfed8d53a52dad07843d7d010000009200483045022100e2ba6a8b7355dbbc42f05fe13e6f022003beca033a1719fb1e5beb1548f932b402203e7c7923e3027f8de5ebde4198bb009cf5d2c8ea84d51b548724015667e918c201475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff2d6258016ba19dd0230df5d5afa95e3b6ae324c10081e3045c9fdd9b7542cbfa000000009200483045022100a58477b77db46bbf00ebef186f4330990c6cb33921ebd65c772b41362a0dc816022009612f5c18a9941e2b7e5095705ae95b23a6a2bae316f50053fd1a56820fd73401475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff02de678c55fe9869de3101a8b5f4ef988f9397a15eb50099b3958b0bad63afb90000000092004830450221009667cbc998a0cb2f1e062191872d8da4ccf7d6e2abda243ce176c383d7e0a443022060b26ae9cf51d247655270454ca16c4c23e791670ad9e5e1e6015780660691bc01475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff74604fa5ff47d1be39aebc634080f48cd578a7721ce6cc725d829b6376c6b1af000000009200483045022100a4fee7ea12840a82f327c581feb0ec15d804078f2a654cd2c2006a48e366871602206e648579d73c9ffff17557089f2696111a240bd525f5cda83a3e17278c531b0401475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff650cf09c36f3bb3ba87c1e28dd52603191eca5b5e4b8b59684fe49ba57942d57010000009200483045022100fe678bd147dae771284399aa7c0c57144fd86155f76f04867e5fcb473687a170022057dcc09c4732cdabbd6e8d6e70566fa2d3f2d2441f5da12afa054703ba1c3a3201475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffacdaf23df74b3188c6d889bf804dbaa4a90b1112c280443faa949d10be5591c5000000009200483045022100ca260c13f11071cd053fa61751945bc297919e5b490cbc6638a56f153dfb4d9002205870410f5796fd1706ca39139c73f8c237819e583db6c7f9869b5a3b9a07221801475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffcf37cb845823cbce1b8dd39ed94ee07325078a78f23a43ea915beae157a78730000000009200483045022100cb7e9ac647fb192f3dedb03c4f44024e8e63867dd9c4ce457beebdf453c9c8d102202fc6c1b599ef88bdbd15ffcd9d580ad863334e4eb666d0b96733965f1084c02601475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffd5299aa5046a694513db64984a97176747e867f2c2950c6d983c760c6ed720dd010000009200483045022100fa0d396f130040f43fba8edb5ed4f58d3875e81fc6564f14ffd87a77ea0f4cf7022051f53d82a111a96eddaecfd1937877b113199dc97a0c0ffdd5971d16f80be9e401475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff02102700000000000017a91441a60ddc14b7d84afd6528cd36436ed2364b4d70879d6e16000000000017a914d03723f90bb3a9533a484bf15f352501520408af8700000000

Tx2

0100000009a1ae56879f22f544e8c29bedb361ce63d5348817769634083ccb527ca1896ab9010000009200483045022100c26acdc9b3a53e3c587fd64225b30f031bb0d2f85ae9d4b359d481aebc3fcebe0220504a238138f51d53685750c9a701323bb193c5bceb88fb6543fffe0a030f866b01475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffee0d4fface20dbe158ba1be8d6364a31a36ba2ac3adfed8d53a52dad07843d7d010000009200483045022100e2ba6a8b7355dbbc42f05fe13e6f022003beca033a1719fb1e5beb1548f932b402203e7c7923e3027f8de5ebde4198bb009cf5d2c8ea84d51b548724015667e918c201475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff2d6258016ba19dd0230df5d5afa95e3b6ae324c10081e3045c9fdd9b7542cbfa000000009200483045022100a58477b77db46bbf00ebef186f4330990c6cb33921ebd65c772b41362a0dc816022009612f5c18a9941e2b7e5095705ae95b23a6a2bae316f50053fd1a56820fd73401475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff02de678c55fe9869de3101a8b5f4ef988f9397a15eb50099b3958b0bad63afb90000000092004830450221009667cbc998a0cb2f1e062191872d8da4ccf7d6e2abda243ce176c383d7e0a443022060b26ae9cf51d247655270454ca16c4c23e791670ad9e5e1e6015780660691bc01475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff74604fa5ff47d1be39aebc634080f48cd578a7721ce6cc725d829b6376c6b1af000000009200483045022100a4fee7ea12840a82f327c581feb0ec15d804078f2a654cd2c2006a48e366871602206e648579d73c9ffff17557089f2696111a240bd525f5cda83a3e17278c531b0401475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff650cf09c36f3bb3ba87c1e28dd52603191eca5b5e4b8b59684fe49ba57942d57010000009200483045022100fe678bd147dae771284399aa7c0c57144fd86155f76f04867e5fcb473687a170022057dcc09c4732cdabbd6e8d6e70566fa2d3f2d2441f5da12afa054703ba1c3a3201475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffacdaf23df74b3188c6d889bf804dbaa4a90b1112c280443faa949d10be5591c5000000009200483045022100ca260c13f11071cd053fa61751945bc297919e5b490cbc6638a56f153dfb4d9002205870410f5796fd1706ca39139c73f8c237819e583db6c7f9869b5a3b9a07221801475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffcf37cb845823cbce1b8dd39ed94ee07325078a78f23a43ea915beae157a78730000000009200483045022100cb7e9ac647fb192f3dedb03c4f44024e8e63867dd9c4ce457beebdf453c9c8d102202fc6c1b599ef88bdbd15ffcd9d580ad863334e4eb666d0b96733965f1084c02601475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffffd5299aa5046a694513db64984a97176747e867f2c2950c6d983c760c6ed720dd010000009200483045022100fa0d396f130040f43fba8edb5ed4f58d3875e81fc6564f14ffd87a77ea0f4cf7022051f53d82a111a96eddaecfd1937877b113199dc97a0c0ffdd5971d16f80be9e401475221031515cbaf8c6090279d90983b88f91700a135c4f9acd97a85ca62d383bc2bd907210226dd21551162886a9743231cf103fd78b326a0089a20fc57497017947a7618de52aeffffffff02102700000000000017a91441a60ddc14b7d84afd6528cd36436ed2364b4d70879d6e16000000000017a914d03723f90bb3a9533a484bf15f352501520408af8700000000

Are they suppose to be the same thing?

bjarnemagnussen commented 3 years ago

I see what is happening.

Your way of using the multisig is not right.

Because you use the user private key you have set up the wallet object from the users perspective with

wallet = MultiSigTestnet(
    user_key,
    [user_key.public_key, system_key.public_key],
    2,
)

Then when you call wallet.create_transaction it also signs it with the user key. However when you try to sign the transaction with the system key you must create a specific wallet object from the system's view instead of directly signing with the system key.

So:

wallet_system, = MultiSigTestnet(
    system_key,
    [user_key.public_key, system_key.public_key],
    2,
)
wallet_system.sign_transaction(tx)

Note: I would recommend to actually create the multisig objects as

wallet_system, = MultiSigTestnet(
    system_key,
    (user_key.public_key, system_key.public_key=,
    2,
)

so the public key list is provided as a set. This sorts them lexicograpically and you dont need to provide the correct order when recovering the multisig from the keys.

ukor commented 3 years ago

@bjarnemagnussen - Thank you so much. You are a lifesaver.

Thanks for the suggestion.