Exemplo n.º 1
0
 def test_sign_input(self):
     private_key = PrivateKey(secret=8675309)
     stream = BytesIO(
         bytes.fromhex(
             '010000000199a24308080ab26e6fb65c4eccfadf76749bb5bfa8cb08f291320b3c21e56f0d0d00000000ffffffff02408af701000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac80969800000000001976a914507b27411ccf7f16f10297de6cef3f291623eddf88ac00000000'
         ))
     tx_obj = Tx.parse(stream, testnet=True)
     self.assertTrue(tx_obj.sign_input(0, private_key))
     want = '010000000199a24308080ab26e6fb65c4eccfadf76749bb5bfa8cb08f291320b3c21e56f0d0d0000006b4830450221008ed46aa2cf12d6d81065bfabe903670165b538f65ee9a3385e6327d80c66d3b502203124f804410527497329ec4715e18558082d489b218677bd029e7fa306a72236012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff02408af701000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac80969800000000001976a914507b27411ccf7f16f10297de6cef3f291623eddf88ac00000000'
     self.assertEqual(tx_obj.serialize().hex(), want)
Exemplo n.º 2
0
 def test_address_2(self):
     secrets = (b'[email protected] test1',
                b'[email protected] test2')
     private_keys = [
         PrivateKey(little_endian_to_int(hash256(s))) for s in secrets
     ]
     points = [p.point for p in private_keys]
     redeem_script = multisig_redeem_script(2, points)
     h160 = redeem_script.hash160()
     script_pubkey = p2sh_script(h160)
     self.assertEqual(script_pubkey.address(testnet=True),
                      '2MxEZNps15dAnGX5XaVwZWgoDvjvsDE5XSx')
Exemplo n.º 3
0
 def from_seed(cls, seed, path):
     raw = HMAC(key=b'Bitcoin seed', msg=seed, digestmod=sha512).digest()
     private_key = PrivateKey(secret=int.from_bytes(raw[:32], 'big'))
     chain_code = raw[32:]
     root = cls(
         private_key=private_key,
         chain_code=chain_code,
         depth=0,
         fingerprint=b'\x00\x00\x00\x00',
         child_number=0,
     )
     return root.traverse(path)
Exemplo n.º 4
0
 def from_seed(cls, seed, testnet=False):
     # get hmac_sha512 with b'Bitcoin seed' and seed
     h = hmac_sha512(b'Bitcoin seed', seed)
     # create the private key using the first 32 bytes in big endian
     private_key = PrivateKey(secret=big_endian_to_int(h[:32]))
     # chaincode is the last 32 bytes
     chain_code = h[32:]
     # return an instance of the class
     return cls(
         private_key=private_key,
         chain_code=chain_code,
         testnet=testnet,
     )
Exemplo n.º 5
0
    def test_sign_input(self):
        private_key = PrivateKey(secret=8675309)
        tx_ins = []
        prev_tx = bytes.fromhex('0025bc3c0fa8b7eb55b9437fdbd016870d18e0df0ace7bc9864efc38414147c8')
        tx_ins.append(TxIn(prev_tx, 0))
        tx_outs = []
        h160 = decode_base58('mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2')
        tx_outs.append(TxOut(amount=int(0.99 * 100000000), script_pubkey=p2pkh_script(h160)))
        h160 = decode_base58('mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf')
        tx_outs.append(TxOut(amount=int(0.1 * 100000000), script_pubkey=p2pkh_script(h160)))

        tx = Tx(1, tx_ins, tx_outs, 0, testnet=True)
        self.assertTrue(tx.sign_input(0, private_key))
