Esempio n. 1
0
def sign_transaction_input(tx,
                           input_index,
                           txout_script,
                           intput_script_type,
                           secret,
                           compressed_pubkey=True):
    # Set all txin to empty
    txin_scripts_save = [txin.script for txin in tx.in_list]
    for txin in tx.in_list:
        txin.script = Script([])
    # Set the current input script to the outpoint's script value
    tx.in_list[input_index].script = txout_script
    # Serialize and append hash type
    enctx = TxSerializer().serialize(tx) + b"\x01\x00\x00\x00"
    # Get hash and Sign
    txhash = doublesha256(enctx)
    key = KEY()
    key.set_secret(secret, compressed_pubkey)
    signature = key.sign(txhash) + "\x01"  # append hash_type SIGHASH_ALL
    # Restore Txin scripts
    for txin, script_save in zip(tx.in_list, txin_scripts_save):
        txin.script = script_save
    # Set the signed script
    if intput_script_type == TX_PUBKEYHASH:
        tx.in_list[input_index].script = make_script_pubkeyhash_sig(
            key.get_pubkey(), signature)
    if intput_script_type == TX_PUBKEY:
        tx.in_list[input_index].script = make_script_pubkey_sig(signature)
    def test_shamir_share_private_key(self):
        ssl_add_system_seeds()
        k = KEY()
        k.generate()
        pkey_bignum = k.get_privkey_bignum()
        pubkey = k.get_pubkey()
        numshares = 600
        threshold = 100
        sharenum_bytes = 2
        print "private_key_bignum:", pkey_bignum
        print "public_key:", hexstr(pubkey)
        print "address:", BitcoinAddress.from_publickey(pubkey, MAIN)
        
        field = ZpField()
        V = field.value_type
        ZpPkey = V(pkey_bignum)

        sharer = SecretSharer(field, ZpRandom(field))
        shares = sharer.share(ZpPkey, threshold, [V(i+1) for i in range(numshares)])
        # print shares
        print "Shamir Shares: (%d/%d):" % (threshold, numshares)
        shares_hex = [hexstr(base256encode(int(pt), sharenum_bytes) + base256encode(int(value), 32)) for pt, value in shares]
        
        for share in shares_hex:
            print share
        # Try to reconstruct the private key using the hex encoded shares.
        recombiner = SecretRecombiner(field)
        for i in range(10):
            random4_hex = random.sample(shares_hex, threshold)
            random4_decoded = [decodehexstr(h) for h in random4_hex]
            random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded]
            recombined_pkey_bignum = recombiner.recombine(random4, V(0))
            assert recombined_pkey_bignum == ZpPkey
            k2 = KEY()
            k2.set_privkey_bignum(int(recombined_pkey_bignum))
            assert k2.get_pubkey() == pubkey
            print i
        # With threshold-1 shares this fails
        for i in range(10):
            random4_hex = random.sample(shares_hex, threshold-1)
            random4_decoded = [decodehexstr(h) for h in random4_hex]
            random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded]
            recombined_pkey_bignum = recombiner.recombine(random4, V(0))
            assert recombined_pkey_bignum != ZpPkey
Esempio n. 3
0
 def get_private_key_secret(self, public_key):
     if public_key in self.wallet_database.keys: # private key is not crypted
         k = KEY()
         k.set_privkey(self.wallet_database.keys[public_key].private_key)
         return k.get_secret()
     crypted_secret = self.wallet_database.get_crypted_keys()[public_key]
     for key in self.plain_masterkeys:
         self.crypter.set_key(key, doublesha256(public_key))
         secret = self.crypter.decrypt(crypted_secret)
         k = KEY()
         is_compressed = len(public_key) == 33
         k.set_secret(secret, is_compressed)
         if k.get_pubkey() == public_key:
             return secret
     raise KeyDecryptException("Can't decrypt private key, wallet not unlocked or incorrect masterkey")
