def sign_announce(self, alias, password):
        """Sign a Masternode Announce message for alias."""
        self.check_can_sign_masternode(alias)
        mn = self.get_masternode(alias)
        # Ensure that the masternode's vin is valid.
        if mn.vin.get('scriptSig') is None:
            mn.vin['scriptSig'] = ''
        if mn.vin.get('sequence') is None:
            mn.vin['sequence'] = 0xffffffff
        # Ensure that the masternode's last_ping is current.
        height = self.wallet.get_local_height() - 12
        header = self.wallet.network.get_header(height)
        mn.last_ping.block_hash = Blockchain.hash_header(header)
        mn.last_ping.vin = mn.vin

        # Sign ping with delegate key.
        self.wallet.sign_masternode_ping(mn.last_ping, mn.delegate_key)

        # After creating the Masternode Ping, sign the Masternode Announce.
        address = bitcoin.public_key_to_bc_address(
            mn.collateral_key.decode('hex'))
        mn.sig = self.wallet.sign_message(
            address, mn.serialize_for_sig(update_time=True), password)

        return mn
Example #2
0
def get_address_from_output_script(bytes):
    decoded = [x for x in script_GetOp(bytes)]

    # The Genesis Block, self-payments,
    # and pay-by-IP-address payments look like:
    # 65 BYTES:... CHECKSIG
    match = [opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG]
    if match_decoded(decoded, match):
        return public_key_to_bc_address(decoded[0][1])

    # Pay-by-Bitcoin-address TxOuts look like:
    # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
    match = [opcodes.OP_DUP,
             opcodes.OP_HASH160,
             opcodes.OP_PUSHDATA4,
             opcodes.OP_EQUALVERIFY,
             opcodes.OP_CHECKSIG]
    if match_decoded(decoded, match):
        return hash_160_to_bc_address(decoded[2][1])

    # p2sh
    match = [opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL]
    if match_decoded(decoded, match):
        return hash_160_to_bc_address(decoded[1][1], chain.script_version)

    return "(None)"
Example #3
0
def get_address_from_input_script(bytes):
  decoded = [ x for x in script_GetOp(bytes) ]

  # non-generated TxIn transactions push a signature
  # (seventy-something bytes) and then their public key
  # (65 bytes) onto the stack:
  match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
  if match_decoded(decoded, match):
    return public_key_to_bc_address(decoded[1][1])

  # p2sh transaction, 2 of n
  match = [ opcodes.OP_0, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ] 
  if match_decoded(decoded, match):
    bytes = decoded[3][1]
    dec2 = [ x for x in script_GetOp(bytes) ]

    # 2 of 2
    match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
    if match_decoded(dec2, match2):
      pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ]
      s = multisig_script(pubkeys)
      return hash_160_to_bc_address(hash_160(s.decode('hex')), 5)
 
    # 2 of 3
    match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
    if match_decoded(dec2, match2):
      pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ]
      s = multisig_script(pubkeys)
      return hash_160_to_bc_address(hash_160(s.decode('hex')), 5)

    return "p2sh, unknown"


  return "(None)"
Example #4
0
def get_address_from_output_script(bytes):
    decoded = [x for x in script_GetOp(bytes)]

    # The Genesis Block, self-payments,
    # and pay-by-IP-address payments look like:
    # 65 BYTES:... CHECKSIG
    match = [opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG]
    if match_decoded(decoded, match):
        return public_key_to_bc_address(decoded[0][1])

    # Pay-by-Bitcoin-address TxOuts look like:
    # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
    match = [opcodes.OP_DUP,
             opcodes.OP_HASH160,
             opcodes.OP_PUSHDATA4,
             opcodes.OP_EQUALVERIFY,
             opcodes.OP_CHECKSIG]
    if match_decoded(decoded, match):
        return hash_160_to_bc_address(decoded[2][1])

    # p2sh
    match = [opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL]
    if match_decoded(decoded, match):
        return hash_160_to_bc_address(decoded[1][1], 5)

    return "(None)"
