Technical Guide | Ethereum Wallet Development: Wallet Address Generation Process

Course objectives

  1. Understand what is a segregation testimony
  2. Transaction address type
  3. Programming Practice: Generating Isolated Witness Addresses

You may wonder, what impact does this segregation test have on the development of our bitcoin wallet? Then you will know.

First, what is the isolation testimony

Segregated Witness is called SegWit , short for Segregated Witness. Isolation Witness is an upgrade to Bitcoin to solve a series of serious problems faced by Bitcoin. Pieter Wuille (Bitcoin core developer, Blockstream United) The founder) was first proposed in December 2015. Mainly defined by BIP-141.

We can understand the isolation testimony like this:

Witness: In Bitcoin, it refers to the verification of the legality of a transaction, which is used to prove that it has witness data for the output of certain transactions.

Isolation: The witness data is extracted from the transaction information and stored separately.

SegWit solves the problem

SegWit is a proposed update from Bitcoin Core, the current most popular bitcoin standard client, used by most businesses. Initially, the update was designed to address the scalability of transactions, which is a well-known weakness in Bitcoin software. Although this attack vector is not the most damaging to the user, it has been used in multiple attack cases so far, thus highlighting the need to fix this vulnerability.

At present, the problem of Bitcoin scalability is mainly due to insufficient block capacity. The problem here is that the hard block of the current block is limited to 1 megabyte, which is not enough to bear hundreds of transactions that users try to send every minute. . Therefore, many users must wait in line until their transaction is confirmed. This waiting time may be several hours, or even a few days. As the size of the network expands, the transaction intensity increases, but the block capacity limit remains the same, which means that the problem will continue to deteriorate.

The SegWit solution consists of two parts:

  • First, it can immediately increase the block size limit to 4 megabytes. One thing to note here is that 4 MB is the absolute maximum, and the actual block size will depend on the network conditions. After SegWit is activated, experts predict that block capacity will range from 2 to 2.1 megabytes.
  • Second, to solve the scalability of the transaction, moving a large number of transactions out of the blockchain using the Lightning Network for fast processing is expected to greatly increase the network capacity.

The advantages of Segwit

  • Increase the number of transactions a block can execute.
  • Reduce transaction costs.
  • Reduce the size of each transaction.
  • It is now possible to confirm the transaction faster because the waiting time will be reduced.
  • Helps the scalability of Bitcoin.
  • As the number of transactions per block will increase, it may increase the total cost that miners may charge.
  • Eliminate trade scalability.
  • Helps activate the lightning protocol.

Second, the transaction address type

The address is a 20-byte hash format formatted using Base58check to generate a P2PKH or P2SH bitcoin address. The most common way currently is for users to exchange payment information.

There are two types of common bitcoin transaction addresses:

  • P2PKH (Pay-to-Public-Key-Hash): Pay to the public key hash. It is the most commonly used template, defined by Nakamoto, allowing simple payment to a single public key.
  • P2SH (Pay-to-Script-Hash): Pay to the script, which is the multi-signal transaction output. Defined in BIP16, it allows payment to arbitrarily complex scripts.

The Segregation Witness created two new transaction addresses for Bitcoin:

  • P2WPKH (Pay-to-Witness-Public-Key-Hash): Payment to the quarantine witness public key hash, similar to P2PKH, newly defined in BIP141. It is embedded in the P2SH script, so it can be used by segwit's wallet.
  • P2WSH (Pay-to-Witness-Script-Hash): Payment to the multi-signature isolation witness script hash, similar to P2SH, is another new script format defined by BIP141. It can be embedded in P2SH scripts and addresses so that any wallet can make segwit-compatible payments.

Segregated Witnesses will not be implemented simultaneously across the network. For new and existing customers to coexist, wallet developers will be able to independently upgrade their wallet software to add quarantine witness functionality. After upgrading to a segwit wallet, the P2WPKH and P2WSH payment types are used, and the traditional wallet uses the P2PKH and P2SH payment types. Both forms of witness scripts P2WPKH and P2WSH can be embedded in the P2SH address, starting with an address of "3". P2PKH is an address starting with "1".

For example, suppose there are two people: lixu, zhangsan, lixu's wallet is not upgraded to segwit, but zhangsan's wallet has been upgraded to handle segwit transactions. Both lixu and zhangsan can use normal address transactions, but zhangsan is likely to use segwit to reduce transaction costs. In this case, zhangsan's wallet needs to build a P2SH address containing a segwit script. The lixu wallet thinks this is a normal P2SH address and can be paid without any knowledge of segwit. Then, Zhangsan's wallet can pay for this payment by isolating the transaction, making full use of the quarantine transaction and reducing transaction costs.

So we need to build a segwit address compatible with normal addresses.

Third, programming practice: generate isolated witness address

Code

Output

7EBFC423-8E93-471E-B336-C2AF46969391

7EBFC423-8E93-471E-B336-C2AF46969391

verification

4C5D6822-8F20-49A9-BFCC-9D3CCB933A42

4C5D6822-8F20-49A9-BFCC-9D3CCB933A42

C78D7E3D-CF91-4874-9C0E-5141956BAFA7

C78D7E3D-CF91-4874-9C0E-5141956BAFA7

Code analysis

This only solves the implementation of the getSegWitAddress method. For other code parsing, please see the previous chapter "Generating mnemonics to extended subaddresses".

This is to nest the p2wpkh address in the p2sh address as its redeem field. Also note that you need to specify the network type in both address types. The above code is run on the official Bitcoin network, which is specified

Const myNetwork = bitcoin.networks.bitcoin