iT邦幫忙

2022 iThome 鐵人賽

DAY 17
0

今天來繼續優化我們的錢包專案

今天我們根據Standard Go Project Layout來做優化,將昨天抽出來的function轉到獨立檔案,在internal/wallet/walletUtils.go
裡面的程式碼

package walletUtils

import (
	"github.com/ElrondNetwork/elrond-sdk/erdgo"
	"github.com/pelletier/go-toml"
)

var (
	conf, _ = toml.LoadFile("./configs/config.toml")
)

func GenerateNewMnemonic() (string, error) {
	return erdgo.GenerateNewMnemonic()
}

func GetPrivateKeyFromMnemonic(mnemonic string, account, index uint8) []byte {
	return erdgo.GetPrivateKeyFromMnemonic(mnemonic, account, index)
}

func GetAddressFromPrivateKey(privateKey []byte) (string, error) {
	return erdgo.GetAddressFromPrivateKey(privateKey)
}

func SavePrivateKeyToPemFile(privateKey []byte, filePath string) error {
	return erdgo.SavePrivateKeyToPemFile(privateKey, filePath+".pem")
}

func SavePrivateKeyToJsonFile(privateKey []byte, jsonPass, filePath string) error {
	return erdgo.SavePrivateKeyToJsonFile(privateKey, jsonPass, filePath)
}

func LoadPrivateKeyFromPemFile(filePath string) ([]byte, error) {
	return erdgo.LoadPrivateKeyFromPemFile(filePath)
}

func LoadPrivateKeyFromJsonFile(filePath, filePass string) ([]byte, error) {
	return erdgo.LoadPrivateKeyFromJsonFile(filePath, filePass)
}

函數名大寫是exported,讓他可以在package之外可見,再來看我們的main,這邊是想要設計成小工具,所以檔案放置在cmd/elrondWallet/main.go
程式碼

package main

import (
	walletUtils "elrondWallet/internal/wallet"
	"fmt"
	"github.com/sethvargo/go-password/password"
)

func main() {
	// 1. get New Mnemonic
	mnemonic, _ := walletUtils.GenerateNewMnemonic()
	fmt.Printf("Generated mnemonic: %s \n", mnemonic)
	//// 2. get Private Key from Mnemonic
	privateKey := walletUtils.GetPrivateKeyFromMnemonic(mnemonic, 0, 0)
	fmt.Printf("Generated privateKey: %s \n", privateKey)
	//// 3. get Address from private key
	address, _ := walletUtils.GetAddressFromPrivateKey(privateKey)
	fmt.Printf("Generated wallet: %s \n", address)
	//// 4. export pem file from private key
	err := walletUtils.SavePrivateKeyToPemFile(privateKey, address+".pem")
	if err != nil {
		return
	}
	//// generate password from json
	jsonPass, _ := password.Generate(64, 10, 10, false, false)
	fmt.Printf("Generated json password: %s \n", jsonPass)
	//// 5. export json file from private key
	err = walletUtils.SavePrivateKeyToJsonFile(privateKey, jsonPass, address+".json")
	if err != nil {
		return
	}
	//// 6. import from pem
	pemPrivateKey, _ := walletUtils.LoadPrivateKeyFromPemFile(address + ".pem")
	fmt.Printf("PrivateKey from pem: %s \n", privateKey)
	pemAddress, _ := walletUtils.GetAddressFromPrivateKey(pemPrivateKey)
	fmt.Printf("Address from pem: %s \n", pemAddress)
	//// 7. import from json
	jsonPrivateKey, _ := walletUtils.LoadPrivateKeyFromJsonFile(address+".json", jsonPass)
	fmt.Printf("PrivateKey from json: %s \n", jsonPrivateKey)
	jsonAddress, _ := walletUtils.GetAddressFromPrivateKey(jsonPrivateKey)
	fmt.Printf("Address from json: %s \n", jsonAddress)
}

結構如下
https://ithelp.ithome.com.tw/upload/images/20221001/20140358Z9T4rTPqer.png
明天就來設計呼叫node api然後處理裡面的資訊。


上一篇
[Day 16] Elrond full node (七)
下一篇
[Day 18] Elrond full node (九)
系列文
如何打造屬於自己的區塊鏈錢包服務31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言