Example #5
0
def get_address_from_input_script(bytes):
    try:
        decoded = [x for x in script_GetOp(bytes)]
    except:
        # coinbase transactions raise an exception
        print_error("cannot find address in input script", bytes.encode('hex'))
        return [], [], "(None)"

    # non-generated TxIn transactions push a signature
    # (seventy-something bytes) and then their public key
    # (65 bytes) onto the stack:
    match = [opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4]
    if match_decoded(decoded, match):
        return ([decoded[1][1].encode('hex')],
                [decoded[0][1].encode('hex')],
                public_key_to_bc_address(decoded[1][1]))

    # p2sh transaction, 2 of n
    match = [opcodes.OP_0]
    while len(match) < len(decoded):
        match.append(opcodes.OP_PUSHDATA4)

    if match_decoded(decoded, match):
        redeemScript = decoded[-1][1]
        signatures = map(lambda x: x[1].encode('hex'), decoded[1:-1])

        dec2 = [x for x in script_GetOp(redeemScript)]

        # 2 of 2
        match2 = [opcodes.OP_2,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_2,
                  opcodes.OP_CHECKMULTISIG]
        if match_decoded(dec2, match2):
            pubkeys = [dec2[1][1].encode('hex'),
                       dec2[2][1].encode('hex')]
            return (pubkeys,
                    signatures,
                    hash_160_to_bc_address(hash_160(redeemScript), 5))

        # 2 of 3
        match2 = [opcodes.OP_2,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_3,
                  opcodes.OP_CHECKMULTISIG]
        if match_decoded(dec2, match2):
            pubkeys = [dec2[1][1].encode('hex'),
                       dec2[2][1].encode('hex'),
                       dec2[3][1].encode('hex')]
            return (pubkeys,
                    signatures,
                    hash_160_to_bc_address(hash_160(redeemScript), 5))

    print_error("cannot find address in input script", bytes.encode('hex'))
    return [], [], "(None)"
Example #6
0
def get_address_from_input_script(bytes):
    try:
        decoded = [x for x in script_GetOp(bytes)]
    except:
        # coinbase transactions raise an exception
        print_error("cannot find address in input script", bytes.encode('hex'))
        return [], [], "(None)"

    # non-generated TxIn transactions push a signature
    # (seventy-something bytes) and then their public key
    # (65 bytes) onto the stack:
    match = [opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4]
    if match_decoded(decoded, match):
        return ([decoded[1][1].encode('hex')],
                [decoded[0][1].encode('hex')],
                public_key_to_bc_address(decoded[1][1]))

    # p2sh transaction, 2 of n
    match = [opcodes.OP_0]
    while len(match) < len(decoded):
        match.append(opcodes.OP_PUSHDATA4)

    if match_decoded(decoded, match):
        redeemScript = decoded[-1][1]
        signatures = map(lambda x: x[1].encode('hex'), decoded[1:-1])

        dec2 = [x for x in script_GetOp(redeemScript)]

        # 2 of 2
        match2 = [opcodes.OP_2,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_2,
                  opcodes.OP_CHECKMULTISIG]
        if match_decoded(dec2, match2):
            pubkeys = [dec2[1][1].encode('hex'),
                       dec2[2][1].encode('hex')]
            return (pubkeys,
                    signatures,
                    hash_160_to_bc_address(hash_160(redeemScript), chain.script_version))

        # 2 of 3
        match2 = [opcodes.OP_2,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_PUSHDATA4,
                  opcodes.OP_3,
                  opcodes.OP_CHECKMULTISIG]
        if match_decoded(dec2, match2):
            pubkeys = [dec2[1][1].encode('hex'),
                       dec2[2][1].encode('hex'),
                       dec2[3][1].encode('hex')]
            return (pubkeys,
                    signatures,
                    hash_160_to_bc_address(hash_160(redeemScript), chain.script_version))

    print_error("cannot find address in input script", bytes.encode('hex'))
    return [], [], "(None)"
Example #7
0
 def get_pubkey_derivation(self, x_pubkey):
     if x_pubkey[0:2] in ['02', '03', '04']:
         if x_pubkey in self.keypairs.keys():
             return x_pubkey
     elif x_pubkey[0:2] == 'fd':
         # fixme: this assumes p2pkh
         _, addr = xpubkey_to_address(x_pubkey)
         for pubkey in self.keypairs.keys():
             if public_key_to_bc_address(pubkey.decode('hex')) == addr:
                 return pubkey
