Home Web Front-end JS Tutorial How to implement Bitcoin address in Node.Js

How to implement Bitcoin address in Node.Js

Apr 27, 2018 pm 02:27 PM
javascript node.js address

This time I will bring you Node.JsHow to implement a Bitcoin address, what are the precautions for implementing a Bitcoin address in Node.Js, the following is a practical case, let’s come together take a look.

Use Node.js, IDE uses sublime 3.

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:

##0459DEE66AB619C4A9E215D070052D1AE3A2075E5F58C67516B2E4884 A88C79BE9A5FA8CCD255FB0A7A75DB985072968C72B036ED97BA2EF2DECE2ABCA5BE14 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:

Detailed explanation of the steps to use bootstrap in the vue-cli project

How AngularJs prevents XSS attacks

AngularJS uses Filter to customize filter case details

The above is the detailed content of How to implement Bitcoin address in Node.Js. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Apple after-sales (apple after-sales point address) Apple after-sales (apple after-sales point address) Jan 11, 2024 pm 10:30 PM

Apple’s official after-sales phone number: Apple’s 24-hour service center phone number: 400-666-8800. The after-sales service telephone number for Apple mobile phones is: 400-666-8800. -627-2273. Apple’s customer service manual service hotline is 400-627-2273 for after-sales support; 400-666-8800 for the online store; and the only official Apple phone number is 400-666-8800. Apple's customer service hotline is 400-666-8800. You can call this number to inquire about hardware, software and third-party accessories of Apple products. It should be noted that Apple’s manual customer service does not provide services 24 hours a day. Their service hours are from 9 a.m. to 9 p.m. (Sundays are from 9 a.m. to 9 p.m.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Where can I change my Meituan address? Meituan address modification tutorial! Where can I change my Meituan address? Meituan address modification tutorial! Mar 15, 2024 pm 04:07 PM

1. Where can I change my Meituan address? Meituan address modification tutorial! Method (1) 1. Enter Meituan My Page and click Settings. 2. Select personal information. 3. Click the shipping address again. 4. Finally, select the address you want to modify, click the pen icon on the right side of the address, and modify it. Method (2) 1. On the homepage of the Meituan app, click Takeout, then click More Functions after entering. 2. In the More interface, click Manage Address. 3. In the My Shipping Address interface, select Edit. 4. Modify them one by one according to your needs, and finally click to save the address.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

What is a router address assignment lease? What is a router address assignment lease? Feb 19, 2024 pm 11:37 PM

Router is one of the very important devices in the network, which plays the role of connecting multiple devices and transmitting data. In the configuration of the router, an important parameter is the address lease time (AddressLeaseTime), whose significance is crucial for network connection and management. This article will introduce in detail the meaning and function of router address lease. The address lease period refers to the time period that the router gives the connected device an IP address. After each device is connected to the router, it will automatically obtain a temporary IP address. This address will last for a certain period of time.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles