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: add wallet #11

Merged
merged 1 commit into from
Dec 21, 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
110 changes: 110 additions & 0 deletions wallet/cosmos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package wallet

import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
)

type CosmosWallet struct {
mnemonic string
address []byte
keyName string
bech32Prefix string
}

type WalletAmount struct {
Address string
Denom string
Amount math.Int
}

func NewWallet(keyname string, address []byte, mnemonic string, bech32Prefix string) *CosmosWallet {
return &CosmosWallet{
mnemonic: mnemonic,
address: address,
keyName: keyname,
bech32Prefix: bech32Prefix,
}
}

func NewGeneratedWallet(keyname string, bech32Prefix string) (*CosmosWallet, error) {
entropy, err := bip39.NewEntropy(128)

if err != nil {
return nil, err
}

mnemonic, err := bip39.NewMnemonic(entropy)

if err != nil {
return nil, err
}

hdPath := hd.CreateHDPath(118, 0, 0)

derivedPrivKey, err := hd.Secp256k1.Derive()(mnemonic, "", hdPath.String())

if err != nil {
return nil, err
}

privKey := hd.Secp256k1.Generate()(derivedPrivKey)

address := privKey.PubKey().Address()

return NewWallet(keyname, address, mnemonic, bech32Prefix), nil
}

func (w *CosmosWallet) KeyName() string {
return w.keyName
}

// Get formatted address, passing in a prefix
func (w *CosmosWallet) FormattedAddress() string {
return types.MustBech32ifyAddressBytes(w.bech32Prefix, w.address)
}

// Get mnemonic, only used for relayer wallets
func (w *CosmosWallet) Mnemonic() string {
return w.mnemonic
}

// Get Address with chain's prefix
func (w *CosmosWallet) Address() []byte {
return w.address
}

func (w *CosmosWallet) FormattedAddressWithPrefix(prefix string) string {
return types.MustBech32ifyAddressBytes(prefix, w.address)
}

func (w *CosmosWallet) PublicKey() (cryptotypes.PubKey, error) {
privKey, err := w.PrivateKey()

if err != nil {
return nil, err
}

return privKey.PubKey(), nil
}

func (w *CosmosWallet) PrivateKey() (cryptotypes.PrivKey, error) {
hdPath := hd.CreateHDPath(118, 0, 0)

derivedPrivKey, err := hd.Secp256k1.Derive()(w.mnemonic, "", hdPath.String())

if err != nil {
return nil, err
}

privKey := hd.Secp256k1.Generate()(derivedPrivKey)

return privKey, nil
}

func (w *CosmosWallet) SigningAlgo() string {
return "secp256k1"
}
98 changes: 98 additions & 0 deletions wallet/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module github.com/skip-mev/petri/wallet

go 1.21.3

require (
cosmossdk.io/math v1.2.0
github.com/cosmos/cosmos-sdk v0.50.2
github.com/cosmos/go-bip39 v1.0.0
)

require (
cosmossdk.io/api v0.7.2 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core v0.11.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/log v1.2.1 // indirect
cosmossdk.io/store v1.0.1 // indirect
cosmossdk.io/x/tx v0.12.0 // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20231101195458-481da04154d6 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft v0.38.2 // indirect
github.com/cometbft/cometbft-db v0.9.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.0 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.3 // indirect
github.com/cosmos/gogoproto v1.4.11 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/getsentry/sentry-go v0.25.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-metrics v0.5.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading
Loading