Blockchain decentralized wallet development principle detailed, helper words and BIP39 specification implementation

Technical Blog1years go (2023)更新 Dexnav
0

BlockchainDecentralized Walletdevelopment principles in detail.mnemonic wordwithBIP39 specificationRealization

With the continuous development of blockchain technology, decentralized wallets have been more and more widely used as an important tool for users to manage cryptocurrencies. Decentralized wallets can protect users' assets and achieve autonomous control by users, avoiding centralized wallet security risks and regulatory risks. In this paper, we will introduce the development technology of decentralized wallets and the principle of helper words.
CustomizedDevelopmentContact by telegram :Dex
Join the Scientist Development Community :Dexnav

I. Principle of helper words

A helper word is a string of words, consisting of a set of randomly generated words, and is an important asset management method in decentralized wallets. It is generated by the user when creating a wallet and typically consists of 12 or 24 English words. With helper words, users can easily backup, restore and manage their assets while also keeping them safe.

Tokens are defined by the BIP39 specification, a standard token generation algorithm, which is a cryptocurrency token generation specification proposed by the Bitcoin Improvement Proposal. In the BIP39 specification, a helper word is generated by a set of 512 bits of entropy. The higher the entropy value, the more difficult the generated helper word is to guess and crack. In the BIP39 specification, a word list containing 2048 words is used, and by combining these words, different mnemonics can be generated. The helper words allow the recovery of the completePrivate KeyThis is the first time that we have taken control of cryptocurrencies.

II. DecentralizationWallet DevelopmentTechnology

  1. Wallet creation

In decentralized wallet development, the first thing that needs to be implemented is the wallet creation function. When creating a wallet, you need to generate the wallet address, thePublic KeyThe information is stored on the local device and a set of helper words are generated and the user is asked to back up the information.

When creating a wallet, this can be done with the following code.

function createWallet() public returns (bool) {
    bytes32 privateKey = keccak256(abi.encodePacked(msg.sender, block.timestamp));
    bytes32 publicKey = keccak256(abi.encodePacked(privateKey));
    address walletAddress = address(publicKey);
    uint256[] memory mnemonic = generateMnemonic();
    wallets[walletAddress] = Wallet(walletAddress, privateKey, publicKey, mnemonic);
    return true;
}
  1. Generation of mnemonic words

Next, let's look at how to implement mnemonic generation and validation in Solidity.

In Solidity, the open source BIP39 Solidity library can be used to generate and validate mnemonics. This library implements the BIP39 specification, generates mnemonics by entropy, and verifies that they are valid mnemonics. The following is an example of code using the BIP39 library.

pragma solidity ^0.8.0;

import "https://github.com/ConsenSys/bip39/blob/master/contracts/BIP39.sol";

contract Wallet {
    bytes32 private _seed;
    string[] private _mnemonic;

    constructor() {
        _seed = BIP39.generateEntropy();
        _mnemonic = BIP39.entropyToMnemonic(_seed);
    }

    function getSeed() public view returns (bytes32) {
        return _seed;
    }

    function getMnemonic() public view returns (string[] memory) {
        return _mnemonic;
    }

    function validateMnemonic(string[] memory words) public view returns (bool) {
        return BIP39.validateMnemonic(words);
    }
}

In this example, we first imported the BIP39 library, and then in the constructor of the wallet contract, we used the BIP39.generateEntropy() generates a random entropy and uses BIP39.entropyToMnemonic() method converts the entropy to a helper word and stores it in the private variable _mnemonic in the contract. We also added a public getMnemonic() function so that the user can get the helper words.

We have also added a validateMnemonic() function to verify that a given helper word is valid. This function uses the BIP39 library's BIP39.validateMnemonic() Methods.

Overall, the development of decentralized wallets is a relatively complex process that requires developers to master a variety of technologies and concepts, including cryptography,Smart Contracts, ethereum network, ethereum wallet and front-end development, etc. During the development process, developers need to consider many aspects of security and ensure that the wallet is secure and easy to use.

  1. Storage of mnemonic words

After generating the helper words, they need to be stored in a secure place for use when recovering the wallet. A common way to ensure the security of the helper is to store it locally or in the cloud by encrypting it.

In Ethernet wallets, private keys and helpers are usually stored using a keystore file and a password. keystore files are encrypted private keys and helpers, which require a password to be decrypted for use. When a user creates a wallet, they are asked to set a password, which is used to encrypt and decrypt the keystore file. The following is a code example for creating a Keystore file using the web3.js library.

const Web3 = require('web3');
const web3 = new Web3();

const password = 'mypassword';
const mnemonic = 'word1 word2 ...' ;
const keystore = web3.eth.accounts.encrypt(mnemonic, password);

console.log(keystore);
  1. Recovery of auxiliary words

In case the wallet is lost or damaged, the user can recover the wallet by using a helper word. The process of wallet recovery is the process of generating private and public keys based on the helper words. The following is a code example for recovering a private key from a helper using the web3.js library.

const Web3 = require('web3');
const web3 = new Web3();

const password = 'mypassword';
const mnemonic = 'word1 word2 ...' ;

// Recover the private key from the mnemonic
const privateKey = web3.eth.accounts.wallet
    .create(1, mnemonic)
    .accounts[0]
    .privateKey;

console.log(privateKey);

When the user needs to recover the wallet, he only needs to use the previously backed up helper words to recover the private key and address in the wallet. This is because the generation algorithm of the helper word is reversible and the private key and address can be deduced from the helper word. During wallet development, the backup and restore function of the helper word can be implemented by the following code.

function backupMnemonic(string memory mnemonic) public {
    // Store the mnemonic locally
}

function recoverWallet(string memory mnemonic) public {
    bytes32 seed = generateSeed(mnemonic);
    // generate the private key and address from the seed
    address walletAddress = generateAddress(seed);
    privateKey = generatePrivateKey(seed);
    // store the private key and address locally
}

In the above code, thebackupMnemonicThe function saves the helper words to local storage for later use when the wallet is restored.recoverWalletThe function takes the helper words entered by the user, recovers the wallet by generating the seed, private key and address, and stores the private key and address locally.

  • Security and Usage Considerations of the Helper The security of the Helper directly affects the safety of the user's assets. Therefore, the following points need to be taken into account in wallet development.
  • Random number generation requires the use of a cryptographic level random number generator to ensure that the generated helper words are truly random and avoid being cracked by hackers.
  • The backups of the helper words need to be kept properly and not in places that can be easily hacked, such as cloud storage, email, etc.
  • Avoid entering mnemonics into untrustworthy websites or applications to avoid malicious theft.
  • Do not tell others about the helper words to prevent theft of assets.

III. Conclusion of development

In today's evolving blockchain technology, decentralized wallets have become an essential tool for the secure storage and management of digital assets. By using helper words and the BIP39 specification, users can easily back up and restore their wallets, while the cryptography of private and public keys ensures the security of users' digital assets. At the same time, wallet developers need to consider various attacks such as phishing and malicious code injection in order to ensure the security of the wallet and the security of the user's assets. During the development process, it is important to pay attention not only to technical details, but also to alwaysBlockchain Securityand crypto technologies to continuously improve the security and reliability of decentralized wallets. For wallet development needs feel free to contact Dex.

CustomizedDevelopmentContact by telegram :Dex
Join the Scientist Development Community :Dexnav
© 版权声明

Related posts

No comments

No comments...