Exemplo n.º 6
0
 def test_example_6(self):
     tx_ins = []
     prev_tx = bytes.fromhex('0d6fe5213c0b3291f208cba8bfb59b7476dffacc4e5cb66f6eb20a080843a299')
     prev_index = 13
     tx_ins.append(TxIn(prev_tx, prev_index, Script([]), 0xffffffff))
     tx_outs = []
     change_amount = int(0.33 * 100000000)
     change_h160 = decode_base58('mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2')
     change_script = p2pkh_script(change_h160)
     tx_outs.append(TxOut(amount=change_amount, script_pubkey=change_script))
     target_amount = int(0.1 * 100000000)
     target_h160 = decode_base58('mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf')
     target_script = p2pkh_script(target_h160)
     tx_outs.append(TxOut(amount=target_amount, script_pubkey=target_script))
     transaction = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     z = transaction.sig_hash(0)
     private_key = PrivateKey(secret=8675309)
     der = private_key.sign(z).der()
     sig = der + SIGHASH_ALL.to_bytes(1, 'big')
     sec = private_key.point.sec()
     transaction.tx_ins[0].script_sig = Script([sig, sec])
     want = '010000000199a24308080ab26e6fb65c4eccfadf76749bb5bfa8cb08f291320b3c21e56f0d0d0000006b4830450221008ed46aa2cf12d6d81065bfabe903670165b538f65ee9a3385e6327d80c66d3b502203124f804410527497329ec4715e18558082d489b218677bd029e7fa306a72236012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff02408af701000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac80969800000000001976a914507b27411ccf7f16f10297de6cef3f291623eddf88ac00000000'
     self.assertEqual(transaction.serialize().hex(), want)
Exemplo n.º 7
0
def changewallet(privkey_wif):
    priv = PrivateKey(privkey_wif)
    sec = str(priv.secret)
    print("sec")
    print(sec)
    print(len(sec))
    x = str(int(str(priv.point.x), 16))
    print("x")
    print(x)
    y = str(int(str(priv.point.y), 16))
    print("y")
    print(y)
    ardubridge.writewallet(sec)
    ardubridge.writepubkey(x, y)
Exemplo n.º 8
0
 def test_address_p2wsh(self):
     secrets = (b'[email protected] test1',
                b'[email protected] test2')
     private_keys = [
         PrivateKey(little_endian_to_int(hash256(s))) for s in secrets
     ]
     points = [p.point for p in private_keys]
     witness_script = multisig_redeem_script(2, points)
     h256 = witness_script.sha256()
     script_pubkey = p2wsh_script(h256)
     addr = script_pubkey.address(testnet=True)
     self.assertEqual(
         addr,
         'tb1qevl98yey2nqnhnh5psn3h73zfl9yy5ae3ss4e79qungqa8y0eprsl8gle6')
Exemplo n.º 9
0
 def test_address_p2sh_p2wsh(self):
     secrets = (b'[email protected] test1',
                b'[email protected] test2')
     private_keys = [
         PrivateKey(little_endian_to_int(hash256(s))) for s in secrets
     ]
     points = [p.point for p in private_keys]
     witness_script = multisig_redeem_script(2, points)
     h256 = witness_script.sha256()
     redeem_script = p2wsh_script(h256)
     h160 = redeem_script.hash160()
     script_pubkey = p2sh_script(h160)
     self.assertEqual(script_pubkey.address(testnet=True),
                      '2MuVPpBuS6evYsVeUJ85Z5Z6hokXGdtkYAw')
