Ejemplo n.º 1
0
    def testUnsignedTx(self):
        ustxi = UnsignedTxInput(tx1raw, 1, None, self.pubKey)
        a160_1 = addrStr_to_hash160('mhyjJTq9RsDfhNdjTkga1CKhTiL5VFw85J')[1]
        a160_2 = addrStr_to_hash160('mgoCqfR25kZVApAGFK3Tx5CTNcCppmKwfb')[1]
        dtxo1 = DecoratedTxOut(hash160_to_p2pkhash_script(a160_1),
                               long(1.00 * ONE_BTC))
        dtxo2 = DecoratedTxOut(hash160_to_p2pkhash_script(a160_2),
                               long(0.49 * ONE_BTC))

        ustx = UnsignedTransaction().createFromUnsignedTxIO([ustxi],
                                                            [dtxo1, dtxo2])

        self.assertEqual(len(ustx.ustxInputs), 1)
        self.assertEqual(len(ustx.decorTxOuts), 2)
        self.assertEqual(ustx.lockTime, 0)
        self.assertEqual(ustx.uniqueIDB58, 'J2mRenD7')

        serUstx = ustx.serialize()
        ustx2 = UnsignedTransaction().unserialize(serUstx)
        self.assertEqual(serUstx, ustx2.serialize())

        serUstxASCII = ustx.serializeAscii()
        ustx2 = UnsignedTransaction().unserializeAscii(serUstxASCII)
        self.assertEqual(serUstx, ustx2.serialize())
