Skip to content

Commit

Permalink
add new address
Browse files Browse the repository at this point in the history
  • Loading branch information
liangping committed Mar 7, 2024
1 parent ac609a0 commit ecb46d3
Show file tree
Hide file tree
Showing 13 changed files with 1,104 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import (
)

const (
AccountAddressPrefix = "side"
AccountAddressPrefix = "bc"
Name = "side"
)

Expand Down
2 changes: 2 additions & 0 deletions app/test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func SetupConfig() {
config.SetBech32PrefixForAccount(Bech32Prefix, Bech32Prefix+sdk.PrefixPublic)
config.SetBech32PrefixForValidator(Bech32Prefix+valoper, Bech32Prefix+valoperpub)

config.SetCoinType(84) // use the same coin type as bitcoin

// This is copied from the cosmos sdk v0.43.0-beta1
// source: https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-beta1/types/address.go#L141
config.SetAddressVerifier(func(bytes []byte) error {
Expand Down
1 change: 1 addition & 0 deletions bitcoin/hd/algorithm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package hd
61 changes: 61 additions & 0 deletions bitcoin/hd/algorithm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package hd

import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
amino "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
bip32 "github.com/tyler-smith/go-bip32"
bip39 "github.com/tyler-smith/go-bip39"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

var TestCodec amino.Codec

func init() {

interfaceRegistry := types.NewInterfaceRegistry()
TestCodec = amino.NewProtoCodec(interfaceRegistry)
}

func getCodec() codec.Codec {
registry := codectypes.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
return codec.NewProtoCodec(registry)
}

const (
mnemonic = "picnic rent average infant boat squirrel federal assault mercy purity very motor fossil wheel verify upset box fresh horse vivid copy predict square regret"
xprv = "xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu"
path = "m/86'/0'/0'/0/0"
internalPubkey = "cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115"
expectedAddress = "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr"
)

func TestKeyring(t *testing.T) {

// bip39.NewSeed(mnemonic, nil)
seed, _ := bip39.MnemonicToByteArray(mnemonic)

println(seed)
masterKey, _ := bip32.NewMasterKey(seed)
hd.DerivePrivateKeyForPath([32]byte(masterKey.Key), 0, path)

keyring.NewInMemory(TestCodec)

pk := masterKey.PublicKey()

fmt.Println("PK", pk.String())
// hd.NewMasterKeyFromMnemonic(mnemonic, "", "bitcoin")

// algo, _ := keyring.NewSigningAlgoFromString("secp256k1", ks.options.SupportedAlgosLedger)
// algo.Derive()(&masterKey, "", path)

}
39 changes: 39 additions & 0 deletions bitcoin/keyring/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package keyring

import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)

// AppName defines the Ledger app used for signing. Evmos uses the Ethereum app
const AppName = "Bitcoin"

var (
// SupportedAlgorithms defines the list of signing algorithms used on Evmos:
// - eth_secp256k1 (Ethereum)
SupportedAlgorithms = keyring.SigningAlgoList{hd.Secp256k1}
// SupportedAlgorithmsLedger defines the list of signing algorithms used on Evmos for the Ledger device:
// - secp256k1 (in order to comply with Cosmos SDK)
// The Ledger derivation function is responsible for all signing and address generation.
SupportedAlgorithmsLedger = keyring.SigningAlgoList{hd.Secp256k1}
// LedgerDerivation defines the Evmos Ledger Go derivation (Ethereum app with EIP-712 signing)
//LedgerDerivation = ledger.EvmosLedgerDerivation()
// CreatePubkey uses the ethsecp256k1 pubkey with Ethereum address generation and keccak hashing
// CreatePubkey = func(key []byte) types.PubKey { return &ethsecp256k1.PubKey{Key: key} }
// SkipDERConversion represents whether the signed Ledger output should skip conversion from DER to BER.
// This is set to true for signing performed by the Ledger Ethereum app.
SkipDERConversion = true
)

// EthSecp256k1Option defines a function keys options for the ethereum Secp256k1 curve.
// It supports eth_secp256k1 keys for accounts.
func Option() keyring.Option {
return func(options *keyring.Options) {
options.SupportedAlgos = SupportedAlgorithms
options.SupportedAlgosLedger = SupportedAlgorithmsLedger
// options.LedgerDerivation = func() (cosmosLedger.SECP256K1, error) { return LedgerDerivation() }
// options.LedgerCreateKey = CreatePubkey
options.LedgerAppName = AppName
options.LedgerSigSkipDERConv = SkipDERConversion
}
}
Loading

0 comments on commit ecb46d3

Please sign in to comment.