Encode a segwit address.
559006
def segwit_encode(hrp, witver, witprog):
    """"""
    ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5))
    if segwit_decode(hrp, ret) == (None, None):
        return None
    return ret
141229
def _encode_text(name, value, dummy0, dummy1):
    """"""
    value = _utf_8_encode(value)[0]
    return b"\x02" + name + _PACK_INT(len(value) + 1) + value + b"\x00"
887843
def make_pkh_address(pubkey, witness=False, cashaddr=True):
    '''
    
    '''
    pubkey_hash = utils.hash160(pubkey)
    return _make_pkh_address(pubkey_hash=pubkey_hash,
                             witness=witness,
                             cashaddr=cashaddr)
1155476
def encode(hrp, witver, witprog):
    """"""
    ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5))
    if decode(ret) == (None, None):
        return None
    return ret
1213068
def make_multisig_segwit_info( m, pks ):
    
    pubs = []
    privkeys = []
    for pk in pks:
        priv = BitcoinPrivateKey(pk, compressed=True)
        priv_hex = priv.to_hex()
        pub_hex = priv.public_key().to_hex()

        privkeys.append(priv_hex)
        pubs.append(keylib.key_formatting.compress(pub_hex))

    script = None

    if len(pubs) == 1:
        if m != 1:
            raise ValueError("invalid m: len(pubkeys) == 1")

        # 1 pubkey means p2wpkh
        key_hash = hashing.bin_hash160(pubs[0].decode('hex')).encode('hex')
        script = '160014' + key_hash
        addr = btc_make_p2sh_address(script[2:])

    else:
        # 2+ pubkeys means p2wsh 
        script = make_multisig_script(pubs, m)
        addr = make_multisig_segwit_address_from_witness_script(script)

    return {
        'address': addr,
        'redeem_script': script,
        'private_keys': privkeys,
        'segwit': True,
        'm': m
    }
1061789
def encode(self, uuid, pad_length=None):
        
        if pad_length is None:
            pad_length = self._length
        return int_to_string(uuid.int, self._alphabet, padding=pad_length)
530807
def address_to_pubkey(addr)
      return unless addr.size == 52

      prefix = ADDRESS_PREFIX[addr[0,2]]
      return unless prefix

      v = Bitcoin.decode_base58(addr)
      return if v[0,4] != prefix

      bytes = [v[0, 68]].pack('H*')
      return if v[68, 8] != sha256d(bytes)[0, 8]

      v[4, 64]
    end
1178697
def btc_tx_sighash_segwit(tx, i, prevout_amount, prevout_script, hashcode=SIGHASH_ALL):
    
    txobj = btc_tx_deserialize(tx)

    hash_prevouts = encoding.encode(0, 256, 32)
    hash_sequence = encoding.encode(0, 256, 32)
    hash_outputs = encoding.encode(0, 256, 32)

    if (hashcode & SIGHASH_ANYONECANPAY) == 0:
        prevouts = ''
        for inp in txobj['ins']:
            prevouts += hashing.reverse_hash(inp['outpoint']['hash'])
            prevouts += encoding.encode(inp['outpoint']['index'], 256, 4)[::-1].encode('hex')

        hash_prevouts = hashing.bin_double_sha256(prevouts.decode('hex'))

        # print 'prevouts: {}'.format(prevouts)

    if (hashcode & SIGHASH_ANYONECANPAY) == 0 and (hashcode & 0x1f) != SIGHASH_SINGLE and (hashcode & 0x1f) != SIGHASH_NONE:
        sequences = ''
        for inp in txobj['ins']:
            sequences += encoding.encode(inp['sequence'], 256, 4)[::-1].encode('hex')

        hash_sequence = hashing.bin_double_sha256(sequences.decode('hex'))

        # print 'sequences: {}'.format(sequences)

    if (hashcode & 0x1f) != SIGHASH_SINGLE and (hashcode & 0x1f) != SIGHASH_NONE:
        outputs = ''
        for out in txobj['outs']:
            outputs += encoding.encode(out['value'], 256, 8)[::-1].encode('hex')
            outputs += make_var_string(out['script'])

        hash_outputs = hashing.bin_double_sha256(outputs.decode('hex'))

        # print 'outputs: {}'.format(outputs)

    elif (hashcode & 0x1f) == SIGHASH_SINGLE and i < len(txobj['outs']):
        outputs = ''
        outputs += encoding.encode(txobj['outs'][i]['value'], 256, 8)[::-1].encode('hex')
        outputs += make_var_string(txobj['outs'][i]['script'])

        hash_outputs = hashing.bin_double_sha256(outputs.decode('hex'))

        # print 'outputs: {}'.format(outputs)

    # print 'hash_prevouts: {}'.format(hash_prevouts.encode('hex'))
    # print 'hash_sequence: {}'.format(hash_sequence.encode('hex'))
    # print 'hash_outputs: {}'.format(hash_outputs.encode('hex'))
    # print 'prevout_script: {}'.format(prevout_script)
    # print 'prevout_amount: {}'.format(prevout_amount)

    sighash_preimage = ''
    sighash_preimage += encoding.encode(txobj['version'], 256, 4)[::-1].encode('hex')
    sighash_preimage += hash_prevouts.encode('hex')
    sighash_preimage += hash_sequence.encode('hex')

    # this input's prevout, script, amount, and sequence
    sighash_preimage += hashing.reverse_hash(txobj['ins'][i]['outpoint']['hash'])
    sighash_preimage += encoding.encode(txobj['ins'][i]['outpoint']['index'], 256, 4)[::-1].encode('hex')
    sighash_preimage += make_var_string(prevout_script)
    sighash_preimage += encoding.encode(prevout_amount, 256, 8)[::-1].encode('hex')
    sighash_preimage += encoding.encode(txobj['ins'][i]['sequence'], 256, 4)[::-1].encode('hex')

    sighash_preimage += hash_outputs.encode('hex')
    sighash_preimage += encoding.encode(txobj['locktime'], 256, 4)[::-1].encode('hex')
    sighash_preimage += encoding.encode(hashcode, 256, 4)[::-1].encode('hex')

    sighash = hashing.bin_double_sha256(sighash_preimage.decode('hex')).encode('hex')

    # print 'sighash_preimage: {}'.format(sighash_preimage)
    # print 'sighash: {}'.format(sighash)

    return sighash