Esempio n. 4
0
 def get_private_key_secret(self, public_key):
     if public_key in self.wallet_database.keys:  # private key is not crypted
         k = KEY()
         k.set_privkey(self.wallet_database.keys[public_key].private_key)
         return k.get_secret()
     crypted_secret = self.wallet_database.get_crypted_keys()[public_key]
     for key in self.plain_masterkeys:
         self.crypter.set_key(key, doublesha256(public_key))
         secret = self.crypter.decrypt(crypted_secret)
         k = KEY()
         is_compressed = len(public_key) == 33
         k.set_secret(secret, is_compressed)
         if k.get_pubkey() == public_key:
             return secret
     raise KeyDecryptException(
         "Can't decrypt private key, wallet not unlocked or incorrect masterkey"
     )
Esempio n. 5
0
    def create(self, passphrase):
        self.wallet_database.begin_updates()
        crypter = Crypter()
        #first create masterkey
        master_key = new_masterkey(passphrase)
        plain_masterkey = decrypt_masterkey(master_key, passphrase)
        self.wallet_database.add_master_key(master_key)
        #create transaction pool
        for i in range(100):
            k = KEY()
            k.generate(True)
            public_key = k.get_pubkey()
            crypter.set_key(plain_masterkey, doublesha256(public_key))
            crypted_secret = crypter.encrypt(k.get_secret())
            self.wallet_database.add_crypted_key(public_key, crypted_secret)
            pool_key = WalletPoolKey(i, 60000, time.time(), public_key)
            self.wallet_database.add_poolkey(pool_key)

        self.wallet_database.commit_updates()
        self.load()
Esempio n. 6
0
 def create(self, passphrase):
     self.wallet_database.begin_updates()
     crypter = Crypter()
     #first create masterkey
     master_key =  new_masterkey(passphrase)
     plain_masterkey = decrypt_masterkey(master_key, passphrase)
     self.wallet_database.add_master_key(master_key)
     #create transaction pool
     for i in range(100):
         k = KEY()
         k.generate(True)
         public_key = k.get_pubkey()
         crypter.set_key(plain_masterkey, doublesha256(public_key))
         crypted_secret = crypter.encrypt(k.get_secret())
         self.wallet_database.add_crypted_key(public_key, crypted_secret)
         pool_key = WalletPoolKey(i, 60000, time.time(), public_key)
         self.wallet_database.add_poolkey(pool_key)
         
         
     self.wallet_database.commit_updates()
     self.load()
Esempio n. 7
0
def sign_transaction_input(tx, input_index, txout_script, intput_script_type, secret, compressed_pubkey=True):
    # Set all txin to empty
    txin_scripts_save = [txin.script for txin in tx.in_list]
    for txin in tx.in_list:
        txin.script = Script([])
    # Set the current input script to the outpoint's script value
    tx.in_list[input_index].script = txout_script
    # Serialize and append hash type
    enctx = TxSerializer().serialize(tx) + b"\x01\x00\x00\x00"
    # Get hash and Sign
    txhash = doublesha256(enctx)
    key = KEY()
    key.set_secret(secret, compressed_pubkey)
    signature = key.sign(txhash) + "\x01" # append hash_type SIGHASH_ALL
    # Restore Txin scripts
    for txin, script_save in zip(tx.in_list, txin_scripts_save):
        txin.script = script_save
    # Set the signed script
    if intput_script_type == TX_PUBKEYHASH:
        tx.in_list[input_index].script = make_script_pubkeyhash_sig(key.get_pubkey(), signature)
    if intput_script_type == TX_PUBKEY:
        tx.in_list[input_index].script = make_script_pubkey_sig(signature)
Esempio n. 8
0
 def test_ssl_get_pubkey(self):
     key = KEY()
     key.set_secret(decodehexstr("30d1d8d1d243ab41a80a3cc1481a626a137f771a636b2daca06c1f86cdfecffb"), True)
     self.assertEquals(hexstr(key.get_pubkey()), "030a43196c8bf389c0ce5987a3f4dac57f4ca0d9733c232659717d9404074b4504")