Exemplo n.º 10
0
 def test_sign_p2wpkh(self):
     private_key = PrivateKey(secret=8675309)
     prev_tx = bytes.fromhex(
         '6bfa079532dd9fad6cfbf218edc294fdfa7dd0cb3956375bc864577fb36fad97')
     prev_index = 0
     fee = 500
     tx_in = TxIn(prev_tx, prev_index)
     amount = tx_in.value(testnet=True) - fee
     h160 = decode_base58('mqYz6JpuKukHzPg94y4XNDdPCEJrNkLQcv')
     tx_out = TxOut(amount=amount, script_pubkey=P2PKHScriptPubKey(h160))
     t = Tx(1, [tx_in], [tx_out], 0, testnet=True, segwit=True)
     self.assertTrue(t.sign_input(0, private_key))
     want = '0100000000010197ad6fb37f5764c85b375639cbd07dfafd94c2ed18f2fb6cad9fdd329507fa6b0000000000ffffffff014c400f00000000001976a9146e13971913b9aa89659a9f53d327baa8826f2d7588ac02483045022100feab5b8feefd5e774bdfdc1dc23525b40f1ffaa25a376f8453158614f00fa6cb02204456493d0bc606ebeb3fa008e056bbc96a67cb0c11abcc871bfc2bec60206bf0012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b6700000000'
     self.assertEqual(t.serialize().hex(), want)
Exemplo n.º 11
0
 def test_sign_p2sh_p2wpkh(self):
     private_key = PrivateKey(secret=8675309)
     redeem_script = private_key.point.p2sh_p2wpkh_redeem_script()
     prev_tx = bytes.fromhex(
         '2e19b463bd5c8a3e0f10ae827f5a670f6794fca96394ecf8488321291d1c2ee9')
     prev_index = 1
     fee = 500
     tx_in = TxIn(prev_tx, prev_index)
     amount = tx_in.value(testnet=True) - fee
     h160 = decode_base58('mqYz6JpuKukHzPg94y4XNDdPCEJrNkLQcv')
     tx_out = TxOut(amount=amount, script_pubkey=P2PKHScriptPubKey(h160))
     t = Tx(1, [tx_in], [tx_out], 0, testnet=True, segwit=True)
     self.assertTrue(
         t.sign_input(0, private_key, redeem_script=redeem_script))
     want = '01000000000101e92e1c1d29218348f8ec9463a9fc94670f675a7f82ae100f3e8a5cbd63b4192e0100000017160014d52ad7ca9b3d096a38e752c2018e6fbc40cdf26fffffffff014c400f00000000001976a9146e13971913b9aa89659a9f53d327baa8826f2d7588ac0247304402205e3ae5ac9a0e0a16ae04b0678c5732973ce31051ba9f42193e69843e600d84f2022060a91cbd48899b1bf5d1ffb7532f69ab74bc1701a253a415196b38feb599163b012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b6700000000'
     self.assertEqual(t.serialize().hex(), want)
Exemplo n.º 12
0
 def spend_tx(cls, wifs, utxos, destination_addr, fee=540):
     destination_address_data = cls.get_address_data(destination_addr)
     testnet = destination_address_data['testnet']
     if testnet:
         prefix = bytes([cls.testnet_prefixes[0]])
     else:
         prefix = bytes([cls.p2pkh_prefixes[0]])
     tx_ins = []
     sequence = 0xffffffff
     priv_lookup = {}
     total = 0
     for wif in wifs:
         priv_key = PrivateKey.parse(wif)
         addr = priv_key.point.address(priv_key.compressed, prefix=prefix)
         # look up utxos for each address
         address_data = cls.get_address_data(addr)
         script_pubkey = address_data['script_pubkey']
         spk = script_pubkey.serialize()
         priv_lookup[spk] = priv_key
     if not utxos:
         raise RuntimeError('fetch utxos first')
     for serialized_script_pubkey, prev_tx, prev_index, value in utxos:
         tx_ins.append(
             TxIn(prev_tx,
                  prev_index,
                  b'',
                  sequence,
                  value=value,
                  script_pubkey=serialized_script_pubkey))
         total += value
     num_tx_ins = len(tx_ins)
     if num_tx_ins == 0:
         raise RuntimeError('nothing to spend')
     script_pubkey = destination_address_data['script_pubkey']
     tx_out = TxOut(total - fee, script_pubkey.serialize())
     tx = cls(cls.default_version, tx_ins, [tx_out], 0, testnet=testnet)
     for index, tx_in in enumerate(tx_ins):
         priv_key = priv_lookup[tx_in.script_pubkey().serialize()]
         tx.sign_input(
             index,
             priv_key,
             cls.default_hash_type,
             compressed=priv_key.compressed,
         )
     if not tx.verify():
         raise RuntimeError('failed validation')
     return tx.serialize()
