Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package hd
|
|
|
|
import (
|
|
"github.com/cosmos/go-bip39"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
|
"github.com/cosmos/cosmos-sdk/crypto/types"
|
|
)
|
|
|
|
// PubKeyType defines an algorithm to derive key-pairs which can be used for cryptographic signing.
|
|
type PubKeyType string
|
|
|
|
const (
|
|
// MultiType implies that a pubkey is a multisignature
|
|
MultiType = PubKeyType("multi")
|
|
// Secp256k1Type uses the Bitcoin secp256k1 ECDSA parameters.
|
|
Secp256k1Type = PubKeyType("secp256k1")
|
|
// Ed25519Type represents the Ed25519Type signature system.
|
|
// It is currently not supported for end-user keys (wallets/ledgers).
|
|
Ed25519Type = PubKeyType("ed25519")
|
|
// Sr25519Type represents the Sr25519Type signature system.
|
|
Sr25519Type = PubKeyType("sr25519")
|
|
)
|
|
|
|
// Secp256k1 uses the Bitcoin secp256k1 ECDSA parameters.
|
|
var Secp256k1 = secp256k1Algo{}
|
|
|
|
type (
|
|
DeriveFn func(mnemonic, bip39Passphrase, hdPath string) ([]byte, error)
|
|
GenerateFn func(bz []byte) types.PrivKey
|
|
)
|
|
|
|
type WalletGenerator interface {
|
|
Derive(mnemonic, bip39Passphrase, hdPath string) ([]byte, error)
|
|
Generate(bz []byte) types.PrivKey
|
|
}
|
|
|
|
type secp256k1Algo struct{}
|
|
|
|
func (s secp256k1Algo) Name() PubKeyType {
|
|
return Secp256k1Type
|
|
}
|
|
|
|
// Derive derives and returns the secp256k1 private key for the given seed and HD path.
|
|
func (s secp256k1Algo) Derive() DeriveFn {
|
|
return func(mnemonic, bip39Passphrase, hdPath string) ([]byte, error) {
|
|
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, bip39Passphrase)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
masterPriv, ch := ComputeMastersFromSeed(seed)
|
|
if len(hdPath) == 0 {
|
|
return masterPriv[:], nil
|
|
}
|
|
derivedKey, err := DerivePrivateKeyForPath(masterPriv, ch, hdPath)
|
|
|
|
return derivedKey, err
|
|
}
|
|
}
|
|
|
|
// Generate generates a secp256k1 private key from the given bytes.
|
|
func (s secp256k1Algo) Generate() GenerateFn {
|
|
return func(bz []byte) types.PrivKey {
|
|
bzArr := make([]byte, secp256k1.PrivKeySize)
|
|
copy(bzArr, bz)
|
|
|
|
return &secp256k1.PrivKey{Key: bzArr}
|
|
}
|
|
}
|