Ejemplo n.º 2
0
    def testCreateMultisigTests(self):
        '''
      This is used solely to generate some multi-sig scripts, using known
      public/private keys, that we can then use to test signing, etc.  This
      test doesn't actually test anything, but could be tweaked and then 
      reenabled to produce new data for new tests
      '''

        import textwrap
        asc_nosig = textwrap.dedent("""
         =====TXSIGCOLLECT-5JxmLy4T======================================================
         AQAAAAsRCQcAAAAAAf19AQEAAAALEQkH/XsoFKgwJMVZsviXpv+aOun4BQHRm+Cuvs/X7O/J3n8BAAAA
         /QMBAQAAAAGcgxlJ0d+ZHi+MzG+laL4qTx/jVH/lPbbKmrGLA1oc8AAAAACMSTBGAiEArOklwdcihg72
         fMu+GvnKF+AdFiMmeT7CWV4KMZmA3kcCIQDyjBMqkI6tFVXMG/yhbBhVg7TNYsAGLjM5UWLfVx57WgFB
         BLmTMVBhjWo901GrcZzZMNBUectdX4ZsVyHhMNjZpaAJxlpQqnjiK9PAvrNqOIgMq8itz9S3KDaOs/Kh
         W6/lJNL/////AsAOFgIAAAAAGXapFInbjSGPxqYLm35NTXhzcAkVf8h8iKwwq98DAAAAABl2qRSBj0Gs
         NlhCyvZkoRO2iRw54544foisAAAAAAAA/////wFBBJ6i78WXHp6ywhTupFpF7A0V2jwQEjE9pWO1+7wZ
         qS+IM59Dur1Ut5OC+yUycjeFHQdqemkBDFT97zMCJalmtXwAAALiAQAAAAsRCQfJUkEEagSrmNnkd0rY
         BuMC3d62O+oWtctfIj7ndHjoYbtYPrM2tvvLYLWz1PFVGsReX/xJNkZufZj2x8Dsc2U590aRpkEEaGgH
         N8dtq7gByyIE9X2+TkV55PcQzWfcG0InWSyB6bXPArWsnotMn0m+UlEFa2ptAR5MN/a20X7ea1X6ojUZ
         4kEEuVwknYT0F+PjlaEnQlQotUBnHMFYgeuCjBe3IqU/xZniHKXlbJDzQJiNOTOsx2vrgy/WTKsHjd88
         5zKSMDHRqFOuoH+IAgAAAAAAAAROT05FADIBAAAACxEJBxl2qRRs7kd5CHIvdApqfOmMDp1dyrD6Mois
         gARXAQAAAAAAAAROT05FAA==
         ================================================================================
         """.strip())

        asc_sig = textwrap.dedent("""
         =====TXSIGCOLLECT-5JxmLy4T======================================================
         AQAAAAsRCQcAAAAAAf3EAQEAAAALEQkH/XsoFKgwJMVZsviXpv+aOun4BQHRm+Cuvs/X7O/J3n8BAAAA
         /QMBAQAAAAGcgxlJ0d+ZHi+MzG+laL4qTx/jVH/lPbbKmrGLA1oc8AAAAACMSTBGAiEArOklwdcihg72
         fMu+GvnKF+AdFiMmeT7CWV4KMZmA3kcCIQDyjBMqkI6tFVXMG/yhbBhVg7TNYsAGLjM5UWLfVx57WgFB
         BLmTMVBhjWo901GrcZzZMNBUectdX4ZsVyHhMNjZpaAJxlpQqnjiK9PAvrNqOIgMq8itz9S3KDaOs/Kh
         W6/lJNL/////AsAOFgIAAAAAGXapFInbjSGPxqYLm35NTXhzcAkVf8h8iKwwq98DAAAAABl2qRSBj0Gs
         NlhCyvZkoRO2iRw54544foisAAAAAAAA/////wFBBJ6i78WXHp6ywhTupFpF7A0V2jwQEjE9pWO1+7wZ
         qS+IM59Dur1Ut5OC+yUycjeFHQdqemkBDFT97zMCJalmtXxHMEQCIF12j4Vj1Shf49BkDWwVzf1kRgYr
         4EIPObgRTVPQz2KkAiAQ28gOniv2A5ozeBCk/rpWHTw2DqqkraEUDYLAPr83NQEAAuIBAAAACxEJB8lS
         QQRqBKuY2eR3StgG4wLd3rY76ha1y18iPud0eOhhu1g+sza2+8tgtbPU8VUaxF5f/Ek2Rm59mPbHwOxz
         ZTn3RpGmQQRoaAc3x22ruAHLIgT1fb5ORXnk9xDNZ9wbQidZLIHptc8Ctayei0yfSb5SUQVram0BHkw3
         9rbRft5rVfqiNRniQQS5XCSdhPQX4+OVoSdCVCi1QGccwViB64KMF7cipT/FmeIcpeVskPNAmI05M6zH
         a+uDL9ZMqweN3zznMpIwMdGoU66gf4gCAAAAAAAABE5PTkUAMgEAAAALEQkHGXapFGzuR3kIci90Cmp8
         6YwOnV3KsPoyiKyABFcBAAAAAAAABE5PTkUA
         ================================================================================
         """.strip())

        # For this manual construction to work, I had to save the signed funding
        # transaction
        signedFundMS = hex_to_binary( \
           '0100000001fd7b2814a83024c559b2f897a6ff9a3ae9f80501d19be0aebecfd7'
           'ecefc9de7f010000008a47304402205d768f8563d5285fe3d0640d6c15cdfd64'
           '46062be0420f39b8114d53d0cf62a4022010dbc80e9e2bf6039a337810a4feba'
           '561d3c360eaaa4ada1140d82c03ebf37350141049ea2efc5971e9eb2c214eea4'
           '5a45ec0d15da3c1012313da563b5fbbc19a92f88339f43babd54b79382fb2532'
           '7237851d076a7a69010c54fdef330225a966b57cffffffff02a07f8802000000'
           '00c95241046a04ab98d9e4774ad806e302dddeb63bea16b5cb5f223ee77478e8'
           '61bb583eb336b6fbcb60b5b3d4f1551ac45e5ffc4936466e7d98f6c7c0ec7365'
           '39f74691a6410468680737c76dabb801cb2204f57dbe4e4579e4f710cd67dc1b'
           '4227592c81e9b5cf02b5ac9e8b4c9f49be5251056b6a6d011e4c37f6b6d17ede'
           '6b55faa23519e24104b95c249d84f417e3e395a127425428b540671cc15881eb'
           '828c17b722a53fc599e21ca5e56c90f340988d3933acc76beb832fd64cab078d'
           'df3ce732923031d1a853ae80045701000000001976a9146cee477908722f740a'
           '6a7ce98c0e9d5dcab0fa3288ac00000000')

        #UnsignedTransaction().unserializeAscii(asc_nosig).evaluateSigningStatus().pprint()
        #UnsignedTransaction().unserializeAscii(asc_sig).evaluateSigningStatus().pprint()

        privKeys = [SecureBinaryData(a * 32) for a in ['\xaa', '\xbb', '\xcc']]
        pubKeys = [CryptoECDSA().ComputePublicKey(prv) for prv in privKeys]
        pubStrs = [pubk.toBinStr() for pubk in pubKeys]

        for i, prv in enumerate(privKeys):
            print 'PrivKey %d:', prv.toHexStr()

        msScript = pubkeylist_to_multisig_script(pubStrs, 2)
        msScriptReverse = pubkeylist_to_multisig_script(pubStrs[::-1], 2)
        self.assertEqual(msScript, msScriptReverse)

        for opStr in convertScriptToOpStrings(msScript):
            print '   ', opStr

        dtxo = DecoratedTxOut(msScript, 1.0 * ONE_BTC)

        ustxi = UnsignedTxInput(signedFundMS, 0)
        ustxi.pprint()

        refund1 = addrStr_to_scrAddr('mqSvihZRtKt1J3EBbwBJSHeAYVjdxUnpvf')
        refund2 = addrStr_to_scrAddr('mjAauu6jzmYaE7jrfFgKqLxtvpStmPxcb7')
        dtxo1 = DecoratedTxOut(scrAddr_to_script(refund1),
                               long(0.223 * ONE_BTC))
        dtxo2 = DecoratedTxOut(scrAddr_to_script(refund2),
                               long(0.200 * ONE_BTC))

        ustx = UnsignedTransaction().createFromUnsignedTxIO([ustxi],
                                                            [dtxo1, dtxo2])
        ustx.pprint()
        ustx.evaluateSigningStatus().pprint()

        # Need a candidate tx to test signing
        txObj = ustx.pytxObj

        # Test signing on the individual USTXI
        NOSIG = TXIN_SIGSTAT.NO_SIGNATURE
        SIG = TXIN_SIGSTAT.ALREADY_SIGNED
        for i in [0, 1]:
            for j in [0, 1]:
                for k in [0, 1]:
                    ustxiCopy = UnsignedTxInput().unserialize(
                        ustxi.serialize())
                    if i > 0:
                        ustxiCopy.createAndInsertSignature(txObj, privKeys[0])
                    if j > 0:
                        ustxiCopy.createAndInsertSignature(txObj, privKeys[1])
                    if k > 0:
                        ustxiCopy.createAndInsertSignature(txObj, privKeys[2])
                    sstat = ustxiCopy.evaluateSigningStatus()
                    sstat.pprint()
                    self.assertEqual(sstat.allSigned, (i + j + k) > 1)
                    self.assertEqual(sstat.statusM[0],
                                     NOSIG if i + j + k == 0 else SIG)
                    self.assertEqual(sstat.statusM[1],
                                     NOSIG if i + j + k < 2 else SIG)

        # Now try all this on the full USTX (not just the input
        for i in [0, 1]:
            for j in [0, 1]:
                for k in [0, 1]:
                    ustxCopy = UnsignedTransaction().unserialize(
                        ustx.serialize())
                    if i > 0:
                        ustxCopy.createAndInsertSignatureForInput(
                            0, privKeys[0])
                    if j > 0:
                        ustxCopy.createAndInsertSignatureForInput(
                            0, privKeys[1])
                    if k > 0:
                        ustxCopy.createAndInsertSignatureForInput(
                            0, privKeys[2])
                    sstat = ustxCopy.evaluateSigningStatus()
                    #sstat.pprint()
                    self.assertEqual(sstat.canBroadcast, (i + j + k) > 1)
                    #self.assertEqual(sstat.statusM[0], NOSIG if i+j+k==0 else SIG)
                    #self.assertEqual(sstat.statusM[1], NOSIG if i+j+k<2  else SIG)

        # Now actually sign it and dump out a raw signed tx!
        ustx.createAndInsertSignatureForInput(0, privKeys[0])
        ustx.createAndInsertSignatureForInput(0, privKeys[2])

        print ustx.serializeAscii()
        print binary_to_hex(ustx.getPyTxSignedIfPossible().serialize())