Create a Bitcoin address API in Python on AWS Lambda

Life is not only work, right? Passionate developers write code also for fun 🙂 In this post, we are gonna create a fully functional Bitcoin address generator API from scratch on AWS Lambda!

If you are reading this post, you probably already have a good knowledge of the “crypto world”, also because everyone is talking about it in the last times.

If not, here are some basic concepts:

What is Bitcoin?

In layman’s terms, as simple Wiki suggests, Bitcoin is a digital and global money system currency. It allows people to send or receive money across the internet, even to someone they don’t know or don’t trust. Money can be exchanged without being linked to a real identity. The mathematical field of cryptography is the basis for Bitcoin’s security.”

What is a Bitcoin address?

Basically, an address (or wallet) is the place where you can store your bitcoins. Like a bank account number, you can receive bitcoins on your address and use it to send money to anyone. Technically speaking, is a public key generated from a private key, which you need to access your wallet and move your money.

All bitcoin transactions are anonymous and public, as you can see on https://www.blockchain.com/

Now, it’s time to start with the fun!

Login to your AWS account and create a new Lambda function from scratch choosing python 2.7 as runtime and a basic service role, as we do not need specific permissions.

New lambda function

Of course, you can use your favorite programming language, but we are gonna use Python for this tutorial.

At this point, we should write down the code to create the bitcoin address from a random private key, which will be also returned by the function to allow us to use our wallet.

This python script goes step-by-step through the creation of a valid bitcoin address. I wrote it more than one year ago as an exercise, so keep in mind it could not be “production ready”. Nowadays, there are many libraries that allow you to achieve the goal more easily, like pybitcoin.

The code uses the ecdsa library, which needs to be imported in Lambda as a deployment package.

If you wanna skip the dirty job, you can just select “Upload file from S3” and use this package – https://s3.amazonaws.com/frenxi-dev/python/LambdaBTCAddressGenerator-948eaa54-ea35-4c30-b8a4-3f61b52ee147.zip – which I made available for you with this tutorial.

Lambda deployment package

At this point, we can already test the lambda function and it should return the private key, the WIF format and the public key as a JSON string, easily usable from any API and web application.

Lambda test

Now, is time to make our bitcoin address generator publicly available at an HTTP endpoint, and we are gonna use AWS API Gateway which is designed specifically with this purpose.

In the function designer, always within the Lambda interface, we can add an API gateway as a trigger of the function.

AWS api gateway trigger

Once added, we need to configure our API gateway creating the new API, selecting the authentication method (open is just fine for demo purposes) and the stage (dev in our case).

AWS api gateway invoke

Click Add, save the function and… boom! You HTTP api endpoint is up, running and of course, triggering your Bitcoin Address Generator Lambda function returns the expected values in JSON format:

{
"public_address":"1A2qoVdYp75itwTnmx7dUi7apVroUaPuxL",
"private_key":"309914a791e3836b580b1df1ab91ad42f57acbf6b20d3e91337e4d1bd8e2839e",
"private_wif":"5JBgx8ydRWLnxpE1zmQyegEzEgcvxfyQJxJvuvsXqK1buywahaW"
}

Feel free to test my POC at https://6xzaun2udc.execute-api.us-east-1.amazonaws.com/dev/LambdaBTCAddressGenerator

Some nice-to-have on your API Gateway

By default, the API Gateway accepts any kind of request. For our tool, we can limit to only GET method deleting the existing ANY method and adding a new one limited to GET requests.

Also, we left our API open to invoke without authentication, but is a good idea to limit its usage to a lower rate.

Use case scenarios for our brand new tool!

All right, now we have a basic but fully functional Bitcoin Address Generator API… well done! But what we can really do with it? Here are a few ideas:

  • make the tool available for third-party sites in order to securely generate addresses without provisioning the code and the infrastructure
  • launch a crypto wallet app, implementing some additional APIs for moving money
  • use it for your own Bitcoins (not recommended)!

What’s the next?

A couple of ideas to extend this make could be:

  • implement parameters in the Lambda function, to allow users to generate addresses from a given private key, or calculate the private key from a given string
  • implement a web interface to better visualize the JSON output

Have fun!

Leave a comment

Your email address will not be published. Required fields are marked *