<p>I solved the problem. This is the code:</p>

<pre><code>import os
import hashlib
from hashlib import sha256


def ripemd160(x):
    d = hashlib.new("ripemd160")
    d.update(x)
    return d


P = 2 ** 256 - 2 ** 32 - 2 ** 9 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 4 - 1
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
     0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)
B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"


def point_add(p, q):
    xp, yp = p
    xq, yq = q

    if p == q:
        l = pow(2 * yp % P, P - 2, P) * (3 * xp * xp) % P
    else:
        l = pow(xq - xp, P - 2, P) * (yq - yp) % P

    xr = (l ** 2 - xp - xq) % P
    yr = (l * xp - l * xr - yp) % P

    return xr, yr


def point_mul(p, d):
    n = p
    q = None

    for i in range(256):
        if d &amp; (1 &lt;&lt; i):
            if q is None:
                q = n
            else:
                q = point_add(q, n)

        n = point_add(n, n)

    return q


def point_bytes(p):
    x, y = p

    return b"\x04" + x.to_bytes(32, "big") + y.to_bytes(32, "big")


def b58_encode(d):
    out = ""
    p = 0
    x = 0

    while d[0] == 0:
        out += "1"
        d = d[1:]

    for i, v in enumerate(d[::-1]):
        x += v * (256 ** i)

    while x &gt; 58 ** (p + 1):
        p += 1

    while p &gt;= 0:
        a, x = divmod(x, 58 ** p)
        out += B58[a]
        p -= 1

    return out


def make_address(privkey):
    q = point_mul(G, int.from_bytes(privkey, "big"))
    hash160 = ripemd160(sha256(point_bytes(q)).digest()).digest()
    addr = b"\x00" + hash160
    checksum = sha256(sha256(addr).digest()).digest()[:4]
    addr += checksum

    wif = b"\x80" + privkey
    checksum = sha256(sha256(wif).digest()).digest()[:4]
    wif += checksum

    addr = b58_encode(addr)
    wif = b58_encode(wif)

    return addr, wif


print("=========================")


from ecdsa import SigningKey, SECP256k1

sk = SigningKey.generate(curve=SECP256k1)
vk = sk.get_verifying_key()
addr, wif = make_address(sk.to_string())
print("Address: " + addr)
print("Privkey: " + wif)
</code></pre>

CC BY-SA 4.0
2022-03-19T06:12:16.063Z
Thanks but I keep getting the following error:

TypeError: point_bytes() takes 1 positional argument but 2 were given
jennie788
CC BY-SA 3.0
2017-12-22T11:47:26.183Z
2017-12-22T11:47:26.183Z
Marcos Aguayo
<p>I have the following code:</p>

<pre><code>import binascii
import hashlib
from fastecdsa import keys, curve

# generate a private key for curve P256
priv_key = keys.gen_private_key(curve.secp256k1)
print (priv_key)
print ("______")

# get the public key corresponding to the private key we just generated
pub_key = keys.get_public_key(priv_key, curve.secp256k1)
print (pub_key)
</code></pre>

<p>Which returns this:</p>

<pre><code>68553277193328358088381091283586955911631072878513122367770244549235879948867
______
(defdba7ac050b698bc63134bbc495064b5a99125b023d8ae92d21ca43be77961,
 68ed2abc9d36bae64947b94dd15864e3b2f7601b7009c5b9bae8a8775553fea0)
</code></pre>

<p>The first one is the private key, and the second one is the public key. How can I convert these numbers to Bitcoin format? </p>

<p>I've tried with this script: <a href="https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/ec-math.py" rel="nofollow noreferrer">https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/ec-math.py</a></p>

<p>But doesn't work in Python 3.5</p>