1199197
def encode(self):
        """"""
        if len(self.data) == 0:
            log.debug("Encoding an empty DAT packet")
        data = self.data
        if not isinstance(self.data, bytes):
            data = self.data.encode('ascii')
        fmt = b"!HH%ds" % len(data)
        self.buffer = struct.pack(fmt,
                                  self.opcode,
                                  self.blocknumber,
                                  data)
        return self
527908
def script_hex_to_address( script_hex, blockchain='bitcoin', **blockchain_opts):
    
    if blockchain == 'bitcoin':
        return btc_script_hex_to_address(script_hex, **blockchain_opts)
    else:
        raise ValueError("Unknown blockchain '{}'".format(blockchain))
1291443
function pubKeyToAddr(pubKey, pubKeyHash) {
  pubKeyHash = pubKeyHash || zconfig.mainnet.pubKeyHash;

  const hash160 = zcrypto.hash160(Buffer.from(pubKey, 'hex'));
  return bs58check.encode(Buffer.from(pubKeyHash + hash160, 'hex')).toString('hex');
}
606084
def segwit_address(self):
        """"""
        # Only make segwit-address if all public keys are compressed
        if self._segwit_address is None and self.is_compressed:
            self._segwit_address = multisig_to_segwit_address(self.public_keys,
                self.m, version=self.version)
        return self._segwit_address
525311
def to_i
      self.address.split(':').inject(0) do |bits,char|
        bits = ((bits << 8) | char.hex)
      end
    end
314416
def sig_encode(r, s):
    """"""
    r = util.assuan_serialize(util.num2bytes(r, 32))
    s = util.assuan_serialize(util.num2bytes(s, 32))
    return b'(7:sig-val(5:ecdsa(1:r32:' + r + b')(1:s32:' + s + b')))'
1025811
def format_rpc(self, address, rpc_id, payload):
        """"""

        addr_word = (rpc_id | (address << 16) | ((1 << 1) << 24))

        send_length = len(payload)
        if len(payload) < 20:
            payload = payload + b'\0'*(20 - len(payload))

        payload_words = struct.unpack("<5L", payload)

        return self.base_address + self.RPC_TLS_OFFSET + 8, ([addr_word, send_length, 0] + [x for x in payload_words])
689578
def b58encode(v):
    

    long_value = 0
    for c in v:
        long_value = long_value * 256 + c

    result = ""
    while long_value >= __b58base:
        div, mod = divmod(long_value, __b58base)
        result = __b58chars[mod] + result
        long_value = div
    result = __b58chars[long_value] + result

    # Bitcoin does a little leading-zero-compression:
    # leading 0-bytes in the input become leading-1s
    nPad = 0
    for c in v:
        if c == 0:
            nPad += 1
        else:
            break

    return (__b58chars[0] * nPad) + result
1296907
def btc_tx_get_hash( tx_serialized, hashcode=None ):
    
    if btc_tx_is_segwit(tx_serialized):
        raise ValueError('Segwit transaction: {}'.format(tx_serialized))

    tx_bin = binascii.unhexlify(tx_serialized)
    if hashcode:
        return binascii.hexlify( hashing.bin_double_sha256(tx_bin + encoding.encode(int(hashcode), 256, 4)[::-1]) )

    else:
        return binascii.hexlify( hashing.bin_double_sha256(tx_bin)[::-1] )