Home Web Front-end JS Tutorial How to use Node.Js to generate a Bitcoin address

How to use Node.Js to generate a Bitcoin address

Jun 02, 2018 pm 02:23 PM
javascript node.js operate

This time I will show you how to operate Node.JsGenerate a Bitcoin address, and what are the precautions for operating Node.Js to generate a Bitcoin address. The following is a practical case. Let’s take a look.

Use Node.js, IDE uses sublime 3.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

var randomBytes = require('randombytes')

var BigInteger = require('bigi')

var ecurve = require('ecurve')

var crypto = require('crypto')

var cs = require('coinstring')

var secp256k1 = ecurve.getCurveByName('secp256k1')

var randombytes = randomBytes(32).toString('hex')

var privateKey = new Buffer(randombytes, 'hex')

console.log("私钥:" + privateKey.toString('hex'))

var ecparams = ecurve.getCurveByName('secp256k1')

var curvePt = ecparams.G.multiply(BigInteger.fromBuffer(privateKey))

var x = curvePt.affineX.toBuffer(32)

var y = curvePt.affineY.toBuffer(32)

var publicKey = Buffer.concat([new Buffer([0x04]), x, y])

console.log("标准地址:" + publicKey.toString('hex'))

//compressed

publicKey = curvePt.getEncoded(true) //true forces compressed public key

console.log("compressed:" + publicKey.toString('hex'))

var sha = crypto.createHash('sha256').update(publicKey).digest()

var pubkeyHash = crypto.createHash('rmd160').update(sha).digest()

// pubkeyHash of compressed public key

console.log("pubkeyHash:" + pubkeyHash.toString('hex')) 

// address of compressed public key

console.log("压缩地址:" + cs.encode(pubkeyHash, 0x0)) //<-- 0x0 is for public addresses

//这里还缺失校验和Base58编码

console.log(cs.encode(privateKey, 0x80)) //<--- 0x80 is for private addresses

console.log(cs.encode(Buffer.concat([privateKey, new Buffer([0])]), 0x80)) // <-- compressed private address

Copy after login

Generate Bitcoin address

1. Generate a random private key. The private key is a 32-byte number. For example:

8F72F6B29E6E225A36B68DFE333C7CE5E55D83249D3D2CD6332671FA445C4DD3

2. Elliptic curve calculation public key After generating the private key, we use the elliptic curve encryption algorithm (ECDSA-secp256k1) to calculate the uncompressed public key corresponding to the private key. The generated public key is 65 bytes in total. First The first byte is 0x04, the last 32 bytes are the X coordinate, and the last 32 bytes are the Y coordinate: Public key P. DB985072968C72B036ED97BA2EF2DECE2ABCA5BE14792

Public key:

0459DEE66AB619C4A9E215D070052D1AE3A2075E5F58C67516B2E4884A88C79BE9A5FA8CCD255FB0A7A75DB985072968C72B036ED97BA2EF2DECE2ABCA5BE14 792

3. Calculate the SHA-256 hash value of the public key

##ae9c74647a8c2f50fd832e397e36dbad05d86db3fe3d959a7c8a07c1ddda40c6

4. Calculate the RIPEMD-160 hash value

05f9d05358aab2a28f19910036e67a7295b14aac

5. Add the address version number (Bitcoin mainnet 0x00)

0005f9d05358aab2a28f19910036e67a7295b14aac

In fact, this is almost the same, which is the compressed address finally generated by the above code.

But in actual Bitcoin, verification must be added

6. Calculate the SHA-256 hash value

##9f35b0c37977a302512c22f586dd8da4ae1d20399f2ad3f75df23fbc024b4b2d

7. Calculate the SHA-256 hash value again

4b4f9bc87616687957db64efaf4efb2c00d1d93d549a0b70b15812936046d0ac

8. Take the first 4 bytes of the previous step result (8-digit hexadecimal System)

4b4f9bc8

9. Add these 4 bytes to the end of the compression address generated in step 5

0005f9d05358aab2a28f19910036e67a7295b14aac4b4f9bc8

10. Use Base58 encoding

Base58 consists of 1-9 and English characters except i, l, 0, o. Base58 encode the result of the previous step and get:

1YbeKoyePe8gxyAYh4E3Qyqb15Nnepmod

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!

Recommended reading:

How to use vue.js and element-ui to implement the menu tree structure

How to use JS Decorator function

The above is the detailed content of How to use Node.Js to generate a Bitcoin address. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

How to bind WeChat on Ele.me How to bind WeChat on Ele.me Apr 01, 2024 pm 03:46 PM

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];

See all articles