コード例 #1
0
def bytearray_to_int(data, decode_small_int=True):
    """Decode a byte array into an integer.

    If decode_small_int is True, a small integer will
    be returned if data is a small int opcode.
    """
    num = _bignum.vch2bn(data[::-1])
    # Decode num if it's a small integer.
    if decode_small_int:
        try:
            return CScriptOp(num).decode_op_n()
        except Exception:
            pass
    return num
コード例 #2
0
ファイル: keys.py プロジェクト: mickeystone/bitcoin_spv
    def add_private_key(self):
        seckey_str = input('\nInsert private key (WIF-compressed format):')
        if len(seckey_str) != 52 or (seckey_str[0] != 'K'
                                     and seckey_str[0] != 'L'):
            print("The key format is not valid!")
            return
        seckey = CBitcoinSecret(seckey_str)
        '''
        Calculate and store each data element of the scriptPubKey/scriptSig related to this private key.
        These data elements will be used in the Bloom Filters.
         
        From BIP 37:
        For each output, test each data element of the output script. This means each hash and key in the output script is tested independently. 
        For each input, test each data element of the input script (note: input scripts only ever contain data elements).
        
        The default scriptPubKey/scriptSig used by Bitcoin Core 0.16.0 are:
        - scriptPubKey: OP_HASH160 [20-byte-hash of {OP_0 hash160[pubkey]}] OP_EQUAL
        - scriptSig: 0x16 OP_0 hash160[pubkey]
        
        Note: 0x16 => The next opcode bytes is data to be pushed onto the stack

        The data element of the scriptSig should be only hash160[pubkey].
        Using only that data element the bloom filter doesn't work properly.
        Instead the filter works well using OP_0 hash160[pubkey].
        '''
        scriptPubKey_data_element = bitcoin.core.Hash160(
            CScript([OP_0, bitcoin.core.Hash160(seckey.pub)]))
        scriptSig_data_element = CScript(
            [OP_0, bitcoin.core.Hash160(seckey.pub)])
        # Calculate and store also scriptPubKey/scriptSig
        scriptSig = CScript(
            [CScriptOp(0x16), OP_0,
             bitcoin.core.Hash160(seckey.pub)])
        scriptPubKey = CScript([OP_0, bitcoin.core.Hash160(seckey.pub)
                                ]).to_p2sh_scriptPubKey()
        self.keys[seckey_str] = {
            "data_elements":
            [b2x(scriptPubKey_data_element),
             b2x(scriptSig_data_element)],
            "scriptSig":
            b2x(scriptSig),
            "scriptPubKey":
            b2x(scriptPubKey)
        }
        self.save_to_file()
コード例 #3
0
def scriptstuff2dict(opcode, data, idx):
    return {
        'opcode': str(CScriptOp(opcode)),
        'data': b2lx(data) if data else None,
        'sop_idx': idx
    }
コード例 #4
0
 def is_witness_scriptpubkey(self):
     """Returns true if this is a scriptpubkey signaling segregated witness
     data. """
     return 3 <= len(self) <= 42 and CScriptOp(
         struct.unpack('<b', self[0:1])[0]).is_small_int()