在前一篇文章中介绍了如何使用钱包来生成私钥,本文将介绍如何生成地址。

address命令

address命令用于生成地址和罗列出已有地址。

// addressCmd 地址命令主入口,包含创建地址和查看地址列表功能
func (c *WalletCommand) addressCmd() *cobra.Command {
	addressCmd := &cobra.Command{
		Use:   "address",
		Short: "Manage btc address",
		Long:  "Manage btc address",
	}
	addressCmd.AddCommand(
		c.newAddressCmd(),
		c.listAddressCmd(),
	)
	return addressCmd
}

// newAddressCmd 从提供 wif 私钥创建 btc 地址
func (c *WalletCommand) newAddressCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "new",
		Short: "Create a new address from wif key, example: ./wallet address create wif network[testnet|mainnet]",
		Long:  "Create a new address from wif key, example: ./wallet address create wif network[testnet|mainnet]",
		RunE:  c.runNewAddressCmd,
	}
}

// runNewAddressCmd 从提供 wif 私钥创建 btc 地址
func (c *WalletCommand) runNewAddressCmd(cmd *cobra.Command, args []string) error {
	fmt.Println("new btc address from wif key")
	// TODO: 校验参数合法性
	// 校验并解析 wif 私钥
	wif, err := btcutil.DecodeWIF(string(args[0]))
	if err != nil {
		return errors.Wrap(err, "decode wif failed")
	}
	// 生成 bech32 地址
	bech32Addr, err := address.NewBTCAddressFromWIF(wif).GenBech32Address(utils.GetNetwork(args[1]))
	if err != nil {
		return errors.Wrap(err, "generate bech32 address failed")
	}
	fmt.Println("address: ", bech32Addr)
	return nil
}

// listAddressCmd 列出所有地址,需要提供 key 文件和密码
func (c *WalletCommand) listAddressCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "list",
		Short: "List all addresses, example: ./wallet address list ./key.key password network[testnet|mainnet]",
		Long:  "List all addresses, example: ./wallet address list ./key.key password network[testnet|mainnet]",
		RunE:  c.runListAddressCmd,
	}
}

// runListAddressCmd 列出所有地址,需要提供 key 文件和密码
func (c *WalletCommand) runListAddressCmd(cmd *cobra.Command, args []string) error {
	fmt.Println("address list")
	// TODO: 校验参数合法性
	// 解析 key 文件
	store := storage.NewLocalStorage(args[0])
	// 获取 key 文件中所有的私钥
	keys, err := store.ListKeys()
	if err != nil {
		return errors.Wrap(err, "list keys failed")
	}
	for _, key := range keys {
		// 解密私钥
		decryptedKey, err := utils.AesDecrypt(key, args[1])
		if err != nil {
			return errors.Wrap(err, "decrypt key failed")
		}
		fmt.Println("key: ", string(decryptedKey))
		wif, err := btcutil.DecodeWIF(string(decryptedKey))
		if err != nil {
			return errors.Wrap(err, "decode wif failed")
		}
		// 生成 bech32 地址
		bech32Addr, err := address.NewBTCAddressFromWIF(wif).GenBech32Address(utils.GetNetwork(args[2]))
		if err != nil {
			return errors.Wrap(err, "generate bech32 address failed")
		}
		fmt.Println("address: ", bech32Addr)
	}
	return nil
}

操作示例

下面是 address 命令的简单操作示例:


# 创建地址
.$ ./btc_wallet address new L3qkjKBSc2exZbwZQS2h2DUnL1VRhF94hKmSitVR6MJ1QMGwL7eE testnet
new btc address from wif key
address:  tb1qdd0awhj8tqf2m9a9ge6jtjfz8g2tg8hwpxjgak

# 列出地址
$ ./btc_wallet address list ./key.key 1b983738b1d41babaf955276 testnet                 
address list
key:  L3qkjKBSc2exZbwZQS2h2DUnL1VRhF94hKmSitVR6MJ1QMGwL7eE
address:  tb1qdd0awhj8tqf2m9a9ge6jtjfz8g2tg8hwpxjgak
key:  Kwj3hosFzS8rF6CJePY8EZcngpr6MR9Z8nomKuK7cscgeajgePVh
address:  tb1qtcd0tlxp5r0cj537zphell605el4qxk3ymjjpy
key:  KwKyWaLNqXyDDyC2oHB1EyvNUJ3hHHkNpDKRU3ptGZmW71LEiaig
address:  tb1qdzjy77jnp0kh0wc2680lwfszpwm8dz69pgrhcg

孟斯特

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。

Author: mengbin

blog: mengbin

Github: mengbin92

cnblogs: 恋水无意

腾讯云开发者社区:孟斯特