Example #8
0
 def get_pubkey_derivation(self, x_pubkey):
     if x_pubkey[0:2] in ['02', '03', '04']:
         if x_pubkey in self.keypairs.keys():
             return x_pubkey
     elif x_pubkey[0:2] == 'fd':
         # fixme: this assumes p2pkh
         _, addr = xpubkey_to_address(x_pubkey)
         for pubkey in self.keypairs.keys():
             if public_key_to_bc_address(pubkey.decode('hex')) == addr:
                 return pubkey
Example #9
0
 def get_pubkey_derivation(self, x_pubkey):
     if x_pubkey[0:2] in ['02', '03', '04']:
         if x_pubkey in self.receiving_pubkeys:
             i = self.receiving_pubkeys.index(x_pubkey)
             return (False, i)
     elif x_pubkey[0:2] == 'fd':
         # fixme: this assumes p2pkh
         _, addr = xpubkey_to_address(x_pubkey)
         for i, pubkey in enumerate(self.receiving_pubkeys):
             if public_key_to_bc_address(pubkey.decode('hex')) == addr:
                 return (False, i)
Example #10
0
    def sign(self, wif, current_time=None):
        """Sign the vote."""
        update_time = True
        if current_time is not None:
            self.timestamp = current_time
            update_time = False

        delegate_pubkey = bitcoin.public_key_from_private_key(wif).decode('hex')
        eckey = bitcoin.regenerate_key(wif)
        serialized = unicode(self.serialize_for_sig(update_time=update_time)).encode('utf-8')
        return eckey.sign_message(serialized, bitcoin.is_compressed(wif),
                bitcoin.public_key_to_bc_address(delegate_pubkey))
Example #11
0
def extract_public_key(bytes):
  decoded = [ x for x in script_GetOp(bytes) ]

  # non-generated TxIn transactions push a signature
  # (seventy-something bytes) and then their public key
  # (65 bytes) onto the stack:
  match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
  if match_decoded(decoded, match):
    return public_key_to_bc_address(decoded[1][1])

  # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
  # 65 BYTES:... CHECKSIG
  match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
  if match_decoded(decoded, match):
    return public_key_to_bc_address(decoded[0][1])

  # Pay-by-Bitcoin-address TxOuts look like:
  # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
  match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
  if match_decoded(decoded, match):
    return hash_160_to_bc_address(decoded[2][1])

  return "(None)"
Example #12
0
    def sign(self, wif, current_time=None):
        """Sign the masternode announce message.

        If current_time is specified, sig_time will not be updated.
        """
        update_time = True
        if current_time is not None:
            self.sig_time = current_time
            update_time = False
        eckey = bitcoin.regenerate_key(wif)

        serialized = self.serialize_for_sig(update_time=update_time)
        self.sig = eckey.sign_message(
            serialized, bitcoin.is_compressed(wif),
            bitcoin.public_key_to_bc_address(
                self.collateral_key.decode('hex')))
        return self.sig
Example #13
0
def xpubkey_to_address(x_pubkey):
    if x_pubkey[0:2] in ['02','03','04']:
        pubkey = x_pubkey
    elif x_pubkey[0:2] == 'ff':
        xpub, s = BIP32_KeyStore.parse_xpubkey(x_pubkey)
        pubkey = BIP32_KeyStore.get_pubkey_from_xpub(xpub, s)
    elif x_pubkey[0:2] == 'fe':
        mpk, s = Old_KeyStore.parse_xpubkey(x_pubkey)
        pubkey = Old_KeyStore.get_pubkey_from_mpk(mpk, s[0], s[1])
    elif x_pubkey[0:2] == 'fd':
        addrtype = ord(x_pubkey[2:4].decode('hex'))
        hash160 = x_pubkey[4:].decode('hex')
        pubkey = None
        address = hash_160_to_bc_address(hash160, addrtype)
    else:
        raise BaseException("Cannnot parse pubkey")
    if pubkey:
        address = public_key_to_bc_address(pubkey.decode('hex'))
    return pubkey, address