CC BY-SA 3.0
2017-12-18T10:03:02.547Z
Do you want to create the keys in WIF format ?? or do you want to create the associated address to those keys ??
Matt
CC BY-SA 3.0
2017-12-18T10:10:46.063Z
Both. I'd like to have the private key in WIF and a Bitcoin Address.
Marcos Aguayo
<private-key><public-key><python><ecdsa>
ecdsa - Create Private key and Bitcoin Address
Marcos Aguayo
# ecdsa - Create Private key and Bitcoin Address

(Asked by: username_0 on 2017-12-18)

I have the following code:

```
import binascii
import hashlib
from fastecdsa import keys, curve

# generate a private key for curve P256
priv_key = keys.gen_private_key(curve.secp256k1)
print (priv_key)
print ("______")

# get the public key corresponding to the private key we just generated
pub_key = keys.get_public_key(priv_key, curve.secp256k1)
print (pub_key)

```

Which returns this:

```
68553277193328358088381091283586955911631072878513122367770244549235879948867
______
(defdba7ac050b698bc63134bbc495064b5a99125b023d8ae92d21ca43be77961,
 68ed2abc9d36bae64947b94dd15864e3b2f7601b7009c5b9bae8a8775553fea0)

```

The first one is the private key, and the second one is the public key. How can I convert these numbers to Bitcoin format?

I've tried with this script: <https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/ec-math.py>

But doesn't work in Python 3.5

**Tags:** private-key, public-key, python, ecdsa

> **Comments:**
> **username_1**: Do you want to create the keys in WIF format ?? or do you want to create the associated address to those keys ??  
> **username_0**: Both. I'd like to have the private key in WIF and a Bitcoin Address.  

---

## 1 answers

### Answer by username_0 on 2017-12-22. Score: 3 | Accepted answer

I solved the problem. This is the code:

```
import os
import hashlib
from hashlib import sha256

def ripemd160(x):
    d = hashlib.new("ripemd160")
    d.update(x)
    return d

P = 2 ** 256 - 2 ** 32 - 2 ** 9 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 4 - 1
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
     0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)
B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

def point_add(p, q):
    xp, yp = p
    xq, yq = q

    if p == q:
        l = pow(2 * yp % P, P - 2, P) * (3 * xp * xp) % P
    else:
        l = pow(xq - xp, P - 2, P) * (yq - yp) % P

    xr = (l ** 2 - xp - xq) % P
    yr = (l * xp - l * xr - yp) % P

    return xr, yr

def point_mul(p, d):
    n = p
    q = None

    for i in range(256):
        if d & (1 << i):
            if q is None:
                q = n
            else:
                q = point_add(q, n)

        n = point_add(n, n)

    return q

def point_bytes(p):
    x, y = p

    return b"\x04" + x.to_bytes(32, "big") + y.to_bytes(32, "big")

def b58_encode(d):
    out = ""
    p = 0
    x = 0

    while d[0] == 0:
        out += "1"
        d = d[1:]

    for i, v in enumerate(d[::-1]):
        x += v * (256 ** i)

    while x > 58 ** (p + 1):
        p += 1

    while p >= 0:
        a, x = divmod(x, 58 ** p)
        out += B58[a]
        p -= 1

    return out

def make_address(privkey):
    q = point_mul(G, int.from_bytes(privkey, "big"))
    hash160 = ripemd160(sha256(point_bytes(q)).digest()).digest()
    addr = b"\x00" + hash160
    checksum = sha256(sha256(addr).digest()).digest()[:4]
    addr += checksum

    wif = b"\x80" + privkey
    checksum = sha256(sha256(wif).digest()).digest()[:4]
    wif += checksum

    addr = b58_encode(addr)
    wif = b58_encode(wif)

    return addr, wif

print("=========================")

from ecdsa import SigningKey, SECP256k1

sk = SigningKey.generate(curve=SECP256k1)
vk = sk.get_verifying_key()
addr, wif = make_address(sk.to_string())
print("Address: " + addr)
print("Privkey: " + wif)

```

> **Comments:**
> **username_2**: Thanks but I keep getting the following error:  TypeError: point_bytes() takes 1 positional argument but 2 were given  

---

