1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

add more tests

This commit is contained in:
Anton Akentev 2024-05-24 17:38:35 +01:00
parent ed568338e2
commit 595765fc38
No known key found for this signature in database
GPG key ID: 766D0B9F1F3225EC
3 changed files with 88 additions and 3 deletions

1
go.mod
View file

@ -55,6 +55,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/pprof v0.0.0-20240402174815-29b9bb013b0f // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect

4
go.sum
View file

@ -80,8 +80,8 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=

View file

@ -2,14 +2,49 @@ package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"fmt"
"strings"
"testing"
"github.com/anyproto/go-slip10"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)
func Keccak256(data []byte) []byte {
hash := sha3.NewLegacyKeccak256()
hash.Write(data)
return hash.Sum(nil)
}
func PublicKeyToAddress(pub *ecdsa.PublicKey) string {
// Serialize the public key to a byte slice
pubBytes := elliptic.Marshal(pub.Curve, pub.X, pub.Y)
// Compute the Keccak256 hash of the public key (excluding the first byte)
hash := Keccak256(pubBytes[1:])
// Take the last 20 bytes of the hash as the address
address := hash[12:]
return fmt.Sprintf("0x%x", address)
}
func PrivateKeyToBytes(priv *ecdsa.PrivateKey) []byte {
return priv.D.Bytes()
}
// Encode encodes b as a hex string with 0x prefix.
func Encode(b []byte) string {
enc := make([]byte, len(b)*2+2)
copy(enc, "0x")
hex.Encode(enc[2:], b)
return string(enc)
}
func TestMnemonic(t *testing.T) {
phrase, err := NewMnemonicGenerator().WithWordCount(12)
require.NoError(t, err)
@ -50,6 +85,55 @@ func TestMnemonic(t *testing.T) {
// get address by public key
publicKey := res.EthereumIdentity.Public()
_, ok := publicKey.(*ecdsa.PublicKey)
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
require.Equal(t, true, ok)
// convert publicKeyECDSA to address
ethAddress := PublicKeyToAddress(publicKeyECDSA)
shouldBe := strings.ToLower("0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947")
require.Equal(t, shouldBe, ethAddress)
}
func TestMnemonic_ethereumKeyFromMnemonic(t *testing.T) {
var badPphrase Mnemonic = "tag volcano"
_, err := badPphrase.ethereumKeyFromMnemonic(0, defaultEthereumDerivation)
require.Error(t, err)
// good
var phrase Mnemonic = "tag volcano eight thank tide danger coast health above argue embrace heavy"
pk, err := phrase.ethereumKeyFromMnemonic(0, defaultEthereumDerivation)
require.NoError(t, err)
// check address
publicKey := pk.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
require.Equal(t, true, ok)
// convert publicKeyECDSA to address
ethAddress := PublicKeyToAddress(publicKeyECDSA)
shouldBe := strings.ToLower("0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947")
require.Equal(t, shouldBe, ethAddress)
// what wallet.PrivateKeyHex(account) does
bytes := PrivateKeyToBytes(pk)
pkStr := Encode(bytes)[2:]
require.Equal(t, "63e21d10fd50155dbba0e7d3f7431a400b84b4c2ac1ee38872f82448fe3ecfb9", pkStr)
pk, err = phrase.ethereumKeyFromMnemonic(1, defaultEthereumDerivation)
require.NoError(t, err)
// check address
publicKey = pk.Public()
publicKeyECDSA, ok = publicKey.(*ecdsa.PublicKey)
require.Equal(t, true, ok)
// convert publicKeyECDSA to address
ethAddress = PublicKeyToAddress(publicKeyECDSA)
shouldBe = strings.ToLower("0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559")
require.Equal(t, shouldBe, ethAddress)
bytes = PrivateKeyToBytes(pk)
pkStr = Encode(bytes)[2:]
require.Equal(t, "b31048b0aa87649bdb9016c0ee28c788ddfc45e52cd71cc0da08c47cb4390ae7", pkStr)
}