示例#1
0
文件: util.py 项目: askmeboy/sqlchain
def addr2id(addr, cur=None, rtnPKH=False):
    pkh = addr2pkh(addr)
    if not pkh:
        return None,'' if rtnPKH else None
    addr_id, = unpack('<q', hashlib.sha256(pkh).digest()[:5]+'\0'*3)
    if addr[:2].lower() == coincfg(BECH_HRP): # bech32 has bit 42 set, >20 byte, encode as odd id and stored in bech32 table
        addr_id |= BECH32_FLAG
        if len(pkh) > 20:
            addr_id |= P2SH_FLAG
    elif addr[0] in coincfg(P2SH_CHAR): # encode P2SH flag
        addr_id |= P2SH_FLAG
    if cur:
        tbl = 'bech32' if is_BL32(addr_id) else 'address'
        cur.execute("select id from {0} where id>=%s and id<%s+32 and addr=%s limit 1;".format(tbl), (addr_id,addr_id,pkh))
        row = cur.fetchone()
        return row[0] if row else None
    return (addr_id,pkh) if rtnPKH else addr_id
示例#2
0
def addr2id(addr, cur=None, rtnPKH=False):
    pkh = addr2pkh(addr)
    if not pkh:
        return None,'' if rtnPKH else None
    addr_id, = unpack('<q', hashlib.sha256(pkh).digest()[:5]+'\0'*3)
    if addr[:2].lower() == coincfg(BECH_HRP): # bech32 has bit 42 set, >20 byte, encode as odd id and stored in bech32 table
        addr_id |= BECH32_FLAG
        if len(pkh) > 20:
            addr_id |= P2SH_FLAG
    elif addr[0] in coincfg(P2SH_CHAR): # encode P2SH flag
        addr_id |= P2SH_FLAG
    if cur:
        tbl = 'bech32' if is_BL32(addr_id) else 'address'
        cur.execute("select id from {0} where id>=%s and id<%s+32 and addr=%s limit 1;".format(tbl), (addr_id,addr_id,pkh))
        row = cur.fetchone()
        return row[0] if row else None
    return (addr_id,pkh) if rtnPKH else addr_id
示例#3
0
文件: util.py 项目: askmeboy/sqlchain
def mkaddr(pkh, aid=None, p2sh=False, bech32=False):
    if pkh == '\0'*20 and aid==0:
        return '' # pkh==0 id==0, special case for null address when op_return or non-std script
    if bech32 or (aid and (aid & BECH32_FLAG != 0)):
        return bech32encode(coincfg(BECH_HRP), pkh)
    pad = ''
    an = chr(coincfg(ADDR_PREFIX) if (aid is None and not p2sh) or (aid is not None and (aid & P2SH_FLAG != P2SH_FLAG)) else coincfg(P2SH_PREFIX)) + str(pkh)
    for c in an:
        if c == '\0':
            pad += '1'
        else:
            break
    num = long((an + hashlib.sha256(hashlib.sha256(an).digest()).digest()[0:4]).encode('hex'), 16)
    out = ''
    while num >= 58:
        num,m = divmod(num, 58)
        out = b58[m] + out
    return pad + b58[num] + out
示例#4
0
def mkaddr(pkh, aid=None, p2sh=False, bech32=False):
    if pkh == '\0'*20 and aid==0:
        return '' # pkh==0 id==0, special case for null address when op_return or non-std script
    if bech32 or (aid and (aid & BECH32_FLAG != 0)):
        return bech32encode(coincfg(BECH_HRP), pkh)
    pad = ''
    an = chr(coincfg(ADDR_PREFIX) if (aid is None and not p2sh) or (aid is not None and (aid & P2SH_FLAG != P2SH_FLAG)) else coincfg(P2SH_PREFIX)) + str(pkh)
    for c in an:
        if c == '\0':
            pad += '1'
        else:
            break
    num = long((an + hashlib.sha256(hashlib.sha256(an).digest()).digest()[0:4]).encode('hex'), 16)
    out = ''
    while num >= 58:
        num,m = divmod(num, 58)
        out = b58[m] + out
    return pad + b58[num] + out