Example #14
0
def xpubkey_to_address(x_pubkey):
    if x_pubkey[0:2] in ['02', '03', '04']:
        pubkey = x_pubkey
    elif x_pubkey[0:2] == 'ff':
        xpub, s = BIP32_KeyStore.parse_xpubkey(x_pubkey)
        pubkey = BIP32_KeyStore.get_pubkey_from_xpub(xpub, s)
    elif x_pubkey[0:2] == 'fe':
        mpk, s = Old_KeyStore.parse_xpubkey(x_pubkey)
        pubkey = Old_KeyStore.get_pubkey_from_mpk(mpk, s[0], s[1])
    elif x_pubkey[0:2] == 'fd':
        addrtype = ord(x_pubkey[2:4].decode('hex'))
        hash160 = x_pubkey[4:].decode('hex')
        pubkey = None
        address = hash_160_to_bc_address(hash160, addrtype)
    else:
        raise BaseException("Cannnot parse pubkey")
    if pubkey:
        address = public_key_to_bc_address(pubkey.decode('hex'))
    return pubkey, address
Example #15
0
    def sign(self, wif, delegate_pubkey=None, current_time=None):
        """Sign this ping.

        If delegate_pubkey is specified, it will be used to verify the signature.
        If current_time is specified, sig_time will not be updated.
        """
        update_time = True
        if current_time is not None:
            self.sig_time = current_time
            update_time = False

        eckey = bitcoin.regenerate_key(wif)
        serialized = unicode(
            self.serialize_for_sig(update_time=update_time)).encode('utf-8')

        if not delegate_pubkey:
            delegate_pubkey = bitcoin.public_key_from_private_key(wif).decode(
                'hex')
        self.sig = eckey.sign_message(
            serialized, bitcoin.is_compressed(wif),
            bitcoin.public_key_to_bc_address(delegate_pubkey))
        return self.sig
Example #16
0
def get_address_from_input_script(bytes):
    decoded = [ x for x in script_GetOp(bytes) ]

    # non-generated TxIn transactions push a signature
    # (seventy-something bytes) and then their public key
    # (65 bytes) onto the stack:
    match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
    if match_decoded(decoded, match):
        return None, None, public_key_to_bc_address(decoded[1][1])

    # p2sh transaction, 2 of n
    match = [ opcodes.OP_0 ]
    while len(match) < len(decoded):
        match.append(opcodes.OP_PUSHDATA4)

    if match_decoded(decoded, match):

        redeemScript = decoded[-1][1]
        num = len(match) - 2
        signatures = map(lambda x:x[1].encode('hex'), decoded[1:-1])
        
        dec2 = [ x for x in script_GetOp(redeemScript) ]

        # 2 of 2
        match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
        if match_decoded(dec2, match2):
            pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ]
            return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)
 
        # 2 of 3
        match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
        if match_decoded(dec2, match2):
            pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ]
            return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)

    raise BaseException("no match for scriptsig")
Example #17
0
 def get_address(self, for_change, n):
     pubkey = self.get_pubkey(for_change, n)
     address = public_key_to_bc_address(pubkey.decode('hex'))
     return address
Example #18
0
File: e.py Project: bcpki/bitcoin
def point2addr(p):
  return bitcoin.public_key_to_bc_address(point2pubkey(p))
Example #19
0
File: e.py Project: bcpki/bitcoin
def pubkey2addr(pubkey): 
  return bitcoin.public_key_to_bc_address(toraw(pubkey))
Example #20
0
 def get_address(self, for_change, n):
     pubkey = self.get_pubkey(for_change, n)
     address = public_key_to_bc_address(pubkey.decode('hex'))
     return address
Example #21
0
 def verify(self, addr=None):
     """Verify that our sig is signed with addr's key."""
     if not addr:
         addr = bitcoin.public_key_to_bc_address(
             self.collateral_key.decode('hex'))
     return bitcoin.verify_message(addr, self.sig, self.serialize_for_sig())