Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use web3dart to generate ethernet address for flutter application

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use web3dart to generate Ethernet address for flutter application". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction to web3dart

As a new framework, there are not many options for the Ethernet Square development package suitable for Flutter. Web3dart is a relatively perfect Dart implementation, which includes important features such as JSON RPC encapsulation, offline signature, ABI codec and so on. Its goal is to provide a dart version of web3.js, which can meet the needs of most Flutter applications for docking ethernet block chains.

The installation of web3dart is simple by first adding the web3dart dependency to the pubspec.yaml of the project. For example:

Name: hubwiz_tutorialdependencies: web3dart: git: git://github.com/simolus3/web3dart.git

Then execute the following command to update the project dependency:

~ / hubwiz_tutorial$ pub get

If you want to quickly grasp the method of connecting ethernet block chain with Flutter / Dart application, we recommend Huizhi.com 's online interactive tutorial:

Detailed explanation of the development of Flutter Etay Fang

2. Sample code: generate the address of ethernet place with web3dart

The generation of ethernet address is the functional feature required by most Flutter applications that want to support ethernet block chain. The web3dart package makes this process quite straightforward: web3dart's crypto library provides three API functions for generating the private key, deriving the public key from the private key, and deriving the Ethernet address from the public key:

First, we introduce the necessary libraries:

Import 'dart:math'; / / Randomimport' dart:typed_data'; / / Uint8List import 'package:web3dart/crypto.dart';STEP creating a random private key

To do this, we need to use the secure random number generator in the math library, and then call generateNewPrivateKey () in the crypto library to generate a random private key:

Random rng = Random.secure (); / / secure random number generator BigInt privKey = generateNewPrivateKey (rng); / / generate a new private key STEP derive the public key from the private key

You can derive the public key from the specified private key by directly calling the privateKeyToPublic () function in the crypto library:

Uint8List pubKey = privateKeyToPublic (privKey); / / derive the public key print ('public Key = > ${bytesToHex (pubKey)}') from the private key; / / display its hexadecimal string representation

BytesToHex () is an auxiliary API provided by the crypto library to convert a byte array (Uint8List) into a string represented in hexadecimal.

STEP addresses derive addresses from the public key

Directly call the publicKeyToAddress () method in the crypto library to derive the address code stream from the specified public key code stream:

Uint8List address = publicKeyToAddress (pubKey) / / derive the address String addressHex = bytesToHex from the public key (address, / / address byte array include0x:true, / / including the 0x prefix forcePadLength:40 / / complete to 40 bytes) Print ('address = > ${addressHex}'); / / shows the address "how to use web3dart to generate an Ethernet address for flutter applications". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report