示例#5
0
文件: util.py 项目: askmeboy/sqlchain
def is_address(addr):
    if addr[:2].lower() == coincfg(BECH_HRP):
        return bech32decode(addr) != None
    try:
        n = 0
        for c in addr:
            n = n * 58 + b58.index(c)
        btc = ('%%0%dx' % (25 << 1) % n).decode('hex')[-25:]
        return btc[-4:] == hashlib.sha256(hashlib.sha256(btc[:-4]).digest()).digest()[:4]
    except Exception: # pylint:disable=broad-except
        return False
示例#6
0
def is_address(addr):
    if addr[:2].lower() == coincfg(BECH_HRP):
        return bech32decode(addr) != None
    try:
        n = 0
        for c in addr:
            n = n * 58 + b58.index(c)
        btc = ('%%0%dx' % (25 << 1) % n).decode('hex')[-25:]
        return btc[-4:] == hashlib.sha256(hashlib.sha256(btc[:-4]).digest()).digest()[:4]
    except Exception: # pylint:disable=broad-except
        return False
示例#7
0
文件: util.py 项目: askmeboy/sqlchain
def addr2pkh(addr):
    if addr[:2].lower() == coincfg(BECH_HRP):
        pkh = bech32decode(addr)
        return str(pkh[2:]) if pkh else None
    long_value = 0L
    for (i, c) in enumerate(addr[::-1]):
        long_value += b58.find(c) * (58**i)
    result = ''
    while long_value >= 256:
        div, mod = divmod(long_value, 256)
        result = chr(mod) + result
        long_value = div
    result = chr(long_value) + result
    nPad = 0
    for c in addr:
        if c == b58[0]:
            nPad += 1
        else:
            break
    result = chr(0)*nPad + result
    return result[1:-4]
示例#8
0
def addr2pkh(addr):
    if addr[:2].lower() == coincfg(BECH_HRP):
        pkh = bech32decode(addr)
        return str(pkh[2:]) if pkh else None
    long_value = 0L
    for (i, c) in enumerate(addr[::-1]):
        long_value += b58.find(c) * (58**i)
    result = ''
    while long_value >= 256:
        div, mod = divmod(long_value, 256)
        result = chr(mod) + result
        long_value = div
    result = chr(long_value) + result
    nPad = 0
    for c in addr:
        if c == b58[0]:
            nPad += 1
        else:
            break
    result = chr(0)*nPad + result
    return result[1:-4]
示例#9
0
def findBlocks(cur, blockpath, verbose):
    global lastpos # pylint:disable=global-statement
    filenum,pos = lastpos
    startpos = pos
    blkhash = None
    if filenum > 0:
        while not os.path.exists(blockpath % (filenum+2,)): # we trail by 2 blks file otherwise not reliable
            for _ in range(12):
                sleep(5)
                if sqc.done.isSet():
                    return None
            cur.execute("select 1;") # keepalive during long waits
    try:
        with open(blockpath % filenum, "rb") as fd:
            while not sqc.done.isSet():
                fd.seek(pos)
                buf = fd.read(8)
                if len(buf) < 8:
                    break
                magic,blksize = unpack('<II', buf)
                if magic != coincfg(BLKDAT_MAGIC):
                    if pos-startpos > 1e6: # skip large end gaps
                        break
                    pos += 1
                    continue
                buf = fd.read(80)
                blkhash = sha256(sha256(buf).digest()).digest()
                prevhash = buf[4:36]
                if verbose:
                    log("%05d:%d %s %s" % (filenum, pos, blkhash[::-1].encode('hex')[:32], prevhash[::-1].encode('hex')[:32]) )
                cur.execute("insert ignore into blkdat (id,hash,prevhash,filenum,filepos) values(-1,%s,%s,%s,%s);", (blkhash,prevhash,filenum,pos))
                pos += blksize
                startpos = pos
            lastpos = filenum+1,0
            return blkhash
    except IOError:
        print "No file:", blockpath % filenum
        lastpos = filenum,pos
        sqc.done.set()
        return None
示例#10
0
文件: util.py 项目: askmeboy/sqlchain
def coin_reward(height):
    return float(int(coincfg(BLK_REWARD)) >> int(height // coincfg(HALF_BLKS)))/float(1e8)
示例#11
0
def coin_reward(height):
    return float(int(coincfg(BLK_REWARD)) >> int(height // coincfg(HALF_BLKS)))/float(1e8)