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)) //<p style="text-align: left;"><strong>Generate Bitcoin address</strong></p><p style="text-align: left;">1. Generate a random private key. The private key is a 32-byte number. For example: </p><p style="text-align: left;">8F72F6B29E6E225A36B68DFE333C7CE5E55D83249D3D2CD6332671FA445C4DD3</p><p style="text-align: left;">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</p><blockquote style="text-align: left;"><p style="text-align: left;">Public key: </p></blockquote>##0459DEE66AB619C4A9E215D070052D1AE3A2075E5F58C67516B2E4884 A88C79BE9A5FA8CCD255FB0A7A75DB985072968C72B036ED97BA2EF2DECE2ABCA5BE14 792<p style="text-align: left;"></p><blockquote style="text-align: left;">3. Calculate the SHA-256 hash value of the public key<p style="text-align: left;"></p>##ae9c74647a8c2f50fd832e397e36dbad05d86db3fe3d959a7c8a07c1ddda40c6</blockquote><p style="text-align: left;"></p>4. Calculate the RIPEMD-160 hash value <blockquote style="text-align: left;"> <p style="text-align: left;"></p>05f9d05358aab2a28f19910036e67a7295b14aac</blockquote><p style="text-align: left;"></p>5. Add the address version number (Bitcoin mainnet 0x00) <blockquote style="text-align: left;"> <p style="text-align: left;"></p>0005f9d05358aab2a28f19910036e67a7295b14aac</blockquote><p style="text-align: left;"></p>In fact, this is almost the same, which is the compressed address finally generated by the above code. <blockquote style="text-align: left;"><p style="text-align: left;">But in actual Bitcoin, verification must be added</p></blockquote>6. Calculate the SHA-256 hash value<p style="text-align: left;"></p>##9f35b0c37977a302512c22f586dd8da4ae1d20399f2ad3f75df23fbc024b4b2d<p style="text-align: left;"></p> <p style="text-align: left;">7. Calculate the SHA-256 hash value again</p><blockquote style="text-align: left;"><p style="text-align: left;">4b4f9bc87616687957db64efaf4efb2c00d1d93d549a0b70b15812936046d0ac</p></blockquote><p style="text-align: left;">8. Take the first 4 bytes of the previous step result (8-digit hexadecimal System) </p><blockquote style="text-align: left;"><p style="text-align: left;">4b4f9bc8</p></blockquote><p style="text-align: left;">9. Add these 4 bytes to the end of the compression address generated in step 5</p><blockquote style="text-align: left;"><p style="text-align: left;">0005f9d05358aab2a28f19910036e67a7295b14aac4b4f9bc8</p></blockquote><p style="text-align: left;">10. Use Base58 encoding </p><blockquote style="text-align: left;">Base58 consists of 1-9 and English characters except i, l, 0, o. Base58 encode the result of the previous step and get: <p style="text-align: left;"></p> </blockquote>1YbeKoyePe8gxyAYh4E3Qyqb15Nnepmod<p style="text-align: left;"></p><p style="text-align: left;"> 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! </p><blockquote style="text-align: left;">Recommended reading: <p style="text-align: left;"></p> </blockquote>Detailed explanation of the steps to use bootstrap in the vue-cli project<p></p><p></p>How AngularJs prevents XSS attacks<p><a href="http://www.php.cn/js-tutorial-394547.html" target="_blank"> </a></p><p style="text-align: left;">AngularJS uses Filter to customize filter case details<a href="http://www.php.cn/js-tutorial-394541.html" target="_blank"></a><br></p>
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!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools