Пример #1
0
def serialize_xpub(k):
    b = b''
    b += HD_HEADER_PUBLIC
    b += convert.int_to_byte(k['depth'])
    b += k['fingerprint']
    b += convert.int_to_bytes(k['i'], 4)
    b += k['chaincode']
    b += ecdsa.serialize_pub(k['pub'])
    return convert.bytes_to_b58check(b)
Пример #2
0
def serialize_xpriv(k):
    b = b''
    b += HD_HEADER_PRIVATE
    b += convert.int_to_byte(k['depth'])
    b += k['fingerprint']
    b += convert.int_to_bytes(k['i'], 4)
    b += k['chaincode']
    b += b'\x00' + ecdsa.serialize_priv(k['priv'])
    return convert.bytes_to_b58check(b)
Пример #3
0
def sign_p2pk(tx, i, priv, pub, hashtype = SIGHASH_ALL):
    assert isinstance(tx, dict)
    i = int(i)
    assert isinstance(priv, int)
    assert isinstance(pub, bytes)

    prev_script = script.make_p2pk(pub)
    txhash = sighash(tx, i, prev_script, hashtype)
    sig = ecdsa.sign(txhash, priv)
    sig = ecdsa.serialize_sig(sig) + convert.int_to_byte(hashtype)
    tx["ins"][i]["script"] = script.serialize([sig])
Пример #4
0
def sign_multisig(tx, i, prev_script, privs, pubs, hashtype = SIGHASH_ALL):
    assert isinstance(tx, dict)
    i = int(i)
    assert len(privs) == len(pubs)

    txhash = sighash(tx, i, prev_script, hashtype)

    s = [
        script.Opcode.OP_0,
    ]

    for j in range(len(privs)):
        priv = privs[j]
        pub = pubs[j]
        sig = ecdsa.sign(txhash, priv)
        sig = ecdsa.serialize_sig(sig) + convert.int_to_byte(hashtype)
        s.append(sig)

    tx["ins"][i]["script"] = script.serialize(s)
Пример #5
0
def serialize_unit(unit):
    if isinstance(unit, Opcode):
        return convert.int_to_byte(unit.value)

    if isinstance(unit, bytes):
        if len(unit) <= 75:
            return convert.int_to_byte(len(unit)) + unit
        elif len(unit) < 256:
            return convert.int_to_byte(76) + convert.int_to_byte(len(unit))+unit
        elif len(unit) < 65536:
            return convert.int_to_byte(77) + convert.int_to_bytes_le(len(unit), 2) + unit
        else:
            return convert.int_to_byte(78) + convert.int_to_bytes_le(len(unit), 4) + unit

    raise Exception('Bad script unit type')
Пример #6
0
def encode_var_int(x):
    x = int(x)
    if x < 253: return convert.int_to_byte(x)
    elif x < 65536: return convert.int_to_byte(253) + convert.int_to_bytes_le(x, 2)
    elif x < 4294967296: return convert.int_to_byte(254) + convert.int_to_bytes_le(x, 4)
    else: return convert.int_to_byte(255) + convert.int_to_bytes_le(x, 8)
Пример #7
0
def serialize_pub(pub, compressed=True):
    if compressed:
        return convert.int_to_byte(2 + (pub[1] % 2)) + convert.int_to_bytes(pub[0], 32)
    else:
        return b"\x04" + convert.int_to_bytes(pub[0], 32) + convert.int_to_bytes(pub[1], 32)
Пример #8
0
def serialize_sig(sig):
    _, r, s = sig
    b1, b2 = int_to_bytes_var_length(r), int_to_bytes_var_length(s)
    left = b"\x02" + convert.int_to_byte(len(b1)) + b1
    right = b"\x02" + convert.int_to_byte(len(b2)) + b2
    return b"\x30" + convert.int_to_byte(len(left + right)) + left + right