-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from blinklabs-io/feat/cli
feat: initial cli support
- Loading branch information
Showing
9 changed files
with
215 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ | |
go.work | ||
|
||
# binary | ||
bursa | ||
/bursa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 Blink Labs, LLC. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/blinklabs-io/bursa/internal/cli" | ||
) | ||
|
||
func cliRun() { | ||
cli.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2023 Blink Labs, LLC. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/blinklabs-io/bursa" | ||
"github.com/blinklabs-io/bursa/internal/config" | ||
"github.com/blinklabs-io/bursa/internal/logging" | ||
) | ||
|
||
func NewDefaultWallet(mnemonic string) (*bursa.Wallet, error) { | ||
cfg := config.GetConfig() | ||
logger := logging.GetLogger() | ||
|
||
rootKey, err := bursa.GetRootKeyFromMnemonic(mnemonic) | ||
if err != nil { | ||
logger.Errorf("failed to get root key from mnemonic") | ||
return nil, fmt.Errorf("failed to get root key from mnemonic: %s", err) | ||
} | ||
accountKey := bursa.GetAccountKey(rootKey, 0) | ||
paymentKey := bursa.GetPaymentKey(accountKey, 0) | ||
stakeKey := bursa.GetStakeKey(accountKey, 0) | ||
addr := bursa.GetAddress(accountKey, cfg.Network, 0) | ||
w := &bursa.Wallet{ | ||
Mnemonic: mnemonic, | ||
PaymentAddress: addr.String(), | ||
StakeAddress: addr.ToReward().String(), | ||
PaymentVKey: bursa.GetPaymentVKey(paymentKey), | ||
PaymentSKey: bursa.GetPaymentSKey(paymentKey), | ||
StakeVKey: bursa.GetStakeVKey(stakeKey), | ||
StakeSKey: bursa.GetStakeSKey(stakeKey), | ||
} | ||
return w, nil | ||
} | ||
|
||
func Run() { | ||
// Load Config | ||
cfg, err := config.LoadConfig() | ||
if err != nil { | ||
fmt.Printf("Failed to load config: %s\n", err) | ||
os.Exit(1) | ||
} | ||
// Configure logging | ||
logging.Setup() | ||
logger := logging.GetLogger() | ||
// Sync logger on exit | ||
defer func() { | ||
if err := logger.Sync(); err != nil { | ||
// ignore error | ||
return | ||
} | ||
}() | ||
|
||
// Load mnemonic | ||
mnemonic := cfg.Mnemonic | ||
if mnemonic == "" { | ||
mnemonic, err = bursa.NewMnemonic() | ||
if err != nil { | ||
logger.Fatalf("failed to load mnemonic: %s", err) | ||
} | ||
} | ||
w, err := NewDefaultWallet(mnemonic) | ||
if err != nil { | ||
logger.Fatalf("failed to initialize wallet: %s", err) | ||
} | ||
|
||
logger.Infof("Loaded mnemonic and generated address...") | ||
fmt.Printf("MNEMONIC=%s\n", w.Mnemonic) | ||
fmt.Printf("PAYMENT_ADDRESS=%s\n", w.PaymentAddress) | ||
fmt.Printf("STAKE_ADDRESS=%s\n", w.StakeAddress) | ||
|
||
fmt.Printf("payment.vkey=%s\n", w.PaymentVKey) | ||
fmt.Printf("payment.skey=%s\n", w.PaymentSKey) | ||
fmt.Printf("stake.vkey=%s\n", w.StakeVKey) | ||
fmt.Printf("stake.skey=%s\n", w.StakeSKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package logging | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/blinklabs-io/bursa/internal/config" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type Logger = zap.SugaredLogger | ||
|
||
var globalLogger *Logger | ||
|
||
func Setup() { | ||
cfg := config.GetConfig() | ||
// Build our custom logging config | ||
loggerConfig := zap.NewProductionConfig() | ||
// Change timestamp key name | ||
loggerConfig.EncoderConfig.TimeKey = "timestamp" | ||
// Use a human readable time format | ||
loggerConfig.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339) | ||
|
||
// Set level | ||
if cfg.Logging.Level != "" { | ||
level, err := zapcore.ParseLevel(cfg.Logging.Level) | ||
if err != nil { | ||
log.Fatalf("error configuring logger: %s", err) | ||
} | ||
loggerConfig.Level.SetLevel(level) | ||
} | ||
|
||
// Create the logger | ||
l, err := loggerConfig.Build() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Store the "sugared" version of the logger | ||
globalLogger = l.Sugar() | ||
} | ||
|
||
func GetLogger() *Logger { | ||
return globalLogger | ||
} | ||
|
||
func GetDesugaredLogger() *zap.Logger { | ||
return globalLogger.Desugar() | ||
} | ||
|
||
func GetAccessLogger() *zap.Logger { | ||
return globalLogger.Desugar().With(zap.String("type", "access")).WithOptions(zap.WithCaller(false)) | ||
} |