Exemplo n.º 1
0
 def __get_multisig_script_sig__(self,  script_sig,
                                 keys, signatures,
                                 script_code,
                                 redeem_script,
                                 n, amount=None):
     sig_map = {keys[i]:signatures[i] for i in range(len(keys))}
     pub_keys = get_multisig_public_keys(redeem_script)
     p2wsh = True if isinstance(script_sig, list) else False
     if not p2wsh:
         s = get_stream(script_sig)
         o, d = read_opcode(s)
         while o:
             o, d = read_opcode(s)
             if d and is_valid_signature_encoding(d):
                 for i in range(4):
                     sighash = self.sig_hash(n, script_pub_key=script_code, sighash_type=d[-1])
                     sighash = s2rh(sighash) if isinstance(sighash, str) else sighash
                     pk = public_key_recovery(d[:-1], sighash, i, hex=0)
                     if pk in pub_keys:
                         sig_map[pk] = d
                         break
         # recreate script sig
         r = [OP_0]
         for k in pub_keys:
             try:
                 r.append(op_push_data(sig_map[k]))
             except:
                 pass
         r += [op_push_data(redeem_script)]
     else:
         for w in script_sig:
             if isinstance(w, str):
                 w = bytes.fromhex(w)
             if w and is_valid_signature_encoding(w):
                 d = w[:-1]
                 for i in range(4):
                     sighash = self.sig_hash_segwit(n, amount,
                                                    script_pub_key=script_code,
                                                    sighash_type=w[-1])
                     pk = public_key_recovery(d, sighash, i, hex=0)
                     if pk in pub_keys:
                         sig_map[pk] = w
                         break
         r = [b""]
         for k in pub_keys:
             try:
                 r.append(sig_map[k])
             except:
                 pass
         r += [redeem_script]
     return r
Exemplo n.º 2
0
 def __get_bare_multisig_script_sig__(self,  script_sig, script_pub_key,
                                      keys, signatures, n):
     sig_map = {keys[i]:signatures[i] for i in range(len(keys))}
     pub_keys = get_multisig_public_keys(script_pub_key)
     s = get_stream(script_sig)
     o, d = read_opcode(s)
     while o:
         o, d = read_opcode(s)
         if d and is_valid_signature_encoding(d):
             for i in range(4):
                 sighash = self.sig_hash(n, script_pub_key=script_pub_key, sighash_type=d[-1])
                 sighash = s2rh(sighash) if isinstance(sighash, str) else sighash
                 pk = public_key_recovery(d[:-1], sighash, i, hex=0)
                 if pk in pub_keys:
                     sig_map[pk] = d
                     break
     # recreate script sig
     r = [OP_0]
     for k in pub_keys:
         try:
             r.append(op_push_data(sig_map[k]))
         except:
             pass
     return r
Exemplo n.º 3
0
def is_valid_signature_encoding(sig):
    return __parent__.is_valid_signature_encoding(sig)
Exemplo n.º 4
0
def test_is_valid_signature_encoding():
    s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s)
    s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01010101"
    assert is_valid_signature_encoding(s) == False
    s = "8044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3080022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044020947ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044029047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044992047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044020047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044022080ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044022000098e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0320217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0200217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220807f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False
    s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \
        "d12d482f0220007f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"
    assert is_valid_signature_encoding(s) == False