Exemplo n.º 13
0
def schnorr():
    pk = PrivateKey(randint(0, 2**256))
    point = pk.point  # public point

    def sign(pk, z):
        k = randint(0, 2**256)  # nonce
        R = k * ecc.G  # nonce point
        s = (k + HI(R.sec(), z) * pk.secret) % ecc.N
        return R, s

    def verify(R, s, z):
        Q = s * ecc.G - HI(R.sec(), z) * point
        return Q == R

    z = gen_msg()
    R, s = sign(pk, z)
    assert verify(R, s, z)
Exemplo n.º 14
0
 def child(self, index):
     if index >= 0x80000000:
         raise ValueError('child number should always be less than 2^31')
     sec = self.point.sec()
     data = sec + index.to_bytes(4, 'big')
     raw = HMAC(key=self.chain_code, msg=data, digestmod=sha512).digest()
     point = PrivateKey(int.from_bytes(raw[:32], 'big')).point + self.point
     chain_code = raw[32:]
     depth = self.depth + 1
     fingerprint = hash160(sec)[:4]
     child_number = index
     return HDPublicKey(
         point=point,
         chain_code=chain_code,
         depth=depth,
         fingerprint=fingerprint,
         child_number=child_number,
     )
Exemplo n.º 15
0
def xprv_child(self, index):
    if index >= 0x80000000:
        data = int_to_big_endian(self.private_key.secret, 33) + int_to_big_endian(index, 4)
    else:
        data = self.private_key.point.sec() + int_to_big_endian(index, 4)
    h = hmac_sha512(self.chain_code, data)
    secret = (big_endian_to_int(h[:32]) + self.private_key.secret) % N
    private_key = PrivateKey(secret=secret)
    chain_code = h[32:]
    depth = self.depth + 1
    parent_fingerprint = self.pub.hash160()[:4]
    child_number = index
    return HDPrivateKey(
        private_key=private_key,
        chain_code=chain_code,
        depth=depth,
        parent_fingerprint=parent_fingerprint,
        child_number=child_number,
        testnet=self.testnet,
    )
