Skip to content

Commit 643448f

Browse files
authored
Merge pull request #9 from blinklabs-io/feat/keyfiles
feat: functions for creating various key files
2 parents 3483c75 + 3dc004b commit 643448f

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

bursa.go

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,34 @@
1515
package bursa
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920

2021
"github.com/blinklabs-io/bursa/internal/config"
2122
// TODO: replace these w/ gOuroboros (blinklabs-io/gouroboros#364)
2223
"github.com/fivebinaries/go-cardano-serialization/address"
2324
"github.com/fivebinaries/go-cardano-serialization/bip32"
2425
"github.com/fivebinaries/go-cardano-serialization/network"
26+
"github.com/fxamacker/cbor/v2"
2527
bip39 "github.com/tyler-smith/go-bip39"
2628
)
2729

30+
type KeyFile struct {
31+
Type string `json:"type"`
32+
Description string `json:"description"`
33+
CborHex string `json:"cborHex"`
34+
}
35+
36+
type Wallet struct {
37+
Mnemonic string `json:"mnemonic"`
38+
PaymentAddress string `json:"payment_address"`
39+
StakeAddress string `json:"stake_address"`
40+
PaymentVKey KeyFile `json:"-"`
41+
PaymentSKey KeyFile `json:"-"`
42+
StakeVKey KeyFile `json:"-"`
43+
StakeSKey KeyFile `json:"-"`
44+
}
45+
2846
func NewMnemonic() (string, error) {
2947
entropy, err := bip39.NewEntropy(256)
3048
if err != nil {
@@ -62,10 +80,58 @@ func GetPaymentKey(accountKey bip32.XPrv, num uint32) bip32.XPrv {
6280
return accountKey.Derive(0).Derive(num)
6381
}
6482

83+
func GetPaymentVKey(paymentKey bip32.XPrv) string {
84+
keyCbor, err := cbor.Marshal(paymentKey.Public().PublicKey())
85+
if err != nil {
86+
panic(err)
87+
}
88+
return GetKeyFile(
89+
"PaymentVerificationKeyShelley_ed25519",
90+
"Payment Verification Key",
91+
keyCbor,
92+
)
93+
}
94+
95+
func GetPaymentSKey(paymentKey bip32.XPrv) string {
96+
keyCbor, err := cbor.Marshal(GetExtendedPrivateKey(paymentKey, paymentKey.Public().PublicKey()))
97+
if err != nil {
98+
panic(err)
99+
}
100+
return GetKeyFile(
101+
"PaymentExtendedSigningKeyShelley_ed25519_bip32",
102+
"Payment Signing Key",
103+
keyCbor,
104+
)
105+
}
106+
65107
func GetStakeKey(accountKey bip32.XPrv, num uint32) bip32.XPrv {
66108
return accountKey.Derive(2).Derive(num)
67109
}
68110

111+
func GetStakeVKey(stakeKey bip32.XPrv) string {
112+
keyCbor, err := cbor.Marshal(stakeKey.Public().PublicKey())
113+
if err != nil {
114+
panic(err)
115+
}
116+
return GetKeyFile(
117+
"StakeVerificationKeyShelley_ed25519",
118+
"Stake Verification Key",
119+
keyCbor,
120+
)
121+
}
122+
123+
func GetStakeSKey(stakeKey bip32.XPrv) string {
124+
keyCbor, err := cbor.Marshal(GetExtendedPrivateKey(stakeKey, stakeKey.Public().PublicKey()))
125+
if err != nil {
126+
panic(err)
127+
}
128+
return GetKeyFile(
129+
"StakeExtendedSigningKeyShelley_ed25519_bip32",
130+
"Stake Signing Key",
131+
keyCbor,
132+
)
133+
}
134+
69135
func GetAddress(accountKey bip32.XPrv, net string, num uint32) *address.BaseAddress {
70136
nw := network.TestNet()
71137
if net == "mainnet" {
@@ -95,6 +161,21 @@ func GetExtendedPrivateKey(privateKey []byte, publicKey []byte) bip32.XPrv {
95161
return xprv
96162
}
97163

164+
func GetKeyFile(keyType string, desc string, data []byte) string {
165+
tmp := KeyFile{
166+
Type: keyType,
167+
Description: desc,
168+
CborHex: fmt.Sprintf("%x", data),
169+
}
170+
// Use 4 spaces for indent
171+
ret, err := json.MarshalIndent(tmp, "", " ")
172+
if err != nil {
173+
return ""
174+
}
175+
// Append newline
176+
return fmt.Sprintf("%s\n", ret)
177+
}
178+
98179
func Run() {
99180
// Load Config
100181
cfg, err := config.LoadConfig()
@@ -117,7 +198,12 @@ func Run() {
117198
addr := GetAddress(accountKey, cfg.Network, 0) // TODO: more addresses
118199

119200
fmt.Println("Loaded mnemonic and generated address...")
120-
fmt.Printf("MNEMONIC=%s", mnemonic)
121-
fmt.Printf("PAYMENT_ADDRESS=%s", addr.String())
122-
fmt.Printf("STAKE_ADDRESS=%s", addr.ToReward().String())
201+
fmt.Printf("MNEMONIC=%s\n", mnemonic)
202+
fmt.Printf("PAYMENT_ADDRESS=%s\n", addr.String())
203+
fmt.Printf("STAKE_ADDRESS=%s\n", addr.ToReward().String())
204+
205+
fmt.Printf("payment.vkey=%s", GetPaymentVKey(GetPaymentKey(accountKey, 0)))
206+
fmt.Printf("payment.skey=%s", GetPaymentSKey(GetPaymentKey(accountKey, 0)))
207+
fmt.Printf("stake.vkey=%s", GetStakeVKey(GetStakeKey(accountKey, 0)))
208+
fmt.Printf("stake.vkey=%s", GetStakeSKey(GetStakeKey(accountKey, 0)))
123209
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ go 1.19
44

55
require (
66
github.com/fivebinaries/go-cardano-serialization v0.0.0-20220907134105-ec9b85086588
7+
github.com/fxamacker/cbor/v2 v2.4.0
78
github.com/kelseyhightower/envconfig v1.4.0
89
github.com/tyler-smith/go-bip39 v1.1.0
910
)
1011

1112
require (
1213
github.com/btcsuite/btcutil v1.0.2 // indirect
13-
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
1414
github.com/x448/float16 v0.8.4 // indirect
1515
golang.org/x/crypto v0.12.0 // indirect
1616
golang.org/x/sys v0.11.0 // indirect

0 commit comments

Comments
 (0)