今天開始嘗試使用官方的SDKerdgo - Golang SDK,我自己開發Golang是使用JetBrains的GoLand,只能說自從用了JetBrains全家桶後除了開發前端使用VScode外,其他的都回不去了。
首先先建立專案,
然後安裝sdk後就可以使用了,由於我們需要產生密碼,所以需要另外安裝password generator
go get github.com/ElrondNetwork/elrond-sdk/erdgo
go get -u github.com/sethvargo/go-password/password
簡單的使用函式來做到
Code
package main
import (
"fmt"
"github.com/ElrondNetwork/elrond-sdk/erdgo"
"github.com/sethvargo/go-password/password"
)
func main() {
// 1. get New Mnemonic
mnemonic, _ := erdgo.GenerateNewMnemonic()
fmt.Printf("Generated mnemonic: %s \n", mnemonic)
// 2. get Private Key from Mnemonic
privateKey := erdgo.GetPrivateKeyFromMnemonic(mnemonic, 0, 0)
fmt.Printf("Generated privateKey: %s \n", privateKey)
// 3. get Address from private key
address, _ := erdgo.GetAddressFromPrivateKey(privateKey)
fmt.Printf("Generated address: %s \n", address)
// 4. export pem file from private key
err := erdgo.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 = erdgo.SavePrivateKeyToJsonFile(privateKey, jsonPass, address+".json")
if err != nil {
return
}
// 6. import from pem
pemPrivateKey, _ := erdgo.LoadPrivateKeyFromPemFile(address + ".pem")
fmt.Printf("PrivateKey from pem: %s \n", privateKey)
pemAddress, _ := erdgo.GetAddressFromPrivateKey(pemPrivateKey)
fmt.Printf("Address from pem: %s \n", pemAddress)
// 7. import from json
jsonPrivateKey, _ := erdgo.LoadPrivateKeyFromJsonFile(address+".json", jsonPass)
fmt.Printf("PrivateKey from json: %s \n", jsonPrivateKey)
jsonAddress, _ := erdgo.GetAddressFromPrivateKey(jsonPrivateKey)
fmt.Printf("Address from json: %s \n", jsonAddress)
}
執行結果
可以看到我們成功地使用了sdk產生地址與導出和導入私鑰,
在產生Address的地方需帶入Account和Address index這個主要是之前提過的地址產生加密法的Account和External / Internal