Skip to content

Commit

Permalink
Merge pull request #45 from JackalLabs/legacy-wallet-support
Browse files Browse the repository at this point in the history
Legacy wallet support
  • Loading branch information
TheMarstonConnell authored Jun 24, 2024
2 parents 4515a00 + 8aa4a8b commit 42c83a1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
8 changes: 8 additions & 0 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type APIConfig struct {
IPFSPort int `yaml:"ipfs_port"`
}

// v3 and earlier providers used private key to sign txs
// and by design it can't derive mnemonic seed which makes
// it incompatible with sequoia's wallet creation.
type LegacyWallet struct {
Key string `json:"key"`
Address string `json:"address"`
}

func DefaultConfig() *Config {
return &Config{
QueueInterval: 10,
Expand Down
31 changes: 28 additions & 3 deletions config/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
sequoiaWallet "github.com/JackalLabs/sequoia/wallet"
"github.com/cosmos/go-bip39"
"github.com/desmos-labs/cosmos-go-wallet/wallet"

jsoniter "github.com/json-iterator/go"

"github.com/rs/zerolog/log"
)
import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary

Expand Down Expand Up @@ -83,6 +86,17 @@ func InitWallet(home string) (*wallet.Wallet, error) {
return nil, err
}

config, err := ReadConfigFile(directory)
if err != nil {
return nil, err
}

legacyWallet, err := detectLegacyWallet(home)
if err == nil {
log.Info().Msg("legacy wallet detected")
return sequoiaWallet.CreateWalletPrivKey(legacyWallet.Key, config.ChainCfg)
}

err = createWallet(directory)
if err != nil {
return nil, err
Expand All @@ -98,10 +112,21 @@ func InitWallet(home string) (*wallet.Wallet, error) {
return nil, err
}

config, err := ReadConfigFile(directory)
return sequoiaWallet.CreateWallet(seed.SeedPhrase, seed.DerivationPath, config.ChainCfg)
}

// returns LegacyWallet if "priv_storkey.json" is found at sequoia home directory,
// an error if not found or failed to unmarshal
func detectLegacyWallet(home string) (*LegacyWallet, error) {
dir := os.ExpandEnv(home)

file, err := readFile(dir, "priv_storkey.json")
if err != nil {
return nil, err
}

return sequoiaWallet.CreateWallet(seed.SeedPhrase, seed.DerivationPath, config.ChainCfg)
var legacy LegacyWallet
err = json.Unmarshal(file, &legacy)

return &legacy, err
}
1 change: 1 addition & 0 deletions file_system/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (f *FileSystem) Dump() (map[string]string, error) {
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()

if string(k)[:4] == "tree" {
continue
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ require (
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/coinbase/rosetta-sdk-go v0.7.9 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/confio/ics23/go v0.9.1 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v0.0.0-20221226095112-f3c38ecb5e32 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.3 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.5 // indirect
github.com/cosmos/ibc-go/v4 v4.4.2 // indirect
github.com/cosmos/ibc-go/v4 v4.6.0 // indirect
github.com/cosmos/interchain-accounts v0.2.6 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.4 // indirect
github.com/crackcomm/go-gitignore v0.0.0-20231225121904-e25f5bc08668 // indirect
Expand Down Expand Up @@ -278,7 +278,7 @@ replace (
//github.com/cosmos/cosmos-sdk => ../cosmos-sdk-new

//github.com/desmos-labs/cosmos-go-wallet => ../cosmos-go-wallet
github.com/desmos-labs/cosmos-go-wallet => github.com/TheMarstonConnell/cosmos-go-wallet v0.7.3-0.20231017193534-7de44d189e2a
github.com/desmos-labs/cosmos-go-wallet => github.com/TheMarstonConnell/cosmos-go-wallet v0.7.3-0.20240605140035-368812a1d4d5

//github.com/cosmos/cosmos-sdk => ../cosmos-sdk

Expand All @@ -294,7 +294,7 @@ replace (
github.com/ipfs/go-ds-badger2 => github.com/TheMarstonConnell/go-ds-badger2 v0.0.0-20240304191516-af5ee03005fc

//github.com/jackalLabs/canine-chain/v3 => ../canine-chain
github.com/jackalLabs/canine-chain/v3 => github.com/jackalLabs/canine-chain/v3 v3.0.2-0.20240212161902-b493692f8664 // using the master branch for now before v4 releases
github.com/jackalLabs/canine-chain/v3 => github.com/jackalLabs/canine-chain/v3 v3.0.3-rc.3.0.20240611211706-1d26f5317230 // using the master branch for now before v4 releases

github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.27

Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06/go.mod h1:7erjK
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/TheMarstonConnell/cosmos-go-wallet v0.7.3-0.20231017193534-7de44d189e2a h1:7TBxGLnjrwT7JD1mLGPhkES/Dm2GJN0/wB6w1rR6we8=
github.com/TheMarstonConnell/cosmos-go-wallet v0.7.3-0.20231017193534-7de44d189e2a/go.mod h1:1Qv2CAWgjKx/h/yA3xP/3losGFRxxKbxFDjGIqwhvvc=
github.com/TheMarstonConnell/cosmos-go-wallet v0.7.3-0.20240605140035-368812a1d4d5 h1:0vY/XJzi5TU2KYCSi3l02FZjXIKwBQXwAbyYbSnoNhM=
github.com/TheMarstonConnell/cosmos-go-wallet v0.7.3-0.20240605140035-368812a1d4d5/go.mod h1:1Qv2CAWgjKx/h/yA3xP/3losGFRxxKbxFDjGIqwhvvc=
github.com/TheMarstonConnell/go-ds-badger2 v0.0.0-20240304191516-af5ee03005fc h1:Zm5BYljo+2iwfgKyPKHXt/k5GDAa7vQhB3OZxIrjeKc=
github.com/TheMarstonConnell/go-ds-badger2 v0.0.0-20240304191516-af5ee03005fc/go.mod h1:66WAKUovf4tFU+Yp6LnG3JQAonJyFxjVQNbz1TTncj0=
github.com/TheMarstonConnell/ipfs-lite v0.0.0-20240304191454-94283a9ad1c9 h1:3id6BxQQZYwneoqlCATEy7nVB+gpDqoa4cGaPJ4kriE=
Expand Down Expand Up @@ -310,8 +310,8 @@ github.com/cosmos/gorocksdb v1.2.0 h1:d0l3jJG8M4hBouIZq0mDUHZ+zjOx044J3nGRskwTb4
github.com/cosmos/gorocksdb v1.2.0/go.mod h1:aaKvKItm514hKfNJpUJXnnOWeBnk2GL4+Qw9NHizILw=
github.com/cosmos/iavl v0.19.5 h1:rGA3hOrgNxgRM5wYcSCxgQBap7fW82WZgY78V9po/iY=
github.com/cosmos/iavl v0.19.5/go.mod h1:X9PKD3J0iFxdmgNLa7b2LYWdsGd90ToV5cAONApkEPw=
github.com/cosmos/ibc-go/v4 v4.4.2 h1:PG4Yy0/bw6Hvmha3RZbc53KYzaCwuB07Ot4GLyzcBvo=
github.com/cosmos/ibc-go/v4 v4.4.2/go.mod h1:j/kD2JCIaV5ozvJvaEkWhLxM2zva7/KTM++EtKFYcB8=
github.com/cosmos/ibc-go/v4 v4.6.0 h1:G7kiD4Zf8Wrxc8BXWIKuFnzI0W4wpvRPrl5HwdfTIsA=
github.com/cosmos/ibc-go/v4 v4.6.0/go.mod h1:ksiZHUypws0NVP50E3ea0ivVFO/bfS8q8yLg8yZ2ATQ=
github.com/cosmos/interchain-accounts v0.2.6 h1:TV2M2g1/Rb9MCNw1YePdBKE0rcEczNj1RGHT+2iRYas=
github.com/cosmos/interchain-accounts v0.2.6/go.mod h1:lUzWNzCiCtIEYZefac5+YgEBz2aR39nMS374jIv1c7o=
github.com/cosmos/keyring v1.2.0 h1:8C1lBP9xhImmIabyXW4c3vFjjLiBdGCmfLUfeZlV1Yo=
Expand Down Expand Up @@ -838,8 +838,8 @@ github.com/iris-contrib/jade v1.1.4/go.mod h1:EDqR+ur9piDl6DUgs6qRrlfzmlx/D5Uybo
github.com/iris-contrib/pongo2 v0.0.1/go.mod h1:Ssh+00+3GAZqSQb30AvBRNxBx7rf0GqwkjqxNd0u65g=
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA=
github.com/jackalLabs/canine-chain/v3 v3.0.2-0.20240212161902-b493692f8664 h1:JI/SypHYvEWPSMtcXYD8JpWvBw5cHoH8tqzzbDDaxy8=
github.com/jackalLabs/canine-chain/v3 v3.0.2-0.20240212161902-b493692f8664/go.mod h1:ZjGcbuX0opY//aozW9TRtq+kPYUfTD5aIH6duf1W5E0=
github.com/jackalLabs/canine-chain/v3 v3.0.3-rc.3.0.20240611211706-1d26f5317230 h1:telsMQABaiQawAvI06C+pL1XTaU1xmhFQEFfgslYdEw=
github.com/jackalLabs/canine-chain/v3 v3.0.3-rc.3.0.20240611211706-1d26f5317230/go.mod h1:oBXv9DCT+9g20lTrEHILWGQicCbUlMt+G7/GDU6QkZA=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
Expand Down
25 changes: 25 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wallet

import (
"encoding/hex"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -34,3 +35,27 @@ func CreateWallet(seed string, derivation string, chainCfg types.ChainConfig) (*

return w, err
}

func CreateWalletPrivKey(privKey string, chainCfg types.ChainConfig) (*wallet.Wallet, error) {
key, err := hex.DecodeString(privKey)
if err != nil {
return nil, err
}
// Set up the SDK config with the proper bech32 prefixes
cfg := sdk.GetConfig()
cfg.SetBech32PrefixForAccount(chainCfg.Bech32Prefix, fmt.Sprintf("%spub", chainCfg.Bech32Prefix))

encodingCfg := canine.MakeEncodingConfig()

c, err := client.NewClient(&chainCfg, encodingCfg.Marshaler)
if err != nil {
panic(err)
}

w, err := wallet.NewWalletFromKey(key, c, encodingCfg.TxConfig)
if err != nil {
panic(err)
}

return w, err
}

0 comments on commit 42c83a1

Please sign in to comment.