Exemplo n.º 16
0
 def test_exercise_3_1(self):
     prev_tx = bytes.fromhex(
         'eb581753a4dbd6befeaaaa28a6f4576698ba13a07c03da693a65bce11cf9887a')
     prev_index = 1
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     target_amount = 0.04
     change_address = 'mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2'
     fee = 50000
     secret = 8675309
     private_key = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx, prev_index))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = int(target_amount * 100000000)
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     h160 = decode_base58(change_address)
     script_pubkey = p2pkh_script(h160)
     prev_amount = tx_ins[0].value(testnet=True)
     change_satoshis = prev_amount - target_satoshis - fee
     tx_outs.append(TxOut(change_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     if private_key.point.address(testnet=True) != change_address:
         raise RuntimeError(
             'Private Key does not correspond to Change Address, check priv_key and change_address'
         )
     if tx_ins[0].script_pubkey(
             testnet=True).instructions[2] != decode_base58(change_address):
         raise RuntimeError(
             'Output is not something you can spend with this private key. Check that the prev_tx and prev_index are correct'
         )
     if tx_obj.fee() > 0.05 * 100000000 or tx_obj.fee() <= 0:
         raise RuntimeError(
             'Check that the change amount is reasonable. Fee is {}'.format(
                 tx_obj.fee()))
     self.assertEqual(
         tx_obj.serialize().hex(),
         '01000000017a88f91ce1bc653a69da037ca013ba986657f4a628aaaafebed6dba4531758eb010000006a47304402204ce6e3877ed2e18d2165276cbdba241507ce72b44d8df640eb6cb4d415eaaea002207dffd162da35593d86188ce87a1cbc9d3a5b26391870f19bf1764ca05b315ad9012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff0200093d00000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac7077e401000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac00000000'
     )
Exemplo n.º 17
0
 def parse(cls, s):
     raw = raw_decode_base58(s.read(111), num_bytes=82)
     version = raw[:4]
     if version in (TESTNET_XPRV, TESTNET_YPRV, TESTNET_ZPRV):
         testnet = True
     elif version in (MAINNET_XPRV, MAINNET_YPRV, MAINNET_ZPRV):
         testnet = False
     else:
         raise ValueError('not an xprv, yprv or zprv: {}'.format(version))
     depth = raw[4]
     fingerprint = raw[5:9]
     child_number = int.from_bytes(raw[9:13], 'big')
     chain_code = raw[13:45]
     private_key = PrivateKey(secret=int.from_bytes(raw[46:], 'big'))
     return cls(
         private_key=private_key,
         chain_code=chain_code,
         depth=depth,
         fingerprint=fingerprint,
         child_number=child_number,
         testnet=testnet,
     )
Exemplo n.º 18
0
def Mu(Ns=10):
    pks = [PrivateKey(randint(0, 2**256)) for _ in range(Ns)]
    points = [pk.point for pk in pks]

    def sign(pks, z):
        L = H(*[p.sec() for p in points])
        ks = [randint(0, 2**256) for _ in range(len(pks))]
        Rs = [k * ecc.G for k in ks]
        R = sum(Rs, ecc.S256Point(None, None))
        # aggregate key
        P = sum((HI(L, p.sec()) * p for p in points),
                ecc.S256Point(None, None))
        cs = [HI(R.sec(), z) * HI(L, p.sec()) for p in points]
        s = sum([k + c * pk.secret for k, c, pk in zip(ks, cs, pks)]) % ecc.N
        return R, s, P

    def verify(R, s, z, P):
        return R == s * ecc.G - HI(R.sec(), z) * P

    z = gen_msg()
    R, s, P = sign(pks, z)
    assert verify(R, s, z, P)
Exemplo n.º 19
0
    def test_b2x_sign(self):
        # Generated from ./bitcoin2x-qt -regtest
        wif = 'cVC6z7gnezHZut5yyuCX3x79tfcbauCWiFcBY92Vg4crfu4Maa5B'
        prev_tx = 'b6d073333c1a8e4360b1e2c7fa2ed6b67b74272ad7fabf52a4e4732df5f47dbd'
        prev_index = 0
        prev_value = 5000000000
        destination = 'mrVqpGm7F5MVCwsP4s3fQEN2GAaykJoTu4'
        amount = 4996000000

        priv_key = PrivateKey.parse(wif)
        prev_script_pubkey = B2XTx.get_address_data(
            priv_key.point.address())['script_pubkey'].serialize()
        tx_in = TxIn(unhexlify(prev_tx), prev_index, b'', 0xffffffff, b'\x00',
                     prev_value, prev_script_pubkey)
        script = B2XTx.get_address_data(
            destination)['script_pubkey'].serialize()
        tx_out = TxOut(amount, script)
        tx = B2XTx(2, [tx_in], [tx_out], 0, testnet=True)
        tx.sign(priv_key)
        want = "0200000001bd7df4f52d73e4a452bffad72a27747bb6d62efac7e2b160438e1a3c3373d0b6000000006a47304402201df7c8c97443bd46da751e0051a4395ba3613be3604be97d3c801c21e3d23c79022012ad30b7ffd42ad7bb96f9157519f7e3c35409ed54f783a3c854a596343a6c713121030f96812693c4a50162134cfa307afb63580171963d6c4198e8e5cfeee2c92b60ffffffff0100e9c829010000001976a91478738f2c5a75397eb2f851597261f766a67d9b6388ac00000000"
        self.assertTrue(tx.verify())
        self.assertEqual(tx.serialize().hex(), want)
Exemplo n.º 20
0
 def parse(cls, xprv):
     raw = decode_base58(xprv, num_bytes=82)
     version = raw[:4]
     if version == unhexlify('04358394'):
         testnet = True
     elif version == unhexlify('0488ADE4'):
         testnet = False
     else:
         raise RuntimeError('not an xprv: {}'.format(xprv))
     depth = raw[4]
     fingerprint = raw[5:9]
     child_number = int.from_bytes(raw[9:13], 'big')
     chain_code = raw[13:45]
     private_key = PrivateKey(secret=int.from_bytes(raw[46:], 'big'))
     return cls(
         private_key=private_key,
         chain_code=chain_code,
         depth=depth,
         fingerprint=fingerprint,
         child_number=child_number,
         testnet=testnet,
     )
Exemplo n.º 21
0
 def child(self, index):
     '''Returns the child HDPrivateKey at a particular index.
     Hardened children return for indices >= 0x8000000.
     '''
     # if index >= 0x80000000
     if index >= 0x80000000:
         # the message data is the private key secret in 33 bytes in
         #  big-endian and the index in 4 bytes big-endian.
         data = int_to_big_endian(self.private_key.secret,
                                  33) + int_to_big_endian(index, 4)
     else:
         # the message data is the public key compressed SEC
         #  and the index in 4 bytes big-endian.
         data = self.private_key.point.sec() + int_to_big_endian(index, 4)
     # get the hmac_sha512 with chain code and data
     h = hmac_sha512(self.chain_code, data)
     # the new secret is the first 32 bytes as a big-endian integer
     #  plus the secret mod N
     secret = (big_endian_to_int(h[:32]) + self.private_key.secret) % N
     # create the PrivateKey object
     private_key = PrivateKey(secret=secret)
     # the chain code is the last 32 bytes
     chain_code = h[32:]
     # depth is whatever the current depth + 1
     depth = self.depth + 1
     # parent_fingerprint is the fingerprint of this node
     parent_fingerprint = self.fingerprint()
     # child number is the index
     child_number = index
     # return a new HDPrivateKey instance
     return HDPrivateKey(
         private_key=private_key,
         chain_code=chain_code,
         depth=depth,
         parent_fingerprint=parent_fingerprint,
         child_number=child_number,
         testnet=self.testnet,
     )
Exemplo n.º 22
0
 def test_exercise_5(self):
     prev_tx_1 = bytes.fromhex('11d05ce707c1120248370d1cbf5561d22c4f83aeba0436792c82e0bd57fe2a2f')
     prev_index_1 = 1
     prev_tx_2 = bytes.fromhex('51f61f77bd061b9a0da60d4bedaaf1b1fad0c11e65fdc744797ee22d20b03d15')
     prev_index_2 = 1
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     target_amount = 0.0429
     secret = 8675309
     priv = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx_1, prev_index_1, Script([]), 0xffffffff))
     tx_ins.append(TxIn(prev_tx_2, prev_index_2, Script([]), 0xffffffff))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = int(target_amount * 100000000)
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     self.assertTrue(tx_obj.sign_input(0, priv))
     self.assertTrue(tx_obj.sign_input(1, priv))
     self.assertTrue(tx_obj.verify())
     want = '01000000022f2afe57bde0822c793604baae834f2cd26155bf1c0d37480212c107e75cd011010000006a47304402204cc5fe11b2b025f8fc9f6073b5e3942883bbba266b71751068badeb8f11f0364022070178363f5dea4149581a4b9b9dbad91ec1fd990e3fa14f9de3ccb421fa5b269012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff153db0202de27e7944c7fd651ec1d0fab1f1aaed4b0da60d9a1b06bd771ff651010000006b483045022100b7a938d4679aa7271f0d32d83b61a85eb0180cf1261d44feaad23dfd9799dafb02205ff2f366ddd9555f7146861a8298b7636be8b292090a224c5dc84268480d8be1012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff01d0754100000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     self.assertEqual(tx_obj.serialize().hex(), want)
