Skip to content

Commit

Permalink
chore: Replace key logic from go-ethereum (#1124)
Browse files Browse the repository at this point in the history
* chore: replace private key

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: replace public key

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: replace verify

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: fix hollow accounts

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: add more tests

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: remove geth lib

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

---------

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov authored Nov 11, 2024
1 parent 2c22da8 commit f2a7c4b
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 1,497 deletions.
60 changes: 46 additions & 14 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import (
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/btcsuite/btcd/btcec/v2"
ecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa"

"github.com/decred/dcrd/dcrec/secp256k1/v4"

"github.com/hashgraph/hedera-sdk-go/v2/proto/services"
"github.com/pkg/errors"
"golang.org/x/crypto/pbkdf2"
Expand Down Expand Up @@ -482,17 +486,16 @@ func _DeriveECDSAChildKey(parentKey []byte, chainCode []byte, index uint32) ([]b

isHardened := IsHardenedIndex(index)
input := make([]byte, 37)
key, err := crypto.ToECDSA(parentKey)
if err != nil {
return nil, nil, err
if len(parentKey) != 32 {
return nil, nil, fmt.Errorf("invalid private key length")
}
privKey, pubKey := btcec.PrivKeyFromBytes(parentKey)

if isHardened {
offset := 33 - len(parentKey)
copy(input[offset:], parentKey)
} else {
pubKey := crypto.CompressPubkey(&key.PublicKey)
copy(input, pubKey)
copy(input, pubKey.SerializeCompressed())
}

binary.BigEndian.PutUint32(input[33:37], index)
Expand All @@ -508,8 +511,8 @@ func _DeriveECDSAChildKey(parentKey []byte, chainCode []byte, index uint32) ([]b
ir := i[32:]

ki := new(big.Int)
ki.Add(key.D, il)
ki.Mod(ki, key.Curve.Params().N)
ki.Add(privKey.ToECDSA().D, il)
ki.Mod(ki, privKey.ToECDSA().Curve.Params().N)

return ki.Bytes(), ir, nil
}
Expand Down Expand Up @@ -919,13 +922,42 @@ func Keccak256Hash(data []byte) (h Hash) {
return h
}

// Hash represents the 32 byte Keccak256 hash of arbitrary data.
type Hash [32]byte
func VerifySignature(pubkey, digestHash, signature []byte) bool {
pubKey, err := btcec.ParsePubKey(pubkey)
if err != nil {
return false
}

func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
recoveredKey, _, err := ecdsa.RecoverCompact(signature, digestHash)
if err != nil {
return false
}

func (h Hash) String() string {
return h.Hex()
return pubKey.IsEqual(recoveredKey)
}

func privateKeyFromBytes(privateKey []byte) (*btcec.PrivateKey, error) {
if len(privateKey) != 32 {
return nil, fmt.Errorf("invalid private key length")
}
var allNonPositive bool = true
for _, v := range privateKey {
if v > 0 {
allNonPositive = false
}
}
if allNonPositive {
return nil, fmt.Errorf("invalid private key, zero or negative")
}
pk, _ := btcec.PrivKeyFromBytes(privateKey)
return pk, nil
}

func CompressPubkey(pubKey *secp256k1.PublicKey) []byte {
return pubKey.SerializeCompressed()
}

// Hash represents the 32 byte Keccak256 hash of arbitrary data.
type Hash [32]byte

func (h Hash) Bytes() []byte { return h[:] }
Loading

0 comments on commit f2a7c4b

Please sign in to comment.