Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: functions for creating various key files #9

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions bursa.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@
package bursa

import (
"encoding/json"
"fmt"

"github.com/blinklabs-io/bursa/internal/config"
// TODO: replace these w/ gOuroboros (blinklabs-io/gouroboros#364)
"github.com/fivebinaries/go-cardano-serialization/address"
"github.com/fivebinaries/go-cardano-serialization/bip32"
"github.com/fivebinaries/go-cardano-serialization/network"
"github.com/fxamacker/cbor/v2"
bip39 "github.com/tyler-smith/go-bip39"
)

type KeyFile struct {
Type string `json:"type"`
Description string `json:"description"`
CborHex string `json:"cborHex"`
}

type Wallet struct {
Mnemonic string `json:"mnemonic"`
PaymentAddress string `json:"payment_address"`
StakeAddress string `json:"stake_address"`
PaymentVKey KeyFile `json:"-"`
PaymentSKey KeyFile `json:"-"`
StakeVKey KeyFile `json:"-"`
StakeSKey KeyFile `json:"-"`
}

func NewMnemonic() (string, error) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
Expand Down Expand Up @@ -62,10 +80,58 @@ func GetPaymentKey(accountKey bip32.XPrv, num uint32) bip32.XPrv {
return accountKey.Derive(0).Derive(num)
}

func GetPaymentVKey(paymentKey bip32.XPrv) string {
keyCbor, err := cbor.Marshal(paymentKey.Public().PublicKey())
if err != nil {
panic(err)
}
return GetKeyFile(
"PaymentVerificationKeyShelley_ed25519",
"Payment Verification Key",
keyCbor,
)
}

func GetPaymentSKey(paymentKey bip32.XPrv) string {
keyCbor, err := cbor.Marshal(GetExtendedPrivateKey(paymentKey, paymentKey.Public().PublicKey()))
if err != nil {
panic(err)
}
return GetKeyFile(
"PaymentExtendedSigningKeyShelley_ed25519_bip32",
"Payment Signing Key",
keyCbor,
)
}

func GetStakeKey(accountKey bip32.XPrv, num uint32) bip32.XPrv {
return accountKey.Derive(2).Derive(num)
}

func GetStakeVKey(stakeKey bip32.XPrv) string {
keyCbor, err := cbor.Marshal(stakeKey.Public().PublicKey())
if err != nil {
panic(err)
}
return GetKeyFile(
"StakeVerificationKeyShelley_ed25519",
"Stake Verification Key",
keyCbor,
)
}

func GetStakeSKey(stakeKey bip32.XPrv) string {
keyCbor, err := cbor.Marshal(GetExtendedPrivateKey(stakeKey, stakeKey.Public().PublicKey()))
if err != nil {
panic(err)
}
return GetKeyFile(
"StakeExtendedSigningKeyShelley_ed25519_bip32",
"Stake Signing Key",
keyCbor,
)
}

func GetAddress(accountKey bip32.XPrv, net string, num uint32) *address.BaseAddress {
nw := network.TestNet()
if net == "mainnet" {
Expand Down Expand Up @@ -95,6 +161,21 @@ func GetExtendedPrivateKey(privateKey []byte, publicKey []byte) bip32.XPrv {
return xprv
}

func GetKeyFile(keyType string, desc string, data []byte) string {
tmp := KeyFile{
Type: keyType,
Description: desc,
CborHex: fmt.Sprintf("%x", data),
}
// Use 4 spaces for indent
ret, err := json.MarshalIndent(tmp, "", " ")
if err != nil {
return ""
}
// Append newline
return fmt.Sprintf("%s\n", ret)
}

func Run() {
// Load Config
cfg, err := config.LoadConfig()
Expand All @@ -117,7 +198,12 @@ func Run() {
addr := GetAddress(accountKey, cfg.Network, 0) // TODO: more addresses

fmt.Println("Loaded mnemonic and generated address...")
fmt.Printf("MNEMONIC=%s", mnemonic)
fmt.Printf("PAYMENT_ADDRESS=%s", addr.String())
fmt.Printf("STAKE_ADDRESS=%s", addr.ToReward().String())
fmt.Printf("MNEMONIC=%s\n", mnemonic)
fmt.Printf("PAYMENT_ADDRESS=%s\n", addr.String())
fmt.Printf("STAKE_ADDRESS=%s\n", addr.ToReward().String())

fmt.Printf("payment.vkey=%s", GetPaymentVKey(GetPaymentKey(accountKey, 0)))
fmt.Printf("payment.skey=%s", GetPaymentSKey(GetPaymentKey(accountKey, 0)))
fmt.Printf("stake.vkey=%s", GetStakeVKey(GetStakeKey(accountKey, 0)))
fmt.Printf("stake.vkey=%s", GetStakeSKey(GetStakeKey(accountKey, 0)))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.19

require (
github.com/fivebinaries/go-cardano-serialization v0.0.0-20220907134105-ec9b85086588
github.com/fxamacker/cbor/v2 v2.4.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/tyler-smith/go-bip39 v1.1.0
)

require (
github.com/btcsuite/btcutil v1.0.2 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
Expand Down
Loading