Exemplo n.º 23
0
def BN(Ns=10):  # num sig
    pks = [PrivateKey(randint(0, 2**256)) for _ in range(Ns)]
    points = [pk.point for pk in pks]

    def sign(pks, z):
        ks = [randint(0, 2**256) for _ in range(len(pks))]

        L = H(*[p.sec() for p in points])
        Rs = [k * ecc.G for k in ks]
        R = sum(Rs, ecc.S256Point(None, None))
        cs = [HI(L, p.sec(), R.sec(), z) for p in points]
        s = sum(k + c * pk.secret for k, c, pk in zip(ks, cs, pks)) % ecc.N
        return R, s

    def verify(R, s, z):
        L = H(*[p.sec() for p in points])
        cs = [HI(L, p.sec(), R.sec(), z) for p in points]
        return R == s * ecc.G - sum(
            (c * p for c, p in zip(cs, points)), ecc.S256Point(None, None))

    z = gen_msg()
    R, s = sign(pks, z)
    assert verify(R, s, z)
Exemplo n.º 24
0
def xprv_raw_parse(cls, s):
    version = s.read(4)
    if version in (TESTNET_XPRV, TESTNET_YPRV, TESTNET_ZPRV):
        testnet = True
    elif version in (MAINNET_XPRV, MAINNET_YPRV, MAINNET_ZPRV):
        testnet = False
    else:
        raise ValueError('not an xprv, yprv or zprv: {}'.format(version))
    depth = byte_to_int(s.read(1))
    parent_fingerprint = s.read(4)
    child_number = big_endian_to_int(s.read(4))
    chain_code = s.read(32)
    if byte_to_int(s.read(1)) != 0:
        raise ValueError('private key should be preceded by a zero byte')
    private_key = PrivateKey(secret=big_endian_to_int(s.read(32)))
    return cls(
        private_key=private_key,
        chain_code=chain_code,
        depth=depth,
        parent_fingerprint=parent_fingerprint,
        child_number=child_number,
        testnet=testnet,
    )
Exemplo n.º 25
0
    def test_sign_input(self):
        private_key = PrivateKey(secret=8675309)
        tx_ins = []
        prev_tx = unhexlify('0025bc3c0fa8b7eb55b9437fdbd016870d18e0df0ace7bc9864efc38414147c8')
        tx_ins.append(TxIn(
            prev_tx=prev_tx,
            prev_index=0,
            script_sig = b'',
            sequence = 0xffffffff,
        ))
        tx_outs = []
        h160 = decode_base58('mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2')
        tx_outs.append(TxOut(amount=int(0.99*100000000), script_pubkey=p2pkh_script(h160)))
        h160 = decode_base58('mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf')
        tx_outs.append(TxOut(amount=int(0.1*100000000), script_pubkey=p2pkh_script(h160)))

        tx = Tx(
            version=1,
            tx_ins=tx_ins,
            tx_outs=tx_outs,
            locktime=0,
            testnet=True,
        )
        self.assertTrue(tx.sign_input(0, private_key, SIGHASH_ALL))
Exemplo n.º 26
0
 def test_exercise_3_2(self):
     prev_tx_1 = bytes.fromhex(
         '89cbfe2eddaddf1eb11f5c4adf6adaa9bca4adc01b2a3d03f8dd36125c068af4')
     prev_index_1 = 0
     prev_tx_2 = bytes.fromhex(
         '19069e1304d95f70e03311d9d58ee821e0978e83ecfc47a30af7cd10fca55cf4')
     prev_index_2 = 0
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     fee = 50000
     secret = 61740721216174072121
     private_key = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx_1, prev_index_1))
     tx_ins.append(TxIn(prev_tx_2, prev_index_2))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = tx_ins[0].value(True) + tx_ins[1].value(True) - fee
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     tx_obj.sign_input(1, private_key)
     if tx_ins[0].script_pubkey(
             testnet=True).instructions[2] != decode_base58(
                 private_key.point.address(testnet=True)):
         raise RuntimeError(
             'Output is not something you can spend with this private key. Check that the prev_tx and prev_index are correct'
         )
     if tx_obj.fee() > 0.05 * 100000000 or tx_obj.fee() <= 0:
         raise RuntimeError(
             'Check that the change amount is reasonable. Fee is {}'.format(
                 tx_obj.fee()))
     self.assertEqual(
         tx_obj.serialize().hex(),
         '0100000002f48a065c1236ddf8033d2a1bc0ada4bca9da6adf4a5c1fb11edfaddd2efecb89000000006a47304402204b9ee431a2f5deaefb5282a34d7dcfdb47d55b1e3ce00cac4c6b6e6f0f0e8d58022062710e84786d2c6c89ddda5a149b45088b15230c6b825f0f21490f99bd74c81d012103f96f3a1efd31e1a8d7078118ee56bff7355d58907ce0f865f5f0b3dbe34e55befffffffff45ca5fc10cdf70aa347fcec838e97e021e88ed5d91133e0705fd904139e0619000000006a473044022073d7217b2d582e55978284c2628015a14e3490e835c76488eb29b63de15d17920220384e4b5282c911273efd4d98170e7092e10a729d142db17f4725c15364fa4ecc012103f96f3a1efd31e1a8d7078118ee56bff7355d58907ce0f865f5f0b3dbe34e55beffffffff01021f320a000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     )
Exemplo n.º 27
0
from ecc import PrivateKey
from helper import hash256, little_endian_to_int

mypass = b'KBU_201601071_PARKDONGHYUNE'
mysecret = little_endian_to_int(hash256(mypass))

P = PrivateKey(mysecret)

print(P.point.address(testnet=True))
Exemplo n.º 28
0
import pprint

raw="""\
0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f86/5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU
04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec6874/5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk
048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d46213/5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw\
"""
from ecc import PrivateKey, S256Point
from helper import little_endian_to_int, big_endian_to_int
from binascii import unhexlify, hexlify
import script
import helper
from tx import TxIn, TxOut, Tx

key_pairs = [x.split('/') for x in map(str.strip,raw.split('\n'))]
key_pairs = [(S256Point.parse(unhexlify(sec)), PrivateKey.parse(wif, compressed=False)) for sec, wif in key_pairs]
assert all(p == pk.point for p,pk in key_pairs)
pubkeys = [p for p, _ in key_pairs]

OP_n = lambda n: 0x51 + n - 1

n_required = 2
elements = [OP_n(n_required)] + [pk.sec(compressed=False) for pk in pubkeys] + [OP_n(len(pubkeys))] + [174]
redeemScript = script.Script(elements)

address=helper.h160_to_p2sh_address(helper.hash160(redeemScript.serialize()), testnet=False)
redeemScript=hexlify(redeemScript.serialize())
assert address=="3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC"
assert redeemScript=="52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353ae".encode('ascii')
print(hexlify(helper.hash160(unhexlify(redeemScript))))
Exemplo n.º 29
0
from ecc import PrivateKey
from helper import hash256, little_endian_to_int

passphrase = b'pickmeupimscared1419'
secret = little_endian_to_int(hash256(passphrase))
priv = PrivateKey(secret)
print(priv.point.address(testnet=True))
Exemplo n.º 30
0
from ecc import PrivateKey

priv = PrivateKey(5000)
print(priv.point.sec(compressed=False).hex())

priv = PrivateKey(2018**5)
print(priv.point.sec(compressed=False).hex())

priv = PrivateKey(0xdeadbeef12345)
print(priv.point.sec(